blob: 00232267a26a6e7d2f5777cea313e7e2645c9781 [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"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Bill Wendling30346342010-04-05 22:59:21 +000017#include "llvm/Constants.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000018#include "llvm/Module.h"
David Greene829b3e82009-08-19 21:52:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000022#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattnerf62e3ee2010-01-16 21:57:06 +000025#include "llvm/Target/Mangler.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner21dc46e2010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000030#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel61880932010-04-19 19:14:02 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000032#include "llvm/Analysis/DebugInfo.h"
Devang Patela86114b2010-10-25 20:45:32 +000033#include "llvm/ADT/Statistic.h"
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000034#include "llvm/ADT/STLExtras.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000035#include "llvm/ADT/StringExtras.h"
Devang Patel6c74a872010-04-27 19:46:33 +000036#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/ErrorHandling.h"
Devang Patel1973df22010-01-26 21:39:14 +000039#include "llvm/Support/ValueHandle.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000040#include "llvm/Support/FormattedStream.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000041#include "llvm/Support/Timer.h"
42#include "llvm/System/Path.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000043using namespace llvm;
44
Devang Patel6c74a872010-04-27 19:46:33 +000045static cl::opt<bool> PrintDbgScope("print-dbgscope", cl::Hidden,
46 cl::desc("Print DbgScope information for each machine instruction"));
47
Jim Grosbacha8683bb2010-07-21 21:21:52 +000048static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
Devang Patel30265c42010-07-07 20:12:52 +000049 cl::Hidden,
Devang Patel6c74a872010-04-27 19:46:33 +000050 cl::desc("Disable debug info printing"));
51
Dan Gohman7421ae42010-05-07 01:08:53 +000052static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
53 cl::desc("Make an absense of debug location information explicit."),
54 cl::init(false));
55
Nick Lewycky90b2ac22010-10-26 00:51:57 +000056#ifndef NDEBUG
Devang Patela86114b2010-10-25 20:45:32 +000057STATISTIC(BlocksWithoutLineNo, "Number of blocks without any line number");
Nick Lewycky90b2ac22010-10-26 00:51:57 +000058#endif
Devang Patela86114b2010-10-25 20:45:32 +000059
Bill Wendlingfcc14142010-04-07 09:28:04 +000060namespace {
61 const char *DWARFGroupName = "DWARF Emission";
62 const char *DbgTimerName = "DWARF Debug Writer";
63} // end anonymous namespace
64
Bill Wendling2f921f82009-05-15 09:23:25 +000065//===----------------------------------------------------------------------===//
66
67/// Configuration values for initial hash set sizes (log2).
68///
Bill Wendling2f921f82009-05-15 09:23:25 +000069static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +000070
71namespace llvm {
72
73//===----------------------------------------------------------------------===//
74/// CompileUnit - This dwarf writer support class manages information associate
75/// with a source file.
Nick Lewyckyb7993d62009-11-17 08:11:44 +000076class CompileUnit {
Bill Wendling2f921f82009-05-15 09:23:25 +000077 /// ID - File identifier for source.
78 ///
79 unsigned ID;
80
81 /// Die - Compile unit debug information entry.
82 ///
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +000083 const OwningPtr<DIE> CUDie;
Bill Wendling2f921f82009-05-15 09:23:25 +000084
Jeffrey Yasskin0708de12010-03-11 18:29:55 +000085 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel92e8c652009-11-21 00:31:03 +000086 DIE *IndexTyDie;
87
Devang Patel9a0339f2010-07-07 20:49:57 +000088 /// MDNodeToDieMap - Tracks the mapping of unit level debug informaton
Bill Wendling2f921f82009-05-15 09:23:25 +000089 /// variables to debug information entries.
Devang Patel9a0339f2010-07-07 20:49:57 +000090 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
Bill Wendling2f921f82009-05-15 09:23:25 +000091
Devang Patel9a0339f2010-07-07 20:49:57 +000092 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug informaton
Bill Wendling2f921f82009-05-15 09:23:25 +000093 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel9a0339f2010-07-07 20:49:57 +000094 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
Bill Wendling2f921f82009-05-15 09:23:25 +000095
96 /// Globals - A map of globally visible named entities for this unit.
97 ///
98 StringMap<DIE*> Globals;
99
Devang Patel04d2f2d2009-11-24 01:14:22 +0000100 /// GlobalTypes - A map of globally visible types for this unit.
101 ///
102 StringMap<DIE*> GlobalTypes;
103
Bill Wendling2f921f82009-05-15 09:23:25 +0000104public:
105 CompileUnit(unsigned I, DIE *D)
Devang Patel930143b2009-11-21 02:48:08 +0000106 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000107
108 // Accessors.
Devang Patel04d2f2d2009-11-24 01:14:22 +0000109 unsigned getID() const { return ID; }
Jeffrey Yasskin0708de12010-03-11 18:29:55 +0000110 DIE* getCUDie() const { return CUDie.get(); }
Devang Patel04d2f2d2009-11-24 01:14:22 +0000111 const StringMap<DIE*> &getGlobals() const { return Globals; }
112 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000113
114 /// hasContent - Return true if this compile unit has something to write out.
115 ///
Devang Patel930143b2009-11-21 02:48:08 +0000116 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000117
Devang Patel930143b2009-11-21 02:48:08 +0000118 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling2f921f82009-05-15 09:23:25 +0000119 ///
Benjamin Kramer96956ed2010-03-31 20:15:45 +0000120 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000121
Devang Patel04d2f2d2009-11-24 01:14:22 +0000122 /// addGlobalType - Add a new global type to the compile unit.
123 ///
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000124 void addGlobalType(StringRef Name, DIE *Die) {
125 GlobalTypes[Name] = Die;
Devang Patel04d2f2d2009-11-24 01:14:22 +0000126 }
127
Devang Patele064ad42009-11-20 21:37:22 +0000128 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling2f921f82009-05-15 09:23:25 +0000129 /// specified debug variable.
Devang Patel9a0339f2010-07-07 20:49:57 +0000130 DIE *getDIE(const MDNode *N) { return MDNodeToDieMap.lookup(N); }
Jim Grosbach042483e2009-11-21 23:12:12 +0000131
Devang Patele064ad42009-11-20 21:37:22 +0000132 /// insertDIE - Insert DIE into the map.
Devang Patel32cc43c2010-05-07 20:54:48 +0000133 void insertDIE(const MDNode *N, DIE *D) {
Devang Patel9a0339f2010-07-07 20:49:57 +0000134 MDNodeToDieMap.insert(std::make_pair(N, D));
Devang Patele064ad42009-11-20 21:37:22 +0000135 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000136
Devang Patele064ad42009-11-20 21:37:22 +0000137 /// getDIEEntry - Returns the debug information entry for the speciefied
138 /// debug variable.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000139 DIEEntry *getDIEEntry(const MDNode *N) {
140 DenseMap<const MDNode *, DIEEntry *>::iterator I =
141 MDNodeToDIEEntryMap.find(N);
Devang Patel9a0339f2010-07-07 20:49:57 +0000142 if (I == MDNodeToDIEEntryMap.end())
Devang Patel1f4690c2009-12-15 19:16:48 +0000143 return NULL;
144 return I->second;
145 }
Devang Patele064ad42009-11-20 21:37:22 +0000146
147 /// insertDIEEntry - Insert debug information entry into the map.
Devang Patel32cc43c2010-05-07 20:54:48 +0000148 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
Devang Patel9a0339f2010-07-07 20:49:57 +0000149 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling2f921f82009-05-15 09:23:25 +0000150 }
151
Devang Patel930143b2009-11-21 02:48:08 +0000152 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling2f921f82009-05-15 09:23:25 +0000153 ///
Devang Patel930143b2009-11-21 02:48:08 +0000154 void addDie(DIE *Buffer) {
155 this->CUDie->addChild(Buffer);
Bill Wendling2f921f82009-05-15 09:23:25 +0000156 }
Devang Patel92e8c652009-11-21 00:31:03 +0000157
158 // getIndexTyDie - Get an anonymous type for index type.
159 DIE *getIndexTyDie() {
160 return IndexTyDie;
161 }
162
Jim Grosbach00e9c612009-11-22 19:20:36 +0000163 // setIndexTyDie - Set D as anonymous type for index which can be reused
164 // later.
Devang Patel92e8c652009-11-21 00:31:03 +0000165 void setIndexTyDie(DIE *D) {
166 IndexTyDie = D;
167 }
168
Bill Wendling2f921f82009-05-15 09:23:25 +0000169};
170
171//===----------------------------------------------------------------------===//
172/// DbgVariable - This class is used to track local variable information.
173///
Devang Patelf3d7c082009-11-16 21:53:40 +0000174class DbgVariable {
Bill Wendling2f921f82009-05-15 09:23:25 +0000175 DIVariable Var; // Variable Descriptor.
Devang Patel9fc11702010-05-25 23:40:22 +0000176 DIE *TheDIE; // Variable DIE.
177 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
Bill Wendling2f921f82009-05-15 09:23:25 +0000178public:
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000179 // AbsVar may be NULL.
Devang Patel9fc11702010-05-25 23:40:22 +0000180 DbgVariable(DIVariable V) : Var(V), TheDIE(0), DotDebugLocOffset(~0U) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000181
182 // Accessors.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000183 DIVariable getVariable() const { return Var; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000184 void setDIE(DIE *D) { TheDIE = D; }
185 DIE *getDIE() const { return TheDIE; }
Devang Patel9fc11702010-05-25 23:40:22 +0000186 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
187 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000188 StringRef getName() const { return Var.getName(); }
189 unsigned getTag() const { return Var.getTag(); }
190 bool variableHasComplexAddress() const {
191 assert(Var.Verify() && "Invalid complex DbgVariable!");
192 return Var.hasComplexAddress();
193 }
194 bool isBlockByrefVariable() const {
195 assert(Var.Verify() && "Invalid complex DbgVariable!");
196 return Var.isBlockByrefVariable();
197 }
198 unsigned getNumAddrElements() const {
199 assert(Var.Verify() && "Invalid complex DbgVariable!");
200 return Var.getNumAddrElements();
201 }
202 uint64_t getAddrElement(unsigned i) const {
203 return Var.getAddrElement(i);
204 }
205 DIType getType() const {
206 DIType Ty = Var.getType();
207 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
208 // addresses instead.
209 if (Var.isBlockByrefVariable()) {
210 /* Byref variables, in Blocks, are declared by the programmer as
211 "SomeType VarName;", but the compiler creates a
212 __Block_byref_x_VarName struct, and gives the variable VarName
213 either the struct, or a pointer to the struct, as its type. This
214 is necessary for various behind-the-scenes things the compiler
215 needs to do with by-reference variables in blocks.
216
217 However, as far as the original *programmer* is concerned, the
218 variable should still have type 'SomeType', as originally declared.
219
220 The following function dives into the __Block_byref_x_VarName
221 struct to find the original type of the variable. This will be
222 passed back to the code generating the type for the Debug
223 Information Entry for the variable 'VarName'. 'VarName' will then
224 have the original type 'SomeType' in its debug information.
225
226 The original type 'SomeType' will be the type of the field named
227 'VarName' inside the __Block_byref_x_VarName struct.
228
229 NOTE: In order for this to not completely fail on the debugger
230 side, the Debug Information Entry for the variable VarName needs to
231 have a DW_AT_location that tells the debugger how to unwind through
232 the pointers and __Block_byref_x_VarName struct to find the actual
233 value of the variable. The function addBlockByrefType does this. */
234 DIType subType = Ty;
235 unsigned tag = Ty.getTag();
236
237 if (tag == dwarf::DW_TAG_pointer_type) {
238 DIDerivedType DTy = DIDerivedType(Ty);
239 subType = DTy.getTypeDerivedFrom();
240 }
241
242 DICompositeType blockStruct = DICompositeType(subType);
243 DIArray Elements = blockStruct.getTypeArray();
244
245 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
246 DIDescriptor Element = Elements.getElement(i);
247 DIDerivedType DT = DIDerivedType(Element);
248 if (getName() == DT.getName())
249 return (DT.getTypeDerivedFrom());
250 }
251 return Ty;
252 }
253 return Ty;
254 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000255};
256
257//===----------------------------------------------------------------------===//
Devang Patel6c74a872010-04-27 19:46:33 +0000258/// DbgRange - This is used to track range of instructions with identical
259/// debug info scope.
260///
261typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
262
263//===----------------------------------------------------------------------===//
Bill Wendling2f921f82009-05-15 09:23:25 +0000264/// DbgScope - This class is used to track scope information.
265///
Devang Patelf3d7c082009-11-16 21:53:40 +0000266class DbgScope {
Bill Wendling2f921f82009-05-15 09:23:25 +0000267 DbgScope *Parent; // Parent to this scope.
Jim Grosbach042483e2009-11-21 23:12:12 +0000268 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel1973df22010-01-26 21:39:14 +0000269 // Location at which this scope is inlined.
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000270 AssertingVH<const MDNode> InlinedAtLocation;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000271 bool AbstractScope; // Abstract Scope
Devang Patel787f94c2009-10-01 18:25:23 +0000272 const MachineInstr *LastInsn; // Last instruction of this scope.
273 const MachineInstr *FirstInsn; // First instruction of this scope.
Devang Patel6c74a872010-04-27 19:46:33 +0000274 unsigned DFSIn, DFSOut;
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000275 // Scopes defined in scope. Contents not owned.
276 SmallVector<DbgScope *, 4> Scopes;
277 // Variables declared in scope. Contents owned.
278 SmallVector<DbgVariable *, 8> Variables;
Devang Patel6c74a872010-04-27 19:46:33 +0000279 SmallVector<DbgRange, 4> Ranges;
Owen Anderson9becc182009-06-24 22:53:20 +0000280 // Private state for dump()
281 mutable unsigned IndentLevel;
Bill Wendling2f921f82009-05-15 09:23:25 +0000282public:
Devang Patel32cc43c2010-05-07 20:54:48 +0000283 DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000284 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Devang Patel6c74a872010-04-27 19:46:33 +0000285 LastInsn(0), FirstInsn(0),
286 DFSIn(0), DFSOut(0), IndentLevel(0) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000287 virtual ~DbgScope();
288
289 // Accessors.
290 DbgScope *getParent() const { return Parent; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000291 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000292 DIDescriptor getDesc() const { return Desc; }
Devang Patel32cc43c2010-05-07 20:54:48 +0000293 const MDNode *getInlinedAt() const { return InlinedAtLocation; }
294 const MDNode *getScopeNode() const { return Desc; }
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000295 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
Devang Patel406798a2010-08-09 18:51:29 +0000296 const SmallVector<DbgVariable *, 8> &getDbgVariables() { return Variables; }
Devang Patel6c74a872010-04-27 19:46:33 +0000297 const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
298
299 /// openInsnRange - This scope covers instruction range starting from MI.
300 void openInsnRange(const MachineInstr *MI) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000301 if (!FirstInsn)
Devang Patel6c74a872010-04-27 19:46:33 +0000302 FirstInsn = MI;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000303
Devang Patel6c74a872010-04-27 19:46:33 +0000304 if (Parent)
305 Parent->openInsnRange(MI);
306 }
307
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000308 /// extendInsnRange - Extend the current instruction range covered by
Devang Patel6c74a872010-04-27 19:46:33 +0000309 /// this scope.
310 void extendInsnRange(const MachineInstr *MI) {
311 assert (FirstInsn && "MI Range is not open!");
312 LastInsn = MI;
313 if (Parent)
314 Parent->extendInsnRange(MI);
315 }
316
317 /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
318 /// until now. This is used when a new scope is encountered while walking
319 /// machine instructions.
320 void closeInsnRange(DbgScope *NewScope = NULL) {
321 assert (LastInsn && "Last insn missing!");
322 Ranges.push_back(DbgRange(FirstInsn, LastInsn));
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000323 FirstInsn = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +0000324 LastInsn = NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000325 // If Parent dominates NewScope then do not close Parent's instruction
Devang Patel6c74a872010-04-27 19:46:33 +0000326 // range.
327 if (Parent && (!NewScope || !Parent->dominates(NewScope)))
328 Parent->closeInsnRange(NewScope);
329 }
330
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000331 void setAbstractScope() { AbstractScope = true; }
332 bool isAbstractScope() const { return AbstractScope; }
Devang Patel6c74a872010-04-27 19:46:33 +0000333
334 // Depth First Search support to walk and mainpluate DbgScope hierarchy.
335 unsigned getDFSOut() const { return DFSOut; }
336 void setDFSOut(unsigned O) { DFSOut = O; }
337 unsigned getDFSIn() const { return DFSIn; }
338 void setDFSIn(unsigned I) { DFSIn = I; }
339 bool dominates(const DbgScope *S) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000340 if (S == this)
Devang Patel6c74a872010-04-27 19:46:33 +0000341 return true;
342 if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
343 return true;
344 return false;
345 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000346
Devang Patel930143b2009-11-21 02:48:08 +0000347 /// addScope - Add a scope to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000348 ///
Devang Patel930143b2009-11-21 02:48:08 +0000349 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000350
Devang Patel930143b2009-11-21 02:48:08 +0000351 /// addVariable - Add a variable to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000352 ///
Devang Patel930143b2009-11-21 02:48:08 +0000353 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000354
Bill Wendling2f921f82009-05-15 09:23:25 +0000355#ifndef NDEBUG
356 void dump() const;
357#endif
358};
Devang Patel6c74a872010-04-27 19:46:33 +0000359
Chris Lattnerf5d06362010-04-05 04:09:20 +0000360} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000361
362#ifndef NDEBUG
363void DbgScope::dump() const {
David Greenec230cb92009-12-24 00:31:35 +0000364 raw_ostream &err = dbgs();
Chris Lattner81e8e022009-08-23 00:51:00 +0000365 err.indent(IndentLevel);
Devang Patel32cc43c2010-05-07 20:54:48 +0000366 const MDNode *N = Desc;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000367 N->dump();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000368 if (AbstractScope)
369 err << "Abstract Scope\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000370
371 IndentLevel += 2;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000372 if (!Scopes.empty())
373 err << "Children ...\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000374 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
375 if (Scopes[i] != this)
376 Scopes[i]->dump();
377
378 IndentLevel -= 2;
379}
380#endif
381
Bill Wendling2f921f82009-05-15 09:23:25 +0000382DbgScope::~DbgScope() {
Bill Wendling2f921f82009-05-15 09:23:25 +0000383 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
384 delete Variables[j];
Bill Wendling2f921f82009-05-15 09:23:25 +0000385}
386
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000387DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Devang Patel1a0df9a2010-05-10 22:49:55 +0000388 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000389 AbbreviationsSet(InitAbbreviationsSetSize),
Devang Patel12563b32010-04-16 23:33:45 +0000390 CurrentFnDbgScope(0), PrevLabel(NULL) {
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000391 NextStringPoolNumber = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000392
Chris Lattnere58b5472010-04-04 23:10:38 +0000393 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
394 DwarfStrSectionSym = TextSectionSym = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000395 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000396 FunctionBeginSym = FunctionEndSym = 0;
Devang Patel9c160e12010-07-08 20:10:35 +0000397 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Dan Gohman6e681a52010-06-18 15:56:31 +0000398 {
399 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
400 beginModule(M);
Torok Edwinf8dba242010-04-07 10:44:46 +0000401 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000402}
403DwarfDebug::~DwarfDebug() {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000404 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
405 DIEBlocks[j]->~DIEBlock();
Bill Wendling2f921f82009-05-15 09:23:25 +0000406}
407
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000408MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
409 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
410 if (Entry.first) return Entry.first;
411
412 Entry.second = NextStringPoolNumber++;
Chris Lattnera179b522010-04-04 19:25:43 +0000413 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000414}
415
416
Devang Patel930143b2009-11-21 02:48:08 +0000417/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000418///
Devang Patel930143b2009-11-21 02:48:08 +0000419void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000420 // Profile the node so that we can make it unique.
421 FoldingSetNodeID ID;
422 Abbrev.Profile(ID);
423
424 // Check the set for priors.
425 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
426
427 // If it's newly added.
428 if (InSet == &Abbrev) {
429 // Add to abbreviation list.
430 Abbreviations.push_back(&Abbrev);
431
432 // Assign the vector position + 1 as its number.
433 Abbrev.setNumber(Abbreviations.size());
434 } else {
435 // Assign existing abbreviation number.
436 Abbrev.setNumber(InSet->getNumber());
437 }
438}
439
Devang Patel930143b2009-11-21 02:48:08 +0000440/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendlingbcad77a2009-05-20 23:24:48 +0000441/// information entry.
Devang Patel930143b2009-11-21 02:48:08 +0000442DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000443 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000444 return Value;
445}
446
Devang Patel930143b2009-11-21 02:48:08 +0000447/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000448///
Devang Patel930143b2009-11-21 02:48:08 +0000449void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000450 unsigned Form, uint64_t Integer) {
451 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000452 DIEValue *Value = Integer == 1 ?
Devang Patel9c160e12010-07-08 20:10:35 +0000453 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000454 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000455}
456
Devang Patel930143b2009-11-21 02:48:08 +0000457/// addSInt - Add an signed integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000458///
Devang Patel930143b2009-11-21 02:48:08 +0000459void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000460 unsigned Form, int64_t Integer) {
461 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000462 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000463 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000464}
465
Devang Patel8c339592009-12-02 15:25:16 +0000466/// addString - Add a string attribute data and value. DIEString only
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000467/// keeps string reference.
Devang Patel930143b2009-11-21 02:48:08 +0000468void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerc3f23b82010-01-23 03:11:46 +0000469 StringRef String) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000470 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patel930143b2009-11-21 02:48:08 +0000471 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000472}
473
Devang Patel930143b2009-11-21 02:48:08 +0000474/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000475///
Devang Patel930143b2009-11-21 02:48:08 +0000476void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000477 const MCSymbol *Label) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000478 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patel930143b2009-11-21 02:48:08 +0000479 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000480}
481
Devang Patel930143b2009-11-21 02:48:08 +0000482/// addDelta - Add a label delta attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000483///
Devang Patel930143b2009-11-21 02:48:08 +0000484void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbc9210c2010-03-08 22:23:36 +0000485 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000486 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patel930143b2009-11-21 02:48:08 +0000487 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000488}
489
Chris Lattner3f3fb972010-04-05 05:24:55 +0000490/// addDIEEntry - Add a DIE attribute data and value.
491///
492void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
493 DIE *Entry) {
494 Die->addValue(Attribute, Form, createDIEEntry(Entry));
495}
496
497
Devang Patel930143b2009-11-21 02:48:08 +0000498/// addBlock - Add block data.
Bill Wendling2f921f82009-05-15 09:23:25 +0000499///
Devang Patel930143b2009-11-21 02:48:08 +0000500void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling2f921f82009-05-15 09:23:25 +0000501 DIEBlock *Block) {
Chris Lattner5a00dea2010-04-05 00:18:22 +0000502 Block->ComputeSize(Asm);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000503 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patel930143b2009-11-21 02:48:08 +0000504 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000505}
506
Devang Patel930143b2009-11-21 02:48:08 +0000507/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000508/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000509void DwarfDebug::addSourceLine(DIE *Die, DIVariable V) {
Devang Patel0625af22010-05-07 23:33:41 +0000510 // Verify variable.
Devang Patelb6511a32010-08-09 20:20:05 +0000511 if (!V.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000512 return;
513
Devang Patelb6511a32010-08-09 20:20:05 +0000514 unsigned Line = V.getLineNumber();
Devang Pateldd1c2892010-10-08 17:18:54 +0000515 if (Line == 0)
516 return;
Devang Patelb6511a32010-08-09 20:20:05 +0000517 unsigned FileID = GetOrCreateSourceID(V.getContext().getDirectory(),
518 V.getContext().getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000519 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000520 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
521 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000522}
523
Devang Patel930143b2009-11-21 02:48:08 +0000524/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000525/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000526void DwarfDebug::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patel0625af22010-05-07 23:33:41 +0000527 // Verify global variable.
Devang Patelb6511a32010-08-09 20:20:05 +0000528 if (!G.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000529 return;
530
Devang Patelb6511a32010-08-09 20:20:05 +0000531 unsigned Line = G.getLineNumber();
Devang Pateldd1c2892010-10-08 17:18:54 +0000532 if (Line == 0)
533 return;
Devang Patelb6511a32010-08-09 20:20:05 +0000534 unsigned FileID = GetOrCreateSourceID(G.getContext().getDirectory(),
535 G.getContext().getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000536 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000537 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
538 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000539}
Devang Patelb2de5fa2009-08-31 22:47:13 +0000540
Devang Patel930143b2009-11-21 02:48:08 +0000541/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000542/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000543void DwarfDebug::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patel0625af22010-05-07 23:33:41 +0000544 // Verify subprogram.
Devang Patelb6511a32010-08-09 20:20:05 +0000545 if (!SP.Verify())
Devang Patelb2de5fa2009-08-31 22:47:13 +0000546 return;
Caroline Tice183a5192009-09-11 18:25:54 +0000547 // If the line number is 0, don't add it.
Devang Patelb6511a32010-08-09 20:20:05 +0000548 if (SP.getLineNumber() == 0)
Caroline Tice183a5192009-09-11 18:25:54 +0000549 return;
550
Devang Patelb6511a32010-08-09 20:20:05 +0000551 unsigned Line = SP.getLineNumber();
552 if (!SP.getContext().Verify())
Devang Patel8119fe872010-03-08 22:02:50 +0000553 return;
Devang Patelb6511a32010-08-09 20:20:05 +0000554 unsigned FileID = GetOrCreateSourceID(SP.getDirectory(),
555 SP.getFilename());
Devang Patelb2de5fa2009-08-31 22:47:13 +0000556 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000557 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
558 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patelb2de5fa2009-08-31 22:47:13 +0000559}
560
Devang Patel930143b2009-11-21 02:48:08 +0000561/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000562/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000563void DwarfDebug::addSourceLine(DIE *Die, DIType Ty) {
Devang Patel0625af22010-05-07 23:33:41 +0000564 // Verify type.
Devang Patelb6511a32010-08-09 20:20:05 +0000565 if (!Ty.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000566 return;
567
Devang Patelb6511a32010-08-09 20:20:05 +0000568 unsigned Line = Ty.getLineNumber();
Devang Pateldd1c2892010-10-08 17:18:54 +0000569 if (Line == 0 || !Ty.getContext().Verify())
Devang Patel8119fe872010-03-08 22:02:50 +0000570 return;
Devang Patel1c758652010-10-28 19:50:08 +0000571 unsigned FileID = GetOrCreateSourceID(Ty.getDirectory(),
572 Ty.getFilename());
Bill Wendling2f921f82009-05-15 09:23:25 +0000573 assert(FileID && "Invalid file id");
Devang Patel930143b2009-11-21 02:48:08 +0000574 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
575 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling2f921f82009-05-15 09:23:25 +0000576}
577
Devang Patel1f4690c2009-12-15 19:16:48 +0000578/// addSourceLine - Add location information to specified debug information
579/// entry.
Devang Patelb1e07b32010-08-10 04:09:06 +0000580void DwarfDebug::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patel0625af22010-05-07 23:33:41 +0000581 // Verify namespace.
Devang Patelb6511a32010-08-09 20:20:05 +0000582 if (!NS.Verify())
Devang Patel1f4690c2009-12-15 19:16:48 +0000583 return;
584
Devang Patelb6511a32010-08-09 20:20:05 +0000585 unsigned Line = NS.getLineNumber();
Devang Pateldd1c2892010-10-08 17:18:54 +0000586 if (Line == 0)
587 return;
Devang Patelb6511a32010-08-09 20:20:05 +0000588 StringRef FN = NS.getFilename();
589 StringRef Dir = NS.getDirectory();
Devang Patel1f4690c2009-12-15 19:16:48 +0000590
591 unsigned FileID = GetOrCreateSourceID(Dir, FN);
592 assert(FileID && "Invalid file id");
593 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
594 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
595}
596
Devang Patel2cfc3af2010-08-31 06:11:28 +0000597/// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
598/// on provided frame index.
599void DwarfDebug::addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI) {
600 MachineLocation Location;
601 unsigned FrameReg;
602 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
603 int Offset = RI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
604 Location.set(FrameReg, Offset);
605
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000606 if (DV->variableHasComplexAddress())
607 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
608 else if (DV->isBlockByrefVariable())
609 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
610 else
611 addAddress(Die, dwarf::DW_AT_location, Location);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000612}
613
Devang Patel930143b2009-11-21 02:48:08 +0000614/// addComplexAddress - Start with the address based on the location provided,
Mike Stump14cf8ec2009-09-30 00:08:22 +0000615/// and generate the DWARF information necessary to find the actual variable
616/// given the extra address information encoded in the DIVariable, starting from
617/// the starting location. Add the DWARF information to the die.
618///
Devang Patel930143b2009-11-21 02:48:08 +0000619void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump14cf8ec2009-09-30 00:08:22 +0000620 unsigned Attribute,
621 const MachineLocation &Location) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000622 DIType Ty = DV->getType();
Mike Stump14cf8ec2009-09-30 00:08:22 +0000623
624 // Decode the original location, and use that as the start of the byref
625 // variable's location.
Chris Lattner3a383cb2010-04-05 00:13:49 +0000626 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stump14cf8ec2009-09-30 00:08:22 +0000627 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000628 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stump14cf8ec2009-09-30 00:08:22 +0000629
630 if (Location.isReg()) {
631 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000632 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000633 } else {
634 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel930143b2009-11-21 02:48:08 +0000635 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
636 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000637 }
638 } else {
639 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000640 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000641 else {
Devang Patel930143b2009-11-21 02:48:08 +0000642 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
643 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000644 }
645
Devang Patel930143b2009-11-21 02:48:08 +0000646 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump14cf8ec2009-09-30 00:08:22 +0000647 }
648
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000649 for (unsigned i = 0, N = DV->getNumAddrElements(); i < N; ++i) {
650 uint64_t Element = DV->getAddrElement(i);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000651
652 if (Element == DIFactory::OpPlus) {
Devang Patel930143b2009-11-21 02:48:08 +0000653 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000654 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
Mike Stump14cf8ec2009-09-30 00:08:22 +0000655 } else if (Element == DIFactory::OpDeref) {
Devang Patel930143b2009-11-21 02:48:08 +0000656 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000657 } else llvm_unreachable("unknown DIFactory Opcode");
658 }
659
660 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000661 addBlock(Die, Attribute, 0, Block);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000662}
663
Caroline Ticec87c1e22009-08-31 21:19:37 +0000664/* Byref variables, in Blocks, are declared by the programmer as "SomeType
665 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
666 gives the variable VarName either the struct, or a pointer to the struct, as
667 its type. This is necessary for various behind-the-scenes things the
668 compiler needs to do with by-reference variables in Blocks.
669
670 However, as far as the original *programmer* is concerned, the variable
671 should still have type 'SomeType', as originally declared.
672
Devang Patel930143b2009-11-21 02:48:08 +0000673 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticec87c1e22009-08-31 21:19:37 +0000674 struct to find the original type of the variable, which is then assigned to
675 the variable's Debug Information Entry as its real type. So far, so good.
676 However now the debugger will expect the variable VarName to have the type
677 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000678 expression that explains to the debugger how to navigate through the
Caroline Ticec87c1e22009-08-31 21:19:37 +0000679 pointers and struct to find the actual variable of type SomeType.
680
681 The following function does just that. We start by getting
682 the "normal" location for the variable. This will be the location
683 of either the struct __Block_byref_x_VarName or the pointer to the
684 struct __Block_byref_x_VarName.
685
686 The struct will look something like:
687
688 struct __Block_byref_x_VarName {
689 ... <various fields>
690 struct __Block_byref_x_VarName *forwarding;
691 ... <various other fields>
692 SomeType VarName;
693 ... <maybe more fields>
694 };
695
696 If we are given the struct directly (as our starting point) we
697 need to tell the debugger to:
698
699 1). Add the offset of the forwarding field.
700
Dan Gohman4a618822010-02-10 16:03:48 +0000701 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Ticec87c1e22009-08-31 21:19:37 +0000702 struct to use (the real one may have been copied onto the heap).
703
704 3). Add the offset for the field VarName, to find the actual variable.
705
706 If we started with a pointer to the struct, then we need to
707 dereference that pointer first, before the other steps.
708 Translating this into DWARF ops, we will need to append the following
709 to the current location description for the variable:
710
711 DW_OP_deref -- optional, if we start with a pointer
712 DW_OP_plus_uconst <forward_fld_offset>
713 DW_OP_deref
714 DW_OP_plus_uconst <varName_fld_offset>
715
716 That is what this function does. */
717
Devang Patel930143b2009-11-21 02:48:08 +0000718/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticec87c1e22009-08-31 21:19:37 +0000719/// provided, and generate the DWARF information necessary to find the
720/// actual Block variable (navigating the Block struct) based on the
721/// starting location. Add the DWARF information to the die. For
722/// more information, read large comment just above here.
723///
Devang Patel930143b2009-11-21 02:48:08 +0000724void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000725 unsigned Attribute,
726 const MachineLocation &Location) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000727 DIType Ty = DV->getType();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000728 DIType TmpTy = Ty;
729 unsigned Tag = Ty.getTag();
730 bool isPointer = false;
731
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000732 StringRef varName = DV->getName();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000733
734 if (Tag == dwarf::DW_TAG_pointer_type) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000735 DIDerivedType DTy = DIDerivedType(Ty);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000736 TmpTy = DTy.getTypeDerivedFrom();
737 isPointer = true;
738 }
739
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000740 DICompositeType blockStruct = DICompositeType(TmpTy);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000741
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000742 // Find the __forwarding field and the variable field in the __Block_byref
743 // struct.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000744 DIArray Fields = blockStruct.getTypeArray();
745 DIDescriptor varField = DIDescriptor();
746 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000747
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000748 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
749 DIDescriptor Element = Fields.getElement(i);
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000750 DIDerivedType DT = DIDerivedType(Element);
Devang Patel2d9caf92009-11-25 17:36:49 +0000751 StringRef fieldName = DT.getName();
752 if (fieldName == "__forwarding")
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000753 forwardingField = Element;
Devang Patel2d9caf92009-11-25 17:36:49 +0000754 else if (fieldName == varName)
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000755 varField = Element;
756 }
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000757
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000758 // Get the offsets for the forwarding field and the variable field.
Chris Lattner71696ef2010-03-31 06:06:37 +0000759 unsigned forwardingFieldOffset =
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000760 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
Chris Lattner71696ef2010-03-31 06:06:37 +0000761 unsigned varFieldOffset =
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000762 DIDerivedType(varField).getOffsetInBits() >> 3;
Caroline Ticec87c1e22009-08-31 21:19:37 +0000763
Mike Stump944fa252009-09-24 23:21:26 +0000764 // Decode the original location, and use that as the start of the byref
765 // variable's location.
Chris Lattner3a383cb2010-04-05 00:13:49 +0000766 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000767 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000768 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000769
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000770 if (Location.isReg()) {
771 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000772 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000773 else {
774 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel930143b2009-11-21 02:48:08 +0000775 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
776 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000777 }
778 } else {
779 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000780 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000781 else {
Devang Patel930143b2009-11-21 02:48:08 +0000782 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
783 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000784 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000785
Devang Patel930143b2009-11-21 02:48:08 +0000786 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000787 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000788
Mike Stump944fa252009-09-24 23:21:26 +0000789 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000790 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000791 if (isPointer)
Devang Patel930143b2009-11-21 02:48:08 +0000792 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000793
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000794 // Next add the offset for the '__forwarding' field:
795 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
796 // adding the offset if it's 0.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000797 if (forwardingFieldOffset > 0) {
Devang Patel930143b2009-11-21 02:48:08 +0000798 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
799 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000800 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000801
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000802 // Now dereference the __forwarding field to get to the real __Block_byref
803 // struct: DW_OP_deref.
Devang Patel930143b2009-11-21 02:48:08 +0000804 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000805
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000806 // Now that we've got the real __Block_byref... struct, add the offset
807 // for the variable's field to get to the location of the actual variable:
808 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000809 if (varFieldOffset > 0) {
Devang Patel930143b2009-11-21 02:48:08 +0000810 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
811 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000812 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000813
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000814 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000815 addBlock(Die, Attribute, 0, Block);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000816}
817
Devang Patel930143b2009-11-21 02:48:08 +0000818/// addAddress - Add an address attribute to a die based on the location
Bill Wendling2f921f82009-05-15 09:23:25 +0000819/// provided.
Devang Patel930143b2009-11-21 02:48:08 +0000820void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000821 const MachineLocation &Location) {
Chris Lattner3a383cb2010-04-05 00:13:49 +0000822 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling2f921f82009-05-15 09:23:25 +0000823 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer74729ae2010-03-31 19:34:01 +0000824 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel804fcd42010-09-22 21:10:38 +0000825
Devang Patele7559662010-11-02 17:37:00 +0000826 if (RI->getFrameRegister(*Asm->MF) == Location.getReg()
Devang Patel804fcd42010-09-22 21:10:38 +0000827 && Location.getOffset()) {
828 // If variable offset is based in frame register then use fbreg.
829 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
830 addSInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
831 addBlock(Die, Attribute, 0, Block);
832 return;
833 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000834
835 if (Location.isReg()) {
836 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000837 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000838 } else {
Devang Patel930143b2009-11-21 02:48:08 +0000839 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
840 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000841 }
842 } else {
843 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000844 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000845 } else {
Devang Patel930143b2009-11-21 02:48:08 +0000846 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
847 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000848 }
849
Devang Patel930143b2009-11-21 02:48:08 +0000850 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling2f921f82009-05-15 09:23:25 +0000851 }
852
Devang Patel930143b2009-11-21 02:48:08 +0000853 addBlock(Die, Attribute, 0, Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000854}
855
Devang Patel173b2b92010-04-28 01:03:09 +0000856/// addRegisterAddress - Add register location entry in variable DIE.
Devang Patele1c53f22010-05-20 16:36:41 +0000857bool DwarfDebug::addRegisterAddress(DIE *Die, const MCSymbol *VS,
Devang Patel173b2b92010-04-28 01:03:09 +0000858 const MachineOperand &MO) {
859 assert (MO.isReg() && "Invalid machine operand!");
860 if (!MO.getReg())
861 return false;
862 MachineLocation Location;
863 Location.set(MO.getReg());
864 addAddress(Die, dwarf::DW_AT_location, Location);
Devang Patele1c53f22010-05-20 16:36:41 +0000865 if (VS)
Devang Patel173b2b92010-04-28 01:03:09 +0000866 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
867 return true;
868}
869
870/// addConstantValue - Add constant value entry in variable DIE.
Devang Patele1c53f22010-05-20 16:36:41 +0000871bool DwarfDebug::addConstantValue(DIE *Die, const MCSymbol *VS,
Devang Patel173b2b92010-04-28 01:03:09 +0000872 const MachineOperand &MO) {
873 assert (MO.isImm() && "Invalid machine operand!");
874 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
875 unsigned Imm = MO.getImm();
876 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
877 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patele1c53f22010-05-20 16:36:41 +0000878 if (VS)
Devang Patel173b2b92010-04-28 01:03:09 +0000879 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
880 return true;
881}
882
883/// addConstantFPValue - Add constant value entry in variable DIE.
Devang Patele1c53f22010-05-20 16:36:41 +0000884bool DwarfDebug::addConstantFPValue(DIE *Die, const MCSymbol *VS,
Devang Patel173b2b92010-04-28 01:03:09 +0000885 const MachineOperand &MO) {
886 assert (MO.isFPImm() && "Invalid machine operand!");
887 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
888 APFloat FPImm = MO.getFPImm()->getValueAPF();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000889
Devang Patel173b2b92010-04-28 01:03:09 +0000890 // Get the raw data form of the floating point.
891 const APInt FltVal = FPImm.bitcastToAPInt();
892 const char *FltPtr = (const char*)FltVal.getRawData();
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000893
Devang Patel173b2b92010-04-28 01:03:09 +0000894 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
895 bool LittleEndian = Asm->getTargetData().isLittleEndian();
896 int Incr = (LittleEndian ? 1 : -1);
897 int Start = (LittleEndian ? 0 : NumBytes - 1);
898 int Stop = (LittleEndian ? NumBytes : -1);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000899
Devang Patel173b2b92010-04-28 01:03:09 +0000900 // Output the constant to DWARF one byte at a time.
901 for (; Start != Stop; Start += Incr)
902 addUInt(Block, 0, dwarf::DW_FORM_data1,
903 (unsigned char)0xFF & FltPtr[Start]);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000904
Devang Patel173b2b92010-04-28 01:03:09 +0000905 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patele1c53f22010-05-20 16:36:41 +0000906 if (VS)
907 addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000908 return true;
Devang Patel173b2b92010-04-28 01:03:09 +0000909}
910
911
Devang Patel2b75ed22009-12-10 19:14:49 +0000912/// addToContextOwner - Add Die into the list of its context owner's children.
913void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel3b548aa2010-03-08 20:52:55 +0000914 if (Context.isType()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000915 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
Devang Patel2b75ed22009-12-10 19:14:49 +0000916 ContextDIE->addChild(Die);
Devang Patel1f4690c2009-12-15 19:16:48 +0000917 } else if (Context.isNameSpace()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000918 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
Devang Patel1f4690c2009-12-15 19:16:48 +0000919 ContextDIE->addChild(Die);
Stuart Hastingsafe54f12010-06-11 20:08:44 +0000920 } else if (Context.isSubprogram()) {
Devang Patel185051c2010-09-27 23:15:27 +0000921 DIE *ContextDIE = createSubprogramDIE(DISubprogram(Context));
Stuart Hastingsafe54f12010-06-11 20:08:44 +0000922 ContextDIE->addChild(Die);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000923 } else if (DIE *ContextDIE = getCompileUnit(Context)->getDIE(Context))
Devang Patel2b75ed22009-12-10 19:14:49 +0000924 ContextDIE->addChild(Die);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000925 else
Devang Patel1a0df9a2010-05-10 22:49:55 +0000926 getCompileUnit(Context)->addDie(Die);
Devang Patel2b75ed22009-12-10 19:14:49 +0000927}
928
Devang Patelb5b60ea2009-12-10 18:05:33 +0000929/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
930/// given DIType.
931DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
Devang Patel1a0df9a2010-05-10 22:49:55 +0000932 CompileUnit *TypeCU = getCompileUnit(Ty);
933 DIE *TyDIE = TypeCU->getDIE(Ty);
Devang Patelb5b60ea2009-12-10 18:05:33 +0000934 if (TyDIE)
935 return TyDIE;
936
937 // Create new type.
938 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000939 TypeCU->insertDIE(Ty, TyDIE);
Devang Patelb5b60ea2009-12-10 18:05:33 +0000940 if (Ty.isBasicType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000941 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000942 else if (Ty.isCompositeType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000943 constructTypeDIE(*TyDIE, DICompositeType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000944 else {
945 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patelcfa8e9d2010-05-07 18:11:54 +0000946 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
Devang Patelb5b60ea2009-12-10 18:05:33 +0000947 }
948
Devang Patel2b75ed22009-12-10 19:14:49 +0000949 addToContextOwner(TyDIE, Ty.getContext());
Devang Patelb5b60ea2009-12-10 18:05:33 +0000950 return TyDIE;
951}
952
Devang Patel930143b2009-11-21 02:48:08 +0000953/// addType - Add a new type attribute to the specified entity.
Devang Patel9ccfb642009-12-09 18:24:21 +0000954void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel8d6a2b72010-05-07 21:45:47 +0000955 if (!Ty.Verify())
Bill Wendling2f921f82009-05-15 09:23:25 +0000956 return;
957
958 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +0000959 CompileUnit *TypeCU = getCompileUnit(Ty);
960 DIEEntry *Entry = TypeCU->getDIEEntry(Ty);
Bill Wendling2f921f82009-05-15 09:23:25 +0000961 // If it exists then use the existing value.
Devang Patel930143b2009-11-21 02:48:08 +0000962 if (Entry) {
963 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000964 return;
965 }
966
Bill Wendling2f921f82009-05-15 09:23:25 +0000967 // Construct type.
Devang Patelb5b60ea2009-12-10 18:05:33 +0000968 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling2f921f82009-05-15 09:23:25 +0000969
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000970 // Set up proxy.
971 Entry = createDIEEntry(Buffer);
Devang Patel1a0df9a2010-05-10 22:49:55 +0000972 TypeCU->insertDIEEntry(Ty, Entry);
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +0000973
Devang Patel930143b2009-11-21 02:48:08 +0000974 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000975}
976
Devang Patel930143b2009-11-21 02:48:08 +0000977/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000978void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000979 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000980 StringRef Name = BTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000981 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel930143b2009-11-21 02:48:08 +0000982 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling2f921f82009-05-15 09:23:25 +0000983 BTy.getEncoding());
984
985 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +0000986 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000987 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000988 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel930143b2009-11-21 02:48:08 +0000989 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000990}
991
Devang Patel930143b2009-11-21 02:48:08 +0000992/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel9ccfb642009-12-09 18:24:21 +0000993void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000994 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000995 StringRef Name = DTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000996 uint64_t Size = DTy.getSizeInBits() >> 3;
997 unsigned Tag = DTy.getTag();
998
999 // FIXME - Workaround for templates.
1000 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
1001
1002 Buffer.setTag(Tag);
1003
1004 // Map to main type, void will not have a type.
1005 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel9ccfb642009-12-09 18:24:21 +00001006 addType(&Buffer, FromTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001007
1008 // Add name if not anonymous or intermediate type.
Devang Patelae466ef2009-11-30 23:56:56 +00001009 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001010 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +00001011
1012 // Add size if non-zero (derived types might be zero-sized.)
1013 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +00001014 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +00001015
1016 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb5b51592009-11-23 18:43:37 +00001017 if (!DTy.isForwardDecl())
Devang Patelb6511a32010-08-09 20:20:05 +00001018 addSourceLine(&Buffer, DTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001019}
1020
Devang Patel930143b2009-11-21 02:48:08 +00001021/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel9ccfb642009-12-09 18:24:21 +00001022void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001023 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +00001024 StringRef Name = CTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +00001025
1026 uint64_t Size = CTy.getSizeInBits() >> 3;
1027 unsigned Tag = CTy.getTag();
1028 Buffer.setTag(Tag);
1029
1030 switch (Tag) {
1031 case dwarf::DW_TAG_vector_type:
1032 case dwarf::DW_TAG_array_type:
Devang Patel9ccfb642009-12-09 18:24:21 +00001033 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001034 break;
1035 case dwarf::DW_TAG_enumeration_type: {
1036 DIArray Elements = CTy.getTypeArray();
1037
1038 // Add enumerators to enumeration type.
1039 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1040 DIE *ElemDie = NULL;
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001041 DIDescriptor Enum(Elements.getElement(i));
Devang Patel3b548aa2010-03-08 20:52:55 +00001042 if (Enum.isEnumerator()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001043 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
Devang Patel930143b2009-11-21 02:48:08 +00001044 Buffer.addChild(ElemDie);
Devang Patelfafa1fe2009-10-09 17:51:49 +00001045 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001046 }
1047 }
1048 break;
1049 case dwarf::DW_TAG_subroutine_type: {
1050 // Add return type.
1051 DIArray Elements = CTy.getTypeArray();
1052 DIDescriptor RTy = Elements.getElement(0);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001053 addType(&Buffer, DIType(RTy));
Bill Wendling2f921f82009-05-15 09:23:25 +00001054
Devang Patel9a33ec22010-10-06 20:50:40 +00001055 bool isPrototyped = true;
Bill Wendling2f921f82009-05-15 09:23:25 +00001056 // Add arguments.
1057 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001058 DIDescriptor Ty = Elements.getElement(i);
Devang Patel9a33ec22010-10-06 20:50:40 +00001059 if (Ty.isUnspecifiedParameter()) {
1060 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
1061 Buffer.addChild(Arg);
1062 isPrototyped = false;
1063 } else {
1064 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1065 addType(Arg, DIType(Ty));
1066 Buffer.addChild(Arg);
1067 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001068 }
Devang Patel9a33ec22010-10-06 20:50:40 +00001069 // Add prototype flag.
1070 if (isPrototyped)
1071 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001072 }
1073 break;
1074 case dwarf::DW_TAG_structure_type:
1075 case dwarf::DW_TAG_union_type:
1076 case dwarf::DW_TAG_class_type: {
1077 // Add elements to structure type.
1078 DIArray Elements = CTy.getTypeArray();
1079
1080 // A forward struct declared type may not have elements available.
Devang Patel3b548aa2010-03-08 20:52:55 +00001081 unsigned N = Elements.getNumElements();
1082 if (N == 0)
Bill Wendling2f921f82009-05-15 09:23:25 +00001083 break;
1084
1085 // Add elements to structure type.
Devang Patel3b548aa2010-03-08 20:52:55 +00001086 for (unsigned i = 0; i < N; ++i) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001087 DIDescriptor Element = Elements.getElement(i);
1088 DIE *ElemDie = NULL;
Devang Patelcb03b142010-09-29 21:44:16 +00001089 if (Element.isSubprogram()) {
1090 DISubprogram SP(Element);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001091 ElemDie = createSubprogramDIE(DISubprogram(Element));
Devang Patelcb03b142010-09-29 21:44:16 +00001092 if (SP.isProtected())
1093 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1094 dwarf::DW_ACCESS_protected);
1095 else if (SP.isPrivate())
1096 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1097 dwarf::DW_ACCESS_private);
1098 else
1099 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1100 dwarf::DW_ACCESS_public);
Devang Patele1c71462010-10-01 23:31:40 +00001101 if (SP.isExplicit())
1102 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
Devang Patelcb03b142010-09-29 21:44:16 +00001103 }
Devang Patel3b548aa2010-03-08 20:52:55 +00001104 else if (Element.isVariable()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001105 DIVariable DV(Element);
Devang Patel160c92d2010-01-30 01:08:30 +00001106 ElemDie = new DIE(dwarf::DW_TAG_variable);
1107 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1108 DV.getName());
1109 addType(ElemDie, DV.getType());
1110 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1111 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patelb6511a32010-08-09 20:20:05 +00001112 addSourceLine(ElemDie, DV);
Devang Patel3b548aa2010-03-08 20:52:55 +00001113 } else if (Element.isDerivedType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001114 ElemDie = createMemberDIE(DIDerivedType(Element));
Devang Patel3b548aa2010-03-08 20:52:55 +00001115 else
1116 continue;
Devang Patel930143b2009-11-21 02:48:08 +00001117 Buffer.addChild(ElemDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001118 }
1119
Devang Patel3082c012009-08-27 23:51:51 +00001120 if (CTy.isAppleBlockExtension())
Devang Patel930143b2009-11-21 02:48:08 +00001121 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001122
1123 unsigned RLang = CTy.getRunTimeLang();
1124 if (RLang)
Devang Patel930143b2009-11-21 02:48:08 +00001125 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling2f921f82009-05-15 09:23:25 +00001126 dwarf::DW_FORM_data1, RLang);
Devang Patel303a1be2010-01-26 21:16:06 +00001127
1128 DICompositeType ContainingType = CTy.getContainingType();
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001129 if (DIDescriptor(ContainingType).isCompositeType())
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001130 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001131 getOrCreateTypeDIE(DIType(ContainingType)));
Stuart Hastingsafe54f12010-06-11 20:08:44 +00001132 else {
1133 DIDescriptor Context = CTy.getContext();
1134 addToContextOwner(&Buffer, Context);
1135 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001136 break;
1137 }
1138 default:
1139 break;
1140 }
1141
1142 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +00001143 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001144 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +00001145
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001146 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
Devang Patel30265c42010-07-07 20:12:52 +00001147 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1148 {
Bill Wendling2f921f82009-05-15 09:23:25 +00001149 // Add size if non-zero (derived types might be zero-sized.)
1150 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +00001151 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +00001152 else {
1153 // Add zero size if it is not a forward declaration.
1154 if (CTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +00001155 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001156 else
Devang Patel930143b2009-11-21 02:48:08 +00001157 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling2f921f82009-05-15 09:23:25 +00001158 }
1159
1160 // Add source line info if available.
1161 if (!CTy.isForwardDecl())
Devang Patelb6511a32010-08-09 20:20:05 +00001162 addSourceLine(&Buffer, CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001163 }
1164}
1165
Devang Patel930143b2009-11-21 02:48:08 +00001166/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1167void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling2f921f82009-05-15 09:23:25 +00001168 int64_t L = SR.getLo();
1169 int64_t H = SR.getHi();
1170 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1171
Devang Patel930143b2009-11-21 02:48:08 +00001172 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patelf691df32009-08-14 20:59:16 +00001173 if (L)
Devang Patel930143b2009-11-21 02:48:08 +00001174 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Patel8f046022009-12-04 23:10:24 +00001175 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling2f921f82009-05-15 09:23:25 +00001176
Devang Patel930143b2009-11-21 02:48:08 +00001177 Buffer.addChild(DW_Subrange);
Bill Wendling2f921f82009-05-15 09:23:25 +00001178}
1179
Devang Patel930143b2009-11-21 02:48:08 +00001180/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel9ccfb642009-12-09 18:24:21 +00001181void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling2f921f82009-05-15 09:23:25 +00001182 DICompositeType *CTy) {
1183 Buffer.setTag(dwarf::DW_TAG_array_type);
1184 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel930143b2009-11-21 02:48:08 +00001185 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001186
1187 // Emit derived type.
Devang Patel9ccfb642009-12-09 18:24:21 +00001188 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +00001189 DIArray Elements = CTy->getTypeArray();
1190
Devang Patel92e8c652009-11-21 00:31:03 +00001191 // Get an anonymous type for index type.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001192 CompileUnit *TheCU = getCompileUnit(*CTy);
1193 DIE *IdxTy = TheCU->getIndexTyDie();
Devang Patel92e8c652009-11-21 00:31:03 +00001194 if (!IdxTy) {
1195 // Construct an anonymous type for index type.
1196 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel930143b2009-11-21 02:48:08 +00001197 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1198 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel92e8c652009-11-21 00:31:03 +00001199 dwarf::DW_ATE_signed);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001200 TheCU->addDie(IdxTy);
1201 TheCU->setIndexTyDie(IdxTy);
Devang Patel92e8c652009-11-21 00:31:03 +00001202 }
Bill Wendling2f921f82009-05-15 09:23:25 +00001203
1204 // Add subranges to array type.
1205 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1206 DIDescriptor Element = Elements.getElement(i);
1207 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001208 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
Bill Wendling2f921f82009-05-15 09:23:25 +00001209 }
1210}
1211
Devang Patel930143b2009-11-21 02:48:08 +00001212/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel3b548aa2010-03-08 20:52:55 +00001213DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001214 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel3b548aa2010-03-08 20:52:55 +00001215 StringRef Name = ETy.getName();
Devang Patel930143b2009-11-21 02:48:08 +00001216 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel3b548aa2010-03-08 20:52:55 +00001217 int64_t Value = ETy.getEnumValue();
Devang Patel930143b2009-11-21 02:48:08 +00001218 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +00001219 return Enumerator;
1220}
1221
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001222/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
Devang Patel43ef34d2010-01-05 01:46:14 +00001223/// printer to not emit usual symbol prefix before the symbol name is used then
1224/// return linkage name after skipping this special LLVM prefix.
1225static StringRef getRealLinkageName(StringRef LinkageName) {
1226 char One = '\1';
1227 if (LinkageName.startswith(StringRef(&One, 1)))
1228 return LinkageName.substr(1);
1229 return LinkageName;
1230}
1231
Devang Patel930143b2009-11-21 02:48:08 +00001232/// createMemberDIE - Create new member DIE.
Devang Patel18ba0b42010-08-10 04:12:17 +00001233DIE *DwarfDebug::createMemberDIE(DIDerivedType DT) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001234 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel2d9caf92009-11-25 17:36:49 +00001235 StringRef Name = DT.getName();
1236 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001237 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001238
Devang Patel9ccfb642009-12-09 18:24:21 +00001239 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +00001240
Devang Patelb6511a32010-08-09 20:20:05 +00001241 addSourceLine(MemberDie, DT);
Bill Wendling2f921f82009-05-15 09:23:25 +00001242
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001243 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel930143b2009-11-21 02:48:08 +00001244 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel67f56f02009-11-04 22:06:12 +00001245
Bill Wendling2f921f82009-05-15 09:23:25 +00001246 uint64_t Size = DT.getSizeInBits();
Devang Patelf05d5722009-11-04 23:48:00 +00001247 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling2f921f82009-05-15 09:23:25 +00001248
1249 if (Size != FieldSize) {
1250 // Handle bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001251 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1252 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling2f921f82009-05-15 09:23:25 +00001253
1254 uint64_t Offset = DT.getOffsetInBits();
Bill Wendling2f921f82009-05-15 09:23:25 +00001255 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1256 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer2b459982010-01-07 17:50:57 +00001257 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendling2f921f82009-05-15 09:23:25 +00001258 Offset -= FieldOffset;
1259
1260 // Maybe we need to work from the other end.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001261 if (Asm->getTargetData().isLittleEndian())
1262 Offset = FieldSize - (Offset + Size);
Devang Patel930143b2009-11-21 02:48:08 +00001263 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling2f921f82009-05-15 09:23:25 +00001264
Devang Patel67f56f02009-11-04 22:06:12 +00001265 // Here WD_AT_data_member_location points to the anonymous
1266 // field that includes this bit field.
Devang Patel930143b2009-11-21 02:48:08 +00001267 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001268
1269 } else
1270 // This is not a bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001271 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001272
Devang Pateld2316892010-02-03 20:08:48 +00001273 if (DT.getTag() == dwarf::DW_TAG_inheritance
1274 && DT.isVirtual()) {
1275
1276 // For C++, virtual base classes are not at fixed offset. Use following
1277 // expression to extract appropriate offset from vtable.
1278 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1279
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001280 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Pateld2316892010-02-03 20:08:48 +00001281 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1282 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1283 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1284 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1285 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1286 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1287 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1288
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001289 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
Devang Pateld2316892010-02-03 20:08:48 +00001290 VBaseLocationDie);
1291 } else
1292 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001293
1294 if (DT.isProtected())
Devang Pateleb57c592009-12-03 19:11:07 +00001295 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001296 dwarf::DW_ACCESS_protected);
1297 else if (DT.isPrivate())
Devang Pateleb57c592009-12-03 19:11:07 +00001298 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001299 dwarf::DW_ACCESS_private);
Devang Patela1bd5a12010-09-29 19:08:08 +00001300 // Otherwise C++ member and base classes are considered public.
1301 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
Devang Pateleb57c592009-12-03 19:11:07 +00001302 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1303 dwarf::DW_ACCESS_public);
1304 if (DT.isVirtual())
1305 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1306 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling2f921f82009-05-15 09:23:25 +00001307 return MemberDie;
1308}
1309
Devang Patel525dda02009-12-14 16:18:45 +00001310/// createSubprogramDIE - Create new DIE using SP.
Devang Patel185051c2010-09-27 23:15:27 +00001311DIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001312 CompileUnit *SPCU = getCompileUnit(SP);
1313 DIE *SPDie = SPCU->getDIE(SP);
Devang Patel525dda02009-12-14 16:18:45 +00001314 if (SPDie)
1315 return SPDie;
1316
1317 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patelf200b392010-03-02 17:58:15 +00001318 // Constructors and operators for anonymous aggregates do not have names.
Devang Pateld0fa3042010-03-02 01:26:20 +00001319 if (!SP.getName().empty())
1320 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling2f921f82009-05-15 09:23:25 +00001321
Devang Patel2d9caf92009-11-25 17:36:49 +00001322 StringRef LinkageName = SP.getLinkageName();
Devang Patel43ef34d2010-01-05 01:46:14 +00001323 if (!LinkageName.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001324 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel43ef34d2010-01-05 01:46:14 +00001325 getRealLinkageName(LinkageName));
1326
Devang Patelb6511a32010-08-09 20:20:05 +00001327 addSourceLine(SPDie, SP);
Bill Wendling2f921f82009-05-15 09:23:25 +00001328
Devang Patel3a24f922010-10-07 22:03:01 +00001329 if (SP.isPrototyped())
Devang Patel930143b2009-11-21 02:48:08 +00001330 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001331
1332 // Add Return Type.
Devang Patel236526d2009-12-03 01:25:38 +00001333 DICompositeType SPTy = SP.getType();
1334 DIArray Args = SPTy.getTypeArray();
Bill Wendling2f921f82009-05-15 09:23:25 +00001335 unsigned SPTag = SPTy.getTag();
Devang Pateleb57c592009-12-03 19:11:07 +00001336
Devang Patel3b548aa2010-03-08 20:52:55 +00001337 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel9ccfb642009-12-09 18:24:21 +00001338 addType(SPDie, SPTy);
Devang Patel236526d2009-12-03 01:25:38 +00001339 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001340 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel236526d2009-12-03 01:25:38 +00001341
Devang Pateleb57c592009-12-03 19:11:07 +00001342 unsigned VK = SP.getVirtuality();
1343 if (VK) {
1344 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer74729ae2010-03-31 19:34:01 +00001345 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Pateleb57c592009-12-03 19:11:07 +00001346 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1347 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1348 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001349 ContainingTypeMap.insert(std::make_pair(SPDie,
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001350 SP.getContainingType()));
Devang Pateleb57c592009-12-03 19:11:07 +00001351 }
1352
Devang Patel185051c2010-09-27 23:15:27 +00001353 if (!SP.isDefinition()) {
Devang Patel930143b2009-11-21 02:48:08 +00001354 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001355
1356 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel236526d2009-12-03 01:25:38 +00001357 // be handled while processing variables.
1358 DICompositeType SPTy = SP.getType();
1359 DIArray Args = SPTy.getTypeArray();
1360 unsigned SPTag = SPTy.getTag();
1361
Bill Wendling2f921f82009-05-15 09:23:25 +00001362 if (SPTag == dwarf::DW_TAG_subroutine_type)
1363 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1364 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001365 DIType ATy = DIType(DIType(Args.getElement(i)));
Devang Patel6efc8e52010-02-06 01:02:37 +00001366 addType(Arg, ATy);
1367 if (ATy.isArtificial())
1368 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel930143b2009-11-21 02:48:08 +00001369 SPDie->addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +00001370 }
1371 }
1372
Devang Patel999b4992010-02-03 19:57:19 +00001373 if (SP.isArtificial())
1374 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1375
Devang Patelb4e3b902010-04-30 19:38:23 +00001376 if (!SP.isLocalToUnit())
1377 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001378
Devang Patelb4e3b902010-04-30 19:38:23 +00001379 if (SP.isOptimized())
1380 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1381
Jim Grosbach965a73a2010-07-21 23:03:52 +00001382 if (unsigned isa = Asm->getISAEncoding()) {
1383 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1384 }
1385
Devang Patelb4e3b902010-04-30 19:38:23 +00001386 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001387 SPCU->insertDIE(SP, SPDie);
Devang Patelb4e3b902010-04-30 19:38:23 +00001388
Stuart Hastingsafe54f12010-06-11 20:08:44 +00001389 // Add to context owner.
1390 addToContextOwner(SPDie, SP.getContext());
1391
Bill Wendling2f921f82009-05-15 09:23:25 +00001392 return SPDie;
1393}
1394
Devang Patel32cc43c2010-05-07 20:54:48 +00001395DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
Chris Lattner8d2fe282010-03-31 05:36:29 +00001396 assert(N && "Invalid Scope encoding!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001397
1398 DbgScope *AScope = AbstractScopes.lookup(N);
1399 if (AScope)
1400 return AScope;
Jim Grosbach042483e2009-11-21 23:12:12 +00001401
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001402 DbgScope *Parent = NULL;
1403
1404 DIDescriptor Scope(N);
1405 if (Scope.isLexicalBlock()) {
1406 DILexicalBlock DB(N);
1407 DIDescriptor ParentDesc = DB.getContext();
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001408 Parent = getOrCreateAbstractScope(ParentDesc);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001409 }
1410
1411 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1412
1413 if (Parent)
Devang Patel930143b2009-11-21 02:48:08 +00001414 Parent->addScope(AScope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001415 AScope->setAbstractScope();
1416 AbstractScopes[N] = AScope;
1417 if (DIDescriptor(N).isSubprogram())
1418 AbstractScopesList.push_back(AScope);
1419 return AScope;
1420}
Devang Patel75cc16c2009-10-01 20:31:14 +00001421
Devang Patel019922d2010-04-06 23:53:48 +00001422/// isSubprogramContext - Return true if Context is either a subprogram
1423/// or another context nested inside a subprogram.
Devang Patel32cc43c2010-05-07 20:54:48 +00001424static bool isSubprogramContext(const MDNode *Context) {
Devang Patel019922d2010-04-06 23:53:48 +00001425 if (!Context)
1426 return false;
1427 DIDescriptor D(Context);
1428 if (D.isSubprogram())
1429 return true;
1430 if (D.isType())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001431 return isSubprogramContext(DIType(Context).getContext());
Devang Patel019922d2010-04-06 23:53:48 +00001432 return false;
1433}
1434
Jim Grosbach042483e2009-11-21 23:12:12 +00001435/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +00001436/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1437/// If there are global variables in this scope then create and insert
1438/// DIEs for these variables.
Devang Patel32cc43c2010-05-07 20:54:48 +00001439DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001440 CompileUnit *SPCU = getCompileUnit(SPNode);
1441 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patela37a95e2010-07-07 22:20:57 +00001442
Chris Lattner3a383cb2010-04-05 00:13:49 +00001443 assert(SPDie && "Unable to find subprogram DIE!");
1444 DISubprogram SP(SPNode);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001445
Chris Lattner3a383cb2010-04-05 00:13:49 +00001446 // There is not any need to generate specification DIE for a function
1447 // defined at compile unit level. If a function is defined inside another
1448 // function then gdb prefers the definition at top level and but does not
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001449 // expect specification DIE in parent function. So avoid creating
Chris Lattner3a383cb2010-04-05 00:13:49 +00001450 // specification DIE for a function defined inside a function.
1451 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001452 !SP.getContext().isFile() &&
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001453 !isSubprogramContext(SP.getContext())) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00001454 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001455
1456 // Add arguments.
Chris Lattner3a383cb2010-04-05 00:13:49 +00001457 DICompositeType SPTy = SP.getType();
1458 DIArray Args = SPTy.getTypeArray();
1459 unsigned SPTag = SPTy.getTag();
1460 if (SPTag == dwarf::DW_TAG_subroutine_type)
1461 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1462 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001463 DIType ATy = DIType(DIType(Args.getElement(i)));
Chris Lattner3a383cb2010-04-05 00:13:49 +00001464 addType(Arg, ATy);
1465 if (ATy.isArtificial())
1466 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1467 SPDie->addChild(Arg);
1468 }
1469 DIE *SPDeclDie = SPDie;
1470 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001471 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
Chris Lattner3a383cb2010-04-05 00:13:49 +00001472 SPDeclDie);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001473 SPCU->addDie(SPDie);
Chris Lattner3a383cb2010-04-05 00:13:49 +00001474 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001475
Devang Patela37a95e2010-07-07 22:20:57 +00001476 // Pick up abstract subprogram DIE.
1477 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
1478 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1479 addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
1480 dwarf::DW_FORM_ref4, AbsSPDIE);
1481 SPCU->addDie(SPDie);
1482 }
1483
Chris Lattner3a383cb2010-04-05 00:13:49 +00001484 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1485 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1486 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1487 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1488 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1489 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1490 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +00001491
Chris Lattner3a383cb2010-04-05 00:13:49 +00001492 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001493}
1494
Jim Grosbach042483e2009-11-21 23:12:12 +00001495/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +00001496/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1497DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +00001498
1499 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1500 if (Scope->isAbstractScope())
1501 return ScopeDIE;
1502
1503 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
1504 if (Ranges.empty())
1505 return 0;
1506
1507 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
1508 if (Ranges.size() > 1) {
1509 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001510 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Patel6c74a872010-04-27 19:46:33 +00001511 // DW_AT_ranges appropriately.
1512 addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
1513 DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
1514 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
1515 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +00001516 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
1517 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +00001518 }
1519 DebugRangeSymbols.push_back(NULL);
1520 DebugRangeSymbols.push_back(NULL);
1521 return ScopeDIE;
1522 }
1523
Devang Patel9fc11702010-05-25 23:40:22 +00001524 const MCSymbol *Start = getLabelBeforeInsn(RI->first);
1525 const MCSymbol *End = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001526
Devang Patel9fc11702010-05-25 23:40:22 +00001527 if (End == 0) return 0;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001528
Chris Lattnere13c3722010-03-09 01:58:53 +00001529 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1530 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001531
Devang Patel6c74a872010-04-27 19:46:33 +00001532 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
1533 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001534
1535 return ScopeDIE;
1536}
1537
Devang Patel930143b2009-11-21 02:48:08 +00001538/// constructInlinedScopeDIE - This scope represents inlined body of
1539/// a function. Construct DIE to represent this concrete inlined copy
1540/// of the function.
1541DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel6c74a872010-04-27 19:46:33 +00001542
1543 const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001544 assert (Ranges.empty() == false
Devang Patel6c74a872010-04-27 19:46:33 +00001545 && "DbgScope does not have instruction markers!");
1546
1547 // FIXME : .debug_inlined section specification does not clearly state how
1548 // to emit inlined scope that is split into multiple instruction ranges.
1549 // For now, use first instruction range and emit low_pc/high_pc pair and
1550 // corresponding .debug_inlined section entry for this pair.
1551 SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
Devang Patel9fc11702010-05-25 23:40:22 +00001552 const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
1553 const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001554
Devang Patel4c6bd662010-07-08 22:39:20 +00001555 if (StartLabel == 0 || EndLabel == 0) {
Devang Patel6c74a872010-04-27 19:46:33 +00001556 assert (0 && "Unexpected Start and End labels for a inlined scope!");
1557 return 0;
1558 }
Chris Lattnere13c3722010-03-09 01:58:53 +00001559 assert(StartLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001560 "Invalid starting label for an inlined scope!");
Chris Lattnere13c3722010-03-09 01:58:53 +00001561 assert(EndLabel->isDefined() &&
Chris Lattnerc3b70f62010-03-09 01:51:43 +00001562 "Invalid end label for an inlined scope!");
Devang Patel6c74a872010-04-27 19:46:33 +00001563
Devang Patel3b548aa2010-03-08 20:52:55 +00001564 if (!Scope->getScopeNode())
Devang Patelbc97f6b2010-03-08 19:20:38 +00001565 return NULL;
Devang Patel3b548aa2010-03-08 20:52:55 +00001566 DIScope DS(Scope->getScopeNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001567 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1568
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001569 DISubprogram InlinedSP = getDISubprogram(DS);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001570 CompileUnit *TheCU = getCompileUnit(InlinedSP);
1571 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Chris Lattner8d2fe282010-03-31 05:36:29 +00001572 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patel930143b2009-11-21 02:48:08 +00001573 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001574 dwarf::DW_FORM_ref4, OriginDIE);
1575
Chris Lattner085b6522010-03-09 00:31:02 +00001576 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattnere13c3722010-03-09 01:58:53 +00001577 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001578
1579 InlinedSubprogramDIEs.insert(OriginDIE);
1580
1581 // Track the start label for this inlined function.
Devang Patel32cc43c2010-05-07 20:54:48 +00001582 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001583 I = InlineInfo.find(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001584
1585 if (I == InlineInfo.end()) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001586 InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
Jim Grosbach00e9c612009-11-22 19:20:36 +00001587 ScopeDIE));
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001588 InlinedSPNodes.push_back(InlinedSP);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001589 } else
Chris Lattner085b6522010-03-09 00:31:02 +00001590 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001591
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001592 DILocation DL(Scope->getInlinedAt());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001593 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
Devang Patel930143b2009-11-21 02:48:08 +00001594 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001595
1596 return ScopeDIE;
1597}
1598
Devang Patel930143b2009-11-21 02:48:08 +00001599
1600/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel9ccfb642009-12-09 18:24:21 +00001601DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001602 StringRef Name = DV->getName();
Devang Patel2d9caf92009-11-25 17:36:49 +00001603 if (Name.empty())
Devang Patel97f99fa2009-11-13 02:25:26 +00001604 return NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001605
1606 // Translate tag to proper Dwarf tag. The result variable is dropped for
1607 // now.
1608 unsigned Tag;
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001609 switch (DV->getTag()) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001610 case dwarf::DW_TAG_return_variable:
1611 return NULL;
1612 case dwarf::DW_TAG_arg_variable:
1613 Tag = dwarf::DW_TAG_formal_parameter;
1614 break;
1615 case dwarf::DW_TAG_auto_variable: // fall thru
1616 default:
1617 Tag = dwarf::DW_TAG_variable;
1618 break;
1619 }
1620
1621 // Define variable debug information entry.
1622 DIE *VariableDie = new DIE(Tag);
1623
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001624 DIE *AbsDIE = NULL;
Devang Patele1c53f22010-05-20 16:36:41 +00001625 DenseMap<const DbgVariable *, const DbgVariable *>::iterator
1626 V2AVI = VarToAbstractVarMap.find(DV);
1627 if (V2AVI != VarToAbstractVarMap.end())
1628 AbsDIE = V2AVI->second->getDIE();
Jim Grosbach042483e2009-11-21 23:12:12 +00001629
Devang Patele1c53f22010-05-20 16:36:41 +00001630 if (AbsDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001631 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001632 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001633 else {
Devang Patel930143b2009-11-21 02:48:08 +00001634 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001635 addSourceLine(VariableDie, DV->getVariable());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001636
1637 // Add variable type.
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001638 addType(VariableDie, DV->getType());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001639 }
1640
Devang Patel6d9f9fe2010-08-09 21:01:39 +00001641 if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
Devang Patel6efc8e52010-02-06 01:02:37 +00001642 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelbea08d12010-09-29 23:07:21 +00001643 else if (DIVariable(DV->getVariable()).isArtificial())
1644 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel9fc11702010-05-25 23:40:22 +00001645
1646 if (Scope->isAbstractScope()) {
1647 DV->setDIE(VariableDie);
1648 return VariableDie;
1649 }
1650
1651 // Add variable address.
1652
1653 unsigned Offset = DV->getDotDebugLocOffset();
1654 if (Offset != ~0U) {
1655 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1656 Asm->GetTempSymbol("debug_loc", Offset));
1657 DV->setDIE(VariableDie);
1658 UseDotDebugLocEntry.insert(VariableDie);
1659 return VariableDie;
1660 }
1661
1662 // Check if variable is described by a DBG_VALUE instruction.
1663 DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
1664 DbgVariableToDbgInstMap.find(DV);
1665 if (DVI != DbgVariableToDbgInstMap.end()) {
1666 const MachineInstr *DVInsn = DVI->second;
1667 const MCSymbol *DVLabel = findVariableLabel(DV);
1668 bool updated = false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001669 // FIXME : Handle getNumOperands != 3
Devang Patel9fc11702010-05-25 23:40:22 +00001670 if (DVInsn->getNumOperands() == 3) {
Devang Patel86ec8b32010-08-31 22:22:42 +00001671 if (DVInsn->getOperand(0).isReg()) {
1672 const MachineOperand RegOp = DVInsn->getOperand(0);
1673 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1674 if (DVInsn->getOperand(1).isImm() &&
1675 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1676 addVariableAddress(DV, VariableDie, DVInsn->getOperand(1).getImm());
1677 updated = true;
1678 } else
1679 updated = addRegisterAddress(VariableDie, DVLabel, RegOp);
1680 }
Devang Patel9fc11702010-05-25 23:40:22 +00001681 else if (DVInsn->getOperand(0).isImm())
1682 updated = addConstantValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001683 else if (DVInsn->getOperand(0).isFPImm())
1684 updated =
Devang Patel30265c42010-07-07 20:12:52 +00001685 addConstantFPValue(VariableDie, DVLabel, DVInsn->getOperand(0));
Devang Patel9fc11702010-05-25 23:40:22 +00001686 } else {
1687 MachineLocation Location = Asm->getDebugValueLocation(DVInsn);
1688 if (Location.getReg()) {
1689 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1690 if (DVLabel)
1691 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1692 DVLabel);
1693 updated = true;
1694 }
1695 }
1696 if (!updated) {
1697 // If variableDie is not updated then DBG_VALUE instruction does not
1698 // have valid variable info.
1699 delete VariableDie;
1700 return NULL;
1701 }
1702 DV->setDIE(VariableDie);
1703 return VariableDie;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001704 }
Devang Patel9fc11702010-05-25 23:40:22 +00001705
1706 // .. else use frame index, if available.
Devang Patel9fc11702010-05-25 23:40:22 +00001707 int FI = 0;
Devang Patel2cfc3af2010-08-31 06:11:28 +00001708 if (findVariableFrameIndex(DV, &FI))
1709 addVariableAddress(DV, VariableDie, FI);
1710
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001711 DV->setDIE(VariableDie);
1712 return VariableDie;
1713
1714}
Devang Patel930143b2009-11-21 02:48:08 +00001715
Devang Patel04d2f2d2009-11-24 01:14:22 +00001716void DwarfDebug::addPubTypes(DISubprogram SP) {
1717 DICompositeType SPTy = SP.getType();
1718 unsigned SPTag = SPTy.getTag();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001719 if (SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel04d2f2d2009-11-24 01:14:22 +00001720 return;
1721
1722 DIArray Args = SPTy.getTypeArray();
Devang Patel04d2f2d2009-11-24 01:14:22 +00001723 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001724 DIType ATy(Args.getElement(i));
Devang Patel8d6a2b72010-05-07 21:45:47 +00001725 if (!ATy.Verify())
Devang Patel04d2f2d2009-11-24 01:14:22 +00001726 continue;
1727 DICompositeType CATy = getDICompositeType(ATy);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001728 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
Devang Patel12d150e2010-04-13 20:35:04 +00001729 && !CATy.isForwardDecl()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001730 CompileUnit *TheCU = getCompileUnit(CATy);
1731 if (DIEEntry *Entry = TheCU->getDIEEntry(CATy))
1732 TheCU->addGlobalType(CATy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +00001733 }
1734 }
1735}
1736
Devang Patel930143b2009-11-21 02:48:08 +00001737/// constructScopeDIE - Construct a DIE for this scope.
1738DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel3b548aa2010-03-08 20:52:55 +00001739 if (!Scope || !Scope->getScopeNode())
1740 return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001741
Devang Patel3b548aa2010-03-08 20:52:55 +00001742 DIScope DS(Scope->getScopeNode());
1743 DIE *ScopeDIE = NULL;
1744 if (Scope->getInlinedAt())
1745 ScopeDIE = constructInlinedScopeDIE(Scope);
1746 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +00001747 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +00001748 if (Scope->isAbstractScope()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001749 ScopeDIE = getCompileUnit(DS)->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +00001750 // Note down abstract DIE.
1751 if (ScopeDIE)
1752 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
1753 }
Devang Patel3b548aa2010-03-08 20:52:55 +00001754 else
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001755 ScopeDIE = updateSubprogramScopeDIE(DS);
Devang Patel3b548aa2010-03-08 20:52:55 +00001756 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001757 else
Devang Patel3b548aa2010-03-08 20:52:55 +00001758 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel23b2ae62010-03-29 22:59:58 +00001759 if (!ScopeDIE) return NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001760
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001761 // Add variables to scope.
Devang Patel406798a2010-08-09 18:51:29 +00001762 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001763 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel9ccfb642009-12-09 18:24:21 +00001764 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach042483e2009-11-21 23:12:12 +00001765 if (VariableDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001766 ScopeDIE->addChild(VariableDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001767 }
1768
1769 // Add nested scopes.
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001770 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001771 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1772 // Define the Scope debug information entry.
Devang Patel930143b2009-11-21 02:48:08 +00001773 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach042483e2009-11-21 23:12:12 +00001774 if (NestedDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001775 ScopeDIE->addChild(NestedDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001776 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001777
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001778 if (DS.isSubprogram())
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001779 addPubTypes(DISubprogram(DS));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001780
Devang Patel04d2f2d2009-11-24 01:14:22 +00001781 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001782}
1783
Bill Wendling2b128d72009-05-20 23:19:06 +00001784/// GetOrCreateSourceID - Look up the source id with the given directory and
1785/// source file names. If none currently exists, create a new id and insert it
1786/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1787/// maps as well.
Chris Lattner71696ef2010-03-31 06:06:37 +00001788unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendling2b128d72009-05-20 23:19:06 +00001789 unsigned DId;
Devang Patel84a74772010-07-27 20:51:15 +00001790 assert (DirName.empty() == false && "Invalid directory name!");
Devang Patel498877d2010-07-24 00:53:22 +00001791
Devang Patel871d0b12010-09-16 20:57:49 +00001792 // If FE did not provide a file name, then assume stdin.
1793 if (FileName.empty())
1794 return GetOrCreateSourceID(DirName, "<stdin>");
1795
Bill Wendling2b128d72009-05-20 23:19:06 +00001796 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1797 if (DI != DirectoryIdMap.end()) {
1798 DId = DI->getValue();
1799 } else {
1800 DId = DirectoryNames.size() + 1;
1801 DirectoryIdMap[DirName] = DId;
1802 DirectoryNames.push_back(DirName);
1803 }
1804
1805 unsigned FId;
1806 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1807 if (FI != SourceFileIdMap.end()) {
1808 FId = FI->getValue();
1809 } else {
1810 FId = SourceFileNames.size() + 1;
1811 SourceFileIdMap[FileName] = FId;
1812 SourceFileNames.push_back(FileName);
1813 }
1814
1815 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1816 SourceIdMap.find(std::make_pair(DId, FId));
1817 if (SI != SourceIdMap.end())
1818 return SI->second;
1819
1820 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1821 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1822 SourceIds.push_back(std::make_pair(DId, FId));
1823
1824 return SrcId;
1825}
1826
Devang Patel1f4690c2009-12-15 19:16:48 +00001827/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1828DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001829 CompileUnit *TheCU = getCompileUnit(NS);
1830 DIE *NDie = TheCU->getDIE(NS);
Devang Patel1f4690c2009-12-15 19:16:48 +00001831 if (NDie)
1832 return NDie;
1833 NDie = new DIE(dwarf::DW_TAG_namespace);
Devang Patel1a0df9a2010-05-10 22:49:55 +00001834 TheCU->insertDIE(NS, NDie);
Devang Patel1f4690c2009-12-15 19:16:48 +00001835 if (!NS.getName().empty())
1836 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
Devang Patelb6511a32010-08-09 20:20:05 +00001837 addSourceLine(NDie, NS);
Devang Patel1f4690c2009-12-15 19:16:48 +00001838 addToContextOwner(NDie, NS.getContext());
1839 return NDie;
1840}
1841
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001842/// constructCompileUnit - Create new CompileUnit for the given
Devang Patel1a0df9a2010-05-10 22:49:55 +00001843/// metadata node with tag DW_TAG_compile_unit.
Devang Patel32cc43c2010-05-07 20:54:48 +00001844void DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001845 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +00001846 StringRef FN = DIUnit.getFilename();
1847 StringRef Dir = DIUnit.getDirectory();
Devang Patelb2969422009-09-29 18:40:58 +00001848 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendling2b128d72009-05-20 23:19:06 +00001849
1850 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel930143b2009-11-21 02:48:08 +00001851 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelb2969422009-09-29 18:40:58 +00001852 DIUnit.getProducer());
Devang Patel930143b2009-11-21 02:48:08 +00001853 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendling2b128d72009-05-20 23:19:06 +00001854 DIUnit.getLanguage());
Devang Patel930143b2009-11-21 02:48:08 +00001855 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Devang Patelbd798ce2010-04-26 22:54:28 +00001856 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1857 // simplifies debug range entries.
Devang Patel1de21ec2010-06-28 22:22:47 +00001858 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
Devang Pateld22ed622010-03-22 23:11:36 +00001859 // DW_AT_stmt_list is a offset of line number information for this
Devang Patel4a213872010-08-24 00:06:12 +00001860 // compile unit in debug_line section.
Devang Patelea636392010-08-31 23:50:19 +00001861 if (Asm->MAI->doesDwarfUsesAbsoluteLabelForStmtList())
1862 addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_addr,
1863 Asm->GetTempSymbol("section_line"));
1864 else
1865 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendling2b128d72009-05-20 23:19:06 +00001866
Devang Patel2d9caf92009-11-25 17:36:49 +00001867 if (!Dir.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001868 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +00001869 if (DIUnit.isOptimized())
Devang Patel930143b2009-11-21 02:48:08 +00001870 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +00001871
Devang Patel2d9caf92009-11-25 17:36:49 +00001872 StringRef Flags = DIUnit.getFlags();
1873 if (!Flags.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001874 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendling2b128d72009-05-20 23:19:06 +00001875
1876 unsigned RVer = DIUnit.getRunTimeVersion();
1877 if (RVer)
Devang Patel930143b2009-11-21 02:48:08 +00001878 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +00001879 dwarf::DW_FORM_data1, RVer);
1880
Devang Patel1a0df9a2010-05-10 22:49:55 +00001881 CompileUnit *NewCU = new CompileUnit(ID, Die);
1882 if (!FirstCU)
1883 FirstCU = NewCU;
1884 CUMap.insert(std::make_pair(N, NewCU));
Bill Wendling2b128d72009-05-20 23:19:06 +00001885}
1886
Devang Patel1a0df9a2010-05-10 22:49:55 +00001887/// getCompielUnit - Get CompileUnit DIE.
1888CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
1889 assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
1890 DIDescriptor D(N);
1891 const MDNode *CUNode = NULL;
1892 if (D.isCompileUnit())
1893 CUNode = N;
1894 else if (D.isSubprogram())
1895 CUNode = DISubprogram(N).getCompileUnit();
1896 else if (D.isType())
1897 CUNode = DIType(N).getCompileUnit();
1898 else if (D.isGlobalVariable())
1899 CUNode = DIGlobalVariable(N).getCompileUnit();
1900 else if (D.isVariable())
1901 CUNode = DIVariable(N).getCompileUnit();
1902 else if (D.isNameSpace())
1903 CUNode = DINameSpace(N).getCompileUnit();
1904 else if (D.isFile())
1905 CUNode = DIFile(N).getCompileUnit();
1906 else
1907 return FirstCU;
1908
1909 DenseMap<const MDNode *, CompileUnit *>::const_iterator I
1910 = CUMap.find(CUNode);
1911 if (I == CUMap.end())
1912 return FirstCU;
1913 return I->second;
1914}
1915
Devang Patela8652672010-08-23 18:25:56 +00001916/// isUnsignedDIType - Return true if type encoding is unsigned.
1917static bool isUnsignedDIType(DIType Ty) {
1918 DIDerivedType DTy(Ty);
1919 if (DTy.Verify())
1920 return isUnsignedDIType(DTy.getTypeDerivedFrom());
1921
1922 DIBasicType BTy(Ty);
1923 if (BTy.Verify()) {
1924 unsigned Encoding = BTy.getEncoding();
1925 if (Encoding == dwarf::DW_ATE_unsigned ||
1926 Encoding == dwarf::DW_ATE_unsigned_char)
1927 return true;
1928 }
1929 return false;
1930}
Devang Patel1a0df9a2010-05-10 22:49:55 +00001931
1932/// constructGlobalVariableDIE - Construct global variable DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +00001933void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
Devang Patel469c12d22010-08-10 01:37:23 +00001934 DIGlobalVariable GV(N);
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001935
Devang Patelf5d53602009-09-04 23:59:07 +00001936 // If debug information is malformed then ignore it.
Devang Patel469c12d22010-08-10 01:37:23 +00001937 if (GV.Verify() == false)
Devang Patelf5d53602009-09-04 23:59:07 +00001938 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001939
1940 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +00001941 CompileUnit *TheCU = getCompileUnit(N);
Devang Patel469c12d22010-08-10 01:37:23 +00001942 if (TheCU->getDIE(GV))
Devang Patel0751a282009-06-26 01:49:18 +00001943 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001944
Devang Patel469c12d22010-08-10 01:37:23 +00001945 DIType GTy = GV.getType();
Devang Patelb2197462010-08-10 07:11:13 +00001946 DIE *VariableDIE = new DIE(GV.getTag());
1947
1948 bool isGlobalVariable = GV.getGlobal() != NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001949
Devang Patel469c12d22010-08-10 01:37:23 +00001950 // Add name.
1951 addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1952 GV.getDisplayName());
1953 StringRef LinkageName = GV.getLinkageName();
Devang Patelb2197462010-08-10 07:11:13 +00001954 if (!LinkageName.empty() && isGlobalVariable)
Devang Patel469c12d22010-08-10 01:37:23 +00001955 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
1956 getRealLinkageName(LinkageName));
1957 // Add type.
1958 addType(VariableDIE, GTy);
Devang Patel12d150e2010-04-13 20:35:04 +00001959 if (GTy.isCompositeType() && !GTy.getName().empty()
1960 && !GTy.isForwardDecl()) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00001961 DIEEntry *Entry = TheCU->getDIEEntry(GTy);
Chris Lattner8d2fe282010-03-31 05:36:29 +00001962 assert(Entry && "Missing global type!");
Devang Patel1a0df9a2010-05-10 22:49:55 +00001963 TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
Devang Patel04d2f2d2009-11-24 01:14:22 +00001964 }
Devang Patel469c12d22010-08-10 01:37:23 +00001965 // Add scoping info.
1966 if (!GV.isLocalToUnit()) {
1967 addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1968 // Expose as global.
1969 TheCU->addGlobal(GV.getName(), VariableDIE);
1970 }
1971 // Add line number info.
1972 addSourceLine(VariableDIE, GV);
1973 // Add to map.
1974 TheCU->insertDIE(N, VariableDIE);
1975 // Add to context owner.
1976 DIDescriptor GVContext = GV.getContext();
1977 addToContextOwner(VariableDIE, GVContext);
1978 // Add location.
Devang Patelb2197462010-08-10 07:11:13 +00001979 if (isGlobalVariable) {
1980 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1981 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1982 addLabel(Block, 0, dwarf::DW_FORM_udata,
1983 Asm->Mang->getSymbol(GV.getGlobal()));
1984 // Do not create specification DIE if context is either compile unit
1985 // or a subprogram.
1986 if (GV.isDefinition() && !GVContext.isCompileUnit() &&
1987 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1988 // Create specification DIE.
1989 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1990 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1991 dwarf::DW_FORM_ref4, VariableDIE);
1992 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1993 addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1994 TheCU->addDie(VariableSpecDIE);
1995 } else {
1996 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1997 }
1998 } else if (Constant *C = GV.getConstant()) {
1999 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
Devang Patela8652672010-08-23 18:25:56 +00002000 if (isUnsignedDIType(GTy))
Devang Patelb2197462010-08-10 07:11:13 +00002001 addUInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
2002 CI->getZExtValue());
2003 else
2004 addSInt(VariableDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
2005 CI->getSExtValue());
Devang Patelb2197462010-08-10 07:11:13 +00002006 }
Devang Patel469c12d22010-08-10 01:37:23 +00002007 }
Devang Patel0751a282009-06-26 01:49:18 +00002008 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00002009}
2010
Devang Patel1a0df9a2010-05-10 22:49:55 +00002011/// construct SubprogramDIE - Construct subprogram DIE.
Devang Patel32cc43c2010-05-07 20:54:48 +00002012void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00002013 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +00002014
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00002015 // Check for pre-existence.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002016 CompileUnit *TheCU = getCompileUnit(N);
2017 if (TheCU->getDIE(N))
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00002018 return;
2019
Bill Wendling2b128d72009-05-20 23:19:06 +00002020 if (!SP.isDefinition())
2021 // This is a method declaration which will be handled while constructing
2022 // class type.
Devang Patel0751a282009-06-26 01:49:18 +00002023 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00002024
Stuart Hastings4bd3dd92010-04-06 21:38:29 +00002025 DIE *SubprogramDie = createSubprogramDIE(SP);
2026
2027 // Add to map.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002028 TheCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00002029
2030 // Add to context owner.
Devang Patel1f4690c2009-12-15 19:16:48 +00002031 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel512001a2009-12-08 23:21:45 +00002032
Bill Wendling2b128d72009-05-20 23:19:06 +00002033 // Expose as global.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002034 TheCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00002035
Devang Patel0751a282009-06-26 01:49:18 +00002036 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00002037}
2038
Devang Patel930143b2009-11-21 02:48:08 +00002039/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +00002040/// content. Create global DIEs and emit initial debug info sections.
2041/// This is inovked by the target AsmPrinter.
Chris Lattner11980022010-04-04 07:48:20 +00002042void DwarfDebug::beginModule(Module *M) {
Devang Patel6c74a872010-04-27 19:46:33 +00002043 if (DisableDebugInfoPrinting)
2044 return;
2045
Devang Patel63524442009-07-30 18:56:46 +00002046 DebugInfoFinder DbgFinder;
2047 DbgFinder.processModule(*M);
Devang Patel0751a282009-06-26 01:49:18 +00002048
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002049 bool HasDebugInfo = false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002050
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002051 // Scan all the compile-units to see if there are any marked as the main unit.
2052 // if not, we do not generate debug info.
2053 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2054 E = DbgFinder.compile_unit_end(); I != E; ++I) {
2055 if (DICompileUnit(*I).isMain()) {
2056 HasDebugInfo = true;
2057 break;
2058 }
2059 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002060
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002061 if (!HasDebugInfo) return;
2062
2063 // Tell MMI that we have debug info.
2064 MMI->setDebugInfoAvailability(true);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002065
Chris Lattnerd442aa32010-04-04 23:17:54 +00002066 // Emit initial sections.
Chris Lattner7cfa70e2010-04-05 02:19:28 +00002067 EmitSectionLabels();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002068
Bill Wendling2b128d72009-05-20 23:19:06 +00002069 // Create all the compile unit DIEs.
Devang Patel63524442009-07-30 18:56:46 +00002070 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
2071 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00002072 constructCompileUnit(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00002073
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002074 // Create DIEs for each subprogram.
Devang Patel63524442009-07-30 18:56:46 +00002075 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
2076 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00002077 constructSubprogramDIE(*I);
Devang Patel0751a282009-06-26 01:49:18 +00002078
Devang Patel2b75ed22009-12-10 19:14:49 +00002079 // Create DIEs for each global variable.
2080 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
2081 E = DbgFinder.global_variable_end(); I != E; ++I)
2082 constructGlobalVariableDIE(*I);
2083
Devang Patel8e06a5e2010-08-10 20:01:20 +00002084 //getOrCreateTypeDIE
2085 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
2086 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2087 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2088
Devang Patel7a554812010-09-28 18:08:20 +00002089 if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
2090 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
2091 getOrCreateTypeDIE(DIType(NMD->getOperand(i)));
2092
Bill Wendling2b128d72009-05-20 23:19:06 +00002093 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +00002094 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00002095
2096 // Print out .file directives to specify files for .loc directives. These are
2097 // printed out early so that they precede any .loc directives.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002098 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendling2b128d72009-05-20 23:19:06 +00002099 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
2100 // Remember source id starts at 1.
2101 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002102 // FIXME: don't use sys::path for this! This should not depend on the
2103 // host.
Bill Wendling2b128d72009-05-20 23:19:06 +00002104 sys::Path FullPath(getSourceDirectoryName(Id.first));
2105 bool AppendOk =
2106 FullPath.appendComponent(getSourceFileName(Id.second));
2107 assert(AppendOk && "Could not append filename to directory!");
2108 AppendOk = false;
Chris Lattner601ef332010-01-25 18:58:59 +00002109 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendling2b128d72009-05-20 23:19:06 +00002110 }
2111 }
Bill Wendling2b128d72009-05-20 23:19:06 +00002112}
2113
Devang Patel930143b2009-11-21 02:48:08 +00002114/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +00002115///
Devang Patel930143b2009-11-21 02:48:08 +00002116void DwarfDebug::endModule() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002117 if (!FirstCU) return;
Devang Patelf3b2db62010-06-28 18:25:03 +00002118 const Module *M = MMI->getModule();
Devang Pateld0701282010-08-02 17:32:15 +00002119 DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
Devang Patelf3b2db62010-06-28 18:25:03 +00002120 if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
2121 for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
2122 if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
2123 DISubprogram SP(AllSPs->getOperand(SI));
2124 if (!SP.Verify()) continue;
2125
2126 // Collect info for variables that were optimized out.
Devang Patel18efced2010-07-19 17:53:55 +00002127 if (!SP.isDefinition()) continue;
Devang Patelf3b2db62010-06-28 18:25:03 +00002128 StringRef FName = SP.getLinkageName();
2129 if (FName.empty())
2130 FName = SP.getName();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002131 NamedMDNode *NMD =
Devang Patelf3b2db62010-06-28 18:25:03 +00002132 M->getNamedMetadata(Twine("llvm.dbg.lv.", getRealLinkageName(FName)));
2133 if (!NMD) continue;
2134 unsigned E = NMD->getNumOperands();
2135 if (!E) continue;
2136 DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
Devang Pateld0701282010-08-02 17:32:15 +00002137 DeadFnScopeMap[SP] = Scope;
Devang Patelf3b2db62010-06-28 18:25:03 +00002138 for (unsigned I = 0; I != E; ++I) {
2139 DIVariable DV(NMD->getOperand(I));
2140 if (!DV.Verify()) continue;
2141 Scope->addVariable(new DbgVariable(DV));
2142 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002143
Devang Patelf3b2db62010-06-28 18:25:03 +00002144 // Construct subprogram DIE and add variables DIEs.
2145 constructSubprogramDIE(SP);
2146 DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
Devang Patel406798a2010-08-09 18:51:29 +00002147 const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
Devang Patelf3b2db62010-06-28 18:25:03 +00002148 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2149 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
2150 if (VariableDIE)
2151 ScopeDIE->addChild(VariableDIE);
2152 }
2153 }
2154 }
Bill Wendling2b128d72009-05-20 23:19:06 +00002155
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002156 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
2157 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
2158 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
2159 DIE *ISP = *AI;
Devang Patel930143b2009-11-21 02:48:08 +00002160 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002161 }
2162
Devang Patel32cc43c2010-05-07 20:54:48 +00002163 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Pateleb57c592009-12-03 19:11:07 +00002164 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
2165 DIE *SPDie = CI->first;
Devang Patel32cc43c2010-05-07 20:54:48 +00002166 const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
Devang Pateleb57c592009-12-03 19:11:07 +00002167 if (!N) continue;
Devang Patel1a0df9a2010-05-10 22:49:55 +00002168 DIE *NDie = getCompileUnit(N)->getDIE(N);
Devang Pateleb57c592009-12-03 19:11:07 +00002169 if (!NDie) continue;
2170 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Pateleb57c592009-12-03 19:11:07 +00002171 }
2172
Bill Wendling2b128d72009-05-20 23:19:06 +00002173 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002174 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnera179b522010-04-04 19:25:43 +00002175 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002176 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnera179b522010-04-04 19:25:43 +00002177 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendling2b128d72009-05-20 23:19:06 +00002178
2179 // End text sections.
2180 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002181 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnera179b522010-04-04 19:25:43 +00002182 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendling2b128d72009-05-20 23:19:06 +00002183 }
2184
2185 // Emit common frame information.
Devang Patel930143b2009-11-21 02:48:08 +00002186 emitCommonDebugFrame();
Bill Wendling2b128d72009-05-20 23:19:06 +00002187
2188 // Emit function debug frame information
2189 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2190 E = DebugFrames.end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00002191 emitFunctionDebugFrame(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00002192
2193 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +00002194 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +00002195
2196 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +00002197 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002198
2199 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +00002200 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +00002201
Devang Patel4a213872010-08-24 00:06:12 +00002202 // Emit source line correspondence into a debug line section.
2203 emitDebugLines();
2204
Bill Wendling2b128d72009-05-20 23:19:06 +00002205 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +00002206 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +00002207
Devang Patel04d2f2d2009-11-24 01:14:22 +00002208 // Emit info into a debug pubtypes section.
2209 emitDebugPubTypes();
2210
Bill Wendling2b128d72009-05-20 23:19:06 +00002211 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002212 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +00002213
2214 // Emit info into a debug aranges section.
2215 EmitDebugARanges();
2216
2217 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002218 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +00002219
2220 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002221 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002222
2223 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +00002224 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00002225
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002226 // Emit info into a debug str section.
2227 emitDebugStr();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002228
Devang Pateld0701282010-08-02 17:32:15 +00002229 // clean up.
2230 DeleteContainerSeconds(DeadFnScopeMap);
Devang Patel1a0df9a2010-05-10 22:49:55 +00002231 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2232 E = CUMap.end(); I != E; ++I)
2233 delete I->second;
2234 FirstCU = NULL; // Reset for the next Module, if any.
Bill Wendling2b128d72009-05-20 23:19:06 +00002235}
2236
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002237/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002238DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
Chris Lattner915c5f92010-04-02 19:42:39 +00002239 DebugLoc ScopeLoc) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002240
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002241 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002242 if (AbsDbgVariable)
2243 return AbsDbgVariable;
2244
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002245 LLVMContext &Ctx = Var->getContext();
Chris Lattner915c5f92010-04-02 19:42:39 +00002246 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002247 if (!Scope)
2248 return NULL;
2249
Devang Patele1c53f22010-05-20 16:36:41 +00002250 AbsDbgVariable = new DbgVariable(Var);
Devang Patel930143b2009-11-21 02:48:08 +00002251 Scope->addVariable(AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002252 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002253 return AbsDbgVariable;
2254}
2255
Devang Patel490c8ab2010-05-20 19:57:06 +00002256/// collectVariableInfoFromMMITable - Collect variable information from
2257/// side table maintained by MMI.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002258void
Devang Patel490c8ab2010-05-20 19:57:06 +00002259DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
2260 SmallPtrSet<const MDNode *, 16> &Processed) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00002261 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Devang Patel475d32a2009-10-06 01:26:37 +00002262 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2263 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2264 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00002265 const MDNode *Var = VI->first;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002266 if (!Var) continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00002267 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00002268 DIVariable DV(Var);
2269 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002270
Chris Lattner915c5f92010-04-02 19:42:39 +00002271 DbgScope *Scope = 0;
Devang Patel32cc43c2010-05-07 20:54:48 +00002272 if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
Chris Lattner915c5f92010-04-02 19:42:39 +00002273 Scope = ConcreteScopes.lookup(IA);
2274 if (Scope == 0)
2275 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002276
Devang Patelcdb7d442009-11-10 23:20:04 +00002277 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00002278 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00002279 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002280
Devang Patele1c53f22010-05-20 16:36:41 +00002281 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
2282 DbgVariable *RegVar = new DbgVariable(DV);
2283 recordVariableFrameIndex(RegVar, VP.first);
Devang Patel930143b2009-11-21 02:48:08 +00002284 Scope->addVariable(RegVar);
Devang Patele1c53f22010-05-20 16:36:41 +00002285 if (AbsDbgVariable) {
2286 recordVariableFrameIndex(AbsDbgVariable, VP.first);
2287 VarToAbstractVarMap[RegVar] = AbsDbgVariable;
2288 }
Devang Patel475d32a2009-10-06 01:26:37 +00002289 }
Devang Patel490c8ab2010-05-20 19:57:06 +00002290}
Devang Patela3e9c9c2010-03-15 18:33:46 +00002291
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002292/// isDbgValueInUndefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +00002293/// DBG_VALUE instruction, is in undefined reg.
2294static bool isDbgValueInUndefinedReg(const MachineInstr *MI) {
2295 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2296 if (MI->getOperand(0).isReg() && !MI->getOperand(0).getReg())
2297 return true;
2298 return false;
2299}
2300
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002301/// isDbgValueInDefinedReg - Return true if debug value, encoded by
Devang Patel9fc11702010-05-25 23:40:22 +00002302/// DBG_VALUE instruction, is in a defined reg.
2303static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
2304 assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
2305 if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg())
2306 return true;
2307 return false;
2308}
2309
Devang Patel490c8ab2010-05-20 19:57:06 +00002310/// collectVariableInfo - Populate DbgScope entries with variables' info.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002311void
Devang Patel5c0f85c2010-06-25 22:07:34 +00002312DwarfDebug::collectVariableInfo(const MachineFunction *MF,
2313 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002314
Devang Patel490c8ab2010-05-20 19:57:06 +00002315 /// collection info from MMI table.
2316 collectVariableInfoFromMMITable(MF, Processed);
2317
2318 SmallVector<const MachineInstr *, 8> DbgValues;
Devang Patela3e9c9c2010-03-15 18:33:46 +00002319 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattner3a383cb2010-04-05 00:13:49 +00002320 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel490c8ab2010-05-20 19:57:06 +00002321 I != E; ++I)
Devang Patela3e9c9c2010-03-15 18:33:46 +00002322 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2323 II != IE; ++II) {
2324 const MachineInstr *MInsn = II;
Devang Patel9fc11702010-05-25 23:40:22 +00002325 if (!MInsn->isDebugValue() || isDbgValueInUndefinedReg(MInsn))
Devang Patela3e9c9c2010-03-15 18:33:46 +00002326 continue;
Devang Patel490c8ab2010-05-20 19:57:06 +00002327 DbgValues.push_back(MInsn);
2328 }
Devang Patela3e9c9c2010-03-15 18:33:46 +00002329
Devang Patel9fc11702010-05-25 23:40:22 +00002330 // This is a collection of DBV_VALUE instructions describing same variable.
2331 SmallVector<const MachineInstr *, 4> MultipleValues;
Devang Patel490c8ab2010-05-20 19:57:06 +00002332 for(SmallVector<const MachineInstr *, 8>::iterator I = DbgValues.begin(),
2333 E = DbgValues.end(); I != E; ++I) {
2334 const MachineInstr *MInsn = *I;
Devang Patel9fc11702010-05-25 23:40:22 +00002335 MultipleValues.clear();
2336 if (isDbgValueInDefinedReg(MInsn))
2337 MultipleValues.push_back(MInsn);
Devang Patel490c8ab2010-05-20 19:57:06 +00002338 DIVariable DV(MInsn->getOperand(MInsn->getNumOperands() - 1).getMetadata());
2339 if (Processed.count(DV) != 0)
2340 continue;
2341
Devang Patelc2254f62010-06-02 19:05:13 +00002342 const MachineInstr *PrevMI = MInsn;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002343 for (SmallVector<const MachineInstr *, 8>::iterator MI = I+1,
Devang Patel9fc11702010-05-25 23:40:22 +00002344 ME = DbgValues.end(); MI != ME; ++MI) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002345 const MDNode *Var =
Devang Patel9fc11702010-05-25 23:40:22 +00002346 (*MI)->getOperand((*MI)->getNumOperands()-1).getMetadata();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002347 if (Var == DV && isDbgValueInDefinedReg(*MI) &&
Devang Patelc2254f62010-06-02 19:05:13 +00002348 !PrevMI->isIdenticalTo(*MI))
Devang Patel9fc11702010-05-25 23:40:22 +00002349 MultipleValues.push_back(*MI);
Devang Patelc2254f62010-06-02 19:05:13 +00002350 PrevMI = *MI;
Devang Patel9fc11702010-05-25 23:40:22 +00002351 }
2352
Devang Patel490c8ab2010-05-20 19:57:06 +00002353 DbgScope *Scope = findDbgScope(MInsn);
Devang Patel7a9dedf2010-05-27 20:25:04 +00002354 bool CurFnArg = false;
2355 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
2356 DISubprogram(DV.getContext()).describes(MF->getFunction()))
2357 CurFnArg = true;
2358 if (!Scope && CurFnArg)
Devang Patelfbd6c452010-05-21 00:10:20 +00002359 Scope = CurrentFnDbgScope;
Devang Patel490c8ab2010-05-20 19:57:06 +00002360 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00002361 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00002362 continue;
2363
2364 Processed.insert(DV);
Devang Patel490c8ab2010-05-20 19:57:06 +00002365 DbgVariable *RegVar = new DbgVariable(DV);
Devang Patel490c8ab2010-05-20 19:57:06 +00002366 Scope->addVariable(RegVar);
Devang Patel7a9dedf2010-05-27 20:25:04 +00002367 if (!CurFnArg)
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002368 DbgVariableLabelsMap[RegVar] = getLabelBeforeInsn(MInsn);
Devang Patelfbd6c452010-05-21 00:10:20 +00002369 if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
2370 DbgVariableToDbgInstMap[AbsVar] = MInsn;
2371 VarToAbstractVarMap[RegVar] = AbsVar;
Devang Patela3e9c9c2010-03-15 18:33:46 +00002372 }
Devang Patel9fc11702010-05-25 23:40:22 +00002373 if (MultipleValues.size() <= 1) {
2374 DbgVariableToDbgInstMap[RegVar] = MInsn;
2375 continue;
2376 }
2377
2378 // handle multiple DBG_VALUE instructions describing one variable.
Devang Patel9fc11702010-05-25 23:40:22 +00002379 if (DotDebugLocEntries.empty())
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002380 RegVar->setDotDebugLocOffset(0);
2381 else
2382 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002383 const MachineInstr *Begin = NULL;
2384 const MachineInstr *End = NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002385 for (SmallVector<const MachineInstr *, 4>::iterator
2386 MVI = MultipleValues.begin(), MVE = MultipleValues.end();
Devang Patel30265c42010-07-07 20:12:52 +00002387 MVI != MVE; ++MVI) {
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002388 if (!Begin) {
2389 Begin = *MVI;
2390 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002391 }
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002392 End = *MVI;
Devang Patel9fc11702010-05-25 23:40:22 +00002393 MachineLocation MLoc;
Devang Patel0e60e672010-08-04 18:40:52 +00002394 if (Begin->getNumOperands() == 3) {
2395 if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2396 MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2397 } else
2398 MLoc = Asm->getDebugValueLocation(Begin);
2399
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002400 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
2401 const MCSymbol *SLabel = getLabelBeforeInsn(End);
Devang Patel6c378ac2010-08-04 22:07:27 +00002402 if (MLoc.getReg())
2403 DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc));
2404
Devang Patel5a5e0bc2010-05-26 17:29:32 +00002405 Begin = End;
2406 if (MVI + 1 == MVE) {
2407 // If End is the last instruction then its value is valid
Devang Patel9fc11702010-05-25 23:40:22 +00002408 // until the end of the funtion.
Devang Patel0e60e672010-08-04 18:40:52 +00002409 MachineLocation EMLoc;
2410 if (End->getNumOperands() == 3) {
2411 if (End->getOperand(0).isReg() && Begin->getOperand(1).isImm())
2412 EMLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
2413 } else
2414 EMLoc = Asm->getDebugValueLocation(End);
Devang Patel6c378ac2010-08-04 22:07:27 +00002415 if (EMLoc.getReg())
2416 DotDebugLocEntries.
2417 push_back(DotDebugLocEntry(SLabel, FunctionEndSym, EMLoc));
Devang Patel9fc11702010-05-25 23:40:22 +00002418 }
2419 }
2420 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00002421 }
Devang Patele0a94bf2010-05-14 21:01:35 +00002422
2423 // Collect info for variables that were optimized out.
Devang Patelad517352010-06-22 01:01:58 +00002424 const Function *F = MF->getFunction();
2425 const Module *M = F->getParent();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002426 if (NamedMDNode *NMD =
2427 M->getNamedMetadata(Twine("llvm.dbg.lv.",
Devang Patelb6e058d2010-06-22 01:19:38 +00002428 getRealLinkageName(F->getName())))) {
Devang Patele0a94bf2010-05-14 21:01:35 +00002429 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00002430 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel490c8ab2010-05-20 19:57:06 +00002431 if (!DV || !Processed.insert(DV))
Devang Patele0a94bf2010-05-14 21:01:35 +00002432 continue;
2433 DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
2434 if (Scope)
Devang Patele1c53f22010-05-20 16:36:41 +00002435 Scope->addVariable(new DbgVariable(DV));
Devang Patele0a94bf2010-05-14 21:01:35 +00002436 }
2437 }
Devang Patel9fc11702010-05-25 23:40:22 +00002438}
Devang Patele0a94bf2010-05-14 21:01:35 +00002439
Devang Patel9fc11702010-05-25 23:40:22 +00002440/// getLabelBeforeInsn - Return Label preceding the instruction.
2441const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
2442 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2443 LabelsBeforeInsn.find(MI);
2444 if (I == LabelsBeforeInsn.end())
2445 // FunctionBeginSym always preceeds all the instruction in current function.
2446 return FunctionBeginSym;
2447 return I->second;
2448}
2449
2450/// getLabelAfterInsn - Return Label immediately following the instruction.
2451const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
2452 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
2453 LabelsAfterInsn.find(MI);
2454 if (I == LabelsAfterInsn.end())
2455 return NULL;
2456 return I->second;
Devang Patel475d32a2009-10-06 01:26:37 +00002457}
2458
Devang Patelb5694e72010-10-26 17:49:02 +00002459/// beginInstruction - Process beginning of an instruction.
2460void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Devang Patel002d54d2010-05-26 19:37:24 +00002461 if (InsnNeedsLabel.count(MI) == 0) {
2462 LabelsBeforeInsn[MI] = PrevLabel;
Devang Patelbd477be2010-03-29 17:20:31 +00002463 return;
Devang Patel9fc11702010-05-25 23:40:22 +00002464 }
Devang Patel23b2ae62010-03-29 22:59:58 +00002465
Devang Patel002d54d2010-05-26 19:37:24 +00002466 // Check location.
2467 DebugLoc DL = MI->getDebugLoc();
2468 if (!DL.isUnknown()) {
2469 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2470 PrevLabel = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
Devang Patel6c74a872010-04-27 19:46:33 +00002471 PrevInstLoc = DL;
Devang Patel002d54d2010-05-26 19:37:24 +00002472 LabelsBeforeInsn[MI] = PrevLabel;
2473 return;
Devang Patel6c74a872010-04-27 19:46:33 +00002474 }
Devang Patelbd477be2010-03-29 17:20:31 +00002475
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002476 // If location is unknown then use temp label for this DBG_VALUE
Devang Patel002d54d2010-05-26 19:37:24 +00002477 // instruction.
2478 if (MI->isDebugValue()) {
Devang Patelacc32a52010-05-26 21:23:46 +00002479 PrevLabel = MMI->getContext().CreateTempSymbol();
2480 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00002481 LabelsBeforeInsn[MI] = PrevLabel;
2482 return;
2483 }
2484
2485 if (UnknownLocations) {
2486 PrevLabel = recordSourceLine(0, 0, 0);
2487 LabelsBeforeInsn[MI] = PrevLabel;
2488 return;
2489 }
2490
2491 assert (0 && "Instruction is not processed!");
Devang Patel8db360d2009-10-06 01:50:42 +00002492}
2493
Devang Patelb5694e72010-10-26 17:49:02 +00002494/// endInstruction - Process end of an instruction.
2495void DwarfDebug::endInstruction(const MachineInstr *MI) {
Devang Patel3ebd8932010-04-08 16:50:29 +00002496 if (InsnsEndScopeSet.count(MI) != 0) {
2497 // Emit a label if this instruction ends a scope.
2498 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2499 Asm->OutStreamer.EmitLabel(Label);
Devang Patel6c74a872010-04-27 19:46:33 +00002500 LabelsAfterInsn[MI] = Label;
Devang Patel3ebd8932010-04-08 16:50:29 +00002501 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002502}
2503
Devang Patel6c74a872010-04-27 19:46:33 +00002504/// getOrCreateDbgScope - Create DbgScope for the scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002505DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
Devang Patel30265c42010-07-07 20:12:52 +00002506 const MDNode *InlinedAt) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002507 if (!InlinedAt) {
2508 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2509 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00002510 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002511 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2512 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patel6c74a872010-04-27 19:46:33 +00002513 if (DIDescriptor(Scope).isLexicalBlock()) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002514 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002515 getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
Devang Patel6c74a872010-04-27 19:46:33 +00002516 WScope->setParent(Parent);
2517 Parent->addScope(WScope);
2518 }
2519
2520 if (!WScope->getParent()) {
2521 StringRef SPName = DISubprogram(Scope).getLinkageName();
Stuart Hastings9b5005c2010-06-15 23:06:30 +00002522 // We used to check only for a linkage name, but that fails
2523 // since we began omitting the linkage name for private
2524 // functions. The new way is to check for the name in metadata,
2525 // but that's not supported in old .ll test cases. Ergo, we
2526 // check both.
Stuart Hastingsafe54f12010-06-11 20:08:44 +00002527 if (SPName == Asm->MF->getFunction()->getName() ||
2528 DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
Devang Patel6c74a872010-04-27 19:46:33 +00002529 CurrentFnDbgScope = WScope;
2530 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002531
Devang Patel6c74a872010-04-27 19:46:33 +00002532 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002533 }
2534
Devang Patel5c0f85c2010-06-25 22:07:34 +00002535 getOrCreateAbstractScope(Scope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002536 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2537 if (WScope)
Devang Patel6c74a872010-04-27 19:46:33 +00002538 return WScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002539
2540 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2541 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2542 DILocation DL(InlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00002543 DbgScope *Parent =
Devang Patelcfa8e9d2010-05-07 18:11:54 +00002544 getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
Devang Patel6c74a872010-04-27 19:46:33 +00002545 WScope->setParent(Parent);
2546 Parent->addScope(WScope);
2547
2548 ConcreteScopes[InlinedAt] = WScope;
Devang Patel6c74a872010-04-27 19:46:33 +00002549
2550 return WScope;
Devang Patel8db360d2009-10-06 01:50:42 +00002551}
2552
Devang Patel6c74a872010-04-27 19:46:33 +00002553/// hasValidLocation - Return true if debug location entry attached with
2554/// machine instruction encodes valid location info.
2555static bool hasValidLocation(LLVMContext &Ctx,
2556 const MachineInstr *MInsn,
Devang Patel32cc43c2010-05-07 20:54:48 +00002557 const MDNode *&Scope, const MDNode *&InlinedAt) {
Devang Patel6c74a872010-04-27 19:46:33 +00002558 DebugLoc DL = MInsn->getDebugLoc();
2559 if (DL.isUnknown()) return false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002560
Devang Patel32cc43c2010-05-07 20:54:48 +00002561 const MDNode *S = DL.getScope(Ctx);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002562
Devang Patel6c74a872010-04-27 19:46:33 +00002563 // There is no need to create another DIE for compile unit. For all
2564 // other scopes, create one DbgScope now. This will be translated
2565 // into a scope DIE at the end.
2566 if (DIScope(S).isCompileUnit()) return false;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002567
Devang Patel6c74a872010-04-27 19:46:33 +00002568 Scope = S;
2569 InlinedAt = DL.getInlinedAt(Ctx);
2570 return true;
2571}
2572
2573/// calculateDominanceGraph - Calculate dominance graph for DbgScope
2574/// hierarchy.
2575static void calculateDominanceGraph(DbgScope *Scope) {
2576 assert (Scope && "Unable to calculate scop edominance graph!");
2577 SmallVector<DbgScope *, 4> WorkStack;
2578 WorkStack.push_back(Scope);
2579 unsigned Counter = 0;
2580 while (!WorkStack.empty()) {
2581 DbgScope *WS = WorkStack.back();
2582 const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
2583 bool visitedChildren = false;
2584 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
2585 SE = Children.end(); SI != SE; ++SI) {
2586 DbgScope *ChildScope = *SI;
2587 if (!ChildScope->getDFSOut()) {
2588 WorkStack.push_back(ChildScope);
2589 visitedChildren = true;
2590 ChildScope->setDFSIn(++Counter);
2591 break;
2592 }
2593 }
2594 if (!visitedChildren) {
2595 WorkStack.pop_back();
2596 WS->setDFSOut(++Counter);
2597 }
2598 }
2599}
2600
2601/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002602static
Devang Patel6c74a872010-04-27 19:46:33 +00002603void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
2604 DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
2605{
2606#ifndef NDEBUG
2607 unsigned PrevDFSIn = 0;
2608 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2609 I != E; ++I) {
2610 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2611 II != IE; ++II) {
2612 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00002613 const MDNode *Scope = NULL;
2614 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002615
2616 // Check if instruction has valid location information.
2617 if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2618 dbgs() << " [ ";
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002619 if (InlinedAt)
Devang Patel6c74a872010-04-27 19:46:33 +00002620 dbgs() << "*";
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002621 DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
Devang Patel6c74a872010-04-27 19:46:33 +00002622 MI2ScopeMap.find(MInsn);
2623 if (DI != MI2ScopeMap.end()) {
2624 DbgScope *S = DI->second;
2625 dbgs() << S->getDFSIn();
2626 PrevDFSIn = S->getDFSIn();
2627 } else
2628 dbgs() << PrevDFSIn;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002629 } else
Devang Patel6c74a872010-04-27 19:46:33 +00002630 dbgs() << " [ x" << PrevDFSIn;
2631 dbgs() << " ]";
2632 MInsn->dump();
2633 }
2634 dbgs() << "\n";
2635 }
2636#endif
2637}
Devang Patel930143b2009-11-21 02:48:08 +00002638/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner848c7d22010-03-31 05:39:57 +00002639/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattner76555b52010-01-26 23:18:02 +00002640bool DwarfDebug::extractScopeInformation() {
Devang Patel75cc16c2009-10-01 20:31:14 +00002641 // If scope information was extracted using .dbg intrinsics then there is not
2642 // any need to extract these information by scanning each instruction.
2643 if (!DbgScopeMap.empty())
2644 return false;
2645
Dan Gohmane9135cb2010-04-23 01:18:53 +00002646 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6c74a872010-04-27 19:46:33 +00002647 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2648 SmallVector<DbgRange, 4> MIRanges;
2649 DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
Devang Patel32cc43c2010-05-07 20:54:48 +00002650 const MDNode *PrevScope = NULL;
2651 const MDNode *PrevInlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002652 const MachineInstr *RangeBeginMI = NULL;
2653 const MachineInstr *PrevMI = NULL;
Chris Lattner3a383cb2010-04-05 00:13:49 +00002654 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel75cc16c2009-10-01 20:31:14 +00002655 I != E; ++I) {
2656 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2657 II != IE; ++II) {
2658 const MachineInstr *MInsn = II;
Devang Patel32cc43c2010-05-07 20:54:48 +00002659 const MDNode *Scope = NULL;
2660 const MDNode *InlinedAt = NULL;
Devang Patel6c74a872010-04-27 19:46:33 +00002661
2662 // Check if instruction has valid location information.
2663 if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
2664 PrevMI = MInsn;
2665 continue;
2666 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002667
Devang Patel6c74a872010-04-27 19:46:33 +00002668 // If scope has not changed then skip this instruction.
2669 if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
2670 PrevMI = MInsn;
2671 continue;
2672 }
2673
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002674 if (RangeBeginMI) {
2675 // If we have alread seen a beginning of a instruction range and
Devang Patel6c74a872010-04-27 19:46:33 +00002676 // current instruction scope does not match scope of first instruction
2677 // in this range then create a new instruction range.
2678 DbgRange R(RangeBeginMI, PrevMI);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002679 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
Devang Patel30265c42010-07-07 20:12:52 +00002680 PrevInlinedAt);
Devang Patel6c74a872010-04-27 19:46:33 +00002681 MIRanges.push_back(R);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002682 }
Devang Patel6c74a872010-04-27 19:46:33 +00002683
2684 // This is a beginning of a new instruction range.
2685 RangeBeginMI = MInsn;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002686
Devang Patel6c74a872010-04-27 19:46:33 +00002687 // Reset previous markers.
2688 PrevMI = MInsn;
2689 PrevScope = Scope;
2690 PrevInlinedAt = InlinedAt;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002691 }
2692 }
2693
Devang Patel6c74a872010-04-27 19:46:33 +00002694 // Create last instruction range.
2695 if (RangeBeginMI && PrevMI && PrevScope) {
2696 DbgRange R(RangeBeginMI, PrevMI);
2697 MIRanges.push_back(R);
2698 MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
Devang Patel75cc16c2009-10-01 20:31:14 +00002699 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002700
Devang Patel530a0752010-01-04 20:44:00 +00002701 if (!CurrentFnDbgScope)
2702 return false;
2703
Devang Patel6c74a872010-04-27 19:46:33 +00002704 calculateDominanceGraph(CurrentFnDbgScope);
2705 if (PrintDbgScope)
2706 printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
2707
2708 // Find ranges of instructions covered by each DbgScope;
2709 DbgScope *PrevDbgScope = NULL;
2710 for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
2711 RE = MIRanges.end(); RI != RE; ++RI) {
2712 const DbgRange &R = *RI;
2713 DbgScope *S = MI2ScopeMap.lookup(R.first);
2714 assert (S && "Lost DbgScope for a machine instruction!");
2715 if (PrevDbgScope && !PrevDbgScope->dominates(S))
2716 PrevDbgScope->closeInsnRange(S);
2717 S->openInsnRange(R.first);
2718 S->extendInsnRange(R.second);
2719 PrevDbgScope = S;
2720 }
2721
2722 if (PrevDbgScope)
2723 PrevDbgScope->closeInsnRange();
Devang Patel75cc16c2009-10-01 20:31:14 +00002724
Devang Patel359b0132010-04-08 18:43:56 +00002725 identifyScopeMarkers();
Devang Patelf1d5a1e2010-04-08 15:37:09 +00002726
2727 return !DbgScopeMap.empty();
2728}
2729
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002730/// identifyScopeMarkers() -
Devang Patel6c74a872010-04-27 19:46:33 +00002731/// Each DbgScope has first instruction and last instruction to mark beginning
2732/// and end of a scope respectively. Create an inverse map that list scopes
2733/// starts (and ends) with an instruction. One instruction may start (or end)
2734/// multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00002735void DwarfDebug::identifyScopeMarkers() {
Devang Patel7771b7c2010-01-20 02:05:23 +00002736 SmallVector<DbgScope *, 4> WorkList;
2737 WorkList.push_back(CurrentFnDbgScope);
2738 while (!WorkList.empty()) {
Chris Lattner848c7d22010-03-31 05:39:57 +00002739 DbgScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002740
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002741 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002742 if (!Children.empty())
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002743 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patel7771b7c2010-01-20 02:05:23 +00002744 SE = Children.end(); SI != SE; ++SI)
2745 WorkList.push_back(*SI);
2746
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002747 if (S->isAbstractScope())
2748 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002749
Devang Patel6c74a872010-04-27 19:46:33 +00002750 const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
2751 if (Ranges.empty())
2752 continue;
2753 for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
2754 RE = Ranges.end(); RI != RE; ++RI) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002755 assert(RI->first && "DbgRange does not have first instruction!");
2756 assert(RI->second && "DbgRange does not have second instruction!");
Devang Patel6c74a872010-04-27 19:46:33 +00002757 InsnsEndScopeSet.insert(RI->second);
2758 }
Devang Patel75cc16c2009-10-01 20:31:14 +00002759 }
Devang Patel75cc16c2009-10-01 20:31:14 +00002760}
2761
Dan Gohman3df671a2010-04-20 00:37:27 +00002762/// FindFirstDebugLoc - Find the first debug location in the function. This
2763/// is intended to be an approximation for the source position of the
2764/// beginning of the function.
2765static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2766 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2767 I != E; ++I)
2768 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2769 MBBI != MBBE; ++MBBI) {
2770 DebugLoc DL = MBBI->getDebugLoc();
2771 if (!DL.isUnknown())
2772 return DL;
2773 }
2774 return DebugLoc();
2775}
2776
Devang Patela86114b2010-10-25 20:45:32 +00002777#ifndef NDEBUG
2778/// CheckLineNumbers - Count basicblocks whose instructions do not have any
2779/// line number information.
2780static void CheckLineNumbers(const MachineFunction *MF) {
2781 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2782 I != E; ++I) {
2783 bool FoundLineNo = false;
2784 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2785 II != IE; ++II) {
2786 const MachineInstr *MI = II;
2787 if (!MI->getDebugLoc().isUnknown()) {
2788 FoundLineNo = true;
2789 break;
2790 }
2791 }
Devang Patel6e0d5892010-10-28 22:11:59 +00002792 if (!FoundLineNo && I->size())
Devang Patela86114b2010-10-25 20:45:32 +00002793 ++BlocksWithoutLineNo;
2794 }
2795}
2796#endif
2797
Devang Patel930143b2009-11-21 02:48:08 +00002798/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00002799/// emitted immediately after the function entry point.
Chris Lattner76555b52010-01-26 23:18:02 +00002800void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner196dbdc2010-04-05 03:52:55 +00002801 if (!MMI->hasDebugInfo()) return;
Bill Wendlingfcc14142010-04-07 09:28:04 +00002802 if (!extractScopeInformation()) return;
Devang Patel4598eb62009-10-06 18:37:31 +00002803
Devang Patela86114b2010-10-25 20:45:32 +00002804#ifndef NDEBUG
2805 CheckLineNumbers(MF);
2806#endif
2807
Devang Patel6c74a872010-04-27 19:46:33 +00002808 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
2809 Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00002810 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00002811 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00002812
2813 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2814 // function.
Dan Gohman3df671a2010-04-20 00:37:27 +00002815 DebugLoc FDL = FindFirstDebugLoc(MF);
Chris Lattner915c5f92010-04-02 19:42:39 +00002816 if (FDL.isUnknown()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002817
Devang Patel32cc43c2010-05-07 20:54:48 +00002818 const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
Stuart Hastings61475c52010-07-19 23:56:30 +00002819 const MDNode *TheScope = 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002820
Chris Lattner915c5f92010-04-02 19:42:39 +00002821 DISubprogram SP = getDISubprogram(Scope);
2822 unsigned Line, Col;
2823 if (SP.Verify()) {
2824 Line = SP.getLineNumber();
2825 Col = 0;
Stuart Hastings61475c52010-07-19 23:56:30 +00002826 TheScope = SP;
Chris Lattner915c5f92010-04-02 19:42:39 +00002827 } else {
2828 Line = FDL.getLine();
2829 Col = FDL.getCol();
Stuart Hastings61475c52010-07-19 23:56:30 +00002830 TheScope = Scope;
Bill Wendling2b128d72009-05-20 23:19:06 +00002831 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002832
Stuart Hastings61475c52010-07-19 23:56:30 +00002833 recordSourceLine(Line, Col, TheScope);
Devang Patel002d54d2010-05-26 19:37:24 +00002834
Devang Patel21ccf052010-06-02 16:42:51 +00002835 /// ProcessedArgs - Collection of arguments already processed.
2836 SmallPtrSet<const MDNode *, 8> ProcessedArgs;
2837
Devang Patel002d54d2010-05-26 19:37:24 +00002838 DebugLoc PrevLoc;
2839 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2840 I != E; ++I)
2841 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2842 II != IE; ++II) {
2843 const MachineInstr *MI = II;
2844 DebugLoc DL = MI->getDebugLoc();
2845 if (MI->isDebugValue()) {
Devang Patel002d54d2010-05-26 19:37:24 +00002846 assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
2847 DIVariable DV(MI->getOperand(MI->getNumOperands() - 1).getMetadata());
2848 if (!DV.Verify()) continue;
Devang Patel5e6b71c2010-05-27 16:47:30 +00002849 // If DBG_VALUE is for a local variable then it needs a label.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002850 if (DV.getTag() != dwarf::DW_TAG_arg_variable
Devang Patel42939752010-07-01 21:38:08 +00002851 && isDbgValueInUndefinedReg(MI) == false)
Devang Patel002d54d2010-05-26 19:37:24 +00002852 InsnNeedsLabel.insert(MI);
Devang Patel5e6b71c2010-05-27 16:47:30 +00002853 // DBG_VALUE for inlined functions argument needs a label.
Devang Patel42939752010-07-01 21:38:08 +00002854 else if (!DISubprogram(getDISubprogram(DV.getContext())).
2855 describes(MF->getFunction()))
Devang Patel5e6b71c2010-05-27 16:47:30 +00002856 InsnNeedsLabel.insert(MI);
2857 // DBG_VALUE indicating argument location change needs a label.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002858 else if (isDbgValueInUndefinedReg(MI) == false
2859 && !ProcessedArgs.insert(DV))
Devang Patel002d54d2010-05-26 19:37:24 +00002860 InsnNeedsLabel.insert(MI);
2861 } else {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002862 // If location is unknown then instruction needs a location only if
Devang Patel002d54d2010-05-26 19:37:24 +00002863 // UnknownLocations flag is set.
2864 if (DL.isUnknown()) {
2865 if (UnknownLocations && !PrevLoc.isUnknown())
2866 InsnNeedsLabel.insert(MI);
2867 } else if (DL != PrevLoc)
2868 // Otherwise, instruction needs a location only if it is new location.
2869 InsnNeedsLabel.insert(MI);
2870 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002871
Devang Patel002d54d2010-05-26 19:37:24 +00002872 if (!DL.isUnknown() || UnknownLocations)
2873 PrevLoc = DL;
2874 }
2875
2876 PrevLabel = FunctionBeginSym;
Bill Wendling2b128d72009-05-20 23:19:06 +00002877}
2878
Devang Patel930143b2009-11-21 02:48:08 +00002879/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00002880///
Chris Lattner76555b52010-01-26 23:18:02 +00002881void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingfcc14142010-04-07 09:28:04 +00002882 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel2904aa92009-11-12 19:02:56 +00002883
Devang Patel530a0752010-01-04 20:44:00 +00002884 if (CurrentFnDbgScope) {
Devang Patel4a8e6e82010-05-22 00:04:14 +00002885
Devang Patel9fc11702010-05-25 23:40:22 +00002886 // Define end label for subprogram.
2887 FunctionEndSym = Asm->GetTempSymbol("func_end",
2888 Asm->getFunctionNumber());
2889 // Assumes in correct section after the entry point.
2890 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002891
Devang Patel5c0f85c2010-06-25 22:07:34 +00002892 SmallPtrSet<const MDNode *, 16> ProcessedVars;
2893 collectVariableInfo(MF, ProcessedVars);
Devang Patel4a8e6e82010-05-22 00:04:14 +00002894
Devang Patel530a0752010-01-04 20:44:00 +00002895 // Get function line info.
2896 if (!Lines.empty()) {
2897 // Get section line info.
2898 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2899 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2900 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2901 // Append the function info to section info.
2902 SectionLineInfos.insert(SectionLineInfos.end(),
2903 Lines.begin(), Lines.end());
2904 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002905
Devang Patel530a0752010-01-04 20:44:00 +00002906 // Construct abstract scopes.
2907 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Devang Patel5c0f85c2010-06-25 22:07:34 +00002908 AE = AbstractScopesList.end(); AI != AE; ++AI) {
Devang Patel5c0f85c2010-06-25 22:07:34 +00002909 DISubprogram SP((*AI)->getScopeNode());
2910 if (SP.Verify()) {
2911 // Collect info for variables that were optimized out.
2912 StringRef FName = SP.getLinkageName();
2913 if (FName.empty())
2914 FName = SP.getName();
2915 const Module *M = MF->getFunction()->getParent();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002916 if (NamedMDNode *NMD =
2917 M->getNamedMetadata(Twine("llvm.dbg.lv.",
Devang Patel5c0f85c2010-06-25 22:07:34 +00002918 getRealLinkageName(FName)))) {
2919 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Dan Gohman093cb792010-07-21 18:54:18 +00002920 DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
Devang Patel5c0f85c2010-06-25 22:07:34 +00002921 if (!DV || !ProcessedVars.insert(DV))
2922 continue;
Devang Patel648df7b2010-06-30 00:11:08 +00002923 DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
Devang Patel5c0f85c2010-06-25 22:07:34 +00002924 if (Scope)
2925 Scope->addVariable(new DbgVariable(DV));
2926 }
2927 }
2928 }
Devang Patelc5b31092010-06-30 01:40:11 +00002929 if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2930 constructScopeDIE(*AI);
Devang Patel5c0f85c2010-06-25 22:07:34 +00002931 }
Devang Patel30265c42010-07-07 20:12:52 +00002932
Devang Patel075e9b52010-05-04 06:15:30 +00002933 DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002934
Devang Patel075e9b52010-05-04 06:15:30 +00002935 if (!DisableFramePointerElim(*MF))
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002936 addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr,
Devang Patel075e9b52010-05-04 06:15:30 +00002937 dwarf::DW_FORM_flag, 1);
2938
2939
Chris Lattner3a383cb2010-04-05 00:13:49 +00002940 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel530a0752010-01-04 20:44:00 +00002941 MMI->getFrameMoves()));
Bill Wendling2b128d72009-05-20 23:19:06 +00002942 }
2943
Bill Wendling2b128d72009-05-20 23:19:06 +00002944 // Clear debug info
Devang Patelfe189e62010-01-19 01:26:02 +00002945 CurrentFnDbgScope = NULL;
Devang Patel002d54d2010-05-26 19:37:24 +00002946 InsnNeedsLabel.clear();
Devang Patele1c53f22010-05-20 16:36:41 +00002947 DbgVariableToFrameIndexMap.clear();
2948 VarToAbstractVarMap.clear();
2949 DbgVariableToDbgInstMap.clear();
2950 DbgVariableLabelsMap.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002951 DeleteContainerSeconds(DbgScopeMap);
Devang Patel541019d2010-04-09 16:04:20 +00002952 InsnsEndScopeSet.clear();
Devang Patelfe189e62010-01-19 01:26:02 +00002953 ConcreteScopes.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002954 DeleteContainerSeconds(AbstractScopes);
Devang Patelfe189e62010-01-19 01:26:02 +00002955 AbstractScopesList.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00002956 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00002957 LabelsBeforeInsn.clear();
2958 LabelsAfterInsn.clear();
Bill Wendling2b128d72009-05-20 23:19:06 +00002959 Lines.clear();
Devang Patel12563b32010-04-16 23:33:45 +00002960 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00002961}
2962
Devang Patele1c53f22010-05-20 16:36:41 +00002963/// recordVariableFrameIndex - Record a variable's index.
2964void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2965 assert (V && "Invalid DbgVariable!");
2966 DbgVariableToFrameIndexMap[V] = Index;
2967}
2968
2969/// findVariableFrameIndex - Return true if frame index for the variable
2970/// is found. Update FI to hold value of the index.
2971bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2972 assert (V && "Invalid DbgVariable!");
2973 DenseMap<const DbgVariable *, int>::iterator I =
2974 DbgVariableToFrameIndexMap.find(V);
2975 if (I == DbgVariableToFrameIndexMap.end())
2976 return false;
2977 *FI = I->second;
2978 return true;
2979}
2980
2981/// findVariableLabel - Find MCSymbol for the variable.
2982const MCSymbol *DwarfDebug::findVariableLabel(const DbgVariable *V) {
2983 DenseMap<const DbgVariable *, const MCSymbol *>::iterator I
2984 = DbgVariableLabelsMap.find(V);
2985 if (I == DbgVariableLabelsMap.end())
2986 return NULL;
2987 else return I->second;
2988}
2989
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002990/// findDbgScope - Find DbgScope for the debug loc attached with an
Devang Patel490c8ab2010-05-20 19:57:06 +00002991/// instruction.
2992DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2993 DbgScope *Scope = NULL;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002994 LLVMContext &Ctx =
Devang Patel490c8ab2010-05-20 19:57:06 +00002995 MInsn->getParent()->getParent()->getFunction()->getContext();
2996 DebugLoc DL = MInsn->getDebugLoc();
2997
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002998 if (DL.isUnknown())
Devang Patel490c8ab2010-05-20 19:57:06 +00002999 return Scope;
3000
3001 if (const MDNode *IA = DL.getInlinedAt(Ctx))
3002 Scope = ConcreteScopes.lookup(IA);
3003 if (Scope == 0)
3004 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003005
Devang Patel490c8ab2010-05-20 19:57:06 +00003006 return Scope;
3007}
3008
3009
Chris Lattnerba35a672010-03-09 04:54:43 +00003010/// recordSourceLine - Register a source line with debug info. Returns the
3011/// unique label that was emitted and which provides correspondence to
3012/// the source line list.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003013MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel30265c42010-07-07 20:12:52 +00003014 const MDNode *S) {
Devang Patel2d9caf92009-11-25 17:36:49 +00003015 StringRef Dir;
3016 StringRef Fn;
Devang Patel2089d162009-10-05 18:03:19 +00003017
Dan Gohman50849c62010-05-05 23:41:32 +00003018 unsigned Src = 1;
3019 if (S) {
3020 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00003021
Dan Gohman50849c62010-05-05 23:41:32 +00003022 if (Scope.isCompileUnit()) {
3023 DICompileUnit CU(S);
3024 Dir = CU.getDirectory();
3025 Fn = CU.getFilename();
Devang Patelc4b69052010-10-28 17:30:52 +00003026 } else if (Scope.isFile()) {
3027 DIFile F(S);
3028 Dir = F.getDirectory();
3029 Fn = F.getFilename();
Dan Gohman50849c62010-05-05 23:41:32 +00003030 } else if (Scope.isSubprogram()) {
3031 DISubprogram SP(S);
3032 Dir = SP.getDirectory();
3033 Fn = SP.getFilename();
3034 } else if (Scope.isLexicalBlock()) {
3035 DILexicalBlock DB(S);
3036 Dir = DB.getDirectory();
3037 Fn = DB.getFilename();
3038 } else
3039 assert(0 && "Unexpected scope info");
3040
3041 Src = GetOrCreateSourceID(Dir, Fn);
3042 }
3043
Chris Lattner6e52e9d2010-03-14 08:36:50 +00003044 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattnerb4666f42010-03-14 08:15:55 +00003045 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendling2b128d72009-05-20 23:19:06 +00003046
Chris Lattnerba35a672010-03-09 04:54:43 +00003047 Asm->OutStreamer.EmitLabel(Label);
3048 return Label;
Bill Wendling2b128d72009-05-20 23:19:06 +00003049}
3050
Bill Wendling806535f2009-05-20 23:22:40 +00003051//===----------------------------------------------------------------------===//
3052// Emit Methods
3053//===----------------------------------------------------------------------===//
3054
Devang Patel930143b2009-11-21 02:48:08 +00003055/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00003056///
Jim Grosbach00e9c612009-11-22 19:20:36 +00003057unsigned
3058DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00003059 // Get the children.
3060 const std::vector<DIE *> &Children = Die->getChildren();
3061
3062 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00003063 if (!Last && !Children.empty())
Benjamin Kramer74729ae2010-03-31 19:34:01 +00003064 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling480ff322009-05-20 23:21:38 +00003065
3066 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00003067 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00003068
3069 // Get the abbreviation for this DIE.
3070 unsigned AbbrevNumber = Die->getAbbrevNumber();
3071 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3072
3073 // Set DIE offset
3074 Die->setOffset(Offset);
3075
3076 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00003077 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00003078
3079 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
3080 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3081
3082 // Size the DIE attribute values.
3083 for (unsigned i = 0, N = Values.size(); i < N; ++i)
3084 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00003085 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00003086
3087 // Size the DIE children if any.
3088 if (!Children.empty()) {
3089 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
3090 "Children flag not set");
3091
3092 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00003093 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00003094
3095 // End of children marker.
3096 Offset += sizeof(int8_t);
3097 }
3098
3099 Die->setSize(Offset - Die->getOffset());
3100 return Offset;
3101}
3102
Devang Patel930143b2009-11-21 02:48:08 +00003103/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00003104///
Devang Patel930143b2009-11-21 02:48:08 +00003105void DwarfDebug::computeSizeAndOffsets() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003106 unsigned PrevOffset = 0;
3107 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3108 E = CUMap.end(); I != E; ++I) {
3109 // Compute size of compile unit header.
3110 static unsigned Offset = PrevOffset +
3111 sizeof(int32_t) + // Length of Compilation Unit Info
3112 sizeof(int16_t) + // DWARF version number
3113 sizeof(int32_t) + // Offset Into Abbrev. Section
3114 sizeof(int8_t); // Pointer Size (in bytes)
3115 computeSizeAndOffset(I->second->getCUDie(), Offset, true);
3116 PrevOffset = Offset;
3117 }
Bill Wendling480ff322009-05-20 23:21:38 +00003118}
3119
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003120/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
3121/// temporary label to it if SymbolStem is specified.
Chris Lattner6629ca92010-04-04 22:59:04 +00003122static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003123 const char *SymbolStem = 0) {
Chris Lattner6629ca92010-04-04 22:59:04 +00003124 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003125 if (!SymbolStem) return 0;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003126
Chris Lattner6629ca92010-04-04 22:59:04 +00003127 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
3128 Asm->OutStreamer.EmitLabel(TmpSym);
3129 return TmpSym;
3130}
3131
3132/// EmitSectionLabels - Emit initial Dwarf sections with a label at
3133/// the start of each one.
Chris Lattner46355d82010-04-04 22:33:59 +00003134void DwarfDebug::EmitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003135 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00003136
Bill Wendling480ff322009-05-20 23:21:38 +00003137 // Dwarf sections base addresses.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003138 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6629ca92010-04-04 22:59:04 +00003139 DwarfFrameSectionSym =
3140 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
3141 }
Bill Wendling480ff322009-05-20 23:21:38 +00003142
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003143 DwarfInfoSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00003144 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003145 DwarfAbbrevSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00003146 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003147 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003148
Chris Lattner6629ca92010-04-04 22:59:04 +00003149 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003150 EmitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00003151
Devang Patel4a213872010-08-24 00:06:12 +00003152 EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Chris Lattner1fbf53b2010-04-04 23:02:02 +00003153 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
3154 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
3155 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003156 DwarfStrSectionSym =
Chris Lattner6629ca92010-04-04 22:59:04 +00003157 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Devang Patel12563b32010-04-16 23:33:45 +00003158 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
3159 "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00003160
Devang Patel9fc11702010-05-25 23:40:22 +00003161 DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
3162 "section_debug_loc");
3163
Chris Lattner6629ca92010-04-04 22:59:04 +00003164 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattnere58b5472010-04-04 23:10:38 +00003165 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003166}
3167
Devang Patel930143b2009-11-21 02:48:08 +00003168/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00003169///
Devang Patel930143b2009-11-21 02:48:08 +00003170void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00003171 // Get the abbreviation for this DIE.
3172 unsigned AbbrevNumber = Die->getAbbrevNumber();
3173 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
3174
Bill Wendling480ff322009-05-20 23:21:38 +00003175 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00003176 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00003177 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
3178 Twine::utohexstr(Die->getOffset()) + ":0x" +
3179 Twine::utohexstr(Die->getSize()) + " " +
3180 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00003181 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00003182
Jeffrey Yasskin54ebc982010-03-22 18:47:14 +00003183 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling480ff322009-05-20 23:21:38 +00003184 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
3185
3186 // Emit the DIE attribute values.
3187 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3188 unsigned Attr = AbbrevData[i].getAttribute();
3189 unsigned Form = AbbrevData[i].getForm();
3190 assert(Form && "Too many attributes for DIE (check abbreviation)");
3191
Chris Lattner7bde8c02010-04-04 18:52:31 +00003192 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00003193 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003194
Bill Wendling480ff322009-05-20 23:21:38 +00003195 switch (Attr) {
3196 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00003197 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00003198 break;
3199 case dwarf::DW_AT_abstract_origin: {
3200 DIEEntry *E = cast<DIEEntry>(Values[i]);
3201 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003202 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00003203 Asm->EmitInt32(Addr);
3204 break;
3205 }
Devang Patel12563b32010-04-16 23:33:45 +00003206 case dwarf::DW_AT_ranges: {
3207 // DW_AT_range Value encodes offset in debug_range section.
3208 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00003209
3210 if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
3211 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
3212 V->getValue(),
3213 4);
3214 } else {
3215 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
3216 V->getValue(),
3217 DwarfDebugRangeSectionSym,
3218 4);
3219 }
Devang Patel12563b32010-04-16 23:33:45 +00003220 break;
3221 }
Devang Patel9fc11702010-05-25 23:40:22 +00003222 case dwarf::DW_AT_location: {
3223 if (UseDotDebugLocEntry.count(Die) != 0) {
3224 DIELabel *L = cast<DIELabel>(Values[i]);
3225 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
3226 } else
3227 Values[i]->EmitValue(Asm, Form);
3228 break;
3229 }
Devang Patela1bd5a12010-09-29 19:08:08 +00003230 case dwarf::DW_AT_accessibility: {
3231 if (Asm->isVerbose()) {
3232 DIEInteger *V = cast<DIEInteger>(Values[i]);
3233 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
3234 }
3235 Values[i]->EmitValue(Asm, Form);
3236 break;
3237 }
Bill Wendling480ff322009-05-20 23:21:38 +00003238 default:
3239 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003240 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00003241 break;
3242 }
Bill Wendling480ff322009-05-20 23:21:38 +00003243 }
3244
3245 // Emit the DIE children if any.
3246 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
3247 const std::vector<DIE *> &Children = Die->getChildren();
3248
3249 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00003250 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00003251
Chris Lattner7bde8c02010-04-04 18:52:31 +00003252 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00003253 Asm->OutStreamer.AddComment("End Of Children Mark");
3254 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003255 }
3256}
3257
Devang Patel9ccfb642009-12-09 18:24:21 +00003258/// emitDebugInfo - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00003259///
Devang Patel9ccfb642009-12-09 18:24:21 +00003260void DwarfDebug::emitDebugInfo() {
3261 // Start debug info section.
3262 Asm->OutStreamer.SwitchSection(
3263 Asm->getObjFileLowering().getDwarfInfoSection());
Devang Patel1a0df9a2010-05-10 22:49:55 +00003264 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3265 E = CUMap.end(); I != E; ++I) {
3266 CompileUnit *TheCU = I->second;
3267 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003268
Devang Patel1a0df9a2010-05-10 22:49:55 +00003269 // Emit the compile units header.
3270 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
3271 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003272
Devang Patel1a0df9a2010-05-10 22:49:55 +00003273 // Emit size of content not including length itself
3274 unsigned ContentSize = Die->getSize() +
3275 sizeof(int16_t) + // DWARF version number
3276 sizeof(int32_t) + // Offset Into Abbrev. Section
3277 sizeof(int8_t) + // Pointer Size (in bytes)
3278 sizeof(int32_t); // FIXME - extra pad for gdb bug.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003279
Devang Patel1a0df9a2010-05-10 22:49:55 +00003280 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
3281 Asm->EmitInt32(ContentSize);
3282 Asm->OutStreamer.AddComment("DWARF version number");
3283 Asm->EmitInt16(dwarf::DWARF_VERSION);
3284 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
3285 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
3286 DwarfAbbrevSectionSym);
3287 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3288 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003289
Devang Patel1a0df9a2010-05-10 22:49:55 +00003290 emitDIE(Die);
3291 // FIXME - extra padding for gdb bug.
3292 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
3293 Asm->EmitInt8(0);
3294 Asm->EmitInt8(0);
3295 Asm->EmitInt8(0);
3296 Asm->EmitInt8(0);
3297 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
3298 }
Bill Wendling480ff322009-05-20 23:21:38 +00003299}
3300
Devang Patel930143b2009-11-21 02:48:08 +00003301/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00003302///
Devang Patel930143b2009-11-21 02:48:08 +00003303void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00003304 // Check to see if it is worth the effort.
3305 if (!Abbreviations.empty()) {
3306 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003307 Asm->OutStreamer.SwitchSection(
3308 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003309
Chris Lattnera179b522010-04-04 19:25:43 +00003310 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003311
3312 // For each abbrevation.
3313 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
3314 // Get abbreviation data
3315 const DIEAbbrev *Abbrev = Abbreviations[i];
3316
3317 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00003318 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00003319
3320 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003321 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00003322 }
3323
3324 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00003325 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00003326
Chris Lattnera179b522010-04-04 19:25:43 +00003327 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003328 }
3329}
3330
Devang Patel930143b2009-11-21 02:48:08 +00003331/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00003332/// the line matrix.
3333///
Devang Patel930143b2009-11-21 02:48:08 +00003334void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00003335 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00003336 Asm->OutStreamer.AddComment("Extended Op");
3337 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003338
Chris Lattner566cae92010-03-09 23:52:58 +00003339 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003340 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00003341 Asm->OutStreamer.AddComment("DW_LNE_set_address");
3342 Asm->EmitInt8(dwarf::DW_LNE_set_address);
3343
3344 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00003345
Chris Lattnera179b522010-04-04 19:25:43 +00003346 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattner3a383cb2010-04-05 00:13:49 +00003347 Asm->getTargetData().getPointerSize(),
3348 0/*AddrSpace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00003349
3350 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00003351 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
3352 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00003353 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00003354 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003355}
3356
Devang Patel930143b2009-11-21 02:48:08 +00003357/// emitDebugLines - Emit source line information.
Bill Wendling480ff322009-05-20 23:21:38 +00003358///
Devang Patel930143b2009-11-21 02:48:08 +00003359void DwarfDebug::emitDebugLines() {
Bill Wendling480ff322009-05-20 23:21:38 +00003360 // If the target is using .loc/.file, the assembler will be emitting the
3361 // .debug_line table automatically.
Chris Lattner3a383cb2010-04-05 00:13:49 +00003362 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling480ff322009-05-20 23:21:38 +00003363 return;
3364
3365 // Minimum line delta, thus ranging from -10..(255-10).
3366 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
3367 // Maximum line delta, thus ranging from -10..(255-10).
3368 const int MaxLineDelta = 255 + MinLineDelta;
3369
3370 // Start the dwarf line section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003371 Asm->OutStreamer.SwitchSection(
3372 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003373
3374 // Construct the section header.
Chris Lattner566cae92010-03-09 23:52:58 +00003375 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003376 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
3377 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnera179b522010-04-04 19:25:43 +00003378 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003379
Chris Lattner566cae92010-03-09 23:52:58 +00003380 Asm->OutStreamer.AddComment("DWARF version number");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003381 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling480ff322009-05-20 23:21:38 +00003382
Chris Lattner566cae92010-03-09 23:52:58 +00003383 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003384 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
3385 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnera179b522010-04-04 19:25:43 +00003386 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling480ff322009-05-20 23:21:38 +00003387
Chris Lattner566cae92010-03-09 23:52:58 +00003388 Asm->OutStreamer.AddComment("Minimum Instruction Length");
3389 Asm->EmitInt8(1);
3390 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
3391 Asm->EmitInt8(1);
3392 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
3393 Asm->EmitInt8(MinLineDelta);
3394 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
3395 Asm->EmitInt8(MaxLineDelta);
3396 Asm->OutStreamer.AddComment("Special Opcode Base");
3397 Asm->EmitInt8(-MinLineDelta);
Bill Wendling480ff322009-05-20 23:21:38 +00003398
3399 // Line number standard opcode encodings argument count
Chris Lattner566cae92010-03-09 23:52:58 +00003400 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
3401 Asm->EmitInt8(0);
3402 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
3403 Asm->EmitInt8(1);
3404 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
3405 Asm->EmitInt8(1);
3406 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
3407 Asm->EmitInt8(1);
3408 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
3409 Asm->EmitInt8(1);
3410 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
3411 Asm->EmitInt8(0);
3412 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
3413 Asm->EmitInt8(0);
3414 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
3415 Asm->EmitInt8(0);
3416 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
3417 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003418
3419 // Emit directories.
3420 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003421 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner7bde8c02010-04-04 18:52:31 +00003422 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003423 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling480ff322009-05-20 23:21:38 +00003424 }
3425
Chris Lattner566cae92010-03-09 23:52:58 +00003426 Asm->OutStreamer.AddComment("End of directories");
3427 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003428
3429 // Emit files.
3430 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
3431 // Remember source id starts at 1.
3432 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003433 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner7bde8c02010-04-04 18:52:31 +00003434 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003435 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003436
Chris Lattner9efd1182010-04-04 19:09:29 +00003437 Asm->EmitULEB128(Id.first, "Directory #");
3438 Asm->EmitULEB128(0, "Mod date");
3439 Asm->EmitULEB128(0, "File size");
Bill Wendling480ff322009-05-20 23:21:38 +00003440 }
3441
Chris Lattner566cae92010-03-09 23:52:58 +00003442 Asm->OutStreamer.AddComment("End of files");
3443 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00003444
Chris Lattnera179b522010-04-04 19:25:43 +00003445 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003446
3447 // A sequence for each text section.
3448 unsigned SecSrcLinesSize = SectionSourceLines.size();
3449
3450 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
3451 // Isolate current sections line info.
3452 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
3453
Bill Wendling480ff322009-05-20 23:21:38 +00003454 // Dwarf assumes we start with first line of first source file.
3455 unsigned Source = 1;
3456 unsigned Line = 1;
3457
3458 // Construct rows of the address, source, line, column matrix.
3459 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
3460 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattnerb4666f42010-03-14 08:15:55 +00003461 MCSymbol *Label = LineInfo.getLabel();
Chris Lattneree95d4c2010-03-14 02:20:58 +00003462 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling480ff322009-05-20 23:21:38 +00003463
Chris Lattner3d72a672010-03-09 23:38:23 +00003464 if (Asm->isVerbose()) {
Chris Lattner7e998b72010-03-10 01:04:13 +00003465 std::pair<unsigned, unsigned> SrcID =
Bill Wendling480ff322009-05-20 23:21:38 +00003466 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner7e998b72010-03-10 01:04:13 +00003467 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattnera26fbe42010-03-10 02:29:31 +00003468 "/" +
3469 Twine(getSourceFileName(SrcID.second)) +
Chris Lattner7e998b72010-03-10 01:04:13 +00003470 ":" + Twine(LineInfo.getLine()));
Bill Wendling480ff322009-05-20 23:21:38 +00003471 }
3472
3473 // Define the line address.
Chris Lattner566cae92010-03-09 23:52:58 +00003474 Asm->OutStreamer.AddComment("Extended Op");
3475 Asm->EmitInt8(0);
3476 Asm->OutStreamer.AddComment("Op size");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003477 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00003478
3479 Asm->OutStreamer.AddComment("DW_LNE_set_address");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003480 Asm->EmitInt8(dwarf::DW_LNE_set_address);
Chris Lattner566cae92010-03-09 23:52:58 +00003481
3482 Asm->OutStreamer.AddComment("Location label");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003483 Asm->OutStreamer.EmitSymbolValue(Label,
3484 Asm->getTargetData().getPointerSize(),
Chris Lattneree95d4c2010-03-14 02:20:58 +00003485 0/*AddrSpace*/);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003486
Bill Wendling480ff322009-05-20 23:21:38 +00003487 // If change of source, then switch to the new source.
3488 if (Source != LineInfo.getSourceID()) {
3489 Source = LineInfo.getSourceID();
Chris Lattner566cae92010-03-09 23:52:58 +00003490 Asm->OutStreamer.AddComment("DW_LNS_set_file");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003491 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner9efd1182010-04-04 19:09:29 +00003492 Asm->EmitULEB128(Source, "New Source");
Bill Wendling480ff322009-05-20 23:21:38 +00003493 }
3494
3495 // If change of line.
3496 if (Line != LineInfo.getLine()) {
3497 // Determine offset.
3498 int Offset = LineInfo.getLine() - Line;
3499 int Delta = Offset - MinLineDelta;
3500
3501 // Update line.
3502 Line = LineInfo.getLine();
3503
3504 // If delta is small enough and in range...
3505 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
3506 // ... then use fast opcode.
Chris Lattner566cae92010-03-09 23:52:58 +00003507 Asm->OutStreamer.AddComment("Line Delta");
3508 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling480ff322009-05-20 23:21:38 +00003509 } else {
3510 // ... otherwise use long hand.
Chris Lattner566cae92010-03-09 23:52:58 +00003511 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling480ff322009-05-20 23:21:38 +00003512 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner9efd1182010-04-04 19:09:29 +00003513 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner566cae92010-03-09 23:52:58 +00003514 Asm->OutStreamer.AddComment("DW_LNS_copy");
3515 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling480ff322009-05-20 23:21:38 +00003516 }
3517 } else {
3518 // Copy the previous row (different address or source)
Chris Lattner566cae92010-03-09 23:52:58 +00003519 Asm->OutStreamer.AddComment("DW_LNS_copy");
3520 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling480ff322009-05-20 23:21:38 +00003521 }
3522 }
3523
Devang Patel930143b2009-11-21 02:48:08 +00003524 emitEndOfLineMatrix(j + 1);
Bill Wendling480ff322009-05-20 23:21:38 +00003525 }
3526
3527 if (SecSrcLinesSize == 0)
3528 // Because we're emitting a debug_line section, we still need a line
3529 // table. The linker and friends expect it to exist. If there's nothing to
3530 // put into it, emit an empty table.
Devang Patel930143b2009-11-21 02:48:08 +00003531 emitEndOfLineMatrix(1);
Bill Wendling480ff322009-05-20 23:21:38 +00003532
Chris Lattnera179b522010-04-04 19:25:43 +00003533 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003534}
3535
Devang Patel930143b2009-11-21 02:48:08 +00003536/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling480ff322009-05-20 23:21:38 +00003537///
Devang Patel930143b2009-11-21 02:48:08 +00003538void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003539 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003540 return;
3541
Chris Lattner3a383cb2010-04-05 00:13:49 +00003542 int stackGrowth = Asm->getTargetData().getPointerSize();
3543 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3544 TargetFrameInfo::StackGrowsDown)
3545 stackGrowth *= -1;
Bill Wendling480ff322009-05-20 23:21:38 +00003546
3547 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003548 Asm->OutStreamer.SwitchSection(
3549 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003550
Chris Lattnera179b522010-04-04 19:25:43 +00003551 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner566cae92010-03-09 23:52:58 +00003552 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003553 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
3554 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003555
Chris Lattnera179b522010-04-04 19:25:43 +00003556 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner566cae92010-03-09 23:52:58 +00003557 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling480ff322009-05-20 23:21:38 +00003558 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner566cae92010-03-09 23:52:58 +00003559 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling480ff322009-05-20 23:21:38 +00003560 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner566cae92010-03-09 23:52:58 +00003561 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003562 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner9efd1182010-04-04 19:09:29 +00003563 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
3564 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner566cae92010-03-09 23:52:58 +00003565 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003566 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling480ff322009-05-20 23:21:38 +00003567 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling480ff322009-05-20 23:21:38 +00003568
3569 std::vector<MachineMove> Moves;
3570 RI->getInitialFrameState(Moves);
3571
Chris Lattneraabc6042010-04-04 23:41:46 +00003572 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling480ff322009-05-20 23:21:38 +00003573
Chris Lattner9e06e532010-04-28 01:05:45 +00003574 Asm->EmitAlignment(2);
Chris Lattnera179b522010-04-04 19:25:43 +00003575 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling480ff322009-05-20 23:21:38 +00003576}
3577
Devang Patel930143b2009-11-21 02:48:08 +00003578/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling480ff322009-05-20 23:21:38 +00003579/// section.
Chris Lattner2c3f4782010-03-13 07:26:18 +00003580void DwarfDebug::
3581emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003582 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003583 return;
3584
3585 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003586 Asm->OutStreamer.SwitchSection(
3587 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003588
Chris Lattner566cae92010-03-09 23:52:58 +00003589 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner2c3f4782010-03-13 07:26:18 +00003590 MCSymbol *DebugFrameBegin =
Chris Lattnera179b522010-04-04 19:25:43 +00003591 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattner2c3f4782010-03-13 07:26:18 +00003592 MCSymbol *DebugFrameEnd =
Chris Lattnera179b522010-04-04 19:25:43 +00003593 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerf1429f12010-04-04 19:58:12 +00003594 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003595
Chris Lattner2c3f4782010-03-13 07:26:18 +00003596 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling480ff322009-05-20 23:21:38 +00003597
Chris Lattner566cae92010-03-09 23:52:58 +00003598 Asm->OutStreamer.AddComment("FDE CIE offset");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003599 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
Chris Lattner70a4fce2010-04-04 23:25:33 +00003600 DwarfFrameSectionSym);
Bill Wendling480ff322009-05-20 23:21:38 +00003601
Chris Lattner566cae92010-03-09 23:52:58 +00003602 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnera179b522010-04-04 19:25:43 +00003603 MCSymbol *FuncBeginSym =
3604 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattner8811e122010-03-13 07:40:56 +00003605 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattner3a383cb2010-04-05 00:13:49 +00003606 Asm->getTargetData().getPointerSize(),
3607 0/*AddrSpace*/);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003608
3609
Chris Lattner566cae92010-03-09 23:52:58 +00003610 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003611 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattner3a383cb2010-04-05 00:13:49 +00003612 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00003613
Chris Lattneraabc6042010-04-04 23:41:46 +00003614 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling480ff322009-05-20 23:21:38 +00003615
Chris Lattner9e06e532010-04-28 01:05:45 +00003616 Asm->EmitAlignment(2);
Chris Lattner2c3f4782010-03-13 07:26:18 +00003617 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling480ff322009-05-20 23:21:38 +00003618}
3619
Devang Patel9ccfb642009-12-09 18:24:21 +00003620/// emitDebugPubNames - Emit visible names into a debug pubnames section.
3621///
3622void DwarfDebug::emitDebugPubNames() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003623 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3624 E = CUMap.end(); I != E; ++I) {
3625 CompileUnit *TheCU = I->second;
3626 // Start the dwarf pubnames section.
3627 Asm->OutStreamer.SwitchSection(
3628 Asm->getObjFileLowering().getDwarfPubNamesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003629
Devang Patel1a0df9a2010-05-10 22:49:55 +00003630 Asm->OutStreamer.AddComment("Length of Public Names Info");
3631 Asm->EmitLabelDifference(
3632 Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
3633 Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003634
Devang Patel1a0df9a2010-05-10 22:49:55 +00003635 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
3636 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003637
Devang Patel1a0df9a2010-05-10 22:49:55 +00003638 Asm->OutStreamer.AddComment("DWARF Version");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003639 Asm->EmitInt16(dwarf::DWARF_VERSION);
3640
Devang Patel1a0df9a2010-05-10 22:49:55 +00003641 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003642 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
Devang Patel1a0df9a2010-05-10 22:49:55 +00003643 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003644
Devang Patel1a0df9a2010-05-10 22:49:55 +00003645 Asm->OutStreamer.AddComment("Compilation Unit Length");
3646 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3647 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3648 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003649
Devang Patel1a0df9a2010-05-10 22:49:55 +00003650 const StringMap<DIE*> &Globals = TheCU->getGlobals();
3651 for (StringMap<DIE*>::const_iterator
3652 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3653 const char *Name = GI->getKeyData();
3654 DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003655
Devang Patel1a0df9a2010-05-10 22:49:55 +00003656 Asm->OutStreamer.AddComment("DIE offset");
3657 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003658
Devang Patel1a0df9a2010-05-10 22:49:55 +00003659 if (Asm->isVerbose())
3660 Asm->OutStreamer.AddComment("External Name");
3661 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
3662 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003663
Devang Patel1a0df9a2010-05-10 22:49:55 +00003664 Asm->OutStreamer.AddComment("End Mark");
3665 Asm->EmitInt32(0);
3666 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
3667 TheCU->getID()));
Bill Wendling480ff322009-05-20 23:21:38 +00003668 }
Bill Wendling480ff322009-05-20 23:21:38 +00003669}
3670
Devang Patel04d2f2d2009-11-24 01:14:22 +00003671void DwarfDebug::emitDebugPubTypes() {
Devang Patel1a0df9a2010-05-10 22:49:55 +00003672 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
3673 E = CUMap.end(); I != E; ++I) {
3674 CompileUnit *TheCU = I->second;
3675 // Start the dwarf pubnames section.
3676 Asm->OutStreamer.SwitchSection(
3677 Asm->getObjFileLowering().getDwarfPubTypesSection());
3678 Asm->OutStreamer.AddComment("Length of Public Types Info");
3679 Asm->EmitLabelDifference(
3680 Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
3681 Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003682
Devang Patel1a0df9a2010-05-10 22:49:55 +00003683 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3684 TheCU->getID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003685
Devang Patel1a0df9a2010-05-10 22:49:55 +00003686 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
3687 Asm->EmitInt16(dwarf::DWARF_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003688
Devang Patel1a0df9a2010-05-10 22:49:55 +00003689 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
3690 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
3691 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003692
Devang Patel1a0df9a2010-05-10 22:49:55 +00003693 Asm->OutStreamer.AddComment("Compilation Unit Length");
3694 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
3695 Asm->GetTempSymbol("info_begin", TheCU->getID()),
3696 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003697
Devang Patel1a0df9a2010-05-10 22:49:55 +00003698 const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
3699 for (StringMap<DIE*>::const_iterator
3700 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3701 const char *Name = GI->getKeyData();
3702 DIE * Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003703
Devang Patel1a0df9a2010-05-10 22:49:55 +00003704 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3705 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003706
Devang Patel1a0df9a2010-05-10 22:49:55 +00003707 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
3708 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3709 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003710
Devang Patel1a0df9a2010-05-10 22:49:55 +00003711 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003712 Asm->EmitInt32(0);
Devang Patel1a0df9a2010-05-10 22:49:55 +00003713 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3714 TheCU->getID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00003715 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00003716}
3717
Devang Patel930143b2009-11-21 02:48:08 +00003718/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00003719///
Devang Patel930143b2009-11-21 02:48:08 +00003720void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00003721 // Check to see if it is worth the effort.
Chris Lattner3d72a672010-03-09 23:38:23 +00003722 if (StringPool.empty()) return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003723
Chris Lattner3d72a672010-03-09 23:38:23 +00003724 // Start the dwarf str section.
3725 Asm->OutStreamer.SwitchSection(
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003726 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003727
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003728 // Get all of the string pool entries and put them in an array by their ID so
3729 // we can sort them.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003730 SmallVector<std::pair<unsigned,
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003731 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003732
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003733 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3734 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3735 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003736
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003737 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003738
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003739 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00003740 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003741 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003742
Chris Lattner3d72a672010-03-09 23:38:23 +00003743 // Emit the string itself.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00003744 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling480ff322009-05-20 23:21:38 +00003745 }
3746}
3747
Devang Patel930143b2009-11-21 02:48:08 +00003748/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00003749///
Devang Patel930143b2009-11-21 02:48:08 +00003750void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003751 if (DotDebugLocEntries.empty())
3752 return;
3753
Bill Wendling480ff322009-05-20 23:21:38 +00003754 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003755 Asm->OutStreamer.SwitchSection(
Devang Patel9fc11702010-05-25 23:40:22 +00003756 Asm->getObjFileLowering().getDwarfLocSection());
3757 unsigned char Size = Asm->getTargetData().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003758 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
3759 unsigned index = 1;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003760 for (SmallVector<DotDebugLocEntry, 4>::iterator
3761 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00003762 I != E; ++I, ++index) {
Devang Patel9fc11702010-05-25 23:40:22 +00003763 DotDebugLocEntry Entry = *I;
Devang Patel9fc11702010-05-25 23:40:22 +00003764 if (Entry.isEmpty()) {
3765 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
3766 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00003767 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00003768 } else {
3769 Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
3770 Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
3771 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
3772 unsigned Reg = RI->getDwarfRegNum(Entry.Loc.getReg(), false);
Devang Patel0e60e672010-08-04 18:40:52 +00003773 if (int Offset = Entry.Loc.getOffset()) {
3774 // If the value is at a certain offset from frame register then
3775 // use DW_OP_fbreg.
3776 unsigned OffsetSize = Offset ? MCAsmInfo::getSLEB128Size(Offset) : 1;
Devang Patel9fc11702010-05-25 23:40:22 +00003777 Asm->OutStreamer.AddComment("Loc expr size");
Devang Patel0e60e672010-08-04 18:40:52 +00003778 Asm->EmitInt16(1 + OffsetSize);
3779 Asm->OutStreamer.AddComment(
3780 dwarf::OperationEncodingString(dwarf::DW_OP_fbreg));
3781 Asm->EmitInt8(dwarf::DW_OP_fbreg);
3782 Asm->OutStreamer.AddComment("Offset");
3783 Asm->EmitSLEB128(Offset);
Devang Patel9fc11702010-05-25 23:40:22 +00003784 } else {
Devang Patel0e60e672010-08-04 18:40:52 +00003785 if (Reg < 32) {
3786 Asm->OutStreamer.AddComment("Loc expr size");
3787 Asm->EmitInt16(1);
3788 Asm->OutStreamer.AddComment(
Devang Patel6d21f612010-08-04 20:32:36 +00003789 dwarf::OperationEncodingString(dwarf::DW_OP_reg0 + Reg));
Devang Patel0e60e672010-08-04 18:40:52 +00003790 Asm->EmitInt8(dwarf::DW_OP_reg0 + Reg);
3791 } else {
3792 Asm->OutStreamer.AddComment("Loc expr size");
3793 Asm->EmitInt16(1 + MCAsmInfo::getULEB128Size(Reg));
3794 Asm->EmitInt8(dwarf::DW_OP_regx);
3795 Asm->EmitULEB128(Reg);
3796 }
Devang Patel9fc11702010-05-25 23:40:22 +00003797 }
3798 }
3799 }
Bill Wendling480ff322009-05-20 23:21:38 +00003800}
3801
3802/// EmitDebugARanges - Emit visible names into a debug aranges section.
3803///
3804void DwarfDebug::EmitDebugARanges() {
3805 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003806 Asm->OutStreamer.SwitchSection(
3807 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00003808}
3809
Devang Patel930143b2009-11-21 02:48:08 +00003810/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00003811///
Devang Patel930143b2009-11-21 02:48:08 +00003812void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00003813 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003814 Asm->OutStreamer.SwitchSection(
Devang Patel12563b32010-04-16 23:33:45 +00003815 Asm->getObjFileLowering().getDwarfRangesSection());
Devang Patel6c74a872010-04-27 19:46:33 +00003816 unsigned char Size = Asm->getTargetData().getPointerSize();
3817 for (SmallVector<const MCSymbol *, 8>::iterator
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003818 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Patel6c74a872010-04-27 19:46:33 +00003819 I != E; ++I) {
3820 if (*I)
3821 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
Devang Patel12563b32010-04-16 23:33:45 +00003822 else
Devang Patel6c74a872010-04-27 19:46:33 +00003823 Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
Devang Patel12563b32010-04-16 23:33:45 +00003824 }
Bill Wendling480ff322009-05-20 23:21:38 +00003825}
3826
Devang Patel930143b2009-11-21 02:48:08 +00003827/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00003828///
Devang Patel930143b2009-11-21 02:48:08 +00003829void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00003830 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00003831 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00003832 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003833 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00003834 }
3835}
3836
Devang Patel930143b2009-11-21 02:48:08 +00003837/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00003838/// Section Header:
3839/// 1. length of section
3840/// 2. Dwarf version number
3841/// 3. address size.
3842///
3843/// Entries (one "entry" for each function that was inlined):
3844///
3845/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3846/// otherwise offset into __debug_str for regular function name.
3847/// 2. offset into __debug_str section for regular function name.
3848/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3849/// instances for the function.
3850///
3851/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3852/// inlined instance; the die_offset points to the inlined_subroutine die in the
3853/// __debug_info section, and the low_pc is the starting address for the
3854/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00003855void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner3a383cb2010-04-05 00:13:49 +00003856 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00003857 return;
3858
Devang Patel1a0df9a2010-05-10 22:49:55 +00003859 if (!FirstCU)
Bill Wendling480ff322009-05-20 23:21:38 +00003860 return;
3861
Chris Lattner4b7dadb2009-08-19 05:49:37 +00003862 Asm->OutStreamer.SwitchSection(
3863 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerf5c834f2010-01-22 22:09:00 +00003864
Chris Lattner566cae92010-03-09 23:52:58 +00003865 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerf1429f12010-04-04 19:58:12 +00003866 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3867 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling480ff322009-05-20 23:21:38 +00003868
Chris Lattnera179b522010-04-04 19:25:43 +00003869 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00003870
Chris Lattner566cae92010-03-09 23:52:58 +00003871 Asm->OutStreamer.AddComment("Dwarf Version");
3872 Asm->EmitInt16(dwarf::DWARF_VERSION);
3873 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003874 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00003875
Devang Patel32cc43c2010-05-07 20:54:48 +00003876 for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003877 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00003878
Devang Patel32cc43c2010-05-07 20:54:48 +00003879 const MDNode *Node = *I;
3880 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbach00e9c612009-11-22 19:20:36 +00003881 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003882 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00003883 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00003884 StringRef LName = SP.getLinkageName();
3885 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00003886
Chris Lattner566cae92010-03-09 23:52:58 +00003887 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnerc3f23b82010-01-23 03:11:46 +00003888 if (LName.empty()) {
3889 Asm->OutStreamer.EmitBytes(Name, 0);
3890 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00003891 } else
Chris Lattner70a4fce2010-04-04 23:25:33 +00003892 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3893 DwarfStrSectionSym);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003894
Chris Lattner566cae92010-03-09 23:52:58 +00003895 Asm->OutStreamer.AddComment("Function name");
Chris Lattner70a4fce2010-04-04 23:25:33 +00003896 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner9efd1182010-04-04 19:09:29 +00003897 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling480ff322009-05-20 23:21:38 +00003898
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003899 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00003900 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner7bde8c02010-04-04 18:52:31 +00003901 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattner085b6522010-03-09 00:31:02 +00003902 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00003903
Chris Lattner7bde8c02010-04-04 18:52:31 +00003904 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner3a383cb2010-04-05 00:13:49 +00003905 Asm->OutStreamer.EmitSymbolValue(LI->first,
3906 Asm->getTargetData().getPointerSize(),0);
Bill Wendling480ff322009-05-20 23:21:38 +00003907 }
3908 }
3909
Chris Lattnera179b522010-04-04 19:25:43 +00003910 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling480ff322009-05-20 23:21:38 +00003911}