blob: fe8c95e8f7020f886bfa7456c034fb54541827c0 [file] [log] [blame]
Bill Wendlingb12b3d72009-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 Lattner2ab0c442010-03-09 00:39:24 +000013
Devang Patel15e723d2009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000015#include "DwarfDebug.h"
16#include "llvm/Module.h"
David Greened87baff2009-08-19 21:52:55 +000017#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner855e8f52010-03-09 01:58:53 +000019#include "llvm/MC/MCAsmInfo.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000020#include "llvm/MC/MCSection.h"
Chris Lattner73266f92009-08-19 05:49:37 +000021#include "llvm/MC/MCStreamer.h"
Chris Lattner855e8f52010-03-09 01:58:53 +000022#include "llvm/MC/MCSymbol.h"
Chris Lattner31a54742010-01-16 21:57:06 +000023#include "llvm/Target/Mangler.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000024#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000026#include "llvm/Target/TargetLoweringObjectFile.h"
27#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattnerf5377682009-08-24 03:52:50 +000028#include "llvm/ADT/StringExtras.h"
Daniel Dunbarc74255d2009-10-13 06:47:08 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/ErrorHandling.h"
Devang Patelae89d3b2010-01-26 21:39:14 +000031#include "llvm/Support/ValueHandle.h"
Chris Lattnerad653482010-01-22 22:09:00 +000032#include "llvm/Support/FormattedStream.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000033#include "llvm/Support/Timer.h"
34#include "llvm/System/Path.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000035using namespace llvm;
36
Bill Wendlingb12b3d72009-05-15 09:23:25 +000037//===----------------------------------------------------------------------===//
38
39/// Configuration values for initial hash set sizes (log2).
40///
Bill Wendlingb12b3d72009-05-15 09:23:25 +000041static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendlingb12b3d72009-05-15 09:23:25 +000042
43namespace llvm {
44
45//===----------------------------------------------------------------------===//
46/// CompileUnit - This dwarf writer support class manages information associate
47/// with a source file.
Nick Lewyckyee68f452009-11-17 08:11:44 +000048class CompileUnit {
Bill Wendlingb12b3d72009-05-15 09:23:25 +000049 /// ID - File identifier for source.
50 ///
51 unsigned ID;
52
53 /// Die - Compile unit debug information entry.
54 ///
Jeffrey Yasskinc3545712010-03-11 18:29:55 +000055 OwningPtr<DIE> CUDie;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000056
Jeffrey Yasskinc3545712010-03-11 18:29:55 +000057 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel1233f912009-11-21 00:31:03 +000058 DIE *IndexTyDie;
59
Bill Wendlingb12b3d72009-05-15 09:23:25 +000060 /// GVToDieMap - Tracks the mapping of unit level debug informaton
61 /// variables to debug information entries.
Devang Patel15e723d2009-08-28 23:24:31 +000062 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel7d707f92010-01-19 06:19:05 +000063 DenseMap<MDNode *, DIE *> GVToDieMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000064
65 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
66 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel15e723d2009-08-28 23:24:31 +000067 /// FIXME : Rename
Devang Patel7d707f92010-01-19 06:19:05 +000068 DenseMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000069
70 /// Globals - A map of globally visible named entities for this unit.
71 ///
72 StringMap<DIE*> Globals;
73
Devang Patelec13b4f2009-11-24 01:14:22 +000074 /// GlobalTypes - A map of globally visible types for this unit.
75 ///
76 StringMap<DIE*> GlobalTypes;
77
Bill Wendlingb12b3d72009-05-15 09:23:25 +000078public:
79 CompileUnit(unsigned I, DIE *D)
Devang Patelc50078e2009-11-21 02:48:08 +000080 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +000081
82 // Accessors.
Devang Patelec13b4f2009-11-24 01:14:22 +000083 unsigned getID() const { return ID; }
Jeffrey Yasskinc3545712010-03-11 18:29:55 +000084 DIE* getCUDie() const { return CUDie.get(); }
Devang Patelec13b4f2009-11-24 01:14:22 +000085 const StringMap<DIE*> &getGlobals() const { return Globals; }
86 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000087
88 /// hasContent - Return true if this compile unit has something to write out.
89 ///
Devang Patelc50078e2009-11-21 02:48:08 +000090 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000091
Devang Patelc50078e2009-11-21 02:48:08 +000092 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +000093 ///
Devang Patelc50078e2009-11-21 02:48:08 +000094 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000095
Devang Patelec13b4f2009-11-24 01:14:22 +000096 /// addGlobalType - Add a new global type to the compile unit.
97 ///
98 void addGlobalType(const std::string &Name, DIE *Die) {
99 GlobalTypes[Name] = Die;
100 }
101
Devang Pateld90672c2009-11-20 21:37:22 +0000102 /// getDIE - Returns the debug information entry map slot for the
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000103 /// specified debug variable.
Devang Pateld90672c2009-11-20 21:37:22 +0000104 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach652b7432009-11-21 23:12:12 +0000105
Devang Pateld90672c2009-11-20 21:37:22 +0000106 /// insertDIE - Insert DIE into the map.
107 void insertDIE(MDNode *N, DIE *D) {
108 GVToDieMap.insert(std::make_pair(N, D));
109 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000110
Devang Pateld90672c2009-11-20 21:37:22 +0000111 /// getDIEEntry - Returns the debug information entry for the speciefied
112 /// debug variable.
Devang Patel8287d662009-12-15 19:16:48 +0000113 DIEEntry *getDIEEntry(MDNode *N) {
Devang Patel7d707f92010-01-19 06:19:05 +0000114 DenseMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel8287d662009-12-15 19:16:48 +0000115 if (I == GVToDIEEntryMap.end())
116 return NULL;
117 return I->second;
118 }
Devang Pateld90672c2009-11-20 21:37:22 +0000119
120 /// insertDIEEntry - Insert debug information entry into the map.
121 void insertDIEEntry(MDNode *N, DIEEntry *E) {
122 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000123 }
124
Devang Patelc50078e2009-11-21 02:48:08 +0000125 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000126 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000127 void addDie(DIE *Buffer) {
128 this->CUDie->addChild(Buffer);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000129 }
Devang Patel1233f912009-11-21 00:31:03 +0000130
131 // getIndexTyDie - Get an anonymous type for index type.
132 DIE *getIndexTyDie() {
133 return IndexTyDie;
134 }
135
Jim Grosbachb23f2422009-11-22 19:20:36 +0000136 // setIndexTyDie - Set D as anonymous type for index which can be reused
137 // later.
Devang Patel1233f912009-11-21 00:31:03 +0000138 void setIndexTyDie(DIE *D) {
139 IndexTyDie = D;
140 }
141
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000142};
143
144//===----------------------------------------------------------------------===//
145/// DbgVariable - This class is used to track local variable information.
146///
Devang Patel4cb32c32009-11-16 21:53:40 +0000147class DbgVariable {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000148 DIVariable Var; // Variable Descriptor.
149 unsigned FrameIndex; // Variable frame index.
Devang Patel90a0fe32009-11-10 23:06:00 +0000150 DbgVariable *AbstractVar; // Abstract variable for this variable.
151 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000152public:
Devang Patel90a0fe32009-11-10 23:06:00 +0000153 DbgVariable(DIVariable V, unsigned I)
154 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000155
156 // Accessors.
Devang Patel90a0fe32009-11-10 23:06:00 +0000157 DIVariable getVariable() const { return Var; }
158 unsigned getFrameIndex() const { return FrameIndex; }
159 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
160 DbgVariable *getAbstractVariable() const { return AbstractVar; }
161 void setDIE(DIE *D) { TheDIE = D; }
162 DIE *getDIE() const { return TheDIE; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000163};
164
165//===----------------------------------------------------------------------===//
166/// DbgScope - This class is used to track scope information.
167///
Devang Patel4cb32c32009-11-16 21:53:40 +0000168class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000169 DbgScope *Parent; // Parent to this scope.
Jim Grosbach652b7432009-11-21 23:12:12 +0000170 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patelae89d3b2010-01-26 21:39:14 +0000171 // Location at which this scope is inlined.
172 AssertingVH<MDNode> InlinedAtLocation;
Devang Patel90a0fe32009-11-10 23:06:00 +0000173 bool AbstractScope; // Abstract Scope
Chris Lattner855e8f52010-03-09 01:58:53 +0000174 MCSymbol *StartLabel; // Label ID of the beginning of scope.
175 MCSymbol *EndLabel; // Label ID of the end of scope.
Devang Patel90ecd192009-10-01 18:25:23 +0000176 const MachineInstr *LastInsn; // Last instruction of this scope.
177 const MachineInstr *FirstInsn; // First instruction of this scope.
Douglas Gregordf49dc72010-03-08 02:58:37 +0000178 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
179 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbar41716322009-09-19 20:40:05 +0000180
Owen Anderson696d4862009-06-24 22:53:20 +0000181 // Private state for dump()
182 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000183public:
Devang Pateldd7bb432009-10-14 21:08:09 +0000184 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000185 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Chris Lattner855e8f52010-03-09 01:58:53 +0000186 StartLabel(0), EndLabel(0),
Devang Pateldd7bb432009-10-14 21:08:09 +0000187 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000188 virtual ~DbgScope();
189
190 // Accessors.
191 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000192 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000193 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach652b7432009-11-21 23:12:12 +0000194 MDNode *getInlinedAt() const {
Devang Patelae89d3b2010-01-26 21:39:14 +0000195 return InlinedAtLocation;
Devang Pateldd7bb432009-10-14 21:08:09 +0000196 }
Devang Patel90a0fe32009-11-10 23:06:00 +0000197 MDNode *getScopeNode() const { return Desc.getNode(); }
Chris Lattner855e8f52010-03-09 01:58:53 +0000198 MCSymbol *getStartLabel() const { return StartLabel; }
199 MCSymbol *getEndLabel() const { return EndLabel; }
Douglas Gregordf49dc72010-03-08 02:58:37 +0000200 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
201 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Chris Lattner855e8f52010-03-09 01:58:53 +0000202 void setStartLabel(MCSymbol *S) { StartLabel = S; }
203 void setEndLabel(MCSymbol *E) { EndLabel = E; }
Devang Patel90ecd192009-10-01 18:25:23 +0000204 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
205 const MachineInstr *getLastInsn() { return LastInsn; }
206 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000207 void setAbstractScope() { AbstractScope = true; }
208 bool isAbstractScope() const { return AbstractScope; }
Devang Patel90ecd192009-10-01 18:25:23 +0000209 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000210
Devang Patelc50078e2009-11-21 02:48:08 +0000211 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000212 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000213 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000214
Devang Patelc50078e2009-11-21 02:48:08 +0000215 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000216 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000217 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000218
Devang Patel98e77302010-01-04 20:44:00 +0000219 void fixInstructionMarkers(DenseMap<const MachineInstr *,
220 unsigned> &MIIndexMap) {
Devang Patel6a260102009-10-01 20:31:14 +0000221 assert (getFirstInsn() && "First instruction is missing!");
Devang Patel98e77302010-01-04 20:44:00 +0000222
223 // Use the end of last child scope as end of this scope.
Douglas Gregordf49dc72010-03-08 02:58:37 +0000224 SmallVector<DbgScope *, 4> &Scopes = getScopes();
Devang Patel3b620bf2010-01-05 16:59:17 +0000225 const MachineInstr *LastInsn = getFirstInsn();
Devang Patel98e77302010-01-04 20:44:00 +0000226 unsigned LIndex = 0;
227 if (Scopes.empty()) {
228 assert (getLastInsn() && "Inner most scope does not have last insn!");
229 return;
230 }
Douglas Gregordf49dc72010-03-08 02:58:37 +0000231 for (SmallVector<DbgScope *, 4>::iterator SI = Scopes.begin(),
Devang Patel98e77302010-01-04 20:44:00 +0000232 SE = Scopes.end(); SI != SE; ++SI) {
233 DbgScope *DS = *SI;
234 DS->fixInstructionMarkers(MIIndexMap);
235 const MachineInstr *DSLastInsn = DS->getLastInsn();
236 unsigned DSI = MIIndexMap[DSLastInsn];
237 if (DSI > LIndex) {
238 LastInsn = DSLastInsn;
239 LIndex = DSI;
240 }
241 }
Devang Patel067e6d92010-02-17 02:20:34 +0000242
243 unsigned CurrentLastInsnIndex = 0;
244 if (const MachineInstr *CL = getLastInsn())
245 CurrentLastInsnIndex = MIIndexMap[CL];
246 unsigned FIndex = MIIndexMap[getFirstInsn()];
247
248 // Set LastInsn as the last instruction for this scope only if
249 // it follows
250 // 1) this scope's first instruction and
251 // 2) current last instruction for this scope, if any.
252 if (LIndex >= CurrentLastInsnIndex && LIndex >= FIndex)
253 setLastInsn(LastInsn);
Devang Patel6a260102009-10-01 20:31:14 +0000254 }
255
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000256#ifndef NDEBUG
257 void dump() const;
258#endif
259};
260
261#ifndef NDEBUG
262void DbgScope::dump() const {
David Greenedbc06132009-12-24 00:31:35 +0000263 raw_ostream &err = dbgs();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000264 err.indent(IndentLevel);
Devang Patel90a0fe32009-11-10 23:06:00 +0000265 MDNode *N = Desc.getNode();
266 N->dump();
Chris Lattner855e8f52010-03-09 01:58:53 +0000267 err << " [" << StartLabel << ", " << EndLabel << "]\n";
Devang Patel90a0fe32009-11-10 23:06:00 +0000268 if (AbstractScope)
269 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000270
271 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000272 if (!Scopes.empty())
273 err << "Children ...\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000274 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
275 if (Scopes[i] != this)
276 Scopes[i]->dump();
277
278 IndentLevel -= 2;
279}
280#endif
281
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000282DbgScope::~DbgScope() {
Douglas Gregordf49dc72010-03-08 02:58:37 +0000283 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
284 delete Scopes[i];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000285 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
286 delete Variables[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000287}
288
289} // end llvm namespace
290
Chris Lattner621c44d2009-08-22 20:48:53 +0000291DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Chris Lattner819669a2010-03-09 00:00:15 +0000292 : DwarfPrinter(OS, A, T), ModuleCU(0),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000293 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patelc50078e2009-11-21 02:48:08 +0000294 DIEValues(), StringPool(),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000295 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel90a0fe32009-11-10 23:06:00 +0000296 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000297 if (TimePassesIsEnabled)
Chris Lattner13bf2e52009-12-28 07:41:18 +0000298 DebugTimer = new Timer("Dwarf Debug Writer");
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000299}
300DwarfDebug::~DwarfDebug() {
Devang Patelc50078e2009-11-21 02:48:08 +0000301 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
302 delete DIEValues[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000303
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000304 delete DebugTimer;
305}
306
Devang Patelc50078e2009-11-21 02:48:08 +0000307/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000308///
Devang Patelc50078e2009-11-21 02:48:08 +0000309void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000310 // Profile the node so that we can make it unique.
311 FoldingSetNodeID ID;
312 Abbrev.Profile(ID);
313
314 // Check the set for priors.
315 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
316
317 // If it's newly added.
318 if (InSet == &Abbrev) {
319 // Add to abbreviation list.
320 Abbreviations.push_back(&Abbrev);
321
322 // Assign the vector position + 1 as its number.
323 Abbrev.setNumber(Abbreviations.size());
324 } else {
325 // Assign existing abbreviation number.
326 Abbrev.setNumber(InSet->getNumber());
327 }
328}
329
Devang Patelc50078e2009-11-21 02:48:08 +0000330/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000331/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000332DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel1233f912009-11-21 00:31:03 +0000333 DIEEntry *Value = new DIEEntry(Entry);
Devang Patelc50078e2009-11-21 02:48:08 +0000334 DIEValues.push_back(Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000335 return Value;
336}
337
Devang Patelc50078e2009-11-21 02:48:08 +0000338/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000339///
Devang Patelc50078e2009-11-21 02:48:08 +0000340void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000341 unsigned Form, uint64_t Integer) {
342 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000343 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000344 DIEValues.push_back(Value);
345 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000346}
347
Devang Patelc50078e2009-11-21 02:48:08 +0000348/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000349///
Devang Patelc50078e2009-11-21 02:48:08 +0000350void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000351 unsigned Form, int64_t Integer) {
352 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000353 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000354 DIEValues.push_back(Value);
355 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000356}
357
Devang Pateldf0f2152009-12-02 15:25:16 +0000358/// addString - Add a string attribute data and value. DIEString only
359/// keeps string reference.
Devang Patelc50078e2009-11-21 02:48:08 +0000360void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnercc564642010-01-23 03:11:46 +0000361 StringRef String) {
Devang Patel1233f912009-11-21 00:31:03 +0000362 DIEValue *Value = new DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000363 DIEValues.push_back(Value);
364 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000365}
366
Devang Patelc50078e2009-11-21 02:48:08 +0000367/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000368///
Devang Patelc50078e2009-11-21 02:48:08 +0000369void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000370 const MCSymbol *Label) {
Chris Lattner703d12e2010-03-08 22:31:46 +0000371 DIEValue *Value = new DIELabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000372 DIEValues.push_back(Value);
373 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000374}
375
Devang Patelc50078e2009-11-21 02:48:08 +0000376/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000377///
Devang Patelc50078e2009-11-21 02:48:08 +0000378void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000379 const MCSymbol *Label,const MCSymbol *Section,
Chris Lattnerf8bfa882010-03-08 23:23:25 +0000380 bool isEH) {
381 DIEValue *Value = new DIESectionOffset(Label, Section, isEH);
Devang Patelc50078e2009-11-21 02:48:08 +0000382 DIEValues.push_back(Value);
383 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000384}
385
Devang Patelc50078e2009-11-21 02:48:08 +0000386/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000387///
Devang Patelc50078e2009-11-21 02:48:08 +0000388void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000389 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel1233f912009-11-21 00:31:03 +0000390 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000391 DIEValues.push_back(Value);
392 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000393}
394
Devang Patelc50078e2009-11-21 02:48:08 +0000395/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000396///
Devang Patelc50078e2009-11-21 02:48:08 +0000397void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000398 DIEBlock *Block) {
399 Block->ComputeSize(TD);
Devang Patelc50078e2009-11-21 02:48:08 +0000400 DIEValues.push_back(Block);
401 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000402}
403
Devang Patelc50078e2009-11-21 02:48:08 +0000404/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000405/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000406void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000407 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000408 if (!V->getCompileUnit().Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000409 return;
410
411 unsigned Line = V->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000412 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
413 V->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000414 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000415 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
416 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000417}
418
Devang Patelc50078e2009-11-21 02:48:08 +0000419/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000420/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000421void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000422 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000423 if (!G->getCompileUnit().Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000424 return;
425
426 unsigned Line = G->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000427 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
428 G->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000429 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000430 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
431 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000432}
Devang Patel318d70d2009-08-31 22:47:13 +0000433
Devang Patelc50078e2009-11-21 02:48:08 +0000434/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000435/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000436void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel318d70d2009-08-31 22:47:13 +0000437 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000438 if (!SP->getCompileUnit().Verify())
Devang Patel318d70d2009-08-31 22:47:13 +0000439 return;
Caroline Tice9da96d82009-09-11 18:25:54 +0000440 // If the line number is 0, don't add it.
441 if (SP->getLineNumber() == 0)
442 return;
443
Devang Patel318d70d2009-08-31 22:47:13 +0000444 unsigned Line = SP->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000445 if (!SP->getContext().Verify())
446 return;
447 unsigned FileID = GetOrCreateSourceID(SP->getContext().getDirectory(),
448 SP->getContext().getFilename());
Devang Patel318d70d2009-08-31 22:47:13 +0000449 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000450 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
451 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel318d70d2009-08-31 22:47:13 +0000452}
453
Devang Patelc50078e2009-11-21 02:48:08 +0000454/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000455/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000456void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000457 // If there is no compile unit specified, don't add a line #.
458 DICompileUnit CU = Ty->getCompileUnit();
Devang Patel9e592492010-03-08 20:52:55 +0000459 if (!CU.Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000460 return;
461
462 unsigned Line = Ty->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000463 if (!Ty->getContext().Verify())
464 return;
465 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
466 Ty->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000467 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000468 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
469 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000470}
471
Devang Patel8287d662009-12-15 19:16:48 +0000472/// addSourceLine - Add location information to specified debug information
473/// entry.
474void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
475 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000476 if (!NS->getCompileUnit().Verify())
Devang Patel8287d662009-12-15 19:16:48 +0000477 return;
478
479 unsigned Line = NS->getLineNumber();
480 StringRef FN = NS->getFilename();
481 StringRef Dir = NS->getDirectory();
482
483 unsigned FileID = GetOrCreateSourceID(Dir, FN);
484 assert(FileID && "Invalid file id");
485 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
486 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
487}
488
Caroline Tice248d5572009-08-31 21:19:37 +0000489/* Byref variables, in Blocks, are declared by the programmer as
490 "SomeType VarName;", but the compiler creates a
491 __Block_byref_x_VarName struct, and gives the variable VarName
492 either the struct, or a pointer to the struct, as its type. This
493 is necessary for various behind-the-scenes things the compiler
494 needs to do with by-reference variables in blocks.
495
496 However, as far as the original *programmer* is concerned, the
497 variable should still have type 'SomeType', as originally declared.
498
499 The following function dives into the __Block_byref_x_VarName
500 struct to find the original type of the variable. This will be
501 passed back to the code generating the type for the Debug
502 Information Entry for the variable 'VarName'. 'VarName' will then
503 have the original type 'SomeType' in its debug information.
504
505 The original type 'SomeType' will be the type of the field named
506 'VarName' inside the __Block_byref_x_VarName struct.
507
508 NOTE: In order for this to not completely fail on the debugger
509 side, the Debug Information Entry for the variable VarName needs to
510 have a DW_AT_location that tells the debugger how to unwind through
511 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patelc50078e2009-11-21 02:48:08 +0000512 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-08-31 21:19:37 +0000513
514/// Find the type the programmer originally declared the variable to be
515/// and return that type.
516///
Devang Patelc50078e2009-11-21 02:48:08 +0000517DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-08-31 21:19:37 +0000518
519 DIType subType = Ty;
520 unsigned tag = Ty.getTag();
521
522 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000523 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000524 subType = DTy.getTypeDerivedFrom();
525 }
526
527 DICompositeType blockStruct = DICompositeType(subType.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000528 DIArray Elements = blockStruct.getTypeArray();
529
Caroline Tice248d5572009-08-31 21:19:37 +0000530 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
531 DIDescriptor Element = Elements.getElement(i);
532 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000533 if (Name == DT.getName())
Caroline Tice248d5572009-08-31 21:19:37 +0000534 return (DT.getTypeDerivedFrom());
535 }
536
537 return Ty;
538}
539
Devang Patelc50078e2009-11-21 02:48:08 +0000540/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000541/// and generate the DWARF information necessary to find the actual variable
542/// given the extra address information encoded in the DIVariable, starting from
543/// the starting location. Add the DWARF information to the die.
544///
Devang Patelc50078e2009-11-21 02:48:08 +0000545void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000546 unsigned Attribute,
547 const MachineLocation &Location) {
548 const DIVariable &VD = DV->getVariable();
549 DIType Ty = VD.getType();
550
551 // Decode the original location, and use that as the start of the byref
552 // variable's location.
553 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
554 DIEBlock *Block = new DIEBlock();
555
556 if (Location.isReg()) {
557 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000558 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000559 } else {
560 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000561 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
562 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000563 }
564 } else {
565 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000566 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000567 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000568 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
569 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000570 }
571
Devang Patelc50078e2009-11-21 02:48:08 +0000572 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000573 }
574
575 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
576 uint64_t Element = VD.getAddrElement(i);
577
578 if (Element == DIFactory::OpPlus) {
Devang Patelc50078e2009-11-21 02:48:08 +0000579 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
580 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000581 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000582 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000583 } else llvm_unreachable("unknown DIFactory Opcode");
584 }
585
586 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000587 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000588}
589
Caroline Tice248d5572009-08-31 21:19:37 +0000590/* Byref variables, in Blocks, are declared by the programmer as "SomeType
591 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
592 gives the variable VarName either the struct, or a pointer to the struct, as
593 its type. This is necessary for various behind-the-scenes things the
594 compiler needs to do with by-reference variables in Blocks.
595
596 However, as far as the original *programmer* is concerned, the variable
597 should still have type 'SomeType', as originally declared.
598
Devang Patelc50078e2009-11-21 02:48:08 +0000599 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000600 struct to find the original type of the variable, which is then assigned to
601 the variable's Debug Information Entry as its real type. So far, so good.
602 However now the debugger will expect the variable VarName to have the type
603 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbar41716322009-09-19 20:40:05 +0000604 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-08-31 21:19:37 +0000605 pointers and struct to find the actual variable of type SomeType.
606
607 The following function does just that. We start by getting
608 the "normal" location for the variable. This will be the location
609 of either the struct __Block_byref_x_VarName or the pointer to the
610 struct __Block_byref_x_VarName.
611
612 The struct will look something like:
613
614 struct __Block_byref_x_VarName {
615 ... <various fields>
616 struct __Block_byref_x_VarName *forwarding;
617 ... <various other fields>
618 SomeType VarName;
619 ... <maybe more fields>
620 };
621
622 If we are given the struct directly (as our starting point) we
623 need to tell the debugger to:
624
625 1). Add the offset of the forwarding field.
626
Dan Gohmandf1a7ff2010-02-10 16:03:48 +0000627 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000628 struct to use (the real one may have been copied onto the heap).
629
630 3). Add the offset for the field VarName, to find the actual variable.
631
632 If we started with a pointer to the struct, then we need to
633 dereference that pointer first, before the other steps.
634 Translating this into DWARF ops, we will need to append the following
635 to the current location description for the variable:
636
637 DW_OP_deref -- optional, if we start with a pointer
638 DW_OP_plus_uconst <forward_fld_offset>
639 DW_OP_deref
640 DW_OP_plus_uconst <varName_fld_offset>
641
642 That is what this function does. */
643
Devang Patelc50078e2009-11-21 02:48:08 +0000644/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-08-31 21:19:37 +0000645/// provided, and generate the DWARF information necessary to find the
646/// actual Block variable (navigating the Block struct) based on the
647/// starting location. Add the DWARF information to the die. For
648/// more information, read large comment just above here.
649///
Devang Patelc50078e2009-11-21 02:48:08 +0000650void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000651 unsigned Attribute,
652 const MachineLocation &Location) {
Caroline Tice248d5572009-08-31 21:19:37 +0000653 const DIVariable &VD = DV->getVariable();
654 DIType Ty = VD.getType();
655 DIType TmpTy = Ty;
656 unsigned Tag = Ty.getTag();
657 bool isPointer = false;
658
Devang Patel7f75bbe2009-11-25 17:36:49 +0000659 StringRef varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000660
661 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000662 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000663 TmpTy = DTy.getTypeDerivedFrom();
664 isPointer = true;
665 }
666
667 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
668
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000669 // Find the __forwarding field and the variable field in the __Block_byref
670 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000671 DIArray Fields = blockStruct.getTypeArray();
672 DIDescriptor varField = DIDescriptor();
673 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000674
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000675 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
676 DIDescriptor Element = Fields.getElement(i);
677 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000678 StringRef fieldName = DT.getName();
679 if (fieldName == "__forwarding")
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000680 forwardingField = Element;
Devang Patel7f75bbe2009-11-25 17:36:49 +0000681 else if (fieldName == varName)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000682 varField = Element;
683 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000684
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000685 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000686 unsigned int forwardingFieldOffset =
687 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
688 unsigned int varFieldOffset =
689 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000690
Mike Stump2fd84e22009-09-24 23:21:26 +0000691 // Decode the original location, and use that as the start of the byref
692 // variable's location.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000693 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
694 DIEBlock *Block = new DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000695
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000696 if (Location.isReg()) {
697 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000698 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000699 else {
700 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000701 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
702 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000703 }
704 } else {
705 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000706 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000707 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000708 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
709 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000710 }
Caroline Tice248d5572009-08-31 21:19:37 +0000711
Devang Patelc50078e2009-11-21 02:48:08 +0000712 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000713 }
Caroline Tice248d5572009-08-31 21:19:37 +0000714
Mike Stump2fd84e22009-09-24 23:21:26 +0000715 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000716 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000717 if (isPointer)
Devang Patelc50078e2009-11-21 02:48:08 +0000718 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000719
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000720 // Next add the offset for the '__forwarding' field:
721 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
722 // adding the offset if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000723 if (forwardingFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000724 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
725 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000726 }
Caroline Tice248d5572009-08-31 21:19:37 +0000727
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000728 // Now dereference the __forwarding field to get to the real __Block_byref
729 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000730 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000731
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000732 // Now that we've got the real __Block_byref... struct, add the offset
733 // for the variable's field to get to the location of the actual variable:
734 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000735 if (varFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000736 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
737 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000738 }
Caroline Tice248d5572009-08-31 21:19:37 +0000739
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000740 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000741 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000742}
743
Devang Patelc50078e2009-11-21 02:48:08 +0000744/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000745/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000746void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000747 const MachineLocation &Location) {
748 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
749 DIEBlock *Block = new DIEBlock();
750
751 if (Location.isReg()) {
752 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000753 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000754 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000755 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
756 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000757 }
758 } else {
759 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000760 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000761 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000762 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
763 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000764 }
765
Devang Patelc50078e2009-11-21 02:48:08 +0000766 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000767 }
768
Devang Patelc50078e2009-11-21 02:48:08 +0000769 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000770}
771
Devang Patel1a8f9a82009-12-10 19:14:49 +0000772/// addToContextOwner - Add Die into the list of its context owner's children.
773void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel9e592492010-03-08 20:52:55 +0000774 if (Context.isType()) {
Devang Patel1a8f9a82009-12-10 19:14:49 +0000775 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
776 ContextDIE->addChild(Die);
Devang Patel8287d662009-12-15 19:16:48 +0000777 } else if (Context.isNameSpace()) {
778 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode()));
779 ContextDIE->addChild(Die);
Devang Patel1a8f9a82009-12-10 19:14:49 +0000780 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
781 ContextDIE->addChild(Die);
782 else
783 ModuleCU->addDie(Die);
784}
785
Devang Patel7f139c12009-12-10 18:05:33 +0000786/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
787/// given DIType.
788DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
789 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
790 if (TyDIE)
791 return TyDIE;
792
793 // Create new type.
794 TyDIE = new DIE(dwarf::DW_TAG_base_type);
795 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
796 if (Ty.isBasicType())
797 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
798 else if (Ty.isCompositeType())
799 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
800 else {
801 assert(Ty.isDerivedType() && "Unknown kind of DIType");
802 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
803 }
804
Devang Patel1a8f9a82009-12-10 19:14:49 +0000805 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel7f139c12009-12-10 18:05:33 +0000806 return TyDIE;
807}
808
Devang Patelc50078e2009-11-21 02:48:08 +0000809/// addType - Add a new type attribute to the specified entity.
Devang Patelfe0be132009-12-09 18:24:21 +0000810void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9e592492010-03-08 20:52:55 +0000811 if (!Ty.isValid())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000812 return;
813
814 // Check for pre-existence.
Devang Patelfe0be132009-12-09 18:24:21 +0000815 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000816 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000817 if (Entry) {
818 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000819 return;
820 }
821
822 // Set up proxy.
Devang Patelc50078e2009-11-21 02:48:08 +0000823 Entry = createDIEEntry();
Devang Patelfe0be132009-12-09 18:24:21 +0000824 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000825
826 // Construct type.
Devang Patel7f139c12009-12-10 18:05:33 +0000827 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000828
Devang Patelc50078e2009-11-21 02:48:08 +0000829 Entry->setEntry(Buffer);
830 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000831}
832
Devang Patelc50078e2009-11-21 02:48:08 +0000833/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patelfe0be132009-12-09 18:24:21 +0000834void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000835 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000836 StringRef Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000837 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000838 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000839 BTy.getEncoding());
840
841 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000842 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000843 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000844 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000845 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000846}
847
Devang Patelc50078e2009-11-21 02:48:08 +0000848/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patelfe0be132009-12-09 18:24:21 +0000849void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000850 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000851 StringRef Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000852 uint64_t Size = DTy.getSizeInBits() >> 3;
853 unsigned Tag = DTy.getTag();
854
855 // FIXME - Workaround for templates.
856 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
857
858 Buffer.setTag(Tag);
859
860 // Map to main type, void will not have a type.
861 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelfe0be132009-12-09 18:24:21 +0000862 addType(&Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000863
864 // Add name if not anonymous or intermediate type.
Devang Patel76b80672009-11-30 23:56:56 +0000865 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000866 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000867
868 // Add size if non-zero (derived types might be zero-sized.)
869 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000870 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000871
872 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000873 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000874 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000875}
876
Devang Patelc50078e2009-11-21 02:48:08 +0000877/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +0000878void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000879 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000880 StringRef Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000881
882 uint64_t Size = CTy.getSizeInBits() >> 3;
883 unsigned Tag = CTy.getTag();
884 Buffer.setTag(Tag);
885
886 switch (Tag) {
887 case dwarf::DW_TAG_vector_type:
888 case dwarf::DW_TAG_array_type:
Devang Patelfe0be132009-12-09 18:24:21 +0000889 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000890 break;
891 case dwarf::DW_TAG_enumeration_type: {
892 DIArray Elements = CTy.getTypeArray();
893
894 // Add enumerators to enumeration type.
895 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
896 DIE *ElemDie = NULL;
Devang Patel9e592492010-03-08 20:52:55 +0000897 DIDescriptor Enum(Elements.getElement(i).getNode());
898 if (Enum.isEnumerator()) {
899 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000900 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +0000901 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000902 }
903 }
904 break;
905 case dwarf::DW_TAG_subroutine_type: {
906 // Add return type.
907 DIArray Elements = CTy.getTypeArray();
908 DIDescriptor RTy = Elements.getElement(0);
Devang Patelfe0be132009-12-09 18:24:21 +0000909 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000910
911 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +0000912 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000913
914 // Add arguments.
915 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
916 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
917 DIDescriptor Ty = Elements.getElement(i);
Devang Patelfe0be132009-12-09 18:24:21 +0000918 addType(Arg, DIType(Ty.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000919 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000920 }
921 }
922 break;
923 case dwarf::DW_TAG_structure_type:
924 case dwarf::DW_TAG_union_type:
925 case dwarf::DW_TAG_class_type: {
926 // Add elements to structure type.
927 DIArray Elements = CTy.getTypeArray();
928
929 // A forward struct declared type may not have elements available.
Devang Patel9e592492010-03-08 20:52:55 +0000930 unsigned N = Elements.getNumElements();
931 if (N == 0)
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000932 break;
933
934 // Add elements to structure type.
Devang Patel9e592492010-03-08 20:52:55 +0000935 for (unsigned i = 0; i < N; ++i) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000936 DIDescriptor Element = Elements.getElement(i);
937 DIE *ElemDie = NULL;
Devang Patel9e592492010-03-08 20:52:55 +0000938 if (Element.isSubprogram())
Devang Patel814a12c2009-12-14 16:18:45 +0000939 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Devang Patel9e592492010-03-08 20:52:55 +0000940 else if (Element.isVariable()) {
Devang Patel423dfa02010-01-30 01:08:30 +0000941 DIVariable DV(Element.getNode());
942 ElemDie = new DIE(dwarf::DW_TAG_variable);
943 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
944 DV.getName());
945 addType(ElemDie, DV.getType());
946 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
947 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
948 addSourceLine(ElemDie, &DV);
Devang Patel9e592492010-03-08 20:52:55 +0000949 } else if (Element.isDerivedType())
Devang Patelfe0be132009-12-09 18:24:21 +0000950 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patel9e592492010-03-08 20:52:55 +0000951 else
952 continue;
Devang Patelc50078e2009-11-21 02:48:08 +0000953 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000954 }
955
Devang Patel20b32102009-08-27 23:51:51 +0000956 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +0000957 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000958
959 unsigned RLang = CTy.getRunTimeLang();
960 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +0000961 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000962 dwarf::DW_FORM_data1, RLang);
Devang Patel5ae52402010-01-26 21:16:06 +0000963
964 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel9e592492010-03-08 20:52:55 +0000965 if (DIDescriptor(ContainingType.getNode()).isCompositeType())
Devang Patel5ae52402010-01-26 21:16:06 +0000966 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
967 getOrCreateTypeDIE(DIType(ContainingType.getNode())));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000968 break;
969 }
970 default:
971 break;
972 }
973
974 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000975 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000976 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000977
Devang Pateldc73c372010-01-29 18:34:58 +0000978 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000979 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
980 // Add size if non-zero (derived types might be zero-sized.)
981 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000982 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000983 else {
984 // Add zero size if it is not a forward declaration.
985 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000986 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000987 else
Devang Patelc50078e2009-11-21 02:48:08 +0000988 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000989 }
990
991 // Add source line info if available.
992 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000993 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000994 }
995}
996
Devang Patelc50078e2009-11-21 02:48:08 +0000997/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
998void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000999 int64_t L = SR.getLo();
1000 int64_t H = SR.getHi();
1001 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1002
Devang Patelc50078e2009-11-21 02:48:08 +00001003 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +00001004 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +00001005 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld3df6972009-12-04 23:10:24 +00001006 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001007
Devang Patelc50078e2009-11-21 02:48:08 +00001008 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001009}
1010
Devang Patelc50078e2009-11-21 02:48:08 +00001011/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +00001012void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001013 DICompositeType *CTy) {
1014 Buffer.setTag(dwarf::DW_TAG_array_type);
1015 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001016 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001017
1018 // Emit derived type.
Devang Patelfe0be132009-12-09 18:24:21 +00001019 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001020 DIArray Elements = CTy->getTypeArray();
1021
Devang Patel1233f912009-11-21 00:31:03 +00001022 // Get an anonymous type for index type.
Devang Patelfe0be132009-12-09 18:24:21 +00001023 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel1233f912009-11-21 00:31:03 +00001024 if (!IdxTy) {
1025 // Construct an anonymous type for index type.
1026 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +00001027 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1028 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +00001029 dwarf::DW_ATE_signed);
Devang Patelfe0be132009-12-09 18:24:21 +00001030 ModuleCU->addDie(IdxTy);
1031 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +00001032 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001033
1034 // Add subranges to array type.
1035 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1036 DIDescriptor Element = Elements.getElement(i);
1037 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001038 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001039 }
1040}
1041
Devang Patelc50078e2009-11-21 02:48:08 +00001042/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel9e592492010-03-08 20:52:55 +00001043DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001044 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel9e592492010-03-08 20:52:55 +00001045 StringRef Name = ETy.getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001046 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel9e592492010-03-08 20:52:55 +00001047 int64_t Value = ETy.getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +00001048 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001049 return Enumerator;
1050}
1051
Devang Patel141e1c42010-01-05 01:46:14 +00001052/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1053/// printer to not emit usual symbol prefix before the symbol name is used then
1054/// return linkage name after skipping this special LLVM prefix.
1055static StringRef getRealLinkageName(StringRef LinkageName) {
1056 char One = '\1';
1057 if (LinkageName.startswith(StringRef(&One, 1)))
1058 return LinkageName.substr(1);
1059 return LinkageName;
1060}
1061
Devang Patelc50078e2009-11-21 02:48:08 +00001062/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patelfe0be132009-12-09 18:24:21 +00001063DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001064 // If the global variable was optmized out then no need to create debug info
1065 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +00001066 if (!GV.getGlobal()) return NULL;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001067 if (GV.getDisplayName().empty()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +00001068
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001069 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +00001070 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001071 GV.getDisplayName());
1072
Devang Patel7f75bbe2009-11-25 17:36:49 +00001073 StringRef LinkageName = GV.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001074 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001075 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001076 getRealLinkageName(LinkageName));
1077
Devang Patelfe0be132009-12-09 18:24:21 +00001078 addType(GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001079 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001080 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1081 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001082
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001083 return GVDie;
1084}
1085
Devang Patelc50078e2009-11-21 02:48:08 +00001086/// createMemberDIE - Create new member DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001087DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001088 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel7f75bbe2009-11-25 17:36:49 +00001089 StringRef Name = DT.getName();
1090 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001091 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001092
Devang Patelfe0be132009-12-09 18:24:21 +00001093 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001094
Devang Patelc50078e2009-11-21 02:48:08 +00001095 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001096
Devang Patel7d9fe582009-11-04 22:06:12 +00001097 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001098 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001099
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001100 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001101 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001102
1103 if (Size != FieldSize) {
1104 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001105 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1106 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001107
1108 uint64_t Offset = DT.getOffsetInBits();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001109 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1110 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer631e3e32010-01-07 17:50:57 +00001111 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001112 Offset -= FieldOffset;
1113
1114 // Maybe we need to work from the other end.
1115 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001116 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001117
Devang Patel7d9fe582009-11-04 22:06:12 +00001118 // Here WD_AT_data_member_location points to the anonymous
1119 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001120 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001121
1122 } else
1123 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001124 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001125
Devang Patel84aba942010-02-03 20:08:48 +00001126 if (DT.getTag() == dwarf::DW_TAG_inheritance
1127 && DT.isVirtual()) {
1128
1129 // For C++, virtual base classes are not at fixed offset. Use following
1130 // expression to extract appropriate offset from vtable.
1131 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1132
1133 DIEBlock *VBaseLocationDie = new DIEBlock();
1134 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1135 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1136 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1137 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1138 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1139 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1140 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1141
1142 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1143 VBaseLocationDie);
1144 } else
1145 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001146
1147 if (DT.isProtected())
Devang Patel188c85d2009-12-03 19:11:07 +00001148 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001149 dwarf::DW_ACCESS_protected);
1150 else if (DT.isPrivate())
Devang Patel188c85d2009-12-03 19:11:07 +00001151 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001152 dwarf::DW_ACCESS_private);
Devang Patel188c85d2009-12-03 19:11:07 +00001153 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1154 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1155 dwarf::DW_ACCESS_public);
1156 if (DT.isVirtual())
1157 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1158 dwarf::DW_VIRTUALITY_virtual);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001159 return MemberDie;
1160}
1161
Devang Patel814a12c2009-12-14 16:18:45 +00001162/// createSubprogramDIE - Create new DIE using SP.
1163DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1164 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1165 if (SPDie)
1166 return SPDie;
1167
1168 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel36cd9f82010-03-02 17:58:15 +00001169 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel0ab194f2010-03-02 01:26:20 +00001170 if (!SP.getName().empty())
1171 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001172
Devang Patel7f75bbe2009-11-25 17:36:49 +00001173 StringRef LinkageName = SP.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001174 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001175 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001176 getRealLinkageName(LinkageName));
1177
Devang Patelc50078e2009-11-21 02:48:08 +00001178 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001179
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001180 // Add prototyped tag, if C or ObjC.
1181 unsigned Lang = SP.getCompileUnit().getLanguage();
1182 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1183 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001184 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001185
1186 // Add Return Type.
Devang Patelc1df8792009-12-03 01:25:38 +00001187 DICompositeType SPTy = SP.getType();
1188 DIArray Args = SPTy.getTypeArray();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001189 unsigned SPTag = SPTy.getTag();
Devang Patel188c85d2009-12-03 19:11:07 +00001190
Devang Patel9e592492010-03-08 20:52:55 +00001191 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelfe0be132009-12-09 18:24:21 +00001192 addType(SPDie, SPTy);
Devang Patelc1df8792009-12-03 01:25:38 +00001193 else
Devang Patelfe0be132009-12-09 18:24:21 +00001194 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patelc1df8792009-12-03 01:25:38 +00001195
Devang Patel188c85d2009-12-03 19:11:07 +00001196 unsigned VK = SP.getVirtuality();
1197 if (VK) {
1198 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1199 DIEBlock *Block = new DIEBlock();
1200 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1201 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1202 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel7d707f92010-01-19 06:19:05 +00001203 ContainingTypeMap.insert(std::make_pair(SPDie,
1204 SP.getContainingType().getNode()));
Devang Patel188c85d2009-12-03 19:11:07 +00001205 }
1206
Devang Patel814a12c2009-12-14 16:18:45 +00001207 if (MakeDecl || !SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001208 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001209
1210 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patelc1df8792009-12-03 01:25:38 +00001211 // be handled while processing variables.
1212 DICompositeType SPTy = SP.getType();
1213 DIArray Args = SPTy.getTypeArray();
1214 unsigned SPTag = SPTy.getTag();
1215
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001216 if (SPTag == dwarf::DW_TAG_subroutine_type)
1217 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1218 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc5dc7252010-02-06 01:02:37 +00001219 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1220 addType(Arg, ATy);
1221 if (ATy.isArtificial())
1222 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc50078e2009-11-21 02:48:08 +00001223 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001224 }
1225 }
1226
Devang Patelbf7d00a2010-02-03 19:57:19 +00001227 if (SP.isArtificial())
1228 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1229
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001230 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001231 ModuleCU->insertDIE(SP.getNode(), SPDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001232 return SPDie;
1233}
1234
Devang Patel90a0fe32009-11-10 23:06:00 +00001235/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1236/// Initialize scope and update scope hierarchy.
1237DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1238 MDNode *InlinedAt) {
1239 assert (N && "Invalid Scope encoding!");
1240 assert (MI && "Missing machine instruction!");
1241 bool GetConcreteScope = (MI && InlinedAt);
1242
1243 DbgScope *NScope = NULL;
1244
1245 if (InlinedAt)
1246 NScope = DbgScopeMap.lookup(InlinedAt);
1247 else
1248 NScope = DbgScopeMap.lookup(N);
1249 assert (NScope && "Unable to find working scope!");
1250
1251 if (NScope->getFirstInsn())
1252 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001253
1254 DbgScope *Parent = NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001255 if (GetConcreteScope) {
Devang Pateldd7bb432009-10-14 21:08:09 +00001256 DILocation IL(InlinedAt);
Jim Grosbach652b7432009-11-21 23:12:12 +00001257 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel90a0fe32009-11-10 23:06:00 +00001258 IL.getOrigLocation().getNode());
1259 assert (Parent && "Unable to find Parent scope!");
1260 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001261 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001262 } else if (DIDescriptor(N).isLexicalBlock()) {
1263 DILexicalBlock DB(N);
Devang Patel9e592492010-03-08 20:52:55 +00001264 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1265 NScope->setParent(Parent);
1266 Parent->addScope(NScope);
Devang Pateldd7bb432009-10-14 21:08:09 +00001267 }
Devang Patel6a260102009-10-01 20:31:14 +00001268
Devang Patelf5278f22009-10-27 20:47:17 +00001269 NScope->setFirstInsn(MI);
Devang Patel6a260102009-10-01 20:31:14 +00001270
Devang Patel90a0fe32009-11-10 23:06:00 +00001271 if (!Parent && !InlinedAt) {
Devang Patelce8986f2009-11-11 00:31:36 +00001272 StringRef SPName = DISubprogram(N).getLinkageName();
1273 if (SPName == MF->getFunction()->getName())
1274 CurrentFnDbgScope = NScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00001275 }
Devang Patel6a260102009-10-01 20:31:14 +00001276
Devang Patel90a0fe32009-11-10 23:06:00 +00001277 if (GetConcreteScope) {
1278 ConcreteScopes[InlinedAt] = NScope;
1279 getOrCreateAbstractScope(N);
1280 }
1281
Devang Patelf5278f22009-10-27 20:47:17 +00001282 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001283}
1284
Devang Patel90a0fe32009-11-10 23:06:00 +00001285DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1286 assert (N && "Invalid Scope encoding!");
1287
1288 DbgScope *AScope = AbstractScopes.lookup(N);
1289 if (AScope)
1290 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001291
Devang Patel90a0fe32009-11-10 23:06:00 +00001292 DbgScope *Parent = NULL;
1293
1294 DIDescriptor Scope(N);
1295 if (Scope.isLexicalBlock()) {
1296 DILexicalBlock DB(N);
1297 DIDescriptor ParentDesc = DB.getContext();
Devang Patel9e592492010-03-08 20:52:55 +00001298 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001299 }
1300
1301 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1302
1303 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001304 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001305 AScope->setAbstractScope();
1306 AbstractScopes[N] = AScope;
1307 if (DIDescriptor(N).isSubprogram())
1308 AbstractScopesList.push_back(AScope);
1309 return AScope;
1310}
Devang Patel6a260102009-10-01 20:31:14 +00001311
Jim Grosbach652b7432009-11-21 23:12:12 +00001312/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-11-21 02:48:08 +00001313/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1314/// If there are global variables in this scope then create and insert
1315/// DIEs for these variables.
1316DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001317
Devang Pateld90672c2009-11-20 21:37:22 +00001318 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel90a0fe32009-11-10 23:06:00 +00001319 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patel814a12c2009-12-14 16:18:45 +00001320 DISubprogram SP(SPNode);
Devang Patel530a81e2010-02-05 23:09:20 +00001321 // There is not any need to generate specification DIE for a function
1322 // defined at compile unit level. If a function is defined inside another
1323 // function then gdb prefers the definition at top level and but does not
1324 // expect specification DIE in parent function. So avoid creating
1325 // specification DIE for a function defined inside a function.
1326 if (SP.isDefinition() && !SP.getContext().isCompileUnit()
Devang Patel76667012010-03-11 23:44:52 +00001327 && !SP.getContext().isFile()
Devang Patel530a81e2010-02-05 23:09:20 +00001328 && !SP.getContext().isSubprogram()) {
Devang Patel814a12c2009-12-14 16:18:45 +00001329 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1330 // Add arguments.
1331 DICompositeType SPTy = SP.getType();
1332 DIArray Args = SPTy.getTypeArray();
1333 unsigned SPTag = SPTy.getTag();
1334 if (SPTag == dwarf::DW_TAG_subroutine_type)
1335 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1336 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc5dc7252010-02-06 01:02:37 +00001337 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1338 addType(Arg, ATy);
1339 if (ATy.isArtificial())
1340 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel814a12c2009-12-14 16:18:45 +00001341 SPDie->addChild(Arg);
1342 }
1343 DIE *SPDeclDie = SPDie;
1344 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1345 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1346 SPDeclDie);
Devang Patel814a12c2009-12-14 16:18:45 +00001347 ModuleCU->addDie(SPDie);
1348 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001349
Devang Patelc50078e2009-11-21 02:48:08 +00001350 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001351 getDWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001352 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001353 getDWLabel("func_end", SubprogramCount));
Devang Patel90a0fe32009-11-10 23:06:00 +00001354 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patelc50078e2009-11-21 02:48:08 +00001355 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach652b7432009-11-21 23:12:12 +00001356
Devang Patel90a0fe32009-11-10 23:06:00 +00001357 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001358 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001359
Devang Patel90a0fe32009-11-10 23:06:00 +00001360 return SPDie;
1361}
1362
Jim Grosbach652b7432009-11-21 23:12:12 +00001363/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001364/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1365DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Chris Lattner855e8f52010-03-09 01:58:53 +00001366 MCSymbol *Start = Scope->getStartLabel();
1367 MCSymbol *End = Scope->getEndLabel();
Chris Lattner4ba1da92010-03-09 04:48:35 +00001368 if (Start == 0) return 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00001369
Chris Lattner855e8f52010-03-09 01:58:53 +00001370 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1371 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattner5e423432010-03-09 01:51:43 +00001372
Devang Patel90a0fe32009-11-10 23:06:00 +00001373 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1374 if (Scope->isAbstractScope())
1375 return ScopeDIE;
1376
Devang Patelc50078e2009-11-21 02:48:08 +00001377 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattner855e8f52010-03-09 01:58:53 +00001378 Start ? Start : getDWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001379 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattner855e8f52010-03-09 01:58:53 +00001380 End ? End : getDWLabel("func_end", SubprogramCount));
Devang Patel90a0fe32009-11-10 23:06:00 +00001381
1382 return ScopeDIE;
1383}
1384
Devang Patelc50078e2009-11-21 02:48:08 +00001385/// constructInlinedScopeDIE - This scope represents inlined body of
1386/// a function. Construct DIE to represent this concrete inlined copy
1387/// of the function.
1388DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Chris Lattner855e8f52010-03-09 01:58:53 +00001389 MCSymbol *StartLabel = Scope->getStartLabel();
1390 MCSymbol *EndLabel = Scope->getEndLabel();
Chris Lattner4ba1da92010-03-09 04:48:35 +00001391 if (StartLabel == 0) return 0;
1392
Chris Lattner855e8f52010-03-09 01:58:53 +00001393 assert(StartLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001394 "Invalid starting label for an inlined scope!");
Chris Lattner855e8f52010-03-09 01:58:53 +00001395 assert(EndLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001396 "Invalid end label for an inlined scope!");
Devang Patel9e592492010-03-08 20:52:55 +00001397 if (!Scope->getScopeNode())
Devang Patelbe149872010-03-08 19:20:38 +00001398 return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001399 DIScope DS(Scope->getScopeNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001400 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1401
1402 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001403 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001404 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001405 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001406 dwarf::DW_FORM_ref4, OriginDIE);
1407
Chris Lattneread58652010-03-09 00:31:02 +00001408 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattner855e8f52010-03-09 01:58:53 +00001409 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel90a0fe32009-11-10 23:06:00 +00001410
1411 InlinedSubprogramDIEs.insert(OriginDIE);
1412
1413 // Track the start label for this inlined function.
Devang Patel7d707f92010-01-19 06:19:05 +00001414 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel90a0fe32009-11-10 23:06:00 +00001415 I = InlineInfo.find(InlinedSP.getNode());
1416
1417 if (I == InlineInfo.end()) {
Chris Lattneread58652010-03-09 00:31:02 +00001418 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartLabel,
Jim Grosbachb23f2422009-11-22 19:20:36 +00001419 ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001420 InlinedSPNodes.push_back(InlinedSP.getNode());
1421 } else
Chris Lattneread58652010-03-09 00:31:02 +00001422 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001423
1424 StringPool.insert(InlinedSP.getName());
Devang Patel141e1c42010-01-05 01:46:14 +00001425 StringPool.insert(getRealLinkageName(InlinedSP.getLinkageName()));
1426
Devang Patel90a0fe32009-11-10 23:06:00 +00001427 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001428 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1429 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001430
1431 return ScopeDIE;
1432}
1433
Devang Patelc50078e2009-11-21 02:48:08 +00001434
1435/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelfe0be132009-12-09 18:24:21 +00001436DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001437 // Get the descriptor.
1438 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001439 StringRef Name = VD.getName();
1440 if (Name.empty())
Devang Patelab9a0682009-11-13 02:25:26 +00001441 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001442
1443 // Translate tag to proper Dwarf tag. The result variable is dropped for
1444 // now.
1445 unsigned Tag;
1446 switch (VD.getTag()) {
1447 case dwarf::DW_TAG_return_variable:
1448 return NULL;
1449 case dwarf::DW_TAG_arg_variable:
1450 Tag = dwarf::DW_TAG_formal_parameter;
1451 break;
1452 case dwarf::DW_TAG_auto_variable: // fall thru
1453 default:
1454 Tag = dwarf::DW_TAG_variable;
1455 break;
1456 }
1457
1458 // Define variable debug information entry.
1459 DIE *VariableDie = new DIE(Tag);
1460
1461
1462 DIE *AbsDIE = NULL;
1463 if (DbgVariable *AV = DV->getAbstractVariable())
1464 AbsDIE = AV->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001465
Devang Patel90a0fe32009-11-10 23:06:00 +00001466 if (AbsDIE) {
1467 DIScope DS(Scope->getScopeNode());
1468 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001469 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001470 (void) OriginSPDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001471 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1472 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1473 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001474 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001475 dwarf::DW_FORM_ref4, AbsDIE);
1476 }
1477 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001478 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1479 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001480
1481 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001482 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001483 // addresses instead.
1484 if (VD.isBlockByrefVariable())
Devang Patelfe0be132009-12-09 18:24:21 +00001485 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001486 else
Devang Patelfe0be132009-12-09 18:24:21 +00001487 addType(VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001488 }
1489
1490 // Add variable address.
1491 if (!Scope->isAbstractScope()) {
1492 MachineLocation Location;
Jim Grosbach587d4882009-11-22 20:14:00 +00001493 unsigned FrameReg;
1494 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1495 Location.set(FrameReg, Offset);
Jim Grosbach652b7432009-11-21 23:12:12 +00001496
Devang Patel90a0fe32009-11-10 23:06:00 +00001497 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001498 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001499 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001500 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001501 else
Devang Patelc50078e2009-11-21 02:48:08 +00001502 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001503 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001504
1505 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1506 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001507 DV->setDIE(VariableDie);
1508 return VariableDie;
1509
1510}
Devang Patelc50078e2009-11-21 02:48:08 +00001511
Devang Patelec13b4f2009-11-24 01:14:22 +00001512void DwarfDebug::addPubTypes(DISubprogram SP) {
1513 DICompositeType SPTy = SP.getType();
1514 unsigned SPTag = SPTy.getTag();
1515 if (SPTag != dwarf::DW_TAG_subroutine_type)
1516 return;
1517
1518 DIArray Args = SPTy.getTypeArray();
Devang Patelec13b4f2009-11-24 01:14:22 +00001519 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1520 DIType ATy(Args.getElement(i).getNode());
Devang Patel9e592492010-03-08 20:52:55 +00001521 if (!ATy.isValid())
Devang Patelec13b4f2009-11-24 01:14:22 +00001522 continue;
1523 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel9e592492010-03-08 20:52:55 +00001524 if (DIDescriptor(CATy.getNode()).Verify() && !CATy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001525 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1526 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1527 }
1528 }
1529}
1530
Devang Patelc50078e2009-11-21 02:48:08 +00001531/// constructScopeDIE - Construct a DIE for this scope.
1532DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel9e592492010-03-08 20:52:55 +00001533 if (!Scope || !Scope->getScopeNode())
1534 return NULL;
1535
1536 DIScope DS(Scope->getScopeNode());
1537 DIE *ScopeDIE = NULL;
1538 if (Scope->getInlinedAt())
1539 ScopeDIE = constructInlinedScopeDIE(Scope);
1540 else if (DS.isSubprogram()) {
1541 if (Scope->isAbstractScope())
1542 ScopeDIE = ModuleCU->getDIE(DS.getNode());
1543 else
1544 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
1545 }
1546 else {
1547 ScopeDIE = constructLexicalScopeDIE(Scope);
1548 if (!ScopeDIE) return NULL;
1549 }
1550
Devang Patel90a0fe32009-11-10 23:06:00 +00001551 // Add variables to scope.
Douglas Gregordf49dc72010-03-08 02:58:37 +00001552 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel90a0fe32009-11-10 23:06:00 +00001553 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelfe0be132009-12-09 18:24:21 +00001554 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach652b7432009-11-21 23:12:12 +00001555 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001556 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001557 }
1558
1559 // Add nested scopes.
Douglas Gregordf49dc72010-03-08 02:58:37 +00001560 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel90a0fe32009-11-10 23:06:00 +00001561 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1562 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001563 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001564 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001565 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001566 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001567
1568 if (DS.isSubprogram())
1569 addPubTypes(DISubprogram(DS.getNode()));
1570
1571 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001572}
1573
Bill Wendlingf5839192009-05-20 23:19:06 +00001574/// GetOrCreateSourceID - Look up the source id with the given directory and
1575/// source file names. If none currently exists, create a new id and insert it
1576/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1577/// maps as well.
Devang Patel7f75bbe2009-11-25 17:36:49 +00001578unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001579 unsigned DId;
1580 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1581 if (DI != DirectoryIdMap.end()) {
1582 DId = DI->getValue();
1583 } else {
1584 DId = DirectoryNames.size() + 1;
1585 DirectoryIdMap[DirName] = DId;
1586 DirectoryNames.push_back(DirName);
1587 }
1588
1589 unsigned FId;
1590 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1591 if (FI != SourceFileIdMap.end()) {
1592 FId = FI->getValue();
1593 } else {
1594 FId = SourceFileNames.size() + 1;
1595 SourceFileIdMap[FileName] = FId;
1596 SourceFileNames.push_back(FileName);
1597 }
1598
1599 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1600 SourceIdMap.find(std::make_pair(DId, FId));
1601 if (SI != SourceIdMap.end())
1602 return SI->second;
1603
1604 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1605 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1606 SourceIds.push_back(std::make_pair(DId, FId));
1607
1608 return SrcId;
1609}
1610
Devang Patel8287d662009-12-15 19:16:48 +00001611/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1612DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1613 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1614 if (NDie)
1615 return NDie;
1616 NDie = new DIE(dwarf::DW_TAG_namespace);
1617 ModuleCU->insertDIE(NS.getNode(), NDie);
1618 if (!NS.getName().empty())
1619 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1620 addSourceLine(NDie, &NS);
1621 addToContextOwner(NDie, NS.getContext());
1622 return NDie;
1623}
1624
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001625void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001626 DICompileUnit DIUnit(N);
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001627 // Use first compile unit marked as isMain as the compile unit for this
1628 // module.
1629 if (ModuleCU || !DIUnit.isMain())
1630 return;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001631 StringRef FN = DIUnit.getFilename();
1632 StringRef Dir = DIUnit.getDirectory();
Devang Patelaaf012e2009-09-29 18:40:58 +00001633 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001634
1635 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001636 // FIXME: Why getting the delta between two identical labels??
Devang Patelc50078e2009-11-21 02:48:08 +00001637 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001638 getTempLabel("section_line"), getTempLabel("section_line"),
Bill Wendlingf5839192009-05-20 23:19:06 +00001639 false);
Devang Patelc50078e2009-11-21 02:48:08 +00001640 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001641 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001642 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001643 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001644 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001645
Devang Patel7f75bbe2009-11-25 17:36:49 +00001646 if (!Dir.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001647 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001648 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001649 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001650
Devang Patel7f75bbe2009-11-25 17:36:49 +00001651 StringRef Flags = DIUnit.getFlags();
1652 if (!Flags.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001653 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001654
1655 unsigned RVer = DIUnit.getRunTimeVersion();
1656 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001657 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001658 dwarf::DW_FORM_data1, RVer);
1659
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001660 assert(!ModuleCU &&
1661 "ModuleCU assigned since the top of constructCompileUnit");
1662 ModuleCU = new CompileUnit(ID, Die);
Bill Wendlingf5839192009-05-20 23:19:06 +00001663}
1664
Devang Patelc50078e2009-11-21 02:48:08 +00001665void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001666 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001667
Devang Patel0c03f062009-09-04 23:59:07 +00001668 // If debug information is malformed then ignore it.
1669 if (DI_GV.Verify() == false)
1670 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001671
1672 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001673 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel166f8432009-06-26 01:49:18 +00001674 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001675
Devang Patelfe0be132009-12-09 18:24:21 +00001676 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel6d479632009-12-10 23:25:41 +00001677 if (!VariableDie)
1678 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001679
Bill Wendlingf5839192009-05-20 23:19:06 +00001680 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001681 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001682
1683 // Add to context owner.
Devang Pateldcc0dce2010-01-15 01:12:22 +00001684 DIDescriptor GVContext = DI_GV.getContext();
1685 // Do not create specification DIE if context is either compile unit
1686 // or a subprogram.
1687 if (DI_GV.isDefinition() && !GVContext.isCompileUnit()
Devang Patel76667012010-03-11 23:44:52 +00001688 && !GVContext.isFile() && !GVContext.isSubprogram()) {
Devang Patel8287d662009-12-15 19:16:48 +00001689 // Create specification DIE.
1690 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1691 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1692 dwarf::DW_FORM_ref4, VariableDie);
1693 DIEBlock *Block = new DIEBlock();
1694 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001695 addLabel(Block, 0, dwarf::DW_FORM_udata,
1696 Asm->GetGlobalValueSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001697 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patela0a98582010-02-09 01:58:33 +00001698 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel8287d662009-12-15 19:16:48 +00001699 ModuleCU->addDie(VariableSpecDIE);
1700 } else {
1701 DIEBlock *Block = new DIEBlock();
1702 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001703 addLabel(Block, 0, dwarf::DW_FORM_udata,
1704 Asm->GetGlobalValueSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001705 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1706 }
Devang Pateldcc0dce2010-01-15 01:12:22 +00001707 addToContextOwner(VariableDie, GVContext);
Devang Patel1a8f9a82009-12-10 19:14:49 +00001708
Bill Wendlingf5839192009-05-20 23:19:06 +00001709 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001710 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001711
1712 DIType GTy = DI_GV.getType();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001713 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001714 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1715 assert (Entry && "Missing global type!");
1716 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1717 }
Devang Patel166f8432009-06-26 01:49:18 +00001718 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001719}
1720
Devang Patelc50078e2009-11-21 02:48:08 +00001721void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001722 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001723
1724 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001725 if (ModuleCU->getDIE(N))
Devang Patel166f8432009-06-26 01:49:18 +00001726 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001727
1728 if (!SP.isDefinition())
1729 // This is a method declaration which will be handled while constructing
1730 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001731 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001732
Devang Patelfe0be132009-12-09 18:24:21 +00001733 DIE *SubprogramDie = createSubprogramDIE(SP);
Bill Wendlingf5839192009-05-20 23:19:06 +00001734
1735 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001736 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001737
1738 // Add to context owner.
Devang Patel8287d662009-12-15 19:16:48 +00001739 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patelde2d3682009-12-08 23:21:45 +00001740
Bill Wendlingf5839192009-05-20 23:19:06 +00001741 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001742 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001743
Devang Patel166f8432009-06-26 01:49:18 +00001744 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001745}
1746
Devang Patelc50078e2009-11-21 02:48:08 +00001747/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001748/// content. Create global DIEs and emit initial debug info sections.
1749/// This is inovked by the target AsmPrinter.
Devang Patelc50078e2009-11-21 02:48:08 +00001750void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel59a1d422009-06-25 22:36:02 +00001751 this->M = M;
1752
Bill Wendlingf5839192009-05-20 23:19:06 +00001753 if (TimePassesIsEnabled)
1754 DebugTimer->startTimer();
1755
Devang Patel1b4d6832009-11-11 19:55:08 +00001756 if (!MAI->doesSupportDebugInformation())
1757 return;
1758
Devang Patelfda766d2009-07-30 18:56:46 +00001759 DebugInfoFinder DbgFinder;
1760 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001761
Bill Wendlingf5839192009-05-20 23:19:06 +00001762 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001763 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1764 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001765 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001766
Devang Patel5a3d37f2009-06-29 20:45:18 +00001767 if (!ModuleCU)
Devang Patel7f9e0f32010-03-08 22:02:50 +00001768 return;
Devang Patelf97a05a2009-06-29 20:38:13 +00001769
Devang Patel90a0fe32009-11-10 23:06:00 +00001770 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001771 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1772 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001773 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001774
Devang Patel1a8f9a82009-12-10 19:14:49 +00001775 // Create DIEs for each global variable.
1776 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1777 E = DbgFinder.global_variable_end(); I != E; ++I)
1778 constructGlobalVariableDIE(*I);
1779
Bill Wendlingf5839192009-05-20 23:19:06 +00001780 MMI = mmi;
1781 shouldEmit = true;
1782 MMI->setDebugInfoAvailability(true);
1783
1784 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001785 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001786
1787 // Print out .file directives to specify files for .loc directives. These are
1788 // printed out early so that they precede any .loc directives.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001789 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001790 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1791 // Remember source id starts at 1.
1792 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerad653482010-01-22 22:09:00 +00001793 // FIXME: don't use sys::path for this! This should not depend on the
1794 // host.
Bill Wendlingf5839192009-05-20 23:19:06 +00001795 sys::Path FullPath(getSourceDirectoryName(Id.first));
1796 bool AppendOk =
1797 FullPath.appendComponent(getSourceFileName(Id.second));
1798 assert(AppendOk && "Could not append filename to directory!");
1799 AppendOk = false;
Chris Lattner59be4ea2010-01-25 18:58:59 +00001800 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001801 }
1802 }
1803
1804 // Emit initial sections
Devang Patelc50078e2009-11-21 02:48:08 +00001805 emitInitial();
Bill Wendlingf5839192009-05-20 23:19:06 +00001806
1807 if (TimePassesIsEnabled)
1808 DebugTimer->stopTimer();
1809}
1810
Devang Patelc50078e2009-11-21 02:48:08 +00001811/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001812///
Devang Patelc50078e2009-11-21 02:48:08 +00001813void DwarfDebug::endModule() {
Devang Patel95d477e2009-10-06 00:03:14 +00001814 if (!ModuleCU)
Bill Wendlingf5839192009-05-20 23:19:06 +00001815 return;
1816
1817 if (TimePassesIsEnabled)
1818 DebugTimer->startTimer();
1819
Devang Patel90a0fe32009-11-10 23:06:00 +00001820 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1821 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1822 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1823 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001824 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001825 }
1826
Devang Patelc1df8792009-12-03 01:25:38 +00001827 // Insert top level DIEs.
1828 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1829 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1830 ModuleCU->getCUDie()->addChild(*TI);
1831
Devang Patel7d707f92010-01-19 06:19:05 +00001832 for (DenseMap<DIE *, MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel188c85d2009-12-03 19:11:07 +00001833 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1834 DIE *SPDie = CI->first;
1835 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1836 if (!N) continue;
1837 DIE *NDie = ModuleCU->getDIE(N);
1838 if (!NDie) continue;
1839 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel69aae432010-01-15 00:26:31 +00001840 // FIXME - This is not the correct approach.
1841 // addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel188c85d2009-12-03 19:11:07 +00001842 }
1843
Bill Wendlingf5839192009-05-20 23:19:06 +00001844 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001845 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00001846 Asm->OutStreamer.EmitLabel(getTempLabel("text_end"));
Chris Lattner73266f92009-08-19 05:49:37 +00001847 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00001848 Asm->OutStreamer.EmitLabel(getTempLabel("data_end"));
Bill Wendlingf5839192009-05-20 23:19:06 +00001849
1850 // End text sections.
1851 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001852 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattner6d213e82010-03-08 22:44:40 +00001853 Asm->OutStreamer.EmitLabel(getDWLabel("section_end", i));
Bill Wendlingf5839192009-05-20 23:19:06 +00001854 }
1855
1856 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001857 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001858
1859 // Emit function debug frame information
1860 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1861 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001862 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001863
1864 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001865 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001866
1867 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001868 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001869
1870 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00001871 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00001872
1873 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00001874 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00001875
1876 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00001877 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00001878
Devang Patelec13b4f2009-11-24 01:14:22 +00001879 // Emit info into a debug pubtypes section.
1880 emitDebugPubTypes();
1881
Bill Wendlingf5839192009-05-20 23:19:06 +00001882 // Emit info into a debug str section.
Devang Patelc50078e2009-11-21 02:48:08 +00001883 emitDebugStr();
Bill Wendlingf5839192009-05-20 23:19:06 +00001884
1885 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00001886 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00001887
1888 // Emit info into a debug aranges section.
1889 EmitDebugARanges();
1890
1891 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00001892 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00001893
1894 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00001895 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001896
1897 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00001898 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001899
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001900 delete ModuleCU;
1901 ModuleCU = NULL; // Reset for the next Module, if any.
1902
Bill Wendlingf5839192009-05-20 23:19:06 +00001903 if (TimePassesIsEnabled)
1904 DebugTimer->stopTimer();
1905}
1906
Devang Patel90a0fe32009-11-10 23:06:00 +00001907/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00001908DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1909 unsigned FrameIdx,
Devang Patel90a0fe32009-11-10 23:06:00 +00001910 DILocation &ScopeLoc) {
1911
1912 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1913 if (AbsDbgVariable)
1914 return AbsDbgVariable;
1915
1916 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1917 if (!Scope)
1918 return NULL;
1919
1920 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patelc50078e2009-11-21 02:48:08 +00001921 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00001922 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1923 return AbsDbgVariable;
1924}
1925
Devang Patelc50078e2009-11-21 02:48:08 +00001926/// collectVariableInfo - Populate DbgScope entries with variables' info.
1927void DwarfDebug::collectVariableInfo() {
Devang Patel40c80212009-10-09 22:42:28 +00001928 if (!MMI) return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001929
Devang Patel84139992009-10-06 01:26:37 +00001930 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1931 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1932 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel0a0ea052010-01-22 22:52:10 +00001933 MDNode *Var = VI->first;
Devang Patel90a0fe32009-11-10 23:06:00 +00001934 if (!Var) continue;
Devang Patel6882dff2009-10-08 18:48:03 +00001935 DIVariable DV (Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00001936 std::pair< unsigned, MDNode *> VP = VI->second;
1937 DILocation ScopeLoc(VP.second);
1938
1939 DbgScope *Scope =
1940 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1941 if (!Scope)
Jim Grosbach652b7432009-11-21 23:12:12 +00001942 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelbad42262009-11-10 23:20:04 +00001943 // If variable scope is not found then skip this variable.
1944 if (!Scope)
1945 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00001946
1947 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patelc50078e2009-11-21 02:48:08 +00001948 Scope->addVariable(RegVar);
Jim Grosbachb23f2422009-11-22 19:20:36 +00001949 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1950 ScopeLoc))
Devang Patel90a0fe32009-11-10 23:06:00 +00001951 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patel84139992009-10-06 01:26:37 +00001952 }
1953}
1954
Devang Patelc50078e2009-11-21 02:48:08 +00001955/// beginScope - Process beginning of a scope starting at Label.
Chris Lattner597b0fc2010-03-09 04:54:43 +00001956void DwarfDebug::beginScope(const MachineInstr *MI, MCSymbol *Label) {
Devang Patel393a46d2009-10-06 01:50:42 +00001957 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1958 if (I == DbgScopeBeginMap.end())
1959 return;
Dan Gohman8d34f972009-11-23 21:30:55 +00001960 ScopeVector &SD = I->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00001961 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001962 SDI != SDE; ++SDI)
Chris Lattner597b0fc2010-03-09 04:54:43 +00001963 (*SDI)->setStartLabel(Label);
Devang Patel393a46d2009-10-06 01:50:42 +00001964}
1965
Devang Patelc50078e2009-11-21 02:48:08 +00001966/// endScope - Process end of a scope.
1967void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel393a46d2009-10-06 01:50:42 +00001968 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patelf4348892009-10-06 03:15:38 +00001969 if (I == DbgScopeEndMap.end())
Devang Patel393a46d2009-10-06 01:50:42 +00001970 return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001971
Chris Lattner855e8f52010-03-09 01:58:53 +00001972 MCSymbol *Label = getDWLabel("label", MMI->NextLabelID());
1973 Asm->OutStreamer.EmitLabel(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00001974
Chris Lattner855e8f52010-03-09 01:58:53 +00001975 SmallVector<DbgScope*, 2> &SD = I->second;
Devang Patel393a46d2009-10-06 01:50:42 +00001976 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001977 SDI != SDE; ++SDI)
Chris Lattner855e8f52010-03-09 01:58:53 +00001978 (*SDI)->setEndLabel(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00001979 return;
1980}
1981
1982/// createDbgScope - Create DbgScope for the scope.
1983void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1984
1985 if (!InlinedAt) {
1986 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1987 if (WScope)
1988 return;
1989 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1990 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach652b7432009-11-21 23:12:12 +00001991 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel53addbf2009-11-11 00:18:40 +00001992 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel90a0fe32009-11-10 23:06:00 +00001993 return;
1994 }
1995
1996 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1997 if (WScope)
1998 return;
1999
2000 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2001 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2002 DILocation DL(InlinedAt);
2003 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel393a46d2009-10-06 01:50:42 +00002004}
2005
Devang Patelc50078e2009-11-21 02:48:08 +00002006/// extractScopeInformation - Scan machine instructions in this function
Devang Patel6a260102009-10-01 20:31:14 +00002007/// and collect DbgScopes. Return true, if atleast one scope was found.
Chris Lattner69a76972010-01-26 23:18:02 +00002008bool DwarfDebug::extractScopeInformation() {
Devang Patel6a260102009-10-01 20:31:14 +00002009 // If scope information was extracted using .dbg intrinsics then there is not
2010 // any need to extract these information by scanning each instruction.
2011 if (!DbgScopeMap.empty())
2012 return false;
2013
Devang Patel98e77302010-01-04 20:44:00 +00002014 DenseMap<const MachineInstr *, unsigned> MIIndexMap;
2015 unsigned MIIndex = 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00002016 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6a260102009-10-01 20:31:14 +00002017 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2018 I != E; ++I) {
2019 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2020 II != IE; ++II) {
2021 const MachineInstr *MInsn = II;
Devang Patel98e77302010-01-04 20:44:00 +00002022 MIIndexMap[MInsn] = MIIndex++;
Devang Patel6a260102009-10-01 20:31:14 +00002023 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00002024 if (DL.isUnknown()) continue;
Devang Patel042945f2010-01-16 06:09:35 +00002025 DILocation DLT = MF->getDILocation(DL);
2026 DIScope DLTScope = DLT.getScope();
Devang Patel6a260102009-10-01 20:31:14 +00002027 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002028 // other scopes, create one DbgScope now. This will be translated
Devang Patel6a260102009-10-01 20:31:14 +00002029 // into a scope DIE at the end.
Devang Patel042945f2010-01-16 06:09:35 +00002030 if (DLTScope.isCompileUnit()) continue;
2031 createDbgScope(DLTScope.getNode(), DLT.getOrigLocation().getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00002032 }
2033 }
2034
2035
2036 // Build scope hierarchy using working set of scopes.
2037 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2038 I != E; ++I) {
2039 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2040 II != IE; ++II) {
2041 const MachineInstr *MInsn = II;
2042 DebugLoc DL = MInsn->getDebugLoc();
2043 if (DL.isUnknown()) continue;
Devang Patel042945f2010-01-16 06:09:35 +00002044 DILocation DLT = MF->getDILocation(DL);
2045 DIScope DLTScope = DLT.getScope();
Devang Patel90a0fe32009-11-10 23:06:00 +00002046 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002047 // other scopes, create one DbgScope now. This will be translated
Devang Patel90a0fe32009-11-10 23:06:00 +00002048 // into a scope DIE at the end.
Devang Patel042945f2010-01-16 06:09:35 +00002049 if (DLTScope.isCompileUnit()) continue;
2050 DbgScope *Scope = getUpdatedDbgScope(DLTScope.getNode(), MInsn,
2051 DLT.getOrigLocation().getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00002052 Scope->setLastInsn(MInsn);
Devang Patel6a260102009-10-01 20:31:14 +00002053 }
2054 }
2055
Devang Patel98e77302010-01-04 20:44:00 +00002056 if (!CurrentFnDbgScope)
2057 return false;
2058
2059 CurrentFnDbgScope->fixInstructionMarkers(MIIndexMap);
Devang Patel6a260102009-10-01 20:31:14 +00002060
2061 // Each scope has first instruction and last instruction to mark beginning
2062 // and end of a scope respectively. Create an inverse map that list scopes
2063 // starts (and ends) with an instruction. One instruction may start (or end)
Devang Patelfd311df2010-01-20 02:05:23 +00002064 // multiple scopes. Ignore scopes that are not reachable.
2065 SmallVector<DbgScope *, 4> WorkList;
2066 WorkList.push_back(CurrentFnDbgScope);
2067 while (!WorkList.empty()) {
2068 DbgScope *S = WorkList.back(); WorkList.pop_back();
2069
Douglas Gregordf49dc72010-03-08 02:58:37 +00002070 SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patelfd311df2010-01-20 02:05:23 +00002071 if (!Children.empty())
Douglas Gregordf49dc72010-03-08 02:58:37 +00002072 for (SmallVector<DbgScope *, 4>::iterator SI = Children.begin(),
Devang Patelfd311df2010-01-20 02:05:23 +00002073 SE = Children.end(); SI != SE; ++SI)
2074 WorkList.push_back(*SI);
2075
Devang Patel90a0fe32009-11-10 23:06:00 +00002076 if (S->isAbstractScope())
2077 continue;
Devang Patel6a260102009-10-01 20:31:14 +00002078 const MachineInstr *MI = S->getFirstInsn();
2079 assert (MI && "DbgScope does not have first instruction!");
2080
2081 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2082 if (IDI != DbgScopeBeginMap.end())
2083 IDI->second.push_back(S);
2084 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002085 DbgScopeBeginMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002086
2087 MI = S->getLastInsn();
2088 assert (MI && "DbgScope does not have last instruction!");
2089 IDI = DbgScopeEndMap.find(MI);
2090 if (IDI != DbgScopeEndMap.end())
2091 IDI->second.push_back(S);
2092 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002093 DbgScopeEndMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002094 }
2095
2096 return !DbgScopeMap.empty();
2097}
2098
Devang Patelc50078e2009-11-21 02:48:08 +00002099/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002100/// emitted immediately after the function entry point.
Chris Lattner69a76972010-01-26 23:18:02 +00002101void DwarfDebug::beginFunction(const MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002102 this->MF = MF;
2103
2104 if (!ShouldEmitDwarfDebug()) return;
2105
2106 if (TimePassesIsEnabled)
2107 DebugTimer->startTimer();
2108
Chris Lattner69a76972010-01-26 23:18:02 +00002109 if (!extractScopeInformation())
Devang Patel0feae422009-10-06 18:37:31 +00002110 return;
Devang Patelc50078e2009-11-21 02:48:08 +00002111
2112 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002113
Bill Wendlingf5839192009-05-20 23:19:06 +00002114 // Assumes in correct section after the entry point.
Chris Lattner6d213e82010-03-08 22:44:40 +00002115 Asm->OutStreamer.EmitLabel(getDWLabel("func_begin", ++SubprogramCount));
Bill Wendlingf5839192009-05-20 23:19:06 +00002116
2117 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2118 // function.
Devang Patel40c80212009-10-09 22:42:28 +00002119 DebugLoc FDL = MF->getDefaultDebugLoc();
2120 if (!FDL.isUnknown()) {
Devang Patel042945f2010-01-16 06:09:35 +00002121 DILocation DLT = MF->getDILocation(FDL);
Devang Patel042945f2010-01-16 06:09:35 +00002122 DISubprogram SP = getDISubprogram(DLT.getScope().getNode());
Chris Lattner855e8f52010-03-09 01:58:53 +00002123 unsigned Line, Col;
2124 if (SP.Verify()) {
2125 Line = SP.getLineNumber();
2126 Col = 0;
2127 } else {
2128 Line = DLT.getLineNumber();
2129 Col = DLT.getColumnNumber();
2130 }
2131
Chris Lattner597b0fc2010-03-09 04:54:43 +00002132 recordSourceLine(Line, Col, DLT.getScope().getNode());
Bill Wendlingf5839192009-05-20 23:19:06 +00002133 }
Bill Wendlingf5839192009-05-20 23:19:06 +00002134 if (TimePassesIsEnabled)
2135 DebugTimer->stopTimer();
2136}
2137
Devang Patelc50078e2009-11-21 02:48:08 +00002138/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002139///
Chris Lattner69a76972010-01-26 23:18:02 +00002140void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002141 if (!ShouldEmitDwarfDebug()) return;
2142
2143 if (TimePassesIsEnabled)
2144 DebugTimer->startTimer();
2145
Devang Patel40c80212009-10-09 22:42:28 +00002146 if (DbgScopeMap.empty())
2147 return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002148
Devang Patel98e77302010-01-04 20:44:00 +00002149 if (CurrentFnDbgScope) {
2150 // Define end label for subprogram.
Chris Lattner6d213e82010-03-08 22:44:40 +00002151 Asm->OutStreamer.EmitLabel(getDWLabel("func_end", SubprogramCount));
Devang Patel98e77302010-01-04 20:44:00 +00002152
2153 // Get function line info.
2154 if (!Lines.empty()) {
2155 // Get section line info.
2156 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2157 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2158 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2159 // Append the function info to section info.
2160 SectionLineInfos.insert(SectionLineInfos.end(),
2161 Lines.begin(), Lines.end());
2162 }
2163
2164 // Construct abstract scopes.
2165 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2166 AE = AbstractScopesList.end(); AI != AE; ++AI)
2167 constructScopeDIE(*AI);
2168
2169 constructScopeDIE(CurrentFnDbgScope);
2170
2171 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2172 MMI->getFrameMoves()));
Bill Wendlingf5839192009-05-20 23:19:06 +00002173 }
2174
Bill Wendlingf5839192009-05-20 23:19:06 +00002175 // Clear debug info
Devang Patel387e9c12010-01-19 01:26:02 +00002176 CurrentFnDbgScope = NULL;
Douglas Gregordf49dc72010-03-08 02:58:37 +00002177 DbgScopeMap.clear();
Devang Patel387e9c12010-01-19 01:26:02 +00002178 DbgScopeBeginMap.clear();
2179 DbgScopeEndMap.clear();
2180 ConcreteScopes.clear();
2181 AbstractScopesList.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002182 Lines.clear();
Devang Patel67533ab2009-12-01 18:13:48 +00002183
Bill Wendlingf5839192009-05-20 23:19:06 +00002184 if (TimePassesIsEnabled)
2185 DebugTimer->stopTimer();
2186}
2187
Chris Lattner597b0fc2010-03-09 04:54:43 +00002188/// recordSourceLine - Register a source line with debug info. Returns the
2189/// unique label that was emitted and which provides correspondence to
2190/// the source line list.
2191MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, MDNode *S) {
Devang Patel15e723d2009-08-28 23:24:31 +00002192 if (!MMI)
2193 return 0;
2194
Bill Wendlingf5839192009-05-20 23:19:06 +00002195 if (TimePassesIsEnabled)
2196 DebugTimer->startTimer();
2197
Devang Patel7f75bbe2009-11-25 17:36:49 +00002198 StringRef Dir;
2199 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002200
2201 DIDescriptor Scope(S);
2202 if (Scope.isCompileUnit()) {
2203 DICompileUnit CU(S);
2204 Dir = CU.getDirectory();
2205 Fn = CU.getFilename();
2206 } else if (Scope.isSubprogram()) {
2207 DISubprogram SP(S);
2208 Dir = SP.getDirectory();
2209 Fn = SP.getFilename();
2210 } else if (Scope.isLexicalBlock()) {
2211 DILexicalBlock DB(S);
2212 Dir = DB.getDirectory();
2213 Fn = DB.getFilename();
2214 } else
2215 assert (0 && "Unexpected scope info");
2216
2217 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf5839192009-05-20 23:19:06 +00002218 unsigned ID = MMI->NextLabelID();
2219 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2220
2221 if (TimePassesIsEnabled)
2222 DebugTimer->stopTimer();
2223
Chris Lattner597b0fc2010-03-09 04:54:43 +00002224 MCSymbol *Label = getDWLabel("label", ID);
2225 Asm->OutStreamer.EmitLabel(Label);
2226 return Label;
Bill Wendlingf5839192009-05-20 23:19:06 +00002227}
2228
2229/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2230/// timed. Look up the source id with the given directory and source file
2231/// names. If none currently exists, create a new id and insert it in the
2232/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2233/// well.
2234unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2235 const std::string &FileName) {
2236 if (TimePassesIsEnabled)
2237 DebugTimer->startTimer();
2238
Devang Patelaaf012e2009-09-29 18:40:58 +00002239 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf5839192009-05-20 23:19:06 +00002240
2241 if (TimePassesIsEnabled)
2242 DebugTimer->stopTimer();
2243
2244 return SrcId;
2245}
2246
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002247//===----------------------------------------------------------------------===//
2248// Emit Methods
2249//===----------------------------------------------------------------------===//
2250
Devang Patelc50078e2009-11-21 02:48:08 +00002251/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002252///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002253unsigned
2254DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002255 // Get the children.
2256 const std::vector<DIE *> &Children = Die->getChildren();
2257
2258 // If not last sibling and has children then add sibling offset attribute.
Devang Patelc50078e2009-11-21 02:48:08 +00002259 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002260
2261 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002262 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002263
2264 // Get the abbreviation for this DIE.
2265 unsigned AbbrevNumber = Die->getAbbrevNumber();
2266 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2267
2268 // Set DIE offset
2269 Die->setOffset(Offset);
2270
2271 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002272 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002273
2274 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2275 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2276
2277 // Size the DIE attribute values.
2278 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2279 // Size attribute value.
2280 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2281
2282 // Size the DIE children if any.
2283 if (!Children.empty()) {
2284 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2285 "Children flag not set");
2286
2287 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002288 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002289
2290 // End of children marker.
2291 Offset += sizeof(int8_t);
2292 }
2293
2294 Die->setSize(Offset - Die->getOffset());
2295 return Offset;
2296}
2297
Devang Patelc50078e2009-11-21 02:48:08 +00002298/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002299///
Devang Patelc50078e2009-11-21 02:48:08 +00002300void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002301 // Compute size of compile unit header.
2302 static unsigned Offset =
2303 sizeof(int32_t) + // Length of Compilation Unit Info
2304 sizeof(int16_t) + // DWARF version number
2305 sizeof(int32_t) + // Offset Into Abbrev. Section
2306 sizeof(int8_t); // Pointer Size (in bytes)
2307
Devang Patelc50078e2009-11-21 02:48:08 +00002308 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel5a3d37f2009-06-29 20:45:18 +00002309 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling55fccda2009-05-20 23:21:38 +00002310}
2311
Devang Patelc50078e2009-11-21 02:48:08 +00002312/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling55fccda2009-05-20 23:21:38 +00002313/// tools to recognize the object file contains Dwarf information.
Devang Patelc50078e2009-11-21 02:48:08 +00002314void DwarfDebug::emitInitial() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002315 // Check to see if we already emitted intial headers.
2316 if (didInitial) return;
2317 didInitial = true;
2318
Chris Lattner73266f92009-08-19 05:49:37 +00002319 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002320
Bill Wendling55fccda2009-05-20 23:21:38 +00002321 // Dwarf sections base addresses.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002322 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner73266f92009-08-19 05:49:37 +00002323 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002324 Asm->OutStreamer.EmitLabel(getTempLabel("section_debug_frame"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002325 }
2326
Chris Lattner73266f92009-08-19 05:49:37 +00002327 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002328 Asm->OutStreamer.EmitLabel(getTempLabel("section_info"));
Chris Lattner73266f92009-08-19 05:49:37 +00002329 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002330 Asm->OutStreamer.EmitLabel(getTempLabel("section_abbrev"));
Chris Lattner73266f92009-08-19 05:49:37 +00002331 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002332 Asm->OutStreamer.EmitLabel(getTempLabel("section_aranges"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002333
Chris Lattner73266f92009-08-19 05:49:37 +00002334 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2335 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Chris Lattner6d213e82010-03-08 22:44:40 +00002336 Asm->OutStreamer.EmitLabel(getTempLabel("section_macinfo"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002337 }
2338
Chris Lattner73266f92009-08-19 05:49:37 +00002339 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002340 Asm->OutStreamer.EmitLabel(getTempLabel("section_line"));
Chris Lattner73266f92009-08-19 05:49:37 +00002341 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002342 Asm->OutStreamer.EmitLabel(getTempLabel("section_loc"));
Chris Lattner73266f92009-08-19 05:49:37 +00002343 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002344 Asm->OutStreamer.EmitLabel(getTempLabel("section_pubnames"));
Devang Patelec13b4f2009-11-24 01:14:22 +00002345 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002346 Asm->OutStreamer.EmitLabel(getTempLabel("section_pubtypes"));
Chris Lattner73266f92009-08-19 05:49:37 +00002347 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002348 Asm->OutStreamer.EmitLabel(getTempLabel("section_str"));
Chris Lattner73266f92009-08-19 05:49:37 +00002349 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002350 Asm->OutStreamer.EmitLabel(getTempLabel("section_ranges"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002351
Chris Lattner73266f92009-08-19 05:49:37 +00002352 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002353 Asm->OutStreamer.EmitLabel(getTempLabel("text_begin"));
Chris Lattner73266f92009-08-19 05:49:37 +00002354 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002355 Asm->OutStreamer.EmitLabel(getTempLabel("data_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002356}
2357
Devang Patelc50078e2009-11-21 02:48:08 +00002358/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002359///
Devang Patelc50078e2009-11-21 02:48:08 +00002360void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002361 // Get the abbreviation for this DIE.
2362 unsigned AbbrevNumber = Die->getAbbrevNumber();
2363 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2364
Bill Wendling55fccda2009-05-20 23:21:38 +00002365 // Emit the code (index) for the abbreviation.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002366 if (Asm->VerboseAsm)
2367 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2368 Twine::utohexstr(Die->getOffset()) + ":0x" +
2369 Twine::utohexstr(Die->getSize()) + " " +
2370 dwarf::TagString(Abbrev->getTag()));
2371 EmitULEB128(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002372
2373 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2374 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2375
2376 // Emit the DIE attribute values.
2377 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2378 unsigned Attr = AbbrevData[i].getAttribute();
2379 unsigned Form = AbbrevData[i].getForm();
2380 assert(Form && "Too many attributes for DIE (check abbreviation)");
2381
Chris Lattner91e06b42010-01-24 18:54:17 +00002382 if (Asm->VerboseAsm)
2383 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2384
Bill Wendling55fccda2009-05-20 23:21:38 +00002385 switch (Attr) {
2386 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002387 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002388 break;
2389 case dwarf::DW_AT_abstract_origin: {
2390 DIEEntry *E = cast<DIEEntry>(Values[i]);
2391 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002392 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002393 Asm->EmitInt32(Addr);
2394 break;
2395 }
2396 default:
2397 // Emit an attribute using the defined form.
2398 Values[i]->EmitValue(this, Form);
2399 break;
2400 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002401 }
2402
2403 // Emit the DIE children if any.
2404 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2405 const std::vector<DIE *> &Children = Die->getChildren();
2406
2407 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002408 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002409
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002410 if (Asm->VerboseAsm)
2411 Asm->OutStreamer.AddComment("End Of Children Mark");
2412 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002413 }
2414}
2415
Devang Patelfe0be132009-12-09 18:24:21 +00002416/// emitDebugInfo - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002417///
Devang Patelfe0be132009-12-09 18:24:21 +00002418void DwarfDebug::emitDebugInfo() {
2419 // Start debug info section.
2420 Asm->OutStreamer.SwitchSection(
2421 Asm->getObjFileLowering().getDwarfInfoSection());
2422 DIE *Die = ModuleCU->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002423
2424 // Emit the compile units header.
Chris Lattner6d213e82010-03-08 22:44:40 +00002425 Asm->OutStreamer.EmitLabel(getDWLabel("info_begin", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002426
2427 // Emit size of content not including length itself
2428 unsigned ContentSize = Die->getSize() +
2429 sizeof(int16_t) + // DWARF version number
2430 sizeof(int32_t) + // Offset Into Abbrev. Section
2431 sizeof(int8_t) + // Pointer Size (in bytes)
2432 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2433
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002434 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2435 Asm->EmitInt32(ContentSize);
2436 Asm->OutStreamer.AddComment("DWARF version number");
2437 Asm->EmitInt16(dwarf::DWARF_VERSION);
2438 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002439 EmitSectionOffset(getTempLabel("abbrev_begin"),getTempLabel("section_abbrev"),
2440 true, false);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002441 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2442 Asm->EmitInt8(TD->getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00002443
Devang Patelc50078e2009-11-21 02:48:08 +00002444 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002445 // FIXME - extra padding for gdb bug.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002446 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2447 Asm->EmitInt8(0);
2448 Asm->EmitInt8(0);
2449 Asm->EmitInt8(0);
2450 Asm->EmitInt8(0);
Chris Lattner6d213e82010-03-08 22:44:40 +00002451 Asm->OutStreamer.EmitLabel(getDWLabel("info_end", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002452}
2453
Devang Patelc50078e2009-11-21 02:48:08 +00002454/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002455///
Devang Patelc50078e2009-11-21 02:48:08 +00002456void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002457 // Check to see if it is worth the effort.
2458 if (!Abbreviations.empty()) {
2459 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002460 Asm->OutStreamer.SwitchSection(
2461 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002462
Chris Lattner6d213e82010-03-08 22:44:40 +00002463 Asm->OutStreamer.EmitLabel(getTempLabel("abbrev_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002464
2465 // For each abbrevation.
2466 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2467 // Get abbreviation data
2468 const DIEAbbrev *Abbrev = Abbreviations[i];
2469
2470 // Emit the abbrevations code (base 1 index.)
Chris Lattnerbcc79432010-01-22 23:18:42 +00002471 EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling55fccda2009-05-20 23:21:38 +00002472
2473 // Emit the abbreviations data.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002474 Abbrev->Emit(this);
Bill Wendling55fccda2009-05-20 23:21:38 +00002475 }
2476
2477 // Mark end of abbreviations.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002478 EmitULEB128(0, "EOM(3)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002479
Chris Lattner6d213e82010-03-08 22:44:40 +00002480 Asm->OutStreamer.EmitLabel(getTempLabel("abbrev_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002481 }
2482}
2483
Devang Patelc50078e2009-11-21 02:48:08 +00002484/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002485/// the line matrix.
2486///
Devang Patelc50078e2009-11-21 02:48:08 +00002487void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002488 // Define last address of section.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002489 Asm->OutStreamer.AddComment("Extended Op");
2490 Asm->EmitInt8(0);
2491
2492 Asm->OutStreamer.AddComment("Op size");
2493 Asm->EmitInt8(TD->getPointerSize() + 1);
2494 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2495 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2496
2497 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnereb159d62010-03-10 01:17:49 +00002498
2499 Asm->OutStreamer.EmitSymbolValue(getDWLabel("section_end", SectionEnd),
2500 TD->getPointerSize(), 0/*AddrSpace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00002501
2502 // Mark end of matrix.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002503 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2504 Asm->EmitInt8(0);
Chris Lattnerad653482010-01-22 22:09:00 +00002505 Asm->EmitInt8(1);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002506 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002507}
2508
Devang Patelc50078e2009-11-21 02:48:08 +00002509/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002510///
Devang Patelc50078e2009-11-21 02:48:08 +00002511void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002512 // If the target is using .loc/.file, the assembler will be emitting the
2513 // .debug_line table automatically.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002514 if (MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002515 return;
2516
2517 // Minimum line delta, thus ranging from -10..(255-10).
2518 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2519 // Maximum line delta, thus ranging from -10..(255-10).
2520 const int MaxLineDelta = 255 + MinLineDelta;
2521
2522 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002523 Asm->OutStreamer.SwitchSection(
2524 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002525
2526 // Construct the section header.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002527 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattner6d213e82010-03-08 22:44:40 +00002528 EmitDifference(getTempLabel("line_end"), getTempLabel("line_begin"), true);
Chris Lattner6d213e82010-03-08 22:44:40 +00002529 Asm->OutStreamer.EmitLabel(getTempLabel("line_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002530
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002531 Asm->OutStreamer.AddComment("DWARF version number");
2532 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00002533
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002534 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattner6d213e82010-03-08 22:44:40 +00002535 EmitDifference(getTempLabel("line_prolog_end"),
2536 getTempLabel("line_prolog_begin"), true);
Chris Lattner6d213e82010-03-08 22:44:40 +00002537 Asm->OutStreamer.EmitLabel(getTempLabel("line_prolog_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002538
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002539 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2540 Asm->EmitInt8(1);
2541 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2542 Asm->EmitInt8(1);
2543 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2544 Asm->EmitInt8(MinLineDelta);
2545 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
2546 Asm->EmitInt8(MaxLineDelta);
2547 Asm->OutStreamer.AddComment("Special Opcode Base");
2548 Asm->EmitInt8(-MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00002549
2550 // Line number standard opcode encodings argument count
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002551 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
2552 Asm->EmitInt8(0);
2553 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
2554 Asm->EmitInt8(1);
2555 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
2556 Asm->EmitInt8(1);
2557 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
2558 Asm->EmitInt8(1);
2559 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
2560 Asm->EmitInt8(1);
2561 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
2562 Asm->EmitInt8(0);
2563 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
2564 Asm->EmitInt8(0);
2565 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
2566 Asm->EmitInt8(0);
2567 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
2568 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002569
2570 // Emit directories.
2571 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnercc564642010-01-23 03:11:46 +00002572 const std::string &Dir = getSourceDirectoryName(DI);
2573 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Directory");
2574 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002575 }
2576
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002577 Asm->OutStreamer.AddComment("End of directories");
2578 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002579
2580 // Emit files.
2581 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2582 // Remember source id starts at 1.
2583 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnercc564642010-01-23 03:11:46 +00002584 const std::string &FN = getSourceFileName(Id.second);
2585 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Source");
2586 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2587
Chris Lattnerbcc79432010-01-22 23:18:42 +00002588 EmitULEB128(Id.first, "Directory #");
2589 EmitULEB128(0, "Mod date");
2590 EmitULEB128(0, "File size");
Bill Wendling55fccda2009-05-20 23:21:38 +00002591 }
2592
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002593 Asm->OutStreamer.AddComment("End of files");
2594 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002595
Chris Lattner6d213e82010-03-08 22:44:40 +00002596 Asm->OutStreamer.EmitLabel(getTempLabel("line_prolog_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002597
2598 // A sequence for each text section.
2599 unsigned SecSrcLinesSize = SectionSourceLines.size();
2600
2601 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2602 // Isolate current sections line info.
2603 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2604
Bill Wendling55fccda2009-05-20 23:21:38 +00002605 // Dwarf assumes we start with first line of first source file.
2606 unsigned Source = 1;
2607 unsigned Line = 1;
2608
2609 // Construct rows of the address, source, line, column matrix.
2610 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2611 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner5e423432010-03-09 01:51:43 +00002612 unsigned LabelID = LineInfo.getLabelID();
2613 if (MMI->isLabelDeleted(LabelID)) continue;
Bill Wendling55fccda2009-05-20 23:21:38 +00002614
Caroline Tice9da96d82009-09-11 18:25:54 +00002615 if (LineInfo.getLine() == 0) continue;
2616
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00002617 if (Asm->isVerbose()) {
Chris Lattneree3b40f2010-03-10 01:04:13 +00002618 std::pair<unsigned, unsigned> SrcID =
Bill Wendling55fccda2009-05-20 23:21:38 +00002619 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattneree3b40f2010-03-10 01:04:13 +00002620 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner48fb0b12010-03-10 02:29:31 +00002621 "/" +
2622 Twine(getSourceFileName(SrcID.second)) +
Chris Lattneree3b40f2010-03-10 01:04:13 +00002623 ":" + Twine(LineInfo.getLine()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002624 }
2625
2626 // Define the line address.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002627 Asm->OutStreamer.AddComment("Extended Op");
2628 Asm->EmitInt8(0);
2629 Asm->OutStreamer.AddComment("Op size");
2630 Asm->EmitInt8(TD->getPointerSize() + 1);
2631
2632 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2633 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2634
2635 Asm->OutStreamer.AddComment("Location label");
Chris Lattnereb159d62010-03-10 01:17:49 +00002636 Asm->OutStreamer.EmitSymbolValue(getDWLabel("label", LabelID),
2637 TD->getPointerSize(), 0/*AddrSpace*/);
2638
Bill Wendling55fccda2009-05-20 23:21:38 +00002639 // If change of source, then switch to the new source.
2640 if (Source != LineInfo.getSourceID()) {
2641 Source = LineInfo.getSourceID();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002642 Asm->OutStreamer.AddComment("DW_LNS_set_file");
2643 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002644 EmitULEB128(Source, "New Source");
Bill Wendling55fccda2009-05-20 23:21:38 +00002645 }
2646
2647 // If change of line.
2648 if (Line != LineInfo.getLine()) {
2649 // Determine offset.
2650 int Offset = LineInfo.getLine() - Line;
2651 int Delta = Offset - MinLineDelta;
2652
2653 // Update line.
2654 Line = LineInfo.getLine();
2655
2656 // If delta is small enough and in range...
2657 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2658 // ... then use fast opcode.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002659 Asm->OutStreamer.AddComment("Line Delta");
2660 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00002661 } else {
2662 // ... otherwise use long hand.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002663 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling55fccda2009-05-20 23:21:38 +00002664 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner20f334f2010-01-22 22:56:55 +00002665 EmitSLEB128(Offset, "Line Offset");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002666 Asm->OutStreamer.AddComment("DW_LNS_copy");
2667 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00002668 }
2669 } else {
2670 // Copy the previous row (different address or source)
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002671 Asm->OutStreamer.AddComment("DW_LNS_copy");
2672 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00002673 }
2674 }
2675
Devang Patelc50078e2009-11-21 02:48:08 +00002676 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002677 }
2678
2679 if (SecSrcLinesSize == 0)
2680 // Because we're emitting a debug_line section, we still need a line
2681 // table. The linker and friends expect it to exist. If there's nothing to
2682 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00002683 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002684
Chris Lattner6d213e82010-03-08 22:44:40 +00002685 Asm->OutStreamer.EmitLabel(getTempLabel("line_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002686}
2687
Devang Patelc50078e2009-11-21 02:48:08 +00002688/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002689///
Devang Patelc50078e2009-11-21 02:48:08 +00002690void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002691 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002692 return;
2693
2694 int stackGrowth =
2695 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2696 TargetFrameInfo::StackGrowsUp ?
2697 TD->getPointerSize() : -TD->getPointerSize();
2698
2699 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002700 Asm->OutStreamer.SwitchSection(
2701 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002702
Chris Lattner6d213e82010-03-08 22:44:40 +00002703 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002704 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002705 EmitDifference(getTempLabel("debug_frame_common_end"),
2706 getTempLabel("debug_frame_common_begin"), true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002707
Chris Lattner6d213e82010-03-08 22:44:40 +00002708 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common_begin"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002709 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling55fccda2009-05-20 23:21:38 +00002710 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002711 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling55fccda2009-05-20 23:21:38 +00002712 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002713 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnercc564642010-01-23 03:11:46 +00002714 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002715 EmitULEB128(1, "CIE Code Alignment Factor");
Chris Lattner20f334f2010-01-22 22:56:55 +00002716 EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002717 Asm->OutStreamer.AddComment("CIE RA Column");
Bill Wendling55fccda2009-05-20 23:21:38 +00002718 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling55fccda2009-05-20 23:21:38 +00002719
2720 std::vector<MachineMove> Moves;
2721 RI->getInitialFrameState(Moves);
2722
2723 EmitFrameMoves(NULL, 0, Moves, false);
2724
2725 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattner6d213e82010-03-08 22:44:40 +00002726 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002727}
2728
Devang Patelc50078e2009-11-21 02:48:08 +00002729/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00002730/// section.
2731void
Devang Patelc50078e2009-11-21 02:48:08 +00002732DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002733 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002734 return;
2735
2736 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002737 Asm->OutStreamer.SwitchSection(
2738 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002739
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002740 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002741 EmitDifference(getDWLabel("debug_frame_end", DebugFrameInfo.Number),
2742 getDWLabel("debug_frame_begin", DebugFrameInfo.Number), true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002743
Chris Lattner6d213e82010-03-08 22:44:40 +00002744 Asm->OutStreamer.EmitLabel(getDWLabel("debug_frame_begin",
2745 DebugFrameInfo.Number));
Bill Wendling55fccda2009-05-20 23:21:38 +00002746
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002747 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002748 EmitSectionOffset(getTempLabel("debug_frame_common"),
2749 getTempLabel("section_debug_frame"), true, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00002750
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002751 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnereb159d62010-03-10 01:17:49 +00002752 Asm->OutStreamer.EmitSymbolValue(getDWLabel("func_begin",
2753 DebugFrameInfo.Number),
2754 TD->getPointerSize(), 0/*AddrSpace*/);
2755
2756
2757
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002758 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattner7bc33552010-03-08 22:47:57 +00002759 EmitDifference(getDWLabel("func_end", DebugFrameInfo.Number),
2760 getDWLabel("func_begin", DebugFrameInfo.Number));
Bill Wendling55fccda2009-05-20 23:21:38 +00002761
2762 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2763 false);
2764
2765 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattner6d213e82010-03-08 22:44:40 +00002766 Asm->OutStreamer.EmitLabel(getDWLabel("debug_frame_end",
2767 DebugFrameInfo.Number));
Bill Wendling55fccda2009-05-20 23:21:38 +00002768}
2769
Devang Patelfe0be132009-12-09 18:24:21 +00002770/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2771///
2772void DwarfDebug::emitDebugPubNames() {
2773 // Start the dwarf pubnames section.
2774 Asm->OutStreamer.SwitchSection(
2775 Asm->getObjFileLowering().getDwarfPubNamesSection());
2776
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002777 Asm->OutStreamer.AddComment("Length of Public Names Info");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002778 EmitDifference(getDWLabel("pubnames_end", ModuleCU->getID()),
2779 getDWLabel("pubnames_begin", ModuleCU->getID()), true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002780
Chris Lattner6d213e82010-03-08 22:44:40 +00002781 Asm->OutStreamer.EmitLabel(getDWLabel("pubnames_begin", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002782
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002783 Asm->OutStreamer.AddComment("DWARF Version");
2784 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00002785
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002786 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002787 EmitSectionOffset(getDWLabel("info_begin", ModuleCU->getID()),
2788 getTempLabel("section_info"),
2789 true, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00002790
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002791 Asm->OutStreamer.AddComment("Compilation Unit Length");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002792 EmitDifference(getDWLabel("info_end", ModuleCU->getID()),
2793 getDWLabel("info_begin", ModuleCU->getID()),
Bill Wendling55fccda2009-05-20 23:21:38 +00002794 true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002795
Devang Patelfe0be132009-12-09 18:24:21 +00002796 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling55fccda2009-05-20 23:21:38 +00002797 for (StringMap<DIE*>::const_iterator
2798 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2799 const char *Name = GI->getKeyData();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002800 DIE *Entity = GI->second;
Bill Wendling55fccda2009-05-20 23:21:38 +00002801
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002802 Asm->OutStreamer.AddComment("DIE offset");
2803 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00002804
2805 if (Asm->VerboseAsm)
2806 Asm->OutStreamer.AddComment("External Name");
2807 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002808 }
2809
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002810 Asm->OutStreamer.AddComment("End Mark");
2811 Asm->EmitInt32(0);
Chris Lattner6d213e82010-03-08 22:44:40 +00002812 Asm->OutStreamer.EmitLabel(getDWLabel("pubnames_end", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002813}
2814
Devang Patelec13b4f2009-11-24 01:14:22 +00002815void DwarfDebug::emitDebugPubTypes() {
Devang Patel6f2bdd52009-11-24 19:18:41 +00002816 // Start the dwarf pubnames section.
2817 Asm->OutStreamer.SwitchSection(
2818 Asm->getObjFileLowering().getDwarfPubTypesSection());
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002819 Asm->OutStreamer.AddComment("Length of Public Types Info");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002820 EmitDifference(getDWLabel("pubtypes_end", ModuleCU->getID()),
2821 getDWLabel("pubtypes_begin", ModuleCU->getID()), true);
Devang Patelec13b4f2009-11-24 01:14:22 +00002822
Chris Lattner6d213e82010-03-08 22:44:40 +00002823 Asm->OutStreamer.EmitLabel(getDWLabel("pubtypes_begin", ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00002824
Devang Patel40b6cda2010-02-02 03:47:27 +00002825 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DWARF Version");
2826 Asm->EmitInt16(dwarf::DWARF_VERSION);
Devang Patelec13b4f2009-11-24 01:14:22 +00002827
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002828 Asm->OutStreamer.AddComment("Offset of Compilation ModuleCU Info");
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002829 EmitSectionOffset(getDWLabel("info_begin", ModuleCU->getID()),
2830 getTempLabel("section_info"), true, false);
Devang Patelec13b4f2009-11-24 01:14:22 +00002831
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002832 Asm->OutStreamer.AddComment("Compilation ModuleCU Length");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002833 EmitDifference(getDWLabel("info_end", ModuleCU->getID()),
2834 getDWLabel("info_begin", ModuleCU->getID()),
Devang Patelec13b4f2009-11-24 01:14:22 +00002835 true);
Devang Patelec13b4f2009-11-24 01:14:22 +00002836
2837 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2838 for (StringMap<DIE*>::const_iterator
2839 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2840 const char *Name = GI->getKeyData();
2841 DIE * Entity = GI->second;
2842
Devang Patel40b6cda2010-02-02 03:47:27 +00002843 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DIE offset");
2844 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00002845
2846 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("External Name");
Devang Patel0ceb4892010-02-02 03:37:03 +00002847 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00002848 }
2849
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002850 Asm->OutStreamer.AddComment("End Mark");
2851 Asm->EmitInt32(0);
Chris Lattner6d213e82010-03-08 22:44:40 +00002852 Asm->OutStreamer.EmitLabel(getDWLabel("pubtypes_end", ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00002853}
2854
Devang Patelc50078e2009-11-21 02:48:08 +00002855/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002856///
Devang Patelc50078e2009-11-21 02:48:08 +00002857void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002858 // Check to see if it is worth the effort.
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00002859 if (StringPool.empty()) return;
2860
2861 // Start the dwarf str section.
2862 Asm->OutStreamer.SwitchSection(
Chris Lattner73266f92009-08-19 05:49:37 +00002863 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002864
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00002865 // For each of strings in the string pool.
2866 for (unsigned StringID = 1, N = StringPool.size(); StringID <= N; ++StringID){
2867 // Emit a label for reference from debug information entries.
2868 Asm->OutStreamer.EmitLabel(getDWLabel("string", StringID));
2869
2870 // Emit the string itself.
2871 const std::string &String = StringPool[StringID];
2872 Asm->OutStreamer.EmitBytes(StringRef(String.c_str(), String.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002873 }
2874}
2875
Devang Patelc50078e2009-11-21 02:48:08 +00002876/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002877///
Devang Patelc50078e2009-11-21 02:48:08 +00002878void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002879 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00002880 Asm->OutStreamer.SwitchSection(
2881 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002882}
2883
2884/// EmitDebugARanges - Emit visible names into a debug aranges section.
2885///
2886void DwarfDebug::EmitDebugARanges() {
2887 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002888 Asm->OutStreamer.SwitchSection(
2889 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002890}
2891
Devang Patelc50078e2009-11-21 02:48:08 +00002892/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002893///
Devang Patelc50078e2009-11-21 02:48:08 +00002894void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002895 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002896 Asm->OutStreamer.SwitchSection(
2897 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002898}
2899
Devang Patelc50078e2009-11-21 02:48:08 +00002900/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002901///
Devang Patelc50078e2009-11-21 02:48:08 +00002902void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00002903 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00002904 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002905 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00002906 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002907 }
2908}
2909
Devang Patelc50078e2009-11-21 02:48:08 +00002910/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00002911/// Section Header:
2912/// 1. length of section
2913/// 2. Dwarf version number
2914/// 3. address size.
2915///
2916/// Entries (one "entry" for each function that was inlined):
2917///
2918/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2919/// otherwise offset into __debug_str for regular function name.
2920/// 2. offset into __debug_str section for regular function name.
2921/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2922/// instances for the function.
2923///
2924/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2925/// inlined instance; the die_offset points to the inlined_subroutine die in the
2926/// __debug_info section, and the low_pc is the starting address for the
2927/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00002928void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002929 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002930 return;
2931
Devang Patel5a3d37f2009-06-29 20:45:18 +00002932 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00002933 return;
2934
Chris Lattner73266f92009-08-19 05:49:37 +00002935 Asm->OutStreamer.SwitchSection(
2936 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerad653482010-01-22 22:09:00 +00002937
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002938 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002939 EmitDifference(getDWLabel("debug_inlined_end", 1),
2940 getDWLabel("debug_inlined_begin", 1), true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002941
Chris Lattner6d213e82010-03-08 22:44:40 +00002942 Asm->OutStreamer.EmitLabel(getDWLabel("debug_inlined_begin", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00002943
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002944 Asm->OutStreamer.AddComment("Dwarf Version");
2945 Asm->EmitInt16(dwarf::DWARF_VERSION);
2946 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2947 Asm->EmitInt8(TD->getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00002948
Devang Patel90a0fe32009-11-10 23:06:00 +00002949 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2950 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00002951
Devang Patel90a0fe32009-11-10 23:06:00 +00002952 MDNode *Node = *I;
Devang Patel7d707f92010-01-19 06:19:05 +00002953 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbachb23f2422009-11-22 19:20:36 +00002954 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00002955 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00002956 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00002957 StringRef LName = SP.getLinkageName();
2958 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00002959
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002960 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnercc564642010-01-23 03:11:46 +00002961 if (LName.empty()) {
2962 Asm->OutStreamer.EmitBytes(Name, 0);
2963 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
2964 } else
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002965 EmitSectionOffset(getDWLabel("string",
2966 StringPool.idFor(getRealLinkageName(LName))),
2967 getTempLabel("section_str"), true);
Devang Patel90a0fe32009-11-10 23:06:00 +00002968
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002969 Asm->OutStreamer.AddComment("Function name");
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002970 EmitSectionOffset(getDWLabel("string", StringPool.idFor(Name)),
2971 getTempLabel("section_str"), false, true);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002972 EmitULEB128(Labels.size(), "Inline count");
Bill Wendling55fccda2009-05-20 23:21:38 +00002973
Devang Patel90a0fe32009-11-10 23:06:00 +00002974 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00002975 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattneread58652010-03-09 00:31:02 +00002976 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DIE offset");
2977 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002978
Chris Lattnere90630a2010-03-09 00:26:09 +00002979 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner2ab0c442010-03-09 00:39:24 +00002980 Asm->OutStreamer.EmitSymbolValue(LI->first, TD->getPointerSize(), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002981 }
2982 }
2983
Chris Lattner6d213e82010-03-08 22:44:40 +00002984 Asm->OutStreamer.EmitLabel(getDWLabel("debug_inlined_end", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00002985}