blob: 1b17a9964fd1bb8f1339e798c197e13b96085cca [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 Lattnere6ad12f2009-07-31 18:48:30 +000019#include "llvm/MC/MCSection.h"
Chris Lattner73266f92009-08-19 05:49:37 +000020#include "llvm/MC/MCStreamer.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattner31a54742010-01-16 21:57:06 +000022#include "llvm/Target/Mangler.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000023#include "llvm/Target/TargetData.h"
24#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000025#include "llvm/Target/TargetLoweringObjectFile.h"
26#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattnerf5377682009-08-24 03:52:50 +000027#include "llvm/ADT/StringExtras.h"
Daniel Dunbarc74255d2009-10-13 06:47:08 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
Devang Patelae89d3b2010-01-26 21:39:14 +000030#include "llvm/Support/ValueHandle.h"
Chris Lattnerad653482010-01-22 22:09:00 +000031#include "llvm/Support/FormattedStream.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000032#include "llvm/Support/Timer.h"
33#include "llvm/System/Path.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000034using namespace llvm;
35
Bill Wendlingb12b3d72009-05-15 09:23:25 +000036//===----------------------------------------------------------------------===//
37
38/// Configuration values for initial hash set sizes (log2).
39///
Bill Wendlingb12b3d72009-05-15 09:23:25 +000040static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendlingb12b3d72009-05-15 09:23:25 +000041
42namespace llvm {
43
44//===----------------------------------------------------------------------===//
45/// CompileUnit - This dwarf writer support class manages information associate
46/// with a source file.
Nick Lewyckyee68f452009-11-17 08:11:44 +000047class CompileUnit {
Bill Wendlingb12b3d72009-05-15 09:23:25 +000048 /// ID - File identifier for source.
49 ///
50 unsigned ID;
51
52 /// Die - Compile unit debug information entry.
53 ///
Douglas Gregordf49dc72010-03-08 02:58:37 +000054 DIE *CUDie;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000055
Douglas Gregordf49dc72010-03-08 02:58:37 +000056 /// IndexTyDie - An anonymous type for index type.
Devang Patel1233f912009-11-21 00:31:03 +000057 DIE *IndexTyDie;
58
Bill Wendlingb12b3d72009-05-15 09:23:25 +000059 /// GVToDieMap - Tracks the mapping of unit level debug informaton
60 /// variables to debug information entries.
Devang Patel15e723d2009-08-28 23:24:31 +000061 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel7d707f92010-01-19 06:19:05 +000062 DenseMap<MDNode *, DIE *> GVToDieMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000063
64 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
65 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel15e723d2009-08-28 23:24:31 +000066 /// FIXME : Rename
Devang Patel7d707f92010-01-19 06:19:05 +000067 DenseMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000068
69 /// Globals - A map of globally visible named entities for this unit.
70 ///
71 StringMap<DIE*> Globals;
72
Devang Patelec13b4f2009-11-24 01:14:22 +000073 /// GlobalTypes - A map of globally visible types for this unit.
74 ///
75 StringMap<DIE*> GlobalTypes;
76
Bill Wendlingb12b3d72009-05-15 09:23:25 +000077public:
78 CompileUnit(unsigned I, DIE *D)
Devang Patelc50078e2009-11-21 02:48:08 +000079 : ID(I), CUDie(D), IndexTyDie(0) {}
Douglas Gregordf49dc72010-03-08 02:58:37 +000080 ~CompileUnit() { delete CUDie; delete IndexTyDie; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000081
82 // Accessors.
Devang Patelec13b4f2009-11-24 01:14:22 +000083 unsigned getID() const { return ID; }
Douglas Gregordf49dc72010-03-08 02:58:37 +000084 DIE* getCUDie() const { return CUDie; }
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
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000174 unsigned StartLabelID; // Label ID of the beginning of scope.
175 unsigned EndLabelID; // 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),
Jim Grosbach652b7432009-11-21 23:12:12 +0000186 StartLabelID(0), EndLabelID(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(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000198 unsigned getStartLabelID() const { return StartLabelID; }
199 unsigned getEndLabelID() const { return EndLabelID; }
Douglas Gregordf49dc72010-03-08 02:58:37 +0000200 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
201 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000202 void setStartLabelID(unsigned S) { StartLabelID = S; }
203 void setEndLabelID(unsigned E) { EndLabelID = 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 Lattnerebb8c082009-08-23 00:51:00 +0000267 err << " [" << StartLabelID << ", " << EndLabelID << "]\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()
1327 && !SP.getContext().isSubprogram()) {
Devang Patel814a12c2009-12-14 16:18:45 +00001328 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1329 // Add arguments.
1330 DICompositeType SPTy = SP.getType();
1331 DIArray Args = SPTy.getTypeArray();
1332 unsigned SPTag = SPTy.getTag();
1333 if (SPTag == dwarf::DW_TAG_subroutine_type)
1334 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1335 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc5dc7252010-02-06 01:02:37 +00001336 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1337 addType(Arg, ATy);
1338 if (ATy.isArtificial())
1339 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel814a12c2009-12-14 16:18:45 +00001340 SPDie->addChild(Arg);
1341 }
1342 DIE *SPDeclDie = SPDie;
1343 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1344 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1345 SPDeclDie);
Devang Patel814a12c2009-12-14 16:18:45 +00001346 ModuleCU->addDie(SPDie);
1347 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001348
Devang Patelc50078e2009-11-21 02:48:08 +00001349 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001350 getDWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001351 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001352 getDWLabel("func_end", SubprogramCount));
Devang Patel90a0fe32009-11-10 23:06:00 +00001353 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patelc50078e2009-11-21 02:48:08 +00001354 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach652b7432009-11-21 23:12:12 +00001355
Devang Patel90a0fe32009-11-10 23:06:00 +00001356 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001357 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001358
Devang Patel90a0fe32009-11-10 23:06:00 +00001359 return SPDie;
1360}
1361
Jim Grosbach652b7432009-11-21 23:12:12 +00001362/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001363/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1364DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Chris Lattner5e423432010-03-09 01:51:43 +00001365 unsigned StartID = Scope->getStartLabelID();
1366 unsigned EndID = Scope->getEndLabelID();
Devang Patel90a0fe32009-11-10 23:06:00 +00001367
Chris Lattner5e423432010-03-09 01:51:43 +00001368 assert(!MMI->isLabelDeleted(StartID) &&
1369 "Invalid starting label for an inlined scope!");
1370 assert(!MMI->isLabelDeleted(EndID) &&
1371 "Invalid end label for an inlined scope!");
1372
Devang Patel90a0fe32009-11-10 23:06:00 +00001373 // Ignore empty scopes.
1374 if (StartID == EndID && StartID != 0)
1375 return NULL;
1376
1377 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1378 if (Scope->isAbstractScope())
1379 return ScopeDIE;
1380
Devang Patelc50078e2009-11-21 02:48:08 +00001381 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001382 StartID ? getDWLabel("label", StartID)
1383 : getDWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001384 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001385 EndID ? getDWLabel("label", EndID)
1386 : getDWLabel("func_end", SubprogramCount));
Devang Patel90a0fe32009-11-10 23:06:00 +00001387
1388 return ScopeDIE;
1389}
1390
Devang Patelc50078e2009-11-21 02:48:08 +00001391/// constructInlinedScopeDIE - This scope represents inlined body of
1392/// a function. Construct DIE to represent this concrete inlined copy
1393/// of the function.
1394DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Chris Lattner5e423432010-03-09 01:51:43 +00001395 unsigned StartID = Scope->getStartLabelID();
1396 unsigned EndID = Scope->getEndLabelID();
1397 assert(!MMI->isLabelDeleted(StartID) &&
1398 "Invalid starting label for an inlined scope!");
1399 assert(!MMI->isLabelDeleted(EndID) &&
1400 "Invalid end label for an inlined scope!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001401 // Ignore empty scopes.
Chris Lattner5e423432010-03-09 01:51:43 +00001402 if (StartID == EndID)
Devang Patel90a0fe32009-11-10 23:06:00 +00001403 return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001404 if (!Scope->getScopeNode())
Devang Patelbe149872010-03-08 19:20:38 +00001405 return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001406 DIScope DS(Scope->getScopeNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001407 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1408
1409 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001410 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001411 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001412 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001413 dwarf::DW_FORM_ref4, OriginDIE);
1414
Chris Lattneread58652010-03-09 00:31:02 +00001415 MCSymbol *StartLabel = getDWLabel("label", StartID);
1416
1417 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Devang Patelc50078e2009-11-21 02:48:08 +00001418 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001419 getDWLabel("label", EndID));
Devang Patel90a0fe32009-11-10 23:06:00 +00001420
1421 InlinedSubprogramDIEs.insert(OriginDIE);
1422
1423 // Track the start label for this inlined function.
Devang Patel7d707f92010-01-19 06:19:05 +00001424 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel90a0fe32009-11-10 23:06:00 +00001425 I = InlineInfo.find(InlinedSP.getNode());
1426
1427 if (I == InlineInfo.end()) {
Chris Lattneread58652010-03-09 00:31:02 +00001428 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartLabel,
Jim Grosbachb23f2422009-11-22 19:20:36 +00001429 ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001430 InlinedSPNodes.push_back(InlinedSP.getNode());
1431 } else
Chris Lattneread58652010-03-09 00:31:02 +00001432 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001433
1434 StringPool.insert(InlinedSP.getName());
Devang Patel141e1c42010-01-05 01:46:14 +00001435 StringPool.insert(getRealLinkageName(InlinedSP.getLinkageName()));
1436
Devang Patel90a0fe32009-11-10 23:06:00 +00001437 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001438 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1439 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001440
1441 return ScopeDIE;
1442}
1443
Devang Patelc50078e2009-11-21 02:48:08 +00001444
1445/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelfe0be132009-12-09 18:24:21 +00001446DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001447 // Get the descriptor.
1448 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001449 StringRef Name = VD.getName();
1450 if (Name.empty())
Devang Patelab9a0682009-11-13 02:25:26 +00001451 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001452
1453 // Translate tag to proper Dwarf tag. The result variable is dropped for
1454 // now.
1455 unsigned Tag;
1456 switch (VD.getTag()) {
1457 case dwarf::DW_TAG_return_variable:
1458 return NULL;
1459 case dwarf::DW_TAG_arg_variable:
1460 Tag = dwarf::DW_TAG_formal_parameter;
1461 break;
1462 case dwarf::DW_TAG_auto_variable: // fall thru
1463 default:
1464 Tag = dwarf::DW_TAG_variable;
1465 break;
1466 }
1467
1468 // Define variable debug information entry.
1469 DIE *VariableDie = new DIE(Tag);
1470
1471
1472 DIE *AbsDIE = NULL;
1473 if (DbgVariable *AV = DV->getAbstractVariable())
1474 AbsDIE = AV->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001475
Devang Patel90a0fe32009-11-10 23:06:00 +00001476 if (AbsDIE) {
1477 DIScope DS(Scope->getScopeNode());
1478 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001479 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001480 (void) OriginSPDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001481 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1482 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1483 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001484 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001485 dwarf::DW_FORM_ref4, AbsDIE);
1486 }
1487 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001488 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1489 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001490
1491 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001492 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001493 // addresses instead.
1494 if (VD.isBlockByrefVariable())
Devang Patelfe0be132009-12-09 18:24:21 +00001495 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001496 else
Devang Patelfe0be132009-12-09 18:24:21 +00001497 addType(VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001498 }
1499
1500 // Add variable address.
1501 if (!Scope->isAbstractScope()) {
1502 MachineLocation Location;
Jim Grosbach587d4882009-11-22 20:14:00 +00001503 unsigned FrameReg;
1504 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1505 Location.set(FrameReg, Offset);
Jim Grosbach652b7432009-11-21 23:12:12 +00001506
Devang Patel90a0fe32009-11-10 23:06:00 +00001507 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001508 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001509 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001510 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001511 else
Devang Patelc50078e2009-11-21 02:48:08 +00001512 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001513 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001514
1515 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1516 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001517 DV->setDIE(VariableDie);
1518 return VariableDie;
1519
1520}
Devang Patelc50078e2009-11-21 02:48:08 +00001521
Devang Patelec13b4f2009-11-24 01:14:22 +00001522void DwarfDebug::addPubTypes(DISubprogram SP) {
1523 DICompositeType SPTy = SP.getType();
1524 unsigned SPTag = SPTy.getTag();
1525 if (SPTag != dwarf::DW_TAG_subroutine_type)
1526 return;
1527
1528 DIArray Args = SPTy.getTypeArray();
Devang Patelec13b4f2009-11-24 01:14:22 +00001529 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1530 DIType ATy(Args.getElement(i).getNode());
Devang Patel9e592492010-03-08 20:52:55 +00001531 if (!ATy.isValid())
Devang Patelec13b4f2009-11-24 01:14:22 +00001532 continue;
1533 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel9e592492010-03-08 20:52:55 +00001534 if (DIDescriptor(CATy.getNode()).Verify() && !CATy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001535 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1536 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1537 }
1538 }
1539}
1540
Devang Patelc50078e2009-11-21 02:48:08 +00001541/// constructScopeDIE - Construct a DIE for this scope.
1542DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel9e592492010-03-08 20:52:55 +00001543 if (!Scope || !Scope->getScopeNode())
1544 return NULL;
1545
1546 DIScope DS(Scope->getScopeNode());
1547 DIE *ScopeDIE = NULL;
1548 if (Scope->getInlinedAt())
1549 ScopeDIE = constructInlinedScopeDIE(Scope);
1550 else if (DS.isSubprogram()) {
1551 if (Scope->isAbstractScope())
1552 ScopeDIE = ModuleCU->getDIE(DS.getNode());
1553 else
1554 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
1555 }
1556 else {
1557 ScopeDIE = constructLexicalScopeDIE(Scope);
1558 if (!ScopeDIE) return NULL;
1559 }
1560
Devang Patel90a0fe32009-11-10 23:06:00 +00001561 // Add variables to scope.
Douglas Gregordf49dc72010-03-08 02:58:37 +00001562 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel90a0fe32009-11-10 23:06:00 +00001563 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelfe0be132009-12-09 18:24:21 +00001564 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach652b7432009-11-21 23:12:12 +00001565 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001566 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001567 }
1568
1569 // Add nested scopes.
Douglas Gregordf49dc72010-03-08 02:58:37 +00001570 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel90a0fe32009-11-10 23:06:00 +00001571 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1572 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001573 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001574 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001575 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001576 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001577
1578 if (DS.isSubprogram())
1579 addPubTypes(DISubprogram(DS.getNode()));
1580
1581 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001582}
1583
Bill Wendlingf5839192009-05-20 23:19:06 +00001584/// GetOrCreateSourceID - Look up the source id with the given directory and
1585/// source file names. If none currently exists, create a new id and insert it
1586/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1587/// maps as well.
Devang Patel7f75bbe2009-11-25 17:36:49 +00001588unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001589 unsigned DId;
1590 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1591 if (DI != DirectoryIdMap.end()) {
1592 DId = DI->getValue();
1593 } else {
1594 DId = DirectoryNames.size() + 1;
1595 DirectoryIdMap[DirName] = DId;
1596 DirectoryNames.push_back(DirName);
1597 }
1598
1599 unsigned FId;
1600 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1601 if (FI != SourceFileIdMap.end()) {
1602 FId = FI->getValue();
1603 } else {
1604 FId = SourceFileNames.size() + 1;
1605 SourceFileIdMap[FileName] = FId;
1606 SourceFileNames.push_back(FileName);
1607 }
1608
1609 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1610 SourceIdMap.find(std::make_pair(DId, FId));
1611 if (SI != SourceIdMap.end())
1612 return SI->second;
1613
1614 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1615 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1616 SourceIds.push_back(std::make_pair(DId, FId));
1617
1618 return SrcId;
1619}
1620
Devang Patel8287d662009-12-15 19:16:48 +00001621/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1622DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1623 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1624 if (NDie)
1625 return NDie;
1626 NDie = new DIE(dwarf::DW_TAG_namespace);
1627 ModuleCU->insertDIE(NS.getNode(), NDie);
1628 if (!NS.getName().empty())
1629 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1630 addSourceLine(NDie, &NS);
1631 addToContextOwner(NDie, NS.getContext());
1632 return NDie;
1633}
1634
Devang Patelb9f2c6b2009-12-11 21:37:07 +00001635CompileUnit *DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001636 DICompileUnit DIUnit(N);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001637 StringRef FN = DIUnit.getFilename();
1638 StringRef Dir = DIUnit.getDirectory();
Devang Patelaaf012e2009-09-29 18:40:58 +00001639 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001640
1641 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001642 // FIXME: Why getting the delta between two identical labels??
Devang Patelc50078e2009-11-21 02:48:08 +00001643 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001644 getTempLabel("section_line"), getTempLabel("section_line"),
Bill Wendlingf5839192009-05-20 23:19:06 +00001645 false);
Devang Patelc50078e2009-11-21 02:48:08 +00001646 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001647 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001648 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001649 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001650 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001651
Devang Patel7f75bbe2009-11-25 17:36:49 +00001652 if (!Dir.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001653 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001654 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001655 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001656
Devang Patel7f75bbe2009-11-25 17:36:49 +00001657 StringRef Flags = DIUnit.getFlags();
1658 if (!Flags.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001659 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001660
1661 unsigned RVer = DIUnit.getRunTimeVersion();
1662 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001663 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001664 dwarf::DW_FORM_data1, RVer);
1665
1666 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel5a3d37f2009-06-29 20:45:18 +00001667 if (!ModuleCU && DIUnit.isMain()) {
Devang Patelf97a05a2009-06-29 20:38:13 +00001668 // Use first compile unit marked as isMain as the compile unit
1669 // for this module.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001670 ModuleCU = Unit;
Devang Patelf97a05a2009-06-29 20:38:13 +00001671 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001672
Devang Patelb9f2c6b2009-12-11 21:37:07 +00001673 return Unit;
Bill Wendlingf5839192009-05-20 23:19:06 +00001674}
1675
Devang Patelc50078e2009-11-21 02:48:08 +00001676void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001677 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001678
Devang Patel0c03f062009-09-04 23:59:07 +00001679 // If debug information is malformed then ignore it.
1680 if (DI_GV.Verify() == false)
1681 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001682
1683 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001684 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel166f8432009-06-26 01:49:18 +00001685 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001686
Devang Patelfe0be132009-12-09 18:24:21 +00001687 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel6d479632009-12-10 23:25:41 +00001688 if (!VariableDie)
1689 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001690
Bill Wendlingf5839192009-05-20 23:19:06 +00001691 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001692 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001693
1694 // Add to context owner.
Devang Pateldcc0dce2010-01-15 01:12:22 +00001695 DIDescriptor GVContext = DI_GV.getContext();
1696 // Do not create specification DIE if context is either compile unit
1697 // or a subprogram.
1698 if (DI_GV.isDefinition() && !GVContext.isCompileUnit()
1699 && !GVContext.isSubprogram()) {
Devang Patel8287d662009-12-15 19:16:48 +00001700 // Create specification DIE.
1701 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1702 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1703 dwarf::DW_FORM_ref4, VariableDie);
1704 DIEBlock *Block = new DIEBlock();
1705 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001706 addLabel(Block, 0, dwarf::DW_FORM_udata,
1707 Asm->GetGlobalValueSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001708 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patela0a98582010-02-09 01:58:33 +00001709 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel8287d662009-12-15 19:16:48 +00001710 ModuleCU->addDie(VariableSpecDIE);
1711 } else {
1712 DIEBlock *Block = new DIEBlock();
1713 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001714 addLabel(Block, 0, dwarf::DW_FORM_udata,
1715 Asm->GetGlobalValueSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001716 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1717 }
Devang Pateldcc0dce2010-01-15 01:12:22 +00001718 addToContextOwner(VariableDie, GVContext);
Devang Patel1a8f9a82009-12-10 19:14:49 +00001719
Bill Wendlingf5839192009-05-20 23:19:06 +00001720 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001721 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001722
1723 DIType GTy = DI_GV.getType();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001724 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001725 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1726 assert (Entry && "Missing global type!");
1727 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1728 }
Devang Patel166f8432009-06-26 01:49:18 +00001729 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001730}
1731
Devang Patelc50078e2009-11-21 02:48:08 +00001732void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001733 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001734
1735 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001736 if (ModuleCU->getDIE(N))
Devang Patel166f8432009-06-26 01:49:18 +00001737 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001738
1739 if (!SP.isDefinition())
1740 // This is a method declaration which will be handled while constructing
1741 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001742 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001743
Devang Patelfe0be132009-12-09 18:24:21 +00001744 DIE *SubprogramDie = createSubprogramDIE(SP);
Bill Wendlingf5839192009-05-20 23:19:06 +00001745
1746 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001747 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001748
1749 // Add to context owner.
Devang Patel8287d662009-12-15 19:16:48 +00001750 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patelde2d3682009-12-08 23:21:45 +00001751
Bill Wendlingf5839192009-05-20 23:19:06 +00001752 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001753 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001754
Devang Patel166f8432009-06-26 01:49:18 +00001755 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001756}
1757
Devang Patelc50078e2009-11-21 02:48:08 +00001758/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001759/// content. Create global DIEs and emit initial debug info sections.
1760/// This is inovked by the target AsmPrinter.
Devang Patelc50078e2009-11-21 02:48:08 +00001761void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel59a1d422009-06-25 22:36:02 +00001762 this->M = M;
1763
Bill Wendlingf5839192009-05-20 23:19:06 +00001764 if (TimePassesIsEnabled)
1765 DebugTimer->startTimer();
1766
Devang Patel1b4d6832009-11-11 19:55:08 +00001767 if (!MAI->doesSupportDebugInformation())
1768 return;
1769
Devang Patelfda766d2009-07-30 18:56:46 +00001770 DebugInfoFinder DbgFinder;
1771 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001772
Bill Wendlingf5839192009-05-20 23:19:06 +00001773 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001774 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1775 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001776 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001777
Devang Patel5a3d37f2009-06-29 20:45:18 +00001778 if (!ModuleCU)
Devang Patel7f9e0f32010-03-08 22:02:50 +00001779 return;
Devang Patelf97a05a2009-06-29 20:38:13 +00001780
Devang Patel90a0fe32009-11-10 23:06:00 +00001781 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001782 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1783 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001784 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001785
Devang Patel1a8f9a82009-12-10 19:14:49 +00001786 // Create DIEs for each global variable.
1787 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1788 E = DbgFinder.global_variable_end(); I != E; ++I)
1789 constructGlobalVariableDIE(*I);
1790
Bill Wendlingf5839192009-05-20 23:19:06 +00001791 MMI = mmi;
1792 shouldEmit = true;
1793 MMI->setDebugInfoAvailability(true);
1794
1795 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001796 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001797
1798 // Print out .file directives to specify files for .loc directives. These are
1799 // printed out early so that they precede any .loc directives.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001800 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001801 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1802 // Remember source id starts at 1.
1803 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerad653482010-01-22 22:09:00 +00001804 // FIXME: don't use sys::path for this! This should not depend on the
1805 // host.
Bill Wendlingf5839192009-05-20 23:19:06 +00001806 sys::Path FullPath(getSourceDirectoryName(Id.first));
1807 bool AppendOk =
1808 FullPath.appendComponent(getSourceFileName(Id.second));
1809 assert(AppendOk && "Could not append filename to directory!");
1810 AppendOk = false;
Chris Lattner59be4ea2010-01-25 18:58:59 +00001811 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001812 }
1813 }
1814
1815 // Emit initial sections
Devang Patelc50078e2009-11-21 02:48:08 +00001816 emitInitial();
Bill Wendlingf5839192009-05-20 23:19:06 +00001817
1818 if (TimePassesIsEnabled)
1819 DebugTimer->stopTimer();
1820}
1821
Devang Patelc50078e2009-11-21 02:48:08 +00001822/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001823///
Devang Patelc50078e2009-11-21 02:48:08 +00001824void DwarfDebug::endModule() {
Devang Patel95d477e2009-10-06 00:03:14 +00001825 if (!ModuleCU)
Bill Wendlingf5839192009-05-20 23:19:06 +00001826 return;
1827
1828 if (TimePassesIsEnabled)
1829 DebugTimer->startTimer();
1830
Devang Patel90a0fe32009-11-10 23:06:00 +00001831 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1832 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1833 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1834 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001835 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001836 }
1837
Devang Patelc1df8792009-12-03 01:25:38 +00001838 // Insert top level DIEs.
1839 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1840 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1841 ModuleCU->getCUDie()->addChild(*TI);
1842
Devang Patel7d707f92010-01-19 06:19:05 +00001843 for (DenseMap<DIE *, MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel188c85d2009-12-03 19:11:07 +00001844 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1845 DIE *SPDie = CI->first;
1846 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1847 if (!N) continue;
1848 DIE *NDie = ModuleCU->getDIE(N);
1849 if (!NDie) continue;
1850 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel69aae432010-01-15 00:26:31 +00001851 // FIXME - This is not the correct approach.
1852 // addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel188c85d2009-12-03 19:11:07 +00001853 }
1854
Bill Wendlingf5839192009-05-20 23:19:06 +00001855 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001856 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00001857 Asm->OutStreamer.EmitLabel(getTempLabel("text_end"));
Chris Lattner73266f92009-08-19 05:49:37 +00001858 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00001859 Asm->OutStreamer.EmitLabel(getTempLabel("data_end"));
Bill Wendlingf5839192009-05-20 23:19:06 +00001860
1861 // End text sections.
1862 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001863 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattner6d213e82010-03-08 22:44:40 +00001864 Asm->OutStreamer.EmitLabel(getDWLabel("section_end", i));
Bill Wendlingf5839192009-05-20 23:19:06 +00001865 }
1866
1867 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001868 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001869
1870 // Emit function debug frame information
1871 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1872 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001873 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001874
1875 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001876 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001877
1878 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001879 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001880
1881 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00001882 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00001883
1884 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00001885 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00001886
1887 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00001888 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00001889
Devang Patelec13b4f2009-11-24 01:14:22 +00001890 // Emit info into a debug pubtypes section.
1891 emitDebugPubTypes();
1892
Bill Wendlingf5839192009-05-20 23:19:06 +00001893 // Emit info into a debug str section.
Devang Patelc50078e2009-11-21 02:48:08 +00001894 emitDebugStr();
Bill Wendlingf5839192009-05-20 23:19:06 +00001895
1896 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00001897 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00001898
1899 // Emit info into a debug aranges section.
1900 EmitDebugARanges();
1901
1902 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00001903 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00001904
1905 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00001906 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001907
1908 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00001909 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001910
1911 if (TimePassesIsEnabled)
1912 DebugTimer->stopTimer();
1913}
1914
Devang Patel90a0fe32009-11-10 23:06:00 +00001915/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00001916DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1917 unsigned FrameIdx,
Devang Patel90a0fe32009-11-10 23:06:00 +00001918 DILocation &ScopeLoc) {
1919
1920 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1921 if (AbsDbgVariable)
1922 return AbsDbgVariable;
1923
1924 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1925 if (!Scope)
1926 return NULL;
1927
1928 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patelc50078e2009-11-21 02:48:08 +00001929 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00001930 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1931 return AbsDbgVariable;
1932}
1933
Devang Patelc50078e2009-11-21 02:48:08 +00001934/// collectVariableInfo - Populate DbgScope entries with variables' info.
1935void DwarfDebug::collectVariableInfo() {
Devang Patel40c80212009-10-09 22:42:28 +00001936 if (!MMI) return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001937
Devang Patel84139992009-10-06 01:26:37 +00001938 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1939 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1940 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel0a0ea052010-01-22 22:52:10 +00001941 MDNode *Var = VI->first;
Devang Patel90a0fe32009-11-10 23:06:00 +00001942 if (!Var) continue;
Devang Patel6882dff2009-10-08 18:48:03 +00001943 DIVariable DV (Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00001944 std::pair< unsigned, MDNode *> VP = VI->second;
1945 DILocation ScopeLoc(VP.second);
1946
1947 DbgScope *Scope =
1948 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1949 if (!Scope)
Jim Grosbach652b7432009-11-21 23:12:12 +00001950 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelbad42262009-11-10 23:20:04 +00001951 // If variable scope is not found then skip this variable.
1952 if (!Scope)
1953 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00001954
1955 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patelc50078e2009-11-21 02:48:08 +00001956 Scope->addVariable(RegVar);
Jim Grosbachb23f2422009-11-22 19:20:36 +00001957 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1958 ScopeLoc))
Devang Patel90a0fe32009-11-10 23:06:00 +00001959 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patel84139992009-10-06 01:26:37 +00001960 }
1961}
1962
Devang Patelc50078e2009-11-21 02:48:08 +00001963/// beginScope - Process beginning of a scope starting at Label.
1964void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel393a46d2009-10-06 01:50:42 +00001965 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1966 if (I == DbgScopeBeginMap.end())
1967 return;
Dan Gohman8d34f972009-11-23 21:30:55 +00001968 ScopeVector &SD = I->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00001969 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001970 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00001971 (*SDI)->setStartLabelID(Label);
1972}
1973
Devang Patelc50078e2009-11-21 02:48:08 +00001974/// endScope - Process end of a scope.
1975void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel393a46d2009-10-06 01:50:42 +00001976 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patelf4348892009-10-06 03:15:38 +00001977 if (I == DbgScopeEndMap.end())
Devang Patel393a46d2009-10-06 01:50:42 +00001978 return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001979
1980 unsigned Label = MMI->NextLabelID();
1981 Asm->printLabel(Label);
1982
Devang Patel393a46d2009-10-06 01:50:42 +00001983 SmallVector<DbgScope *, 2> &SD = I->second;
1984 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001985 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00001986 (*SDI)->setEndLabelID(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00001987 return;
1988}
1989
1990/// createDbgScope - Create DbgScope for the scope.
1991void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1992
1993 if (!InlinedAt) {
1994 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1995 if (WScope)
1996 return;
1997 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1998 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach652b7432009-11-21 23:12:12 +00001999 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel53addbf2009-11-11 00:18:40 +00002000 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel90a0fe32009-11-10 23:06:00 +00002001 return;
2002 }
2003
2004 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2005 if (WScope)
2006 return;
2007
2008 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2009 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2010 DILocation DL(InlinedAt);
2011 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel393a46d2009-10-06 01:50:42 +00002012}
2013
Devang Patelc50078e2009-11-21 02:48:08 +00002014/// extractScopeInformation - Scan machine instructions in this function
Devang Patel6a260102009-10-01 20:31:14 +00002015/// and collect DbgScopes. Return true, if atleast one scope was found.
Chris Lattner69a76972010-01-26 23:18:02 +00002016bool DwarfDebug::extractScopeInformation() {
Devang Patel6a260102009-10-01 20:31:14 +00002017 // If scope information was extracted using .dbg intrinsics then there is not
2018 // any need to extract these information by scanning each instruction.
2019 if (!DbgScopeMap.empty())
2020 return false;
2021
Devang Patel98e77302010-01-04 20:44:00 +00002022 DenseMap<const MachineInstr *, unsigned> MIIndexMap;
2023 unsigned MIIndex = 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00002024 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6a260102009-10-01 20:31:14 +00002025 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2026 I != E; ++I) {
2027 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2028 II != IE; ++II) {
2029 const MachineInstr *MInsn = II;
Devang Patel98e77302010-01-04 20:44:00 +00002030 MIIndexMap[MInsn] = MIIndex++;
Devang Patel6a260102009-10-01 20:31:14 +00002031 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00002032 if (DL.isUnknown()) continue;
Devang Patel042945f2010-01-16 06:09:35 +00002033 DILocation DLT = MF->getDILocation(DL);
2034 DIScope DLTScope = DLT.getScope();
Devang Patel6a260102009-10-01 20:31:14 +00002035 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002036 // other scopes, create one DbgScope now. This will be translated
Devang Patel6a260102009-10-01 20:31:14 +00002037 // into a scope DIE at the end.
Devang Patel042945f2010-01-16 06:09:35 +00002038 if (DLTScope.isCompileUnit()) continue;
2039 createDbgScope(DLTScope.getNode(), DLT.getOrigLocation().getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00002040 }
2041 }
2042
2043
2044 // Build scope hierarchy using working set of scopes.
2045 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2046 I != E; ++I) {
2047 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2048 II != IE; ++II) {
2049 const MachineInstr *MInsn = II;
2050 DebugLoc DL = MInsn->getDebugLoc();
2051 if (DL.isUnknown()) continue;
Devang Patel042945f2010-01-16 06:09:35 +00002052 DILocation DLT = MF->getDILocation(DL);
2053 DIScope DLTScope = DLT.getScope();
Devang Patel90a0fe32009-11-10 23:06:00 +00002054 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002055 // other scopes, create one DbgScope now. This will be translated
Devang Patel90a0fe32009-11-10 23:06:00 +00002056 // into a scope DIE at the end.
Devang Patel042945f2010-01-16 06:09:35 +00002057 if (DLTScope.isCompileUnit()) continue;
2058 DbgScope *Scope = getUpdatedDbgScope(DLTScope.getNode(), MInsn,
2059 DLT.getOrigLocation().getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00002060 Scope->setLastInsn(MInsn);
Devang Patel6a260102009-10-01 20:31:14 +00002061 }
2062 }
2063
Devang Patel98e77302010-01-04 20:44:00 +00002064 if (!CurrentFnDbgScope)
2065 return false;
2066
2067 CurrentFnDbgScope->fixInstructionMarkers(MIIndexMap);
Devang Patel6a260102009-10-01 20:31:14 +00002068
2069 // Each scope has first instruction and last instruction to mark beginning
2070 // and end of a scope respectively. Create an inverse map that list scopes
2071 // starts (and ends) with an instruction. One instruction may start (or end)
Devang Patelfd311df2010-01-20 02:05:23 +00002072 // multiple scopes. Ignore scopes that are not reachable.
2073 SmallVector<DbgScope *, 4> WorkList;
2074 WorkList.push_back(CurrentFnDbgScope);
2075 while (!WorkList.empty()) {
2076 DbgScope *S = WorkList.back(); WorkList.pop_back();
2077
Douglas Gregordf49dc72010-03-08 02:58:37 +00002078 SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patelfd311df2010-01-20 02:05:23 +00002079 if (!Children.empty())
Douglas Gregordf49dc72010-03-08 02:58:37 +00002080 for (SmallVector<DbgScope *, 4>::iterator SI = Children.begin(),
Devang Patelfd311df2010-01-20 02:05:23 +00002081 SE = Children.end(); SI != SE; ++SI)
2082 WorkList.push_back(*SI);
2083
Devang Patel90a0fe32009-11-10 23:06:00 +00002084 if (S->isAbstractScope())
2085 continue;
Devang Patel6a260102009-10-01 20:31:14 +00002086 const MachineInstr *MI = S->getFirstInsn();
2087 assert (MI && "DbgScope does not have first instruction!");
2088
2089 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2090 if (IDI != DbgScopeBeginMap.end())
2091 IDI->second.push_back(S);
2092 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002093 DbgScopeBeginMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002094
2095 MI = S->getLastInsn();
2096 assert (MI && "DbgScope does not have last instruction!");
2097 IDI = DbgScopeEndMap.find(MI);
2098 if (IDI != DbgScopeEndMap.end())
2099 IDI->second.push_back(S);
2100 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002101 DbgScopeEndMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002102 }
2103
2104 return !DbgScopeMap.empty();
2105}
2106
Devang Patelc50078e2009-11-21 02:48:08 +00002107/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002108/// emitted immediately after the function entry point.
Chris Lattner69a76972010-01-26 23:18:02 +00002109void DwarfDebug::beginFunction(const MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002110 this->MF = MF;
2111
2112 if (!ShouldEmitDwarfDebug()) return;
2113
2114 if (TimePassesIsEnabled)
2115 DebugTimer->startTimer();
2116
Chris Lattner69a76972010-01-26 23:18:02 +00002117 if (!extractScopeInformation())
Devang Patel0feae422009-10-06 18:37:31 +00002118 return;
Devang Patelc50078e2009-11-21 02:48:08 +00002119
2120 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002121
Bill Wendlingf5839192009-05-20 23:19:06 +00002122 // Assumes in correct section after the entry point.
Chris Lattner6d213e82010-03-08 22:44:40 +00002123 Asm->OutStreamer.EmitLabel(getDWLabel("func_begin", ++SubprogramCount));
Bill Wendlingf5839192009-05-20 23:19:06 +00002124
2125 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2126 // function.
Devang Patel40c80212009-10-09 22:42:28 +00002127 DebugLoc FDL = MF->getDefaultDebugLoc();
2128 if (!FDL.isUnknown()) {
Devang Patel042945f2010-01-16 06:09:35 +00002129 DILocation DLT = MF->getDILocation(FDL);
Devang Patel40c80212009-10-09 22:42:28 +00002130 unsigned LabelID = 0;
Devang Patel042945f2010-01-16 06:09:35 +00002131 DISubprogram SP = getDISubprogram(DLT.getScope().getNode());
Devang Patel9e592492010-03-08 20:52:55 +00002132 if (SP.Verify())
Devang Patel042945f2010-01-16 06:09:35 +00002133 LabelID = recordSourceLine(SP.getLineNumber(), 0,
2134 DLT.getScope().getNode());
Devang Patel40c80212009-10-09 22:42:28 +00002135 else
Devang Patel042945f2010-01-16 06:09:35 +00002136 LabelID = recordSourceLine(DLT.getLineNumber(),
2137 DLT.getColumnNumber(),
2138 DLT.getScope().getNode());
Devang Patel40c80212009-10-09 22:42:28 +00002139 Asm->printLabel(LabelID);
Bill Wendlingf5839192009-05-20 23:19:06 +00002140 }
Bill Wendlingf5839192009-05-20 23:19:06 +00002141 if (TimePassesIsEnabled)
2142 DebugTimer->stopTimer();
2143}
2144
Devang Patelc50078e2009-11-21 02:48:08 +00002145/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002146///
Chris Lattner69a76972010-01-26 23:18:02 +00002147void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002148 if (!ShouldEmitDwarfDebug()) return;
2149
2150 if (TimePassesIsEnabled)
2151 DebugTimer->startTimer();
2152
Devang Patel40c80212009-10-09 22:42:28 +00002153 if (DbgScopeMap.empty())
2154 return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002155
Devang Patel98e77302010-01-04 20:44:00 +00002156 if (CurrentFnDbgScope) {
2157 // Define end label for subprogram.
Chris Lattner6d213e82010-03-08 22:44:40 +00002158 Asm->OutStreamer.EmitLabel(getDWLabel("func_end", SubprogramCount));
Devang Patel98e77302010-01-04 20:44:00 +00002159
2160 // Get function line info.
2161 if (!Lines.empty()) {
2162 // Get section line info.
2163 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2164 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2165 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2166 // Append the function info to section info.
2167 SectionLineInfos.insert(SectionLineInfos.end(),
2168 Lines.begin(), Lines.end());
2169 }
2170
2171 // Construct abstract scopes.
2172 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2173 AE = AbstractScopesList.end(); AI != AE; ++AI)
2174 constructScopeDIE(*AI);
2175
2176 constructScopeDIE(CurrentFnDbgScope);
2177
2178 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2179 MMI->getFrameMoves()));
Bill Wendlingf5839192009-05-20 23:19:06 +00002180 }
2181
Bill Wendlingf5839192009-05-20 23:19:06 +00002182 // Clear debug info
Devang Patel387e9c12010-01-19 01:26:02 +00002183 CurrentFnDbgScope = NULL;
Douglas Gregordf49dc72010-03-08 02:58:37 +00002184 DbgScopeMap.clear();
Devang Patel387e9c12010-01-19 01:26:02 +00002185 DbgScopeBeginMap.clear();
2186 DbgScopeEndMap.clear();
2187 ConcreteScopes.clear();
2188 AbstractScopesList.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002189 Lines.clear();
Devang Patel67533ab2009-12-01 18:13:48 +00002190
Bill Wendlingf5839192009-05-20 23:19:06 +00002191 if (TimePassesIsEnabled)
2192 DebugTimer->stopTimer();
2193}
2194
Devang Patelc50078e2009-11-21 02:48:08 +00002195/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf5839192009-05-20 23:19:06 +00002196/// label. Returns a unique label ID used to generate a label and provide
2197/// correspondence to the source line list.
Jim Grosbach652b7432009-11-21 23:12:12 +00002198unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel946d0ae2009-10-05 18:03:19 +00002199 MDNode *S) {
Devang Patel15e723d2009-08-28 23:24:31 +00002200 if (!MMI)
2201 return 0;
2202
Bill Wendlingf5839192009-05-20 23:19:06 +00002203 if (TimePassesIsEnabled)
2204 DebugTimer->startTimer();
2205
Devang Patel7f75bbe2009-11-25 17:36:49 +00002206 StringRef Dir;
2207 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002208
2209 DIDescriptor Scope(S);
2210 if (Scope.isCompileUnit()) {
2211 DICompileUnit CU(S);
2212 Dir = CU.getDirectory();
2213 Fn = CU.getFilename();
2214 } else if (Scope.isSubprogram()) {
2215 DISubprogram SP(S);
2216 Dir = SP.getDirectory();
2217 Fn = SP.getFilename();
2218 } else if (Scope.isLexicalBlock()) {
2219 DILexicalBlock DB(S);
2220 Dir = DB.getDirectory();
2221 Fn = DB.getFilename();
2222 } else
2223 assert (0 && "Unexpected scope info");
2224
2225 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf5839192009-05-20 23:19:06 +00002226 unsigned ID = MMI->NextLabelID();
2227 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2228
2229 if (TimePassesIsEnabled)
2230 DebugTimer->stopTimer();
2231
2232 return ID;
2233}
2234
2235/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2236/// timed. Look up the source id with the given directory and source file
2237/// names. If none currently exists, create a new id and insert it in the
2238/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2239/// well.
2240unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2241 const std::string &FileName) {
2242 if (TimePassesIsEnabled)
2243 DebugTimer->startTimer();
2244
Devang Patelaaf012e2009-09-29 18:40:58 +00002245 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf5839192009-05-20 23:19:06 +00002246
2247 if (TimePassesIsEnabled)
2248 DebugTimer->stopTimer();
2249
2250 return SrcId;
2251}
2252
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002253//===----------------------------------------------------------------------===//
2254// Emit Methods
2255//===----------------------------------------------------------------------===//
2256
Devang Patelc50078e2009-11-21 02:48:08 +00002257/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002258///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002259unsigned
2260DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002261 // Get the children.
2262 const std::vector<DIE *> &Children = Die->getChildren();
2263
2264 // If not last sibling and has children then add sibling offset attribute.
Devang Patelc50078e2009-11-21 02:48:08 +00002265 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002266
2267 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002268 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002269
2270 // Get the abbreviation for this DIE.
2271 unsigned AbbrevNumber = Die->getAbbrevNumber();
2272 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2273
2274 // Set DIE offset
2275 Die->setOffset(Offset);
2276
2277 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002278 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002279
2280 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2281 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2282
2283 // Size the DIE attribute values.
2284 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2285 // Size attribute value.
2286 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2287
2288 // Size the DIE children if any.
2289 if (!Children.empty()) {
2290 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2291 "Children flag not set");
2292
2293 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002294 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002295
2296 // End of children marker.
2297 Offset += sizeof(int8_t);
2298 }
2299
2300 Die->setSize(Offset - Die->getOffset());
2301 return Offset;
2302}
2303
Devang Patelc50078e2009-11-21 02:48:08 +00002304/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002305///
Devang Patelc50078e2009-11-21 02:48:08 +00002306void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002307 // Compute size of compile unit header.
2308 static unsigned Offset =
2309 sizeof(int32_t) + // Length of Compilation Unit Info
2310 sizeof(int16_t) + // DWARF version number
2311 sizeof(int32_t) + // Offset Into Abbrev. Section
2312 sizeof(int8_t); // Pointer Size (in bytes)
2313
Devang Patelc50078e2009-11-21 02:48:08 +00002314 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel5a3d37f2009-06-29 20:45:18 +00002315 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling55fccda2009-05-20 23:21:38 +00002316}
2317
Devang Patelc50078e2009-11-21 02:48:08 +00002318/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling55fccda2009-05-20 23:21:38 +00002319/// tools to recognize the object file contains Dwarf information.
Devang Patelc50078e2009-11-21 02:48:08 +00002320void DwarfDebug::emitInitial() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002321 // Check to see if we already emitted intial headers.
2322 if (didInitial) return;
2323 didInitial = true;
2324
Chris Lattner73266f92009-08-19 05:49:37 +00002325 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002326
Bill Wendling55fccda2009-05-20 23:21:38 +00002327 // Dwarf sections base addresses.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002328 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner73266f92009-08-19 05:49:37 +00002329 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002330 Asm->OutStreamer.EmitLabel(getTempLabel("section_debug_frame"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002331 }
2332
Chris Lattner73266f92009-08-19 05:49:37 +00002333 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002334 Asm->OutStreamer.EmitLabel(getTempLabel("section_info"));
Chris Lattner73266f92009-08-19 05:49:37 +00002335 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002336 Asm->OutStreamer.EmitLabel(getTempLabel("section_abbrev"));
Chris Lattner73266f92009-08-19 05:49:37 +00002337 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002338 Asm->OutStreamer.EmitLabel(getTempLabel("section_aranges"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002339
Chris Lattner73266f92009-08-19 05:49:37 +00002340 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2341 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Chris Lattner6d213e82010-03-08 22:44:40 +00002342 Asm->OutStreamer.EmitLabel(getTempLabel("section_macinfo"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002343 }
2344
Chris Lattner73266f92009-08-19 05:49:37 +00002345 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002346 Asm->OutStreamer.EmitLabel(getTempLabel("section_line"));
Chris Lattner73266f92009-08-19 05:49:37 +00002347 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002348 Asm->OutStreamer.EmitLabel(getTempLabel("section_loc"));
Chris Lattner73266f92009-08-19 05:49:37 +00002349 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002350 Asm->OutStreamer.EmitLabel(getTempLabel("section_pubnames"));
Devang Patelec13b4f2009-11-24 01:14:22 +00002351 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002352 Asm->OutStreamer.EmitLabel(getTempLabel("section_pubtypes"));
Chris Lattner73266f92009-08-19 05:49:37 +00002353 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002354 Asm->OutStreamer.EmitLabel(getTempLabel("section_str"));
Chris Lattner73266f92009-08-19 05:49:37 +00002355 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002356 Asm->OutStreamer.EmitLabel(getTempLabel("section_ranges"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002357
Chris Lattner73266f92009-08-19 05:49:37 +00002358 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002359 Asm->OutStreamer.EmitLabel(getTempLabel("text_begin"));
Chris Lattner73266f92009-08-19 05:49:37 +00002360 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002361 Asm->OutStreamer.EmitLabel(getTempLabel("data_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002362}
2363
Devang Patelc50078e2009-11-21 02:48:08 +00002364/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002365///
Devang Patelc50078e2009-11-21 02:48:08 +00002366void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002367 // Get the abbreviation for this DIE.
2368 unsigned AbbrevNumber = Die->getAbbrevNumber();
2369 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2370
Chris Lattnerad653482010-01-22 22:09:00 +00002371 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002372
2373 // Emit the code (index) for the abbreviation.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002374 if (Asm->VerboseAsm)
2375 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2376 Twine::utohexstr(Die->getOffset()) + ":0x" +
2377 Twine::utohexstr(Die->getSize()) + " " +
2378 dwarf::TagString(Abbrev->getTag()));
2379 EmitULEB128(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002380
2381 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2382 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2383
2384 // Emit the DIE attribute values.
2385 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2386 unsigned Attr = AbbrevData[i].getAttribute();
2387 unsigned Form = AbbrevData[i].getForm();
2388 assert(Form && "Too many attributes for DIE (check abbreviation)");
2389
Chris Lattner91e06b42010-01-24 18:54:17 +00002390 if (Asm->VerboseAsm)
2391 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2392
Bill Wendling55fccda2009-05-20 23:21:38 +00002393 switch (Attr) {
2394 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002395 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002396 break;
2397 case dwarf::DW_AT_abstract_origin: {
2398 DIEEntry *E = cast<DIEEntry>(Values[i]);
2399 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002400 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002401 Asm->EmitInt32(Addr);
2402 break;
2403 }
2404 default:
2405 // Emit an attribute using the defined form.
2406 Values[i]->EmitValue(this, Form);
Chris Lattner2d2f1002010-01-24 19:01:06 +00002407 O << "\n"; // REMOVE This once all EmitValue impls emit their own newline.
Bill Wendling55fccda2009-05-20 23:21:38 +00002408 break;
2409 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002410 }
2411
2412 // Emit the DIE children if any.
2413 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2414 const std::vector<DIE *> &Children = Die->getChildren();
2415
2416 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002417 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002418
Chris Lattner05ae1d22010-01-22 23:47:11 +00002419 Asm->EmitInt8(0); EOL("End Of Children Mark");
Bill Wendling55fccda2009-05-20 23:21:38 +00002420 }
2421}
2422
Devang Patelfe0be132009-12-09 18:24:21 +00002423/// emitDebugInfo - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002424///
Devang Patelfe0be132009-12-09 18:24:21 +00002425void DwarfDebug::emitDebugInfo() {
2426 // Start debug info section.
2427 Asm->OutStreamer.SwitchSection(
2428 Asm->getObjFileLowering().getDwarfInfoSection());
2429 DIE *Die = ModuleCU->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002430
2431 // Emit the compile units header.
Chris Lattner6d213e82010-03-08 22:44:40 +00002432 Asm->OutStreamer.EmitLabel(getDWLabel("info_begin", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002433
2434 // Emit size of content not including length itself
2435 unsigned ContentSize = Die->getSize() +
2436 sizeof(int16_t) + // DWARF version number
2437 sizeof(int32_t) + // Offset Into Abbrev. Section
2438 sizeof(int8_t) + // Pointer Size (in bytes)
2439 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2440
Chris Lattner05ae1d22010-01-22 23:47:11 +00002441 Asm->EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2442 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF version number");
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002443 EmitSectionOffset(getTempLabel("abbrev_begin"),getTempLabel("section_abbrev"),
2444 true, false);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002445 EOL("Offset Into Abbrev. Section");
2446 Asm->EmitInt8(TD->getPointerSize()); EOL("Address Size (in bytes)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002447
Devang Patelc50078e2009-11-21 02:48:08 +00002448 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002449 // FIXME - extra padding for gdb bug.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002450 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2451 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2452 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2453 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
Chris Lattner6d213e82010-03-08 22:44:40 +00002454 Asm->OutStreamer.EmitLabel(getDWLabel("info_end", ModuleCU->getID()));
Chris Lattnerad653482010-01-22 22:09:00 +00002455 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002456}
2457
Devang Patelc50078e2009-11-21 02:48:08 +00002458/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002459///
Devang Patelc50078e2009-11-21 02:48:08 +00002460void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002461 // Check to see if it is worth the effort.
2462 if (!Abbreviations.empty()) {
2463 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002464 Asm->OutStreamer.SwitchSection(
2465 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002466
Chris Lattner6d213e82010-03-08 22:44:40 +00002467 Asm->OutStreamer.EmitLabel(getTempLabel("abbrev_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002468
2469 // For each abbrevation.
2470 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2471 // Get abbreviation data
2472 const DIEAbbrev *Abbrev = Abbreviations[i];
2473
2474 // Emit the abbrevations code (base 1 index.)
Chris Lattnerbcc79432010-01-22 23:18:42 +00002475 EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling55fccda2009-05-20 23:21:38 +00002476
2477 // Emit the abbreviations data.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002478 Abbrev->Emit(this);
Chris Lattnerad653482010-01-22 22:09:00 +00002479 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002480 }
2481
2482 // Mark end of abbreviations.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002483 EmitULEB128(0, "EOM(3)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002484
Chris Lattner6d213e82010-03-08 22:44:40 +00002485 Asm->OutStreamer.EmitLabel(getTempLabel("abbrev_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002486 }
2487}
2488
Devang Patelc50078e2009-11-21 02:48:08 +00002489/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002490/// the line matrix.
2491///
Devang Patelc50078e2009-11-21 02:48:08 +00002492void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002493 // Define last address of section.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002494 Asm->EmitInt8(0); EOL("Extended Op");
2495 Asm->EmitInt8(TD->getPointerSize() + 1); EOL("Op size");
2496 Asm->EmitInt8(dwarf::DW_LNE_set_address); EOL("DW_LNE_set_address");
Chris Lattner7bc33552010-03-08 22:47:57 +00002497 EmitReference(getDWLabel("section_end", SectionEnd));
2498 EOL("Section end label");
Bill Wendling55fccda2009-05-20 23:21:38 +00002499
2500 // Mark end of matrix.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002501 Asm->EmitInt8(0); EOL("DW_LNE_end_sequence");
Chris Lattnerad653482010-01-22 22:09:00 +00002502 Asm->EmitInt8(1);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002503 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002504}
2505
Devang Patelc50078e2009-11-21 02:48:08 +00002506/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002507///
Devang Patelc50078e2009-11-21 02:48:08 +00002508void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002509 // If the target is using .loc/.file, the assembler will be emitting the
2510 // .debug_line table automatically.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002511 if (MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002512 return;
2513
2514 // Minimum line delta, thus ranging from -10..(255-10).
2515 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2516 // Maximum line delta, thus ranging from -10..(255-10).
2517 const int MaxLineDelta = 255 + MinLineDelta;
2518
2519 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002520 Asm->OutStreamer.SwitchSection(
2521 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002522
2523 // Construct the section header.
Chris Lattner6d213e82010-03-08 22:44:40 +00002524 EmitDifference(getTempLabel("line_end"), getTempLabel("line_begin"), true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002525 EOL("Length of Source Line Info");
Chris Lattner6d213e82010-03-08 22:44:40 +00002526 Asm->OutStreamer.EmitLabel(getTempLabel("line_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002527
Chris Lattner05ae1d22010-01-22 23:47:11 +00002528 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF version number");
Bill Wendling55fccda2009-05-20 23:21:38 +00002529
Chris Lattner6d213e82010-03-08 22:44:40 +00002530 EmitDifference(getTempLabel("line_prolog_end"),
2531 getTempLabel("line_prolog_begin"), true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002532 EOL("Prolog Length");
Chris Lattner6d213e82010-03-08 22:44:40 +00002533 Asm->OutStreamer.EmitLabel(getTempLabel("line_prolog_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002534
Chris Lattner05ae1d22010-01-22 23:47:11 +00002535 Asm->EmitInt8(1); EOL("Minimum Instruction Length");
2536 Asm->EmitInt8(1); EOL("Default is_stmt_start flag");
2537 Asm->EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2538 Asm->EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2539 Asm->EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Bill Wendling55fccda2009-05-20 23:21:38 +00002540
2541 // Line number standard opcode encodings argument count
Chris Lattner05ae1d22010-01-22 23:47:11 +00002542 Asm->EmitInt8(0); EOL("DW_LNS_copy arg count");
2543 Asm->EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2544 Asm->EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2545 Asm->EmitInt8(1); EOL("DW_LNS_set_file arg count");
2546 Asm->EmitInt8(1); EOL("DW_LNS_set_column arg count");
2547 Asm->EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2548 Asm->EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2549 Asm->EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2550 Asm->EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Bill Wendling55fccda2009-05-20 23:21:38 +00002551
2552 // Emit directories.
2553 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnercc564642010-01-23 03:11:46 +00002554 const std::string &Dir = getSourceDirectoryName(DI);
2555 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Directory");
2556 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002557 }
2558
Chris Lattner05ae1d22010-01-22 23:47:11 +00002559 Asm->EmitInt8(0); EOL("End of directories");
Bill Wendling55fccda2009-05-20 23:21:38 +00002560
2561 // Emit files.
2562 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2563 // Remember source id starts at 1.
2564 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnercc564642010-01-23 03:11:46 +00002565 const std::string &FN = getSourceFileName(Id.second);
2566 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Source");
2567 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2568
Chris Lattnerbcc79432010-01-22 23:18:42 +00002569 EmitULEB128(Id.first, "Directory #");
2570 EmitULEB128(0, "Mod date");
2571 EmitULEB128(0, "File size");
Bill Wendling55fccda2009-05-20 23:21:38 +00002572 }
2573
Chris Lattner05ae1d22010-01-22 23:47:11 +00002574 Asm->EmitInt8(0); EOL("End of files");
Bill Wendling55fccda2009-05-20 23:21:38 +00002575
Chris Lattner6d213e82010-03-08 22:44:40 +00002576 Asm->OutStreamer.EmitLabel(getTempLabel("line_prolog_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002577
2578 // A sequence for each text section.
2579 unsigned SecSrcLinesSize = SectionSourceLines.size();
2580
2581 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2582 // Isolate current sections line info.
2583 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2584
Chris Lattner26aabb92009-08-08 23:39:42 +00002585 /*if (Asm->isVerbose()) {
Chris Lattnere6ad12f2009-07-31 18:48:30 +00002586 const MCSection *S = SectionMap[j + 1];
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002587 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling55fccda2009-05-20 23:21:38 +00002588 << S->getName() << '\n';
Chris Lattner26aabb92009-08-08 23:39:42 +00002589 }*/
Chris Lattnerad653482010-01-22 22:09:00 +00002590 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002591
2592 // Dwarf assumes we start with first line of first source file.
2593 unsigned Source = 1;
2594 unsigned Line = 1;
2595
2596 // Construct rows of the address, source, line, column matrix.
2597 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2598 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner5e423432010-03-09 01:51:43 +00002599 unsigned LabelID = LineInfo.getLabelID();
2600 if (MMI->isLabelDeleted(LabelID)) continue;
Bill Wendling55fccda2009-05-20 23:21:38 +00002601
Caroline Tice9da96d82009-09-11 18:25:54 +00002602 if (LineInfo.getLine() == 0) continue;
2603
Bill Wendling55fccda2009-05-20 23:21:38 +00002604 if (!Asm->isVerbose())
Chris Lattnerad653482010-01-22 22:09:00 +00002605 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002606 else {
2607 std::pair<unsigned, unsigned> SourceID =
2608 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002609 O << '\t' << MAI->getCommentString() << ' '
Dan Gohman1792bc62009-12-05 02:00:34 +00002610 << getSourceDirectoryName(SourceID.first) << '/'
Bill Wendling55fccda2009-05-20 23:21:38 +00002611 << getSourceFileName(SourceID.second)
Dan Gohman1792bc62009-12-05 02:00:34 +00002612 << ':' << utostr_32(LineInfo.getLine()) << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002613 }
2614
2615 // Define the line address.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002616 Asm->EmitInt8(0); EOL("Extended Op");
2617 Asm->EmitInt8(TD->getPointerSize() + 1); EOL("Op size");
2618 Asm->EmitInt8(dwarf::DW_LNE_set_address); EOL("DW_LNE_set_address");
Chris Lattner7bc33552010-03-08 22:47:57 +00002619 EmitReference(getDWLabel("label", LabelID)); EOL("Location label");
Bill Wendling55fccda2009-05-20 23:21:38 +00002620
2621 // If change of source, then switch to the new source.
2622 if (Source != LineInfo.getSourceID()) {
2623 Source = LineInfo.getSourceID();
Chris Lattner05ae1d22010-01-22 23:47:11 +00002624 Asm->EmitInt8(dwarf::DW_LNS_set_file); EOL("DW_LNS_set_file");
Chris Lattnerbcc79432010-01-22 23:18:42 +00002625 EmitULEB128(Source, "New Source");
Bill Wendling55fccda2009-05-20 23:21:38 +00002626 }
2627
2628 // If change of line.
2629 if (Line != LineInfo.getLine()) {
2630 // Determine offset.
2631 int Offset = LineInfo.getLine() - Line;
2632 int Delta = Offset - MinLineDelta;
2633
2634 // Update line.
2635 Line = LineInfo.getLine();
2636
2637 // If delta is small enough and in range...
2638 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2639 // ... then use fast opcode.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002640 Asm->EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
Bill Wendling55fccda2009-05-20 23:21:38 +00002641 } else {
2642 // ... otherwise use long hand.
2643 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002644 EOL("DW_LNS_advance_line");
Chris Lattner20f334f2010-01-22 22:56:55 +00002645 EmitSLEB128(Offset, "Line Offset");
Chris Lattner05ae1d22010-01-22 23:47:11 +00002646 Asm->EmitInt8(dwarf::DW_LNS_copy); EOL("DW_LNS_copy");
Bill Wendling55fccda2009-05-20 23:21:38 +00002647 }
2648 } else {
2649 // Copy the previous row (different address or source)
Chris Lattner05ae1d22010-01-22 23:47:11 +00002650 Asm->EmitInt8(dwarf::DW_LNS_copy); EOL("DW_LNS_copy");
Bill Wendling55fccda2009-05-20 23:21:38 +00002651 }
2652 }
2653
Devang Patelc50078e2009-11-21 02:48:08 +00002654 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002655 }
2656
2657 if (SecSrcLinesSize == 0)
2658 // Because we're emitting a debug_line section, we still need a line
2659 // table. The linker and friends expect it to exist. If there's nothing to
2660 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00002661 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002662
Chris Lattner6d213e82010-03-08 22:44:40 +00002663 Asm->OutStreamer.EmitLabel(getTempLabel("line_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002664}
2665
Devang Patelc50078e2009-11-21 02:48:08 +00002666/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002667///
Devang Patelc50078e2009-11-21 02:48:08 +00002668void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002669 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002670 return;
2671
2672 int stackGrowth =
2673 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2674 TargetFrameInfo::StackGrowsUp ?
2675 TD->getPointerSize() : -TD->getPointerSize();
2676
2677 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002678 Asm->OutStreamer.SwitchSection(
2679 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002680
Chris Lattner6d213e82010-03-08 22:44:40 +00002681 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common"));
Chris Lattner2a65fd42010-03-08 23:02:59 +00002682 EmitDifference(getTempLabel("debug_frame_common_end"),
2683 getTempLabel("debug_frame_common_begin"), true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002684 EOL("Length of Common Information Entry");
Bill Wendling55fccda2009-05-20 23:21:38 +00002685
Chris Lattner6d213e82010-03-08 22:44:40 +00002686 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002687 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002688 EOL("CIE Identifier Tag");
Bill Wendling55fccda2009-05-20 23:21:38 +00002689 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002690 EOL("CIE Version");
Chris Lattnercc564642010-01-23 03:11:46 +00002691 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002692 EOL("CIE Augmentation");
Chris Lattnerbcc79432010-01-22 23:18:42 +00002693 EmitULEB128(1, "CIE Code Alignment Factor");
Chris Lattner20f334f2010-01-22 22:56:55 +00002694 EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Bill Wendling55fccda2009-05-20 23:21:38 +00002695 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Chris Lattner05ae1d22010-01-22 23:47:11 +00002696 EOL("CIE RA Column");
Bill Wendling55fccda2009-05-20 23:21:38 +00002697
2698 std::vector<MachineMove> Moves;
2699 RI->getInitialFrameState(Moves);
2700
2701 EmitFrameMoves(NULL, 0, Moves, false);
2702
2703 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattner6d213e82010-03-08 22:44:40 +00002704 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002705}
2706
Devang Patelc50078e2009-11-21 02:48:08 +00002707/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00002708/// section.
2709void
Devang Patelc50078e2009-11-21 02:48:08 +00002710DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002711 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002712 return;
2713
2714 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002715 Asm->OutStreamer.SwitchSection(
2716 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002717
Chris Lattner2a65fd42010-03-08 23:02:59 +00002718 EmitDifference(getDWLabel("debug_frame_end", DebugFrameInfo.Number),
2719 getDWLabel("debug_frame_begin", DebugFrameInfo.Number), true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002720 EOL("Length of Frame Information Entry");
Bill Wendling55fccda2009-05-20 23:21:38 +00002721
Chris Lattner6d213e82010-03-08 22:44:40 +00002722 Asm->OutStreamer.EmitLabel(getDWLabel("debug_frame_begin",
2723 DebugFrameInfo.Number));
Bill Wendling55fccda2009-05-20 23:21:38 +00002724
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002725 EmitSectionOffset(getTempLabel("debug_frame_common"),
2726 getTempLabel("section_debug_frame"), true, false);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002727 EOL("FDE CIE offset");
Bill Wendling55fccda2009-05-20 23:21:38 +00002728
Chris Lattner7bc33552010-03-08 22:47:57 +00002729 EmitReference(getDWLabel("func_begin", DebugFrameInfo.Number));
Chris Lattner05ae1d22010-01-22 23:47:11 +00002730 EOL("FDE initial location");
Chris Lattner7bc33552010-03-08 22:47:57 +00002731 EmitDifference(getDWLabel("func_end", DebugFrameInfo.Number),
2732 getDWLabel("func_begin", DebugFrameInfo.Number));
Chris Lattner05ae1d22010-01-22 23:47:11 +00002733 EOL("FDE address range");
Bill Wendling55fccda2009-05-20 23:21:38 +00002734
2735 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2736 false);
2737
2738 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattner6d213e82010-03-08 22:44:40 +00002739 Asm->OutStreamer.EmitLabel(getDWLabel("debug_frame_end",
2740 DebugFrameInfo.Number));
Bill Wendling55fccda2009-05-20 23:21:38 +00002741}
2742
Devang Patelfe0be132009-12-09 18:24:21 +00002743/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2744///
2745void DwarfDebug::emitDebugPubNames() {
2746 // Start the dwarf pubnames section.
2747 Asm->OutStreamer.SwitchSection(
2748 Asm->getObjFileLowering().getDwarfPubNamesSection());
2749
Chris Lattner2a65fd42010-03-08 23:02:59 +00002750 EmitDifference(getDWLabel("pubnames_end", ModuleCU->getID()),
2751 getDWLabel("pubnames_begin", ModuleCU->getID()), true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002752 EOL("Length of Public Names Info");
Bill Wendling55fccda2009-05-20 23:21:38 +00002753
Chris Lattner6d213e82010-03-08 22:44:40 +00002754 Asm->OutStreamer.EmitLabel(getDWLabel("pubnames_begin", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002755
Chris Lattner05ae1d22010-01-22 23:47:11 +00002756 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF Version");
Bill Wendling55fccda2009-05-20 23:21:38 +00002757
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002758 EmitSectionOffset(getDWLabel("info_begin", ModuleCU->getID()),
2759 getTempLabel("section_info"),
2760 true, false);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002761 EOL("Offset of Compilation Unit Info");
Bill Wendling55fccda2009-05-20 23:21:38 +00002762
Chris Lattner2a65fd42010-03-08 23:02:59 +00002763 EmitDifference(getDWLabel("info_end", ModuleCU->getID()),
2764 getDWLabel("info_begin", ModuleCU->getID()),
Bill Wendling55fccda2009-05-20 23:21:38 +00002765 true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002766 EOL("Compilation Unit Length");
Bill Wendling55fccda2009-05-20 23:21:38 +00002767
Devang Patelfe0be132009-12-09 18:24:21 +00002768 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling55fccda2009-05-20 23:21:38 +00002769 for (StringMap<DIE*>::const_iterator
2770 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2771 const char *Name = GI->getKeyData();
2772 DIE * Entity = GI->second;
2773
Chris Lattner05ae1d22010-01-22 23:47:11 +00002774 Asm->EmitInt32(Entity->getOffset()); EOL("DIE offset");
Chris Lattnercc564642010-01-23 03:11:46 +00002775
2776 if (Asm->VerboseAsm)
2777 Asm->OutStreamer.AddComment("External Name");
2778 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002779 }
2780
Chris Lattner05ae1d22010-01-22 23:47:11 +00002781 Asm->EmitInt32(0); EOL("End Mark");
Chris Lattner6d213e82010-03-08 22:44:40 +00002782 Asm->OutStreamer.EmitLabel(getDWLabel("pubnames_end", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002783}
2784
Devang Patelec13b4f2009-11-24 01:14:22 +00002785void DwarfDebug::emitDebugPubTypes() {
Devang Patel6f2bdd52009-11-24 19:18:41 +00002786 // Start the dwarf pubnames section.
2787 Asm->OutStreamer.SwitchSection(
2788 Asm->getObjFileLowering().getDwarfPubTypesSection());
Chris Lattner2a65fd42010-03-08 23:02:59 +00002789 EmitDifference(getDWLabel("pubtypes_end", ModuleCU->getID()),
2790 getDWLabel("pubtypes_begin", ModuleCU->getID()), true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002791 EOL("Length of Public Types Info");
Devang Patelec13b4f2009-11-24 01:14:22 +00002792
Chris Lattner6d213e82010-03-08 22:44:40 +00002793 Asm->OutStreamer.EmitLabel(getDWLabel("pubtypes_begin", ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00002794
Devang Patel40b6cda2010-02-02 03:47:27 +00002795 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DWARF Version");
2796 Asm->EmitInt16(dwarf::DWARF_VERSION);
Devang Patelec13b4f2009-11-24 01:14:22 +00002797
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002798 EmitSectionOffset(getDWLabel("info_begin", ModuleCU->getID()),
2799 getTempLabel("section_info"), true, false);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002800 EOL("Offset of Compilation ModuleCU Info");
Devang Patelec13b4f2009-11-24 01:14:22 +00002801
Chris Lattner2a65fd42010-03-08 23:02:59 +00002802 EmitDifference(getDWLabel("info_end", ModuleCU->getID()),
2803 getDWLabel("info_begin", ModuleCU->getID()),
Devang Patelec13b4f2009-11-24 01:14:22 +00002804 true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002805 EOL("Compilation ModuleCU Length");
Devang Patelec13b4f2009-11-24 01:14:22 +00002806
2807 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2808 for (StringMap<DIE*>::const_iterator
2809 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2810 const char *Name = GI->getKeyData();
2811 DIE * Entity = GI->second;
2812
Devang Patel40b6cda2010-02-02 03:47:27 +00002813 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DIE offset");
2814 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00002815
2816 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("External Name");
Devang Patel0ceb4892010-02-02 03:37:03 +00002817 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00002818 }
2819
Chris Lattner05ae1d22010-01-22 23:47:11 +00002820 Asm->EmitInt32(0); EOL("End Mark");
Chris Lattner6d213e82010-03-08 22:44:40 +00002821 Asm->OutStreamer.EmitLabel(getDWLabel("pubtypes_end", ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00002822}
2823
Devang Patelc50078e2009-11-21 02:48:08 +00002824/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002825///
Devang Patelc50078e2009-11-21 02:48:08 +00002826void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002827 // Check to see if it is worth the effort.
2828 if (!StringPool.empty()) {
2829 // Start the dwarf str section.
Chris Lattner73266f92009-08-19 05:49:37 +00002830 Asm->OutStreamer.SwitchSection(
2831 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002832
2833 // For each of strings in the string pool.
2834 for (unsigned StringID = 1, N = StringPool.size();
2835 StringID <= N; ++StringID) {
2836 // Emit a label for reference from debug information entries.
Chris Lattner6d213e82010-03-08 22:44:40 +00002837 Asm->OutStreamer.EmitLabel(getDWLabel("string", StringID));
Bill Wendling55fccda2009-05-20 23:21:38 +00002838
2839 // Emit the string itself.
2840 const std::string &String = StringPool[StringID];
Chris Lattnercc564642010-01-23 03:11:46 +00002841 Asm->OutStreamer.EmitBytes(StringRef(String.c_str(), String.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002842 }
2843
Chris Lattnerad653482010-01-22 22:09:00 +00002844 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002845 }
2846}
2847
Devang Patelc50078e2009-11-21 02:48:08 +00002848/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002849///
Devang Patelc50078e2009-11-21 02:48:08 +00002850void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002851 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00002852 Asm->OutStreamer.SwitchSection(
2853 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002854}
2855
2856/// EmitDebugARanges - Emit visible names into a debug aranges section.
2857///
2858void DwarfDebug::EmitDebugARanges() {
2859 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002860 Asm->OutStreamer.SwitchSection(
2861 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002862
2863 // FIXME - Mock up
2864#if 0
2865 CompileUnit *Unit = GetBaseCompileUnit();
2866
2867 // Don't include size of length
Chris Lattner05ae1d22010-01-22 23:47:11 +00002868 Asm->EmitInt32(0x1c); EOL("Length of Address Ranges Info");
Bill Wendling55fccda2009-05-20 23:21:38 +00002869
Chris Lattner05ae1d22010-01-22 23:47:11 +00002870 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("Dwarf Version");
Bill Wendling55fccda2009-05-20 23:21:38 +00002871
2872 EmitReference("info_begin", Unit->getID());
Chris Lattner05ae1d22010-01-22 23:47:11 +00002873 EOL("Offset of Compilation Unit Info");
Bill Wendling55fccda2009-05-20 23:21:38 +00002874
Chris Lattner05ae1d22010-01-22 23:47:11 +00002875 Asm->EmitInt8(TD->getPointerSize()); EOL("Size of Address");
Bill Wendling55fccda2009-05-20 23:21:38 +00002876
Chris Lattner05ae1d22010-01-22 23:47:11 +00002877 Asm->EmitInt8(0); EOL("Size of Segment Descriptor");
Bill Wendling55fccda2009-05-20 23:21:38 +00002878
Chris Lattner05ae1d22010-01-22 23:47:11 +00002879 Asm->EmitInt16(0); EOL("Pad (1)");
2880 Asm->EmitInt16(0); EOL("Pad (2)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002881
2882 // Range 1
Chris Lattner05ae1d22010-01-22 23:47:11 +00002883 EmitReference("text_begin", 0); EOL("Address");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002884 EmitDifference(getTempLabel("text_end"), getTempLabel("text_begin"),
2885 true); EOL("Length");
Bill Wendling55fccda2009-05-20 23:21:38 +00002886
Chris Lattner05ae1d22010-01-22 23:47:11 +00002887 Asm->EmitInt32(0); EOL("EOM (1)");
2888 Asm->EmitInt32(0); EOL("EOM (2)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002889#endif
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 Lattner2a65fd42010-03-08 23:02:59 +00002938 EmitDifference(getDWLabel("debug_inlined_end", 1),
2939 getDWLabel("debug_inlined_begin", 1), true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002940 EOL("Length of Debug Inlined Information Entry");
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 Lattner05ae1d22010-01-22 23:47:11 +00002944 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("Dwarf Version");
2945 Asm->EmitInt8(TD->getPointerSize()); EOL("Address Size (in bytes)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002946
Devang Patel90a0fe32009-11-10 23:06:00 +00002947 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2948 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00002949
Devang Patel90a0fe32009-11-10 23:06:00 +00002950 MDNode *Node = *I;
Devang Patel7d707f92010-01-19 06:19:05 +00002951 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbachb23f2422009-11-22 19:20:36 +00002952 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00002953 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00002954 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00002955 StringRef LName = SP.getLinkageName();
2956 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00002957
Chris Lattnercc564642010-01-23 03:11:46 +00002958 if (LName.empty()) {
2959 Asm->OutStreamer.EmitBytes(Name, 0);
2960 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
2961 } else
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002962 EmitSectionOffset(getDWLabel("string",
2963 StringPool.idFor(getRealLinkageName(LName))),
2964 getTempLabel("section_str"), true);
Devang Patel90a0fe32009-11-10 23:06:00 +00002965
Chris Lattner05ae1d22010-01-22 23:47:11 +00002966 EOL("MIPS linkage name");
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002967 EmitSectionOffset(getDWLabel("string", StringPool.idFor(Name)),
2968 getTempLabel("section_str"), false, true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002969 EOL("Function name");
Chris Lattnerbcc79432010-01-22 23:18:42 +00002970 EmitULEB128(Labels.size(), "Inline count");
Bill Wendling55fccda2009-05-20 23:21:38 +00002971
Devang Patel90a0fe32009-11-10 23:06:00 +00002972 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00002973 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattneread58652010-03-09 00:31:02 +00002974 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DIE offset");
2975 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002976
Chris Lattnere90630a2010-03-09 00:26:09 +00002977 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner2ab0c442010-03-09 00:39:24 +00002978 Asm->OutStreamer.EmitSymbolValue(LI->first, TD->getPointerSize(), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002979 }
2980 }
2981
Chris Lattner6d213e82010-03-08 22:44:40 +00002982 Asm->OutStreamer.EmitLabel(getDWLabel("debug_inlined_end", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00002983}