blob: 8d190372d6aacc501d9c3096a57b6897d8477029 [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"
Chris Lattner855b6bb2010-04-05 05:24:55 +000016#include "DIE.h"
Bill Wendling989a27e2010-04-05 22:59:21 +000017#include "llvm/Constants.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000018#include "llvm/Module.h"
David Greened87baff2009-08-19 21:52:55 +000019#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner855e8f52010-03-09 01:58:53 +000021#include "llvm/MC/MCAsmInfo.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000022#include "llvm/MC/MCSection.h"
Chris Lattner73266f92009-08-19 05:49:37 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattner855e8f52010-03-09 01:58:53 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattner31a54742010-01-16 21:57:06 +000025#include "llvm/Target/Mangler.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000026#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner017c7f92010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000030#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner855b6bb2010-04-05 05:24:55 +000031#include "llvm/Analysis/DebugInfo.h"
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +000032#include "llvm/ADT/STLExtras.h"
Chris Lattnerf5377682009-08-24 03:52:50 +000033#include "llvm/ADT/StringExtras.h"
Daniel Dunbarc74255d2009-10-13 06:47:08 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/ErrorHandling.h"
Devang Patelae89d3b2010-01-26 21:39:14 +000036#include "llvm/Support/ValueHandle.h"
Chris Lattnerad653482010-01-22 22:09:00 +000037#include "llvm/Support/FormattedStream.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000038#include "llvm/Support/Timer.h"
39#include "llvm/System/Path.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000040using namespace llvm;
41
Bill Wendlingce3c6252010-04-07 09:28:04 +000042namespace {
43 const char *DWARFGroupName = "DWARF Emission";
44 const char *DbgTimerName = "DWARF Debug Writer";
45} // end anonymous namespace
46
Bill Wendlingb12b3d72009-05-15 09:23:25 +000047//===----------------------------------------------------------------------===//
48
49/// Configuration values for initial hash set sizes (log2).
50///
Bill Wendlingb12b3d72009-05-15 09:23:25 +000051static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendlingb12b3d72009-05-15 09:23:25 +000052
53namespace llvm {
54
55//===----------------------------------------------------------------------===//
56/// CompileUnit - This dwarf writer support class manages information associate
57/// with a source file.
Nick Lewyckyee68f452009-11-17 08:11:44 +000058class CompileUnit {
Bill Wendlingb12b3d72009-05-15 09:23:25 +000059 /// ID - File identifier for source.
60 ///
61 unsigned ID;
62
63 /// Die - Compile unit debug information entry.
64 ///
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +000065 const OwningPtr<DIE> CUDie;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000066
Jeffrey Yasskinc3545712010-03-11 18:29:55 +000067 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel1233f912009-11-21 00:31:03 +000068 DIE *IndexTyDie;
69
Bill Wendlingb12b3d72009-05-15 09:23:25 +000070 /// GVToDieMap - Tracks the mapping of unit level debug informaton
71 /// variables to debug information entries.
Devang Patel15e723d2009-08-28 23:24:31 +000072 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel7d707f92010-01-19 06:19:05 +000073 DenseMap<MDNode *, DIE *> GVToDieMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000074
75 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
76 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel15e723d2009-08-28 23:24:31 +000077 /// FIXME : Rename
Devang Patel7d707f92010-01-19 06:19:05 +000078 DenseMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000079
80 /// Globals - A map of globally visible named entities for this unit.
81 ///
82 StringMap<DIE*> Globals;
83
Devang Patelec13b4f2009-11-24 01:14:22 +000084 /// GlobalTypes - A map of globally visible types for this unit.
85 ///
86 StringMap<DIE*> GlobalTypes;
87
Bill Wendlingb12b3d72009-05-15 09:23:25 +000088public:
89 CompileUnit(unsigned I, DIE *D)
Devang Patelc50078e2009-11-21 02:48:08 +000090 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +000091
92 // Accessors.
Devang Patelec13b4f2009-11-24 01:14:22 +000093 unsigned getID() const { return ID; }
Jeffrey Yasskinc3545712010-03-11 18:29:55 +000094 DIE* getCUDie() const { return CUDie.get(); }
Devang Patelec13b4f2009-11-24 01:14:22 +000095 const StringMap<DIE*> &getGlobals() const { return Globals; }
96 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000097
98 /// hasContent - Return true if this compile unit has something to write out.
99 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000100 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000101
Devang Patelc50078e2009-11-21 02:48:08 +0000102 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000103 ///
Benjamin Kramer648940b2010-03-31 20:15:45 +0000104 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000105
Devang Patelec13b4f2009-11-24 01:14:22 +0000106 /// addGlobalType - Add a new global type to the compile unit.
107 ///
Benjamin Kramer648940b2010-03-31 20:15:45 +0000108 void addGlobalType(StringRef Name, DIE *Die) {
Devang Patelec13b4f2009-11-24 01:14:22 +0000109 GlobalTypes[Name] = Die;
110 }
111
Devang Pateld90672c2009-11-20 21:37:22 +0000112 /// getDIE - Returns the debug information entry map slot for the
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000113 /// specified debug variable.
Devang Pateld90672c2009-11-20 21:37:22 +0000114 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach652b7432009-11-21 23:12:12 +0000115
Devang Pateld90672c2009-11-20 21:37:22 +0000116 /// insertDIE - Insert DIE into the map.
117 void insertDIE(MDNode *N, DIE *D) {
118 GVToDieMap.insert(std::make_pair(N, D));
119 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000120
Devang Pateld90672c2009-11-20 21:37:22 +0000121 /// getDIEEntry - Returns the debug information entry for the speciefied
122 /// debug variable.
Devang Patel8287d662009-12-15 19:16:48 +0000123 DIEEntry *getDIEEntry(MDNode *N) {
Devang Patel7d707f92010-01-19 06:19:05 +0000124 DenseMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel8287d662009-12-15 19:16:48 +0000125 if (I == GVToDIEEntryMap.end())
126 return NULL;
127 return I->second;
128 }
Devang Pateld90672c2009-11-20 21:37:22 +0000129
130 /// insertDIEEntry - Insert debug information entry into the map.
131 void insertDIEEntry(MDNode *N, DIEEntry *E) {
132 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000133 }
134
Devang Patelc50078e2009-11-21 02:48:08 +0000135 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000136 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000137 void addDie(DIE *Buffer) {
138 this->CUDie->addChild(Buffer);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000139 }
Devang Patel1233f912009-11-21 00:31:03 +0000140
141 // getIndexTyDie - Get an anonymous type for index type.
142 DIE *getIndexTyDie() {
143 return IndexTyDie;
144 }
145
Jim Grosbachb23f2422009-11-22 19:20:36 +0000146 // setIndexTyDie - Set D as anonymous type for index which can be reused
147 // later.
Devang Patel1233f912009-11-21 00:31:03 +0000148 void setIndexTyDie(DIE *D) {
149 IndexTyDie = D;
150 }
151
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000152};
153
154//===----------------------------------------------------------------------===//
155/// DbgVariable - This class is used to track local variable information.
156///
Devang Patel4cb32c32009-11-16 21:53:40 +0000157class DbgVariable {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000158 DIVariable Var; // Variable Descriptor.
159 unsigned FrameIndex; // Variable frame index.
Devang Patel14da02f2010-03-15 18:33:46 +0000160 const MachineInstr *DbgValueMInsn; // DBG_VALUE
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000161 // DbgValueLabel - DBG_VALUE is effective from this label.
162 MCSymbol *DbgValueLabel;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000163 DbgVariable *const AbstractVar; // Abstract variable for this variable.
Devang Patel90a0fe32009-11-10 23:06:00 +0000164 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000165public:
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000166 // AbsVar may be NULL.
167 DbgVariable(DIVariable V, unsigned I, DbgVariable *AbsVar)
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000168 : Var(V), FrameIndex(I), DbgValueMInsn(0),
169 DbgValueLabel(0), AbstractVar(AbsVar), TheDIE(0) {}
Devang Patel14da02f2010-03-15 18:33:46 +0000170 DbgVariable(DIVariable V, const MachineInstr *MI, DbgVariable *AbsVar)
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000171 : Var(V), FrameIndex(0), DbgValueMInsn(MI), DbgValueLabel(0),
172 AbstractVar(AbsVar), TheDIE(0)
Devang Patel14da02f2010-03-15 18:33:46 +0000173 {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000174
175 // Accessors.
Devang Patel90a0fe32009-11-10 23:06:00 +0000176 DIVariable getVariable() const { return Var; }
177 unsigned getFrameIndex() const { return FrameIndex; }
Devang Patel14da02f2010-03-15 18:33:46 +0000178 const MachineInstr *getDbgValue() const { return DbgValueMInsn; }
Devang Patel9a9a4ac2010-03-29 22:59:58 +0000179 MCSymbol *getDbgValueLabel() const { return DbgValueLabel; }
180 void setDbgValueLabel(MCSymbol *L) { DbgValueLabel = L; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000181 DbgVariable *getAbstractVariable() const { return AbstractVar; }
182 void setDIE(DIE *D) { TheDIE = D; }
183 DIE *getDIE() const { return TheDIE; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000184};
185
186//===----------------------------------------------------------------------===//
187/// DbgScope - This class is used to track scope information.
188///
Devang Patel4cb32c32009-11-16 21:53:40 +0000189class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000190 DbgScope *Parent; // Parent to this scope.
Jim Grosbach652b7432009-11-21 23:12:12 +0000191 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patelae89d3b2010-01-26 21:39:14 +0000192 // Location at which this scope is inlined.
193 AssertingVH<MDNode> InlinedAtLocation;
Devang Patel90a0fe32009-11-10 23:06:00 +0000194 bool AbstractScope; // Abstract Scope
Chris Lattner855e8f52010-03-09 01:58:53 +0000195 MCSymbol *StartLabel; // Label ID of the beginning of scope.
196 MCSymbol *EndLabel; // Label ID of the end of scope.
Devang Patel90ecd192009-10-01 18:25:23 +0000197 const MachineInstr *LastInsn; // Last instruction of this scope.
198 const MachineInstr *FirstInsn; // First instruction of this scope.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000199 // Scopes defined in scope. Contents not owned.
200 SmallVector<DbgScope *, 4> Scopes;
201 // Variables declared in scope. Contents owned.
202 SmallVector<DbgVariable *, 8> Variables;
Daniel Dunbar41716322009-09-19 20:40:05 +0000203
Owen Anderson696d4862009-06-24 22:53:20 +0000204 // Private state for dump()
205 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000206public:
Devang Pateldd7bb432009-10-14 21:08:09 +0000207 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000208 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Chris Lattner855e8f52010-03-09 01:58:53 +0000209 StartLabel(0), EndLabel(0),
Devang Pateldd7bb432009-10-14 21:08:09 +0000210 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000211 virtual ~DbgScope();
212
213 // Accessors.
214 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000215 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000216 DIDescriptor getDesc() const { return Desc; }
Chris Lattner0f093f82010-03-13 02:17:42 +0000217 MDNode *getInlinedAt() const { return InlinedAtLocation; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000218 MDNode *getScopeNode() const { return Desc.getNode(); }
Chris Lattner855e8f52010-03-09 01:58:53 +0000219 MCSymbol *getStartLabel() const { return StartLabel; }
220 MCSymbol *getEndLabel() const { return EndLabel; }
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000221 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
222 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Chris Lattner855e8f52010-03-09 01:58:53 +0000223 void setStartLabel(MCSymbol *S) { StartLabel = S; }
224 void setEndLabel(MCSymbol *E) { EndLabel = E; }
Devang Patel90ecd192009-10-01 18:25:23 +0000225 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
226 const MachineInstr *getLastInsn() { return LastInsn; }
227 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000228 void setAbstractScope() { AbstractScope = true; }
229 bool isAbstractScope() const { return AbstractScope; }
Devang Patel90ecd192009-10-01 18:25:23 +0000230 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000231
Devang Patelc50078e2009-11-21 02:48:08 +0000232 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000233 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000234 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000235
Devang Patelc50078e2009-11-21 02:48:08 +0000236 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000237 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000238 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000239
Devang Patel98e77302010-01-04 20:44:00 +0000240 void fixInstructionMarkers(DenseMap<const MachineInstr *,
241 unsigned> &MIIndexMap) {
Chris Lattner8143ce82010-03-31 05:36:29 +0000242 assert(getFirstInsn() && "First instruction is missing!");
Devang Patel98e77302010-01-04 20:44:00 +0000243
244 // Use the end of last child scope as end of this scope.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000245 const SmallVector<DbgScope *, 4> &Scopes = getScopes();
Devang Patel3b620bf2010-01-05 16:59:17 +0000246 const MachineInstr *LastInsn = getFirstInsn();
Devang Patel98e77302010-01-04 20:44:00 +0000247 unsigned LIndex = 0;
248 if (Scopes.empty()) {
Chris Lattner8143ce82010-03-31 05:36:29 +0000249 assert(getLastInsn() && "Inner most scope does not have last insn!");
Devang Patel98e77302010-01-04 20:44:00 +0000250 return;
251 }
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000252 for (SmallVector<DbgScope *, 4>::const_iterator SI = Scopes.begin(),
Devang Patel98e77302010-01-04 20:44:00 +0000253 SE = Scopes.end(); SI != SE; ++SI) {
254 DbgScope *DS = *SI;
255 DS->fixInstructionMarkers(MIIndexMap);
256 const MachineInstr *DSLastInsn = DS->getLastInsn();
257 unsigned DSI = MIIndexMap[DSLastInsn];
258 if (DSI > LIndex) {
259 LastInsn = DSLastInsn;
260 LIndex = DSI;
261 }
262 }
Devang Patel067e6d92010-02-17 02:20:34 +0000263
264 unsigned CurrentLastInsnIndex = 0;
265 if (const MachineInstr *CL = getLastInsn())
266 CurrentLastInsnIndex = MIIndexMap[CL];
267 unsigned FIndex = MIIndexMap[getFirstInsn()];
268
269 // Set LastInsn as the last instruction for this scope only if
270 // it follows
271 // 1) this scope's first instruction and
272 // 2) current last instruction for this scope, if any.
273 if (LIndex >= CurrentLastInsnIndex && LIndex >= FIndex)
274 setLastInsn(LastInsn);
Devang Patel6a260102009-10-01 20:31:14 +0000275 }
276
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000277#ifndef NDEBUG
278 void dump() const;
279#endif
280};
Chris Lattner8b9430c2010-04-05 04:09:20 +0000281
282} // end llvm namespace
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000283
284#ifndef NDEBUG
285void DbgScope::dump() const {
David Greenedbc06132009-12-24 00:31:35 +0000286 raw_ostream &err = dbgs();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000287 err.indent(IndentLevel);
Devang Patel90a0fe32009-11-10 23:06:00 +0000288 MDNode *N = Desc.getNode();
289 N->dump();
Chris Lattner855e8f52010-03-09 01:58:53 +0000290 err << " [" << StartLabel << ", " << EndLabel << "]\n";
Devang Patel90a0fe32009-11-10 23:06:00 +0000291 if (AbstractScope)
292 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000293
294 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000295 if (!Scopes.empty())
296 err << "Children ...\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000297 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
298 if (Scopes[i] != this)
299 Scopes[i]->dump();
300
301 IndentLevel -= 2;
302}
303#endif
304
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000305DbgScope::~DbgScope() {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000306 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
307 delete Variables[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000308}
309
Chris Lattnerec9a1f42010-04-05 05:11:15 +0000310DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000311 : Asm(A), MMI(Asm->MMI), ModuleCU(0),
Chris Lattner2cb53302010-04-05 03:52:55 +0000312 AbbreviationsSet(InitAbbreviationsSetSize),
Bill Wendlingce3c6252010-04-07 09:28:04 +0000313 CurrentFnDbgScope(0) {
Chris Lattner0f093f82010-03-13 02:17:42 +0000314 NextStringPoolNumber = 0;
Chris Lattner66143d22010-04-04 22:59:04 +0000315
Chris Lattner094932e2010-04-04 23:10:38 +0000316 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
317 DwarfStrSectionSym = TextSectionSym = 0;
Edwin Törökc669cb62010-04-07 10:44:46 +0000318
319 if (TimePassesIsEnabled) {
320 NamedRegionTimer T(DbgTimerName, DWARFGroupName);
321 beginModule(M);
322 } else {
323 beginModule(M);
324 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000325}
326DwarfDebug::~DwarfDebug() {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000327 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
328 DIEBlocks[j]->~DIEBlock();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000329}
330
Chris Lattner0f093f82010-03-13 02:17:42 +0000331MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
332 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
333 if (Entry.first) return Entry.first;
334
335 Entry.second = NextStringPoolNumber++;
Chris Lattnerb93558d2010-04-04 19:25:43 +0000336 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
Chris Lattner0f093f82010-03-13 02:17:42 +0000337}
338
339
Devang Patelc50078e2009-11-21 02:48:08 +0000340/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000341///
Devang Patelc50078e2009-11-21 02:48:08 +0000342void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000343 // Profile the node so that we can make it unique.
344 FoldingSetNodeID ID;
345 Abbrev.Profile(ID);
346
347 // Check the set for priors.
348 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
349
350 // If it's newly added.
351 if (InSet == &Abbrev) {
352 // Add to abbreviation list.
353 Abbreviations.push_back(&Abbrev);
354
355 // Assign the vector position + 1 as its number.
356 Abbrev.setNumber(Abbreviations.size());
357 } else {
358 // Assign existing abbreviation number.
359 Abbrev.setNumber(InSet->getNumber());
360 }
361}
362
Devang Patelc50078e2009-11-21 02:48:08 +0000363/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000364/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000365DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000366 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000367 return Value;
368}
369
Devang Patelc50078e2009-11-21 02:48:08 +0000370/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000371///
Devang Patelc50078e2009-11-21 02:48:08 +0000372void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000373 unsigned Form, uint64_t Integer) {
374 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000375 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000376 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000377}
378
Devang Patelc50078e2009-11-21 02:48:08 +0000379/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000380///
Devang Patelc50078e2009-11-21 02:48:08 +0000381void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000382 unsigned Form, int64_t Integer) {
383 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000384 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000385 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000386}
387
Devang Pateldf0f2152009-12-02 15:25:16 +0000388/// addString - Add a string attribute data and value. DIEString only
389/// keeps string reference.
Devang Patelc50078e2009-11-21 02:48:08 +0000390void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnercc564642010-01-23 03:11:46 +0000391 StringRef String) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000392 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000393 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000394}
395
Devang Patelc50078e2009-11-21 02:48:08 +0000396/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000397///
Devang Patelc50078e2009-11-21 02:48:08 +0000398void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000399 const MCSymbol *Label) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000400 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000401 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000402}
403
Devang Patelc50078e2009-11-21 02:48:08 +0000404/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000405///
Devang Patelc50078e2009-11-21 02:48:08 +0000406void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000407 const MCSymbol *Hi, const MCSymbol *Lo) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000408 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000409 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000410}
411
Chris Lattner855b6bb2010-04-05 05:24:55 +0000412/// addDIEEntry - Add a DIE attribute data and value.
413///
414void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
415 DIE *Entry) {
416 Die->addValue(Attribute, Form, createDIEEntry(Entry));
417}
418
419
Devang Patelc50078e2009-11-21 02:48:08 +0000420/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000421///
Devang Patelc50078e2009-11-21 02:48:08 +0000422void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000423 DIEBlock *Block) {
Chris Lattner352c8e22010-04-05 00:18:22 +0000424 Block->ComputeSize(Asm);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000425 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Devang Patelc50078e2009-11-21 02:48:08 +0000426 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000427}
428
Devang Patelc50078e2009-11-21 02:48:08 +0000429/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000430/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000431void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000432 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000433 if (!V->getCompileUnit().Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000434 return;
435
436 unsigned Line = V->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000437 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
438 V->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000439 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000440 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
441 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000442}
443
Devang Patelc50078e2009-11-21 02:48:08 +0000444/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000445/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000446void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000447 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000448 if (!G->getCompileUnit().Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000449 return;
450
451 unsigned Line = G->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000452 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
453 G->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000454 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000455 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
456 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000457}
Devang Patel318d70d2009-08-31 22:47:13 +0000458
Devang Patelc50078e2009-11-21 02:48:08 +0000459/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000460/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000461void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel318d70d2009-08-31 22:47:13 +0000462 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000463 if (!SP->getCompileUnit().Verify())
Devang Patel318d70d2009-08-31 22:47:13 +0000464 return;
Caroline Tice9da96d82009-09-11 18:25:54 +0000465 // If the line number is 0, don't add it.
466 if (SP->getLineNumber() == 0)
467 return;
468
Devang Patel318d70d2009-08-31 22:47:13 +0000469 unsigned Line = SP->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000470 if (!SP->getContext().Verify())
471 return;
Devang Patele485a5d2010-03-24 21:30:35 +0000472 unsigned FileID = GetOrCreateSourceID(SP->getDirectory(),
473 SP->getFilename());
Devang Patel318d70d2009-08-31 22:47:13 +0000474 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000475 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
476 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel318d70d2009-08-31 22:47:13 +0000477}
478
Devang Patelc50078e2009-11-21 02:48:08 +0000479/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000480/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000481void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000482 // If there is no compile unit specified, don't add a line #.
483 DICompileUnit CU = Ty->getCompileUnit();
Devang Patel9e592492010-03-08 20:52:55 +0000484 if (!CU.Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000485 return;
486
487 unsigned Line = Ty->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000488 if (!Ty->getContext().Verify())
489 return;
490 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
491 Ty->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000492 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000493 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
494 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000495}
496
Devang Patel8287d662009-12-15 19:16:48 +0000497/// addSourceLine - Add location information to specified debug information
498/// entry.
499void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
500 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000501 if (!NS->getCompileUnit().Verify())
Devang Patel8287d662009-12-15 19:16:48 +0000502 return;
503
504 unsigned Line = NS->getLineNumber();
505 StringRef FN = NS->getFilename();
506 StringRef Dir = NS->getDirectory();
507
508 unsigned FileID = GetOrCreateSourceID(Dir, FN);
509 assert(FileID && "Invalid file id");
510 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
511 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
512}
513
Caroline Tice248d5572009-08-31 21:19:37 +0000514/* Byref variables, in Blocks, are declared by the programmer as
515 "SomeType VarName;", but the compiler creates a
516 __Block_byref_x_VarName struct, and gives the variable VarName
517 either the struct, or a pointer to the struct, as its type. This
518 is necessary for various behind-the-scenes things the compiler
519 needs to do with by-reference variables in blocks.
520
521 However, as far as the original *programmer* is concerned, the
522 variable should still have type 'SomeType', as originally declared.
523
524 The following function dives into the __Block_byref_x_VarName
525 struct to find the original type of the variable. This will be
526 passed back to the code generating the type for the Debug
527 Information Entry for the variable 'VarName'. 'VarName' will then
528 have the original type 'SomeType' in its debug information.
529
530 The original type 'SomeType' will be the type of the field named
531 'VarName' inside the __Block_byref_x_VarName struct.
532
533 NOTE: In order for this to not completely fail on the debugger
534 side, the Debug Information Entry for the variable VarName needs to
535 have a DW_AT_location that tells the debugger how to unwind through
536 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patelc50078e2009-11-21 02:48:08 +0000537 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-08-31 21:19:37 +0000538
539/// Find the type the programmer originally declared the variable to be
540/// and return that type.
541///
Devang Patelc50078e2009-11-21 02:48:08 +0000542DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-08-31 21:19:37 +0000543
544 DIType subType = Ty;
545 unsigned tag = Ty.getTag();
546
547 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000548 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000549 subType = DTy.getTypeDerivedFrom();
550 }
551
552 DICompositeType blockStruct = DICompositeType(subType.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000553 DIArray Elements = blockStruct.getTypeArray();
554
Caroline Tice248d5572009-08-31 21:19:37 +0000555 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
556 DIDescriptor Element = Elements.getElement(i);
557 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000558 if (Name == DT.getName())
Caroline Tice248d5572009-08-31 21:19:37 +0000559 return (DT.getTypeDerivedFrom());
560 }
561
562 return Ty;
563}
564
Devang Patelc50078e2009-11-21 02:48:08 +0000565/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000566/// and generate the DWARF information necessary to find the actual variable
567/// given the extra address information encoded in the DIVariable, starting from
568/// the starting location. Add the DWARF information to the die.
569///
Devang Patelc50078e2009-11-21 02:48:08 +0000570void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000571 unsigned Attribute,
572 const MachineLocation &Location) {
573 const DIVariable &VD = DV->getVariable();
574 DIType Ty = VD.getType();
575
576 // Decode the original location, and use that as the start of the byref
577 // variable's location.
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000578 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000579 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000580 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000581
582 if (Location.isReg()) {
583 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000584 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000585 } else {
586 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000587 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
588 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000589 }
590 } else {
591 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000592 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000593 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000594 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
595 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000596 }
597
Devang Patelc50078e2009-11-21 02:48:08 +0000598 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000599 }
600
601 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
602 uint64_t Element = VD.getAddrElement(i);
603
604 if (Element == DIFactory::OpPlus) {
Devang Patelc50078e2009-11-21 02:48:08 +0000605 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
606 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000607 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000608 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000609 } else llvm_unreachable("unknown DIFactory Opcode");
610 }
611
612 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000613 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000614}
615
Caroline Tice248d5572009-08-31 21:19:37 +0000616/* Byref variables, in Blocks, are declared by the programmer as "SomeType
617 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
618 gives the variable VarName either the struct, or a pointer to the struct, as
619 its type. This is necessary for various behind-the-scenes things the
620 compiler needs to do with by-reference variables in Blocks.
621
622 However, as far as the original *programmer* is concerned, the variable
623 should still have type 'SomeType', as originally declared.
624
Devang Patelc50078e2009-11-21 02:48:08 +0000625 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000626 struct to find the original type of the variable, which is then assigned to
627 the variable's Debug Information Entry as its real type. So far, so good.
628 However now the debugger will expect the variable VarName to have the type
629 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbar41716322009-09-19 20:40:05 +0000630 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-08-31 21:19:37 +0000631 pointers and struct to find the actual variable of type SomeType.
632
633 The following function does just that. We start by getting
634 the "normal" location for the variable. This will be the location
635 of either the struct __Block_byref_x_VarName or the pointer to the
636 struct __Block_byref_x_VarName.
637
638 The struct will look something like:
639
640 struct __Block_byref_x_VarName {
641 ... <various fields>
642 struct __Block_byref_x_VarName *forwarding;
643 ... <various other fields>
644 SomeType VarName;
645 ... <maybe more fields>
646 };
647
648 If we are given the struct directly (as our starting point) we
649 need to tell the debugger to:
650
651 1). Add the offset of the forwarding field.
652
Dan Gohmandf1a7ff2010-02-10 16:03:48 +0000653 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000654 struct to use (the real one may have been copied onto the heap).
655
656 3). Add the offset for the field VarName, to find the actual variable.
657
658 If we started with a pointer to the struct, then we need to
659 dereference that pointer first, before the other steps.
660 Translating this into DWARF ops, we will need to append the following
661 to the current location description for the variable:
662
663 DW_OP_deref -- optional, if we start with a pointer
664 DW_OP_plus_uconst <forward_fld_offset>
665 DW_OP_deref
666 DW_OP_plus_uconst <varName_fld_offset>
667
668 That is what this function does. */
669
Devang Patelc50078e2009-11-21 02:48:08 +0000670/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-08-31 21:19:37 +0000671/// provided, and generate the DWARF information necessary to find the
672/// actual Block variable (navigating the Block struct) based on the
673/// starting location. Add the DWARF information to the die. For
674/// more information, read large comment just above here.
675///
Devang Patelc50078e2009-11-21 02:48:08 +0000676void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000677 unsigned Attribute,
678 const MachineLocation &Location) {
Caroline Tice248d5572009-08-31 21:19:37 +0000679 const DIVariable &VD = DV->getVariable();
680 DIType Ty = VD.getType();
681 DIType TmpTy = Ty;
682 unsigned Tag = Ty.getTag();
683 bool isPointer = false;
684
Devang Patel7f75bbe2009-11-25 17:36:49 +0000685 StringRef varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000686
687 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000688 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000689 TmpTy = DTy.getTypeDerivedFrom();
690 isPointer = true;
691 }
692
693 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
694
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000695 // Find the __forwarding field and the variable field in the __Block_byref
696 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000697 DIArray Fields = blockStruct.getTypeArray();
698 DIDescriptor varField = DIDescriptor();
699 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000700
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000701 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
702 DIDescriptor Element = Fields.getElement(i);
703 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000704 StringRef fieldName = DT.getName();
705 if (fieldName == "__forwarding")
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000706 forwardingField = Element;
Devang Patel7f75bbe2009-11-25 17:36:49 +0000707 else if (fieldName == varName)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000708 varField = Element;
709 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000710
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000711 // Get the offsets for the forwarding field and the variable field.
Chris Lattner5df82552010-03-31 06:06:37 +0000712 unsigned forwardingFieldOffset =
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000713 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
Chris Lattner5df82552010-03-31 06:06:37 +0000714 unsigned varFieldOffset =
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000715 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000716
Mike Stump2fd84e22009-09-24 23:21:26 +0000717 // Decode the original location, and use that as the start of the byref
718 // variable's location.
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000719 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000720 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000721 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000722
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000723 if (Location.isReg()) {
724 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000725 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000726 else {
727 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000728 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
729 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000730 }
731 } else {
732 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000733 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000734 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000735 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
736 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000737 }
Caroline Tice248d5572009-08-31 21:19:37 +0000738
Devang Patelc50078e2009-11-21 02:48:08 +0000739 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000740 }
Caroline Tice248d5572009-08-31 21:19:37 +0000741
Mike Stump2fd84e22009-09-24 23:21:26 +0000742 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000743 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000744 if (isPointer)
Devang Patelc50078e2009-11-21 02:48:08 +0000745 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000746
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000747 // Next add the offset for the '__forwarding' field:
748 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
749 // adding the offset if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000750 if (forwardingFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000751 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
752 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000753 }
Caroline Tice248d5572009-08-31 21:19:37 +0000754
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000755 // Now dereference the __forwarding field to get to the real __Block_byref
756 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000757 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000758
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000759 // Now that we've got the real __Block_byref... struct, add the offset
760 // for the variable's field to get to the location of the actual variable:
761 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000762 if (varFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000763 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
764 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000765 }
Caroline Tice248d5572009-08-31 21:19:37 +0000766
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000767 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000768 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000769}
770
Devang Patelc50078e2009-11-21 02:48:08 +0000771/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000772/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000773void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000774 const MachineLocation &Location) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +0000775 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000776 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +0000777 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000778
779 if (Location.isReg()) {
780 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000781 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000782 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000783 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
784 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000785 }
786 } else {
787 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000788 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000789 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000790 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
791 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000792 }
793
Devang Patelc50078e2009-11-21 02:48:08 +0000794 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000795 }
796
Devang Patelc50078e2009-11-21 02:48:08 +0000797 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000798}
799
Devang Patel1a8f9a82009-12-10 19:14:49 +0000800/// addToContextOwner - Add Die into the list of its context owner's children.
801void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel9e592492010-03-08 20:52:55 +0000802 if (Context.isType()) {
Devang Patel1a8f9a82009-12-10 19:14:49 +0000803 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
804 ContextDIE->addChild(Die);
Devang Patel8287d662009-12-15 19:16:48 +0000805 } else if (Context.isNameSpace()) {
806 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode()));
807 ContextDIE->addChild(Die);
Devang Patel1a8f9a82009-12-10 19:14:49 +0000808 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
809 ContextDIE->addChild(Die);
810 else
811 ModuleCU->addDie(Die);
812}
813
Devang Patel7f139c12009-12-10 18:05:33 +0000814/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
815/// given DIType.
816DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
817 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
818 if (TyDIE)
819 return TyDIE;
820
821 // Create new type.
822 TyDIE = new DIE(dwarf::DW_TAG_base_type);
823 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
824 if (Ty.isBasicType())
825 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
826 else if (Ty.isCompositeType())
827 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
828 else {
829 assert(Ty.isDerivedType() && "Unknown kind of DIType");
830 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
831 }
832
Devang Patel1a8f9a82009-12-10 19:14:49 +0000833 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel7f139c12009-12-10 18:05:33 +0000834 return TyDIE;
835}
836
Devang Patelc50078e2009-11-21 02:48:08 +0000837/// addType - Add a new type attribute to the specified entity.
Devang Patelfe0be132009-12-09 18:24:21 +0000838void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9e592492010-03-08 20:52:55 +0000839 if (!Ty.isValid())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000840 return;
841
842 // Check for pre-existence.
Devang Patelfe0be132009-12-09 18:24:21 +0000843 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000844 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000845 if (Entry) {
846 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000847 return;
848 }
849
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000850 // Construct type.
Devang Patel7f139c12009-12-10 18:05:33 +0000851 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000852
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000853 // Set up proxy.
854 Entry = createDIEEntry(Buffer);
855 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
856
Devang Patelc50078e2009-11-21 02:48:08 +0000857 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000858}
859
Devang Patelc50078e2009-11-21 02:48:08 +0000860/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patelfe0be132009-12-09 18:24:21 +0000861void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000862 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000863 StringRef Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000864 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000865 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000866 BTy.getEncoding());
867
868 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000869 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000870 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000871 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000872 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000873}
874
Devang Patelc50078e2009-11-21 02:48:08 +0000875/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patelfe0be132009-12-09 18:24:21 +0000876void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000877 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000878 StringRef Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000879 uint64_t Size = DTy.getSizeInBits() >> 3;
880 unsigned Tag = DTy.getTag();
881
882 // FIXME - Workaround for templates.
883 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
884
885 Buffer.setTag(Tag);
886
887 // Map to main type, void will not have a type.
888 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelfe0be132009-12-09 18:24:21 +0000889 addType(&Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000890
891 // Add name if not anonymous or intermediate type.
Devang Patel76b80672009-11-30 23:56:56 +0000892 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000893 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000894
895 // Add size if non-zero (derived types might be zero-sized.)
896 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000897 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000898
899 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000900 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000901 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000902}
903
Devang Patelc50078e2009-11-21 02:48:08 +0000904/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +0000905void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000906 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000907 StringRef Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000908
909 uint64_t Size = CTy.getSizeInBits() >> 3;
910 unsigned Tag = CTy.getTag();
911 Buffer.setTag(Tag);
912
913 switch (Tag) {
914 case dwarf::DW_TAG_vector_type:
915 case dwarf::DW_TAG_array_type:
Devang Patelfe0be132009-12-09 18:24:21 +0000916 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000917 break;
918 case dwarf::DW_TAG_enumeration_type: {
919 DIArray Elements = CTy.getTypeArray();
920
921 // Add enumerators to enumeration type.
922 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
923 DIE *ElemDie = NULL;
Devang Patel9e592492010-03-08 20:52:55 +0000924 DIDescriptor Enum(Elements.getElement(i).getNode());
925 if (Enum.isEnumerator()) {
926 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000927 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +0000928 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000929 }
930 }
931 break;
932 case dwarf::DW_TAG_subroutine_type: {
933 // Add return type.
934 DIArray Elements = CTy.getTypeArray();
935 DIDescriptor RTy = Elements.getElement(0);
Devang Patelfe0be132009-12-09 18:24:21 +0000936 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000937
938 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +0000939 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000940
941 // Add arguments.
942 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
943 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
944 DIDescriptor Ty = Elements.getElement(i);
Devang Patelfe0be132009-12-09 18:24:21 +0000945 addType(Arg, DIType(Ty.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000946 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000947 }
948 }
949 break;
950 case dwarf::DW_TAG_structure_type:
951 case dwarf::DW_TAG_union_type:
952 case dwarf::DW_TAG_class_type: {
953 // Add elements to structure type.
954 DIArray Elements = CTy.getTypeArray();
955
956 // A forward struct declared type may not have elements available.
Devang Patel9e592492010-03-08 20:52:55 +0000957 unsigned N = Elements.getNumElements();
958 if (N == 0)
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000959 break;
960
961 // Add elements to structure type.
Devang Patel9e592492010-03-08 20:52:55 +0000962 for (unsigned i = 0; i < N; ++i) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000963 DIDescriptor Element = Elements.getElement(i);
964 DIE *ElemDie = NULL;
Devang Patel9e592492010-03-08 20:52:55 +0000965 if (Element.isSubprogram())
Devang Patel814a12c2009-12-14 16:18:45 +0000966 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Devang Patel9e592492010-03-08 20:52:55 +0000967 else if (Element.isVariable()) {
Devang Patel423dfa02010-01-30 01:08:30 +0000968 DIVariable DV(Element.getNode());
969 ElemDie = new DIE(dwarf::DW_TAG_variable);
970 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
971 DV.getName());
972 addType(ElemDie, DV.getType());
973 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
974 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
975 addSourceLine(ElemDie, &DV);
Devang Patel9e592492010-03-08 20:52:55 +0000976 } else if (Element.isDerivedType())
Devang Patelfe0be132009-12-09 18:24:21 +0000977 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patel9e592492010-03-08 20:52:55 +0000978 else
979 continue;
Devang Patelc50078e2009-11-21 02:48:08 +0000980 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000981 }
982
Devang Patel20b32102009-08-27 23:51:51 +0000983 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +0000984 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000985
986 unsigned RLang = CTy.getRunTimeLang();
987 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +0000988 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000989 dwarf::DW_FORM_data1, RLang);
Devang Patel5ae52402010-01-26 21:16:06 +0000990
991 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel9e592492010-03-08 20:52:55 +0000992 if (DIDescriptor(ContainingType.getNode()).isCompositeType())
Devang Patel5ae52402010-01-26 21:16:06 +0000993 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
994 getOrCreateTypeDIE(DIType(ContainingType.getNode())));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000995 break;
996 }
997 default:
998 break;
999 }
1000
1001 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +00001002 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001003 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001004
Devang Pateldc73c372010-01-29 18:34:58 +00001005 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001006 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1007 // Add size if non-zero (derived types might be zero-sized.)
1008 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +00001009 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001010 else {
1011 // Add zero size if it is not a forward declaration.
1012 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001013 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001014 else
Devang Patelc50078e2009-11-21 02:48:08 +00001015 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001016 }
1017
1018 // Add source line info if available.
1019 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001020 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001021 }
1022}
1023
Devang Patelc50078e2009-11-21 02:48:08 +00001024/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1025void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001026 int64_t L = SR.getLo();
1027 int64_t H = SR.getHi();
1028 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1029
Devang Patelc50078e2009-11-21 02:48:08 +00001030 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +00001031 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +00001032 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld3df6972009-12-04 23:10:24 +00001033 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001034
Devang Patelc50078e2009-11-21 02:48:08 +00001035 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001036}
1037
Devang Patelc50078e2009-11-21 02:48:08 +00001038/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +00001039void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001040 DICompositeType *CTy) {
1041 Buffer.setTag(dwarf::DW_TAG_array_type);
1042 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001043 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001044
1045 // Emit derived type.
Devang Patelfe0be132009-12-09 18:24:21 +00001046 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001047 DIArray Elements = CTy->getTypeArray();
1048
Devang Patel1233f912009-11-21 00:31:03 +00001049 // Get an anonymous type for index type.
Devang Patelfe0be132009-12-09 18:24:21 +00001050 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel1233f912009-11-21 00:31:03 +00001051 if (!IdxTy) {
1052 // Construct an anonymous type for index type.
1053 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +00001054 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1055 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +00001056 dwarf::DW_ATE_signed);
Devang Patelfe0be132009-12-09 18:24:21 +00001057 ModuleCU->addDie(IdxTy);
1058 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +00001059 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001060
1061 // Add subranges to array type.
1062 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1063 DIDescriptor Element = Elements.getElement(i);
1064 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001065 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001066 }
1067}
1068
Devang Patelc50078e2009-11-21 02:48:08 +00001069/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel9e592492010-03-08 20:52:55 +00001070DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001071 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel9e592492010-03-08 20:52:55 +00001072 StringRef Name = ETy.getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001073 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel9e592492010-03-08 20:52:55 +00001074 int64_t Value = ETy.getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +00001075 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001076 return Enumerator;
1077}
1078
Devang Patel141e1c42010-01-05 01:46:14 +00001079/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1080/// printer to not emit usual symbol prefix before the symbol name is used then
1081/// return linkage name after skipping this special LLVM prefix.
1082static StringRef getRealLinkageName(StringRef LinkageName) {
1083 char One = '\1';
1084 if (LinkageName.startswith(StringRef(&One, 1)))
1085 return LinkageName.substr(1);
1086 return LinkageName;
1087}
1088
Devang Patelc50078e2009-11-21 02:48:08 +00001089/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patelfe0be132009-12-09 18:24:21 +00001090DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001091 // If the global variable was optmized out then no need to create debug info
1092 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +00001093 if (!GV.getGlobal()) return NULL;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001094 if (GV.getDisplayName().empty()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +00001095
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001096 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +00001097 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001098 GV.getDisplayName());
1099
Devang Patel7f75bbe2009-11-25 17:36:49 +00001100 StringRef LinkageName = GV.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001101 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001102 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001103 getRealLinkageName(LinkageName));
1104
Devang Patelfe0be132009-12-09 18:24:21 +00001105 addType(GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001106 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001107 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1108 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001109
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001110 return GVDie;
1111}
1112
Devang Patelc50078e2009-11-21 02:48:08 +00001113/// createMemberDIE - Create new member DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001114DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001115 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel7f75bbe2009-11-25 17:36:49 +00001116 StringRef Name = DT.getName();
1117 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001118 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001119
Devang Patelfe0be132009-12-09 18:24:21 +00001120 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001121
Devang Patelc50078e2009-11-21 02:48:08 +00001122 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001123
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001124 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001125 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001126
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001127 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001128 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001129
1130 if (Size != FieldSize) {
1131 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001132 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1133 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001134
1135 uint64_t Offset = DT.getOffsetInBits();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001136 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1137 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer631e3e32010-01-07 17:50:57 +00001138 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001139 Offset -= FieldOffset;
1140
1141 // Maybe we need to work from the other end.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001142 if (Asm->getTargetData().isLittleEndian())
1143 Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001144 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001145
Devang Patel7d9fe582009-11-04 22:06:12 +00001146 // Here WD_AT_data_member_location points to the anonymous
1147 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001148 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001149
1150 } else
1151 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001152 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001153
Devang Patel84aba942010-02-03 20:08:48 +00001154 if (DT.getTag() == dwarf::DW_TAG_inheritance
1155 && DT.isVirtual()) {
1156
1157 // For C++, virtual base classes are not at fixed offset. Use following
1158 // expression to extract appropriate offset from vtable.
1159 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1160
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001161 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Devang Patel84aba942010-02-03 20:08:48 +00001162 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1163 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1164 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1165 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1166 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1167 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1168 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1169
1170 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1171 VBaseLocationDie);
1172 } else
1173 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001174
1175 if (DT.isProtected())
Devang Patel188c85d2009-12-03 19:11:07 +00001176 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001177 dwarf::DW_ACCESS_protected);
1178 else if (DT.isPrivate())
Devang Patel188c85d2009-12-03 19:11:07 +00001179 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001180 dwarf::DW_ACCESS_private);
Devang Patel188c85d2009-12-03 19:11:07 +00001181 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1182 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1183 dwarf::DW_ACCESS_public);
1184 if (DT.isVirtual())
1185 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1186 dwarf::DW_VIRTUALITY_virtual);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001187 return MemberDie;
1188}
1189
Devang Patel814a12c2009-12-14 16:18:45 +00001190/// createSubprogramDIE - Create new DIE using SP.
1191DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1192 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1193 if (SPDie)
1194 return SPDie;
1195
1196 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel36cd9f82010-03-02 17:58:15 +00001197 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel0ab194f2010-03-02 01:26:20 +00001198 if (!SP.getName().empty())
1199 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001200
Devang Patel7f75bbe2009-11-25 17:36:49 +00001201 StringRef LinkageName = SP.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001202 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001203 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001204 getRealLinkageName(LinkageName));
1205
Devang Patelc50078e2009-11-21 02:48:08 +00001206 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001207
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001208 // Add prototyped tag, if C or ObjC.
1209 unsigned Lang = SP.getCompileUnit().getLanguage();
1210 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1211 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001212 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001213
1214 // Add Return Type.
Devang Patelc1df8792009-12-03 01:25:38 +00001215 DICompositeType SPTy = SP.getType();
1216 DIArray Args = SPTy.getTypeArray();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001217 unsigned SPTag = SPTy.getTag();
Devang Patel188c85d2009-12-03 19:11:07 +00001218
Devang Patel9e592492010-03-08 20:52:55 +00001219 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelfe0be132009-12-09 18:24:21 +00001220 addType(SPDie, SPTy);
Devang Patelc1df8792009-12-03 01:25:38 +00001221 else
Devang Patelfe0be132009-12-09 18:24:21 +00001222 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patelc1df8792009-12-03 01:25:38 +00001223
Devang Patel188c85d2009-12-03 19:11:07 +00001224 unsigned VK = SP.getVirtuality();
1225 if (VK) {
1226 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001227 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel188c85d2009-12-03 19:11:07 +00001228 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1229 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1230 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel7d707f92010-01-19 06:19:05 +00001231 ContainingTypeMap.insert(std::make_pair(SPDie,
1232 SP.getContainingType().getNode()));
Devang Patel188c85d2009-12-03 19:11:07 +00001233 }
1234
Devang Patel814a12c2009-12-14 16:18:45 +00001235 if (MakeDecl || !SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001236 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001237
1238 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patelc1df8792009-12-03 01:25:38 +00001239 // be handled while processing variables.
1240 DICompositeType SPTy = SP.getType();
1241 DIArray Args = SPTy.getTypeArray();
1242 unsigned SPTag = SPTy.getTag();
1243
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001244 if (SPTag == dwarf::DW_TAG_subroutine_type)
1245 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1246 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc5dc7252010-02-06 01:02:37 +00001247 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1248 addType(Arg, ATy);
1249 if (ATy.isArtificial())
1250 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc50078e2009-11-21 02:48:08 +00001251 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001252 }
1253 }
1254
Devang Patelbf7d00a2010-02-03 19:57:19 +00001255 if (SP.isArtificial())
1256 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1257
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001258 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001259 ModuleCU->insertDIE(SP.getNode(), SPDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001260 return SPDie;
1261}
1262
Devang Patel63397752010-04-01 17:16:48 +00001263/// getUpdatedDbgScope - Find DbgScope assicated with the instruction.
1264/// Update scope hierarchy. Create abstract scope if required.
Devang Patel90a0fe32009-11-10 23:06:00 +00001265DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
Chris Lattner8143ce82010-03-31 05:36:29 +00001266 MDNode *InlinedAt) {
1267 assert(N && "Invalid Scope encoding!");
1268 assert(MI && "Missing machine instruction!");
Devang Patel63397752010-04-01 17:16:48 +00001269 bool isAConcreteScope = InlinedAt != 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00001270
1271 DbgScope *NScope = NULL;
1272
1273 if (InlinedAt)
1274 NScope = DbgScopeMap.lookup(InlinedAt);
1275 else
1276 NScope = DbgScopeMap.lookup(N);
Chris Lattner8143ce82010-03-31 05:36:29 +00001277 assert(NScope && "Unable to find working scope!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001278
1279 if (NScope->getFirstInsn())
1280 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001281
1282 DbgScope *Parent = NULL;
Devang Patel63397752010-04-01 17:16:48 +00001283 if (isAConcreteScope) {
Devang Pateldd7bb432009-10-14 21:08:09 +00001284 DILocation IL(InlinedAt);
Jim Grosbach652b7432009-11-21 23:12:12 +00001285 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel90a0fe32009-11-10 23:06:00 +00001286 IL.getOrigLocation().getNode());
Chris Lattner8143ce82010-03-31 05:36:29 +00001287 assert(Parent && "Unable to find Parent scope!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001288 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001289 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001290 } else if (DIDescriptor(N).isLexicalBlock()) {
1291 DILexicalBlock DB(N);
Devang Patel9e592492010-03-08 20:52:55 +00001292 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1293 NScope->setParent(Parent);
1294 Parent->addScope(NScope);
Devang Pateldd7bb432009-10-14 21:08:09 +00001295 }
Devang Patel6a260102009-10-01 20:31:14 +00001296
Devang Patelf5278f22009-10-27 20:47:17 +00001297 NScope->setFirstInsn(MI);
Devang Patel6a260102009-10-01 20:31:14 +00001298
Devang Patel90a0fe32009-11-10 23:06:00 +00001299 if (!Parent && !InlinedAt) {
Devang Patelce8986f2009-11-11 00:31:36 +00001300 StringRef SPName = DISubprogram(N).getLinkageName();
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001301 if (SPName == Asm->MF->getFunction()->getName())
Devang Patelce8986f2009-11-11 00:31:36 +00001302 CurrentFnDbgScope = NScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00001303 }
Devang Patel6a260102009-10-01 20:31:14 +00001304
Devang Patel63397752010-04-01 17:16:48 +00001305 if (isAConcreteScope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001306 ConcreteScopes[InlinedAt] = NScope;
1307 getOrCreateAbstractScope(N);
1308 }
1309
Devang Patelf5278f22009-10-27 20:47:17 +00001310 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001311}
1312
Devang Patel90a0fe32009-11-10 23:06:00 +00001313DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
Chris Lattner8143ce82010-03-31 05:36:29 +00001314 assert(N && "Invalid Scope encoding!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001315
1316 DbgScope *AScope = AbstractScopes.lookup(N);
1317 if (AScope)
1318 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001319
Devang Patel90a0fe32009-11-10 23:06:00 +00001320 DbgScope *Parent = NULL;
1321
1322 DIDescriptor Scope(N);
1323 if (Scope.isLexicalBlock()) {
1324 DILexicalBlock DB(N);
1325 DIDescriptor ParentDesc = DB.getContext();
Devang Patel9e592492010-03-08 20:52:55 +00001326 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001327 }
1328
1329 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1330
1331 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001332 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001333 AScope->setAbstractScope();
1334 AbstractScopes[N] = AScope;
1335 if (DIDescriptor(N).isSubprogram())
1336 AbstractScopesList.push_back(AScope);
1337 return AScope;
1338}
Devang Patel6a260102009-10-01 20:31:14 +00001339
Devang Patel75c10622010-04-06 23:53:48 +00001340/// isSubprogramContext - Return true if Context is either a subprogram
1341/// or another context nested inside a subprogram.
1342bool isSubprogramContext(MDNode *Context) {
1343 if (!Context)
1344 return false;
1345 DIDescriptor D(Context);
1346 if (D.isSubprogram())
1347 return true;
1348 if (D.isType())
1349 return isSubprogramContext(DIType(Context).getContext().getNode());
1350 return false;
1351}
1352
Jim Grosbach652b7432009-11-21 23:12:12 +00001353/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-11-21 02:48:08 +00001354/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1355/// If there are global variables in this scope then create and insert
1356/// DIEs for these variables.
1357DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001358 DIE *SPDie = ModuleCU->getDIE(SPNode);
1359 assert(SPDie && "Unable to find subprogram DIE!");
1360 DISubprogram SP(SPNode);
Chris Lattnerdd21da82010-03-13 07:26:18 +00001361
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001362 // There is not any need to generate specification DIE for a function
1363 // defined at compile unit level. If a function is defined inside another
1364 // function then gdb prefers the definition at top level and but does not
1365 // expect specification DIE in parent function. So avoid creating
1366 // specification DIE for a function defined inside a function.
1367 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
Devang Patel75c10622010-04-06 23:53:48 +00001368 !SP.getContext().isFile() &&
1369 !isSubprogramContext(SP.getContext().getNode())) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001370 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1371
1372 // Add arguments.
1373 DICompositeType SPTy = SP.getType();
1374 DIArray Args = SPTy.getTypeArray();
1375 unsigned SPTag = SPTy.getTag();
1376 if (SPTag == dwarf::DW_TAG_subroutine_type)
1377 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1378 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1379 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1380 addType(Arg, ATy);
1381 if (ATy.isArtificial())
1382 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1383 SPDie->addChild(Arg);
1384 }
1385 DIE *SPDeclDie = SPDie;
1386 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1387 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1388 SPDeclDie);
1389 ModuleCU->addDie(SPDie);
1390 }
1391
1392 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1393 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1394 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1395 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1396 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1397 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1398 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelc5dc7252010-02-06 01:02:37 +00001399
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001400 if (!DISubprogram(SPNode).isLocalToUnit())
1401 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1402
1403 return SPDie;
Devang Patel90a0fe32009-11-10 23:06:00 +00001404}
1405
Jim Grosbach652b7432009-11-21 23:12:12 +00001406/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001407/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1408DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Chris Lattner855e8f52010-03-09 01:58:53 +00001409 MCSymbol *Start = Scope->getStartLabel();
1410 MCSymbol *End = Scope->getEndLabel();
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001411 if (Start == 0 || End == 0) return 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00001412
Chris Lattner855e8f52010-03-09 01:58:53 +00001413 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1414 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattner5e423432010-03-09 01:51:43 +00001415
Devang Patel90a0fe32009-11-10 23:06:00 +00001416 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1417 if (Scope->isAbstractScope())
1418 return ScopeDIE;
1419
Devang Patelc50078e2009-11-21 02:48:08 +00001420 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001421 Start ? Start : Asm->GetTempSymbol("func_begin",
1422 Asm->getFunctionNumber()));
Devang Patelc50078e2009-11-21 02:48:08 +00001423 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001424 End ? End : Asm->GetTempSymbol("func_end",Asm->getFunctionNumber()));
Devang Patel90a0fe32009-11-10 23:06:00 +00001425
1426 return ScopeDIE;
1427}
1428
Devang Patelc50078e2009-11-21 02:48:08 +00001429/// constructInlinedScopeDIE - This scope represents inlined body of
1430/// a function. Construct DIE to represent this concrete inlined copy
1431/// of the function.
1432DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Chris Lattner855e8f52010-03-09 01:58:53 +00001433 MCSymbol *StartLabel = Scope->getStartLabel();
1434 MCSymbol *EndLabel = Scope->getEndLabel();
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001435 if (StartLabel == 0 || EndLabel == 0) return 0;
Chris Lattner4ba1da92010-03-09 04:48:35 +00001436
Chris Lattner855e8f52010-03-09 01:58:53 +00001437 assert(StartLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001438 "Invalid starting label for an inlined scope!");
Chris Lattner855e8f52010-03-09 01:58:53 +00001439 assert(EndLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001440 "Invalid end label for an inlined scope!");
Devang Patel9e592492010-03-08 20:52:55 +00001441 if (!Scope->getScopeNode())
Devang Patelbe149872010-03-08 19:20:38 +00001442 return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001443 DIScope DS(Scope->getScopeNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001444 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1445
1446 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001447 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Chris Lattner8143ce82010-03-31 05:36:29 +00001448 assert(OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001449 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001450 dwarf::DW_FORM_ref4, OriginDIE);
1451
Chris Lattneread58652010-03-09 00:31:02 +00001452 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattner855e8f52010-03-09 01:58:53 +00001453 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel90a0fe32009-11-10 23:06:00 +00001454
1455 InlinedSubprogramDIEs.insert(OriginDIE);
1456
1457 // Track the start label for this inlined function.
Devang Patel7d707f92010-01-19 06:19:05 +00001458 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel90a0fe32009-11-10 23:06:00 +00001459 I = InlineInfo.find(InlinedSP.getNode());
1460
1461 if (I == InlineInfo.end()) {
Chris Lattneread58652010-03-09 00:31:02 +00001462 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartLabel,
Jim Grosbachb23f2422009-11-22 19:20:36 +00001463 ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001464 InlinedSPNodes.push_back(InlinedSP.getNode());
1465 } else
Chris Lattneread58652010-03-09 00:31:02 +00001466 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001467
Devang Patel90a0fe32009-11-10 23:06:00 +00001468 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001469 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1470 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001471
1472 return ScopeDIE;
1473}
1474
Devang Patelc50078e2009-11-21 02:48:08 +00001475
1476/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelfe0be132009-12-09 18:24:21 +00001477DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001478 // Get the descriptor.
1479 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001480 StringRef Name = VD.getName();
1481 if (Name.empty())
Devang Patelab9a0682009-11-13 02:25:26 +00001482 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001483
1484 // Translate tag to proper Dwarf tag. The result variable is dropped for
1485 // now.
1486 unsigned Tag;
1487 switch (VD.getTag()) {
1488 case dwarf::DW_TAG_return_variable:
1489 return NULL;
1490 case dwarf::DW_TAG_arg_variable:
1491 Tag = dwarf::DW_TAG_formal_parameter;
1492 break;
1493 case dwarf::DW_TAG_auto_variable: // fall thru
1494 default:
1495 Tag = dwarf::DW_TAG_variable;
1496 break;
1497 }
1498
1499 // Define variable debug information entry.
1500 DIE *VariableDie = new DIE(Tag);
1501
1502
1503 DIE *AbsDIE = NULL;
1504 if (DbgVariable *AV = DV->getAbstractVariable())
1505 AbsDIE = AV->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001506
Devang Patel90a0fe32009-11-10 23:06:00 +00001507 if (AbsDIE) {
1508 DIScope DS(Scope->getScopeNode());
1509 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001510 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001511 (void) OriginSPDIE;
Chris Lattner8143ce82010-03-31 05:36:29 +00001512 assert(OriginSPDIE && "Unable to find Origin DIE for the SP!");
Devang Patel90a0fe32009-11-10 23:06:00 +00001513 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
Chris Lattner8143ce82010-03-31 05:36:29 +00001514 assert(AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001515 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001516 dwarf::DW_FORM_ref4, AbsDIE);
1517 }
1518 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001519 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1520 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001521
1522 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001523 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001524 // addresses instead.
1525 if (VD.isBlockByrefVariable())
Devang Patelfe0be132009-12-09 18:24:21 +00001526 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001527 else
Devang Patelfe0be132009-12-09 18:24:21 +00001528 addType(VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001529 }
1530
1531 // Add variable address.
1532 if (!Scope->isAbstractScope()) {
Devang Patel14da02f2010-03-15 18:33:46 +00001533 // Check if variable is described by DBG_VALUE instruction.
1534 if (const MachineInstr *DbgValueInsn = DV->getDbgValue()) {
1535 if (DbgValueInsn->getNumOperands() == 3) {
1536 // FIXME : Handle getNumOperands != 3
1537 if (DbgValueInsn->getOperand(0).getType()
1538 == MachineOperand::MO_Register
1539 && DbgValueInsn->getOperand(0).getReg()) {
1540 MachineLocation Location;
1541 Location.set(DbgValueInsn->getOperand(0).getReg());
1542 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001543 if (MCSymbol *VS = DV->getDbgValueLabel())
1544 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1545 VS);
Devang Patel14da02f2010-03-15 18:33:46 +00001546 } else if (DbgValueInsn->getOperand(0).getType() ==
1547 MachineOperand::MO_Immediate) {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001548 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel14da02f2010-03-15 18:33:46 +00001549 unsigned Imm = DbgValueInsn->getOperand(0).getImm();
1550 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
1551 addBlock(VariableDie, dwarf::DW_AT_const_value, 0, Block);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001552 if (MCSymbol *VS = DV->getDbgValueLabel())
1553 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1554 VS);
Bill Wendling989a27e2010-04-05 22:59:21 +00001555 } else if (DbgValueInsn->getOperand(0).getType() ==
1556 MachineOperand::MO_FPImmediate) {
1557 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1558 APFloat FPImm = DbgValueInsn->getOperand(0).getFPImm()->getValueAPF();
1559
1560 // Get the raw data form of the floating point.
1561 const APInt FltVal = FPImm.bitcastToAPInt();
1562 const char *FltPtr = (const char*)FltVal.getRawData();
1563
John McCall6fbbcc872010-04-06 23:35:53 +00001564 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Bill Wendling989a27e2010-04-05 22:59:21 +00001565 bool LittleEndian = Asm->getTargetData().isLittleEndian();
1566 int Incr = (LittleEndian ? 1 : -1);
1567 int Start = (LittleEndian ? 0 : NumBytes - 1);
1568 int Stop = (LittleEndian ? NumBytes : -1);
1569
1570 // Output the constant to DWARF one byte at a time.
1571 for (; Start != Stop; Start += Incr)
1572 addUInt(Block, 0, dwarf::DW_FORM_data1,
1573 (unsigned char)0xFF & FltPtr[Start]);
1574
1575 addBlock(VariableDie, dwarf::DW_AT_const_value, 0, Block);
1576
1577 if (MCSymbol *VS = DV->getDbgValueLabel())
1578 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1579 VS);
Devang Patel14da02f2010-03-15 18:33:46 +00001580 } else {
1581 //FIXME : Handle other operand types.
1582 delete VariableDie;
1583 return NULL;
1584 }
1585 }
1586 } else {
1587 MachineLocation Location;
1588 unsigned FrameReg;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001589 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1590 int Offset = RI->getFrameIndexReference(*Asm->MF, DV->getFrameIndex(),
Chris Lattner5df82552010-03-31 06:06:37 +00001591 FrameReg);
Devang Patel14da02f2010-03-15 18:33:46 +00001592 Location.set(FrameReg, Offset);
1593
1594 if (VD.hasComplexAddress())
1595 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1596 else if (VD.isBlockByrefVariable())
1597 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1598 else
1599 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1600 }
Devang Patel90a0fe32009-11-10 23:06:00 +00001601 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001602
1603 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1604 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001605 DV->setDIE(VariableDie);
1606 return VariableDie;
1607
1608}
Devang Patelc50078e2009-11-21 02:48:08 +00001609
Devang Patelec13b4f2009-11-24 01:14:22 +00001610void DwarfDebug::addPubTypes(DISubprogram SP) {
1611 DICompositeType SPTy = SP.getType();
1612 unsigned SPTag = SPTy.getTag();
1613 if (SPTag != dwarf::DW_TAG_subroutine_type)
1614 return;
1615
1616 DIArray Args = SPTy.getTypeArray();
Devang Patelec13b4f2009-11-24 01:14:22 +00001617 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1618 DIType ATy(Args.getElement(i).getNode());
Devang Patel9e592492010-03-08 20:52:55 +00001619 if (!ATy.isValid())
Devang Patelec13b4f2009-11-24 01:14:22 +00001620 continue;
1621 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel9e592492010-03-08 20:52:55 +00001622 if (DIDescriptor(CATy.getNode()).Verify() && !CATy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001623 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1624 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1625 }
1626 }
1627}
1628
Devang Patelc50078e2009-11-21 02:48:08 +00001629/// constructScopeDIE - Construct a DIE for this scope.
1630DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel9e592492010-03-08 20:52:55 +00001631 if (!Scope || !Scope->getScopeNode())
1632 return NULL;
1633
1634 DIScope DS(Scope->getScopeNode());
1635 DIE *ScopeDIE = NULL;
1636 if (Scope->getInlinedAt())
1637 ScopeDIE = constructInlinedScopeDIE(Scope);
1638 else if (DS.isSubprogram()) {
1639 if (Scope->isAbstractScope())
1640 ScopeDIE = ModuleCU->getDIE(DS.getNode());
1641 else
1642 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
1643 }
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001644 else
Devang Patel9e592492010-03-08 20:52:55 +00001645 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00001646 if (!ScopeDIE) return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001647
Devang Patel90a0fe32009-11-10 23:06:00 +00001648 // Add variables to scope.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001649 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel90a0fe32009-11-10 23:06:00 +00001650 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelfe0be132009-12-09 18:24:21 +00001651 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach652b7432009-11-21 23:12:12 +00001652 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001653 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001654 }
1655
1656 // Add nested scopes.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001657 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel90a0fe32009-11-10 23:06:00 +00001658 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1659 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001660 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001661 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001662 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001663 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001664
1665 if (DS.isSubprogram())
1666 addPubTypes(DISubprogram(DS.getNode()));
1667
1668 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001669}
1670
Bill Wendlingf5839192009-05-20 23:19:06 +00001671/// GetOrCreateSourceID - Look up the source id with the given directory and
1672/// source file names. If none currently exists, create a new id and insert it
1673/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1674/// maps as well.
Chris Lattner5df82552010-03-31 06:06:37 +00001675unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Bill Wendlingf5839192009-05-20 23:19:06 +00001676 unsigned DId;
1677 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1678 if (DI != DirectoryIdMap.end()) {
1679 DId = DI->getValue();
1680 } else {
1681 DId = DirectoryNames.size() + 1;
1682 DirectoryIdMap[DirName] = DId;
1683 DirectoryNames.push_back(DirName);
1684 }
1685
1686 unsigned FId;
1687 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1688 if (FI != SourceFileIdMap.end()) {
1689 FId = FI->getValue();
1690 } else {
1691 FId = SourceFileNames.size() + 1;
1692 SourceFileIdMap[FileName] = FId;
1693 SourceFileNames.push_back(FileName);
1694 }
1695
1696 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1697 SourceIdMap.find(std::make_pair(DId, FId));
1698 if (SI != SourceIdMap.end())
1699 return SI->second;
1700
1701 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1702 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1703 SourceIds.push_back(std::make_pair(DId, FId));
1704
1705 return SrcId;
1706}
1707
Devang Patel8287d662009-12-15 19:16:48 +00001708/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1709DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1710 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1711 if (NDie)
1712 return NDie;
1713 NDie = new DIE(dwarf::DW_TAG_namespace);
1714 ModuleCU->insertDIE(NS.getNode(), NDie);
1715 if (!NS.getName().empty())
1716 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1717 addSourceLine(NDie, &NS);
1718 addToContextOwner(NDie, NS.getContext());
1719 return NDie;
1720}
1721
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001722void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001723 DICompileUnit DIUnit(N);
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001724 // Use first compile unit marked as isMain as the compile unit for this
1725 // module.
1726 if (ModuleCU || !DIUnit.isMain())
1727 return;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001728 StringRef FN = DIUnit.getFilename();
1729 StringRef Dir = DIUnit.getDirectory();
Devang Patelaaf012e2009-09-29 18:40:58 +00001730 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001731
1732 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelc50078e2009-11-21 02:48:08 +00001733 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001734 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001735 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001736 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001737 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Chris Lattner66143d22010-04-04 22:59:04 +00001738 addLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, TextSectionSym);
Devang Patel7ac39832010-03-22 23:11:36 +00001739 addLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerb93558d2010-04-04 19:25:43 +00001740 Asm->GetTempSymbol("text_end"));
Devang Patel7ac39832010-03-22 23:11:36 +00001741 // DW_AT_stmt_list is a offset of line number information for this
1742 // compile unit in debug_line section. It is always zero when only one
1743 // compile unit is emitted in one object file.
1744 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Bill Wendlingf5839192009-05-20 23:19:06 +00001745
Devang Patel7f75bbe2009-11-25 17:36:49 +00001746 if (!Dir.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001747 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001748 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001749 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001750
Devang Patel7f75bbe2009-11-25 17:36:49 +00001751 StringRef Flags = DIUnit.getFlags();
1752 if (!Flags.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001753 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001754
1755 unsigned RVer = DIUnit.getRunTimeVersion();
1756 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001757 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001758 dwarf::DW_FORM_data1, RVer);
1759
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001760 assert(!ModuleCU &&
1761 "ModuleCU assigned since the top of constructCompileUnit");
1762 ModuleCU = new CompileUnit(ID, Die);
Bill Wendlingf5839192009-05-20 23:19:06 +00001763}
1764
Devang Patelc50078e2009-11-21 02:48:08 +00001765void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001766 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001767
Devang Patel0c03f062009-09-04 23:59:07 +00001768 // If debug information is malformed then ignore it.
1769 if (DI_GV.Verify() == false)
1770 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001771
1772 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001773 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel166f8432009-06-26 01:49:18 +00001774 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001775
Devang Patelfe0be132009-12-09 18:24:21 +00001776 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel6d479632009-12-10 23:25:41 +00001777 if (!VariableDie)
1778 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001779
Bill Wendlingf5839192009-05-20 23:19:06 +00001780 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001781 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001782
1783 // Add to context owner.
Devang Pateldcc0dce2010-01-15 01:12:22 +00001784 DIDescriptor GVContext = DI_GV.getContext();
1785 // Do not create specification DIE if context is either compile unit
1786 // or a subprogram.
Chris Lattner5df82552010-03-31 06:06:37 +00001787 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel75c10622010-04-06 23:53:48 +00001788 !GVContext.isFile() &&
1789 !isSubprogramContext(GVContext.getNode())) {
Devang Patel8287d662009-12-15 19:16:48 +00001790 // Create specification DIE.
1791 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1792 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1793 dwarf::DW_FORM_ref4, VariableDie);
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001794 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel8287d662009-12-15 19:16:48 +00001795 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001796 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001797 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001798 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patela0a98582010-02-09 01:58:33 +00001799 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel8287d662009-12-15 19:16:48 +00001800 ModuleCU->addDie(VariableSpecDIE);
1801 } else {
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00001802 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel8287d662009-12-15 19:16:48 +00001803 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001804 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001805 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001806 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1807 }
Devang Pateldcc0dce2010-01-15 01:12:22 +00001808 addToContextOwner(VariableDie, GVContext);
Devang Patel1a8f9a82009-12-10 19:14:49 +00001809
Bill Wendlingf5839192009-05-20 23:19:06 +00001810 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001811 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001812
1813 DIType GTy = DI_GV.getType();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001814 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001815 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
Chris Lattner8143ce82010-03-31 05:36:29 +00001816 assert(Entry && "Missing global type!");
Devang Patelec13b4f2009-11-24 01:14:22 +00001817 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1818 }
Devang Patel166f8432009-06-26 01:49:18 +00001819 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001820}
1821
Devang Patelc50078e2009-11-21 02:48:08 +00001822void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001823 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001824
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001825 // Check for pre-existence.
1826 if (ModuleCU->getDIE(N))
1827 return;
1828
Bill Wendlingf5839192009-05-20 23:19:06 +00001829 if (!SP.isDefinition())
1830 // This is a method declaration which will be handled while constructing
1831 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001832 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001833
Stuart Hastings0f5779e2010-04-06 21:38:29 +00001834 DIE *SubprogramDie = createSubprogramDIE(SP);
1835
1836 // Add to map.
1837 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001838
1839 // Add to context owner.
Devang Patel8287d662009-12-15 19:16:48 +00001840 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patelde2d3682009-12-08 23:21:45 +00001841
Bill Wendlingf5839192009-05-20 23:19:06 +00001842 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001843 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001844
Devang Patel166f8432009-06-26 01:49:18 +00001845 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001846}
1847
Devang Patelc50078e2009-11-21 02:48:08 +00001848/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001849/// content. Create global DIEs and emit initial debug info sections.
1850/// This is inovked by the target AsmPrinter.
Chris Lattner75e113c2010-04-04 07:48:20 +00001851void DwarfDebug::beginModule(Module *M) {
Devang Patelfda766d2009-07-30 18:56:46 +00001852 DebugInfoFinder DbgFinder;
1853 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001854
Chris Lattnera52b6172010-04-05 02:19:28 +00001855 bool HasDebugInfo = false;
1856
1857 // Scan all the compile-units to see if there are any marked as the main unit.
1858 // if not, we do not generate debug info.
1859 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1860 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1861 if (DICompileUnit(*I).isMain()) {
1862 HasDebugInfo = true;
1863 break;
1864 }
1865 }
1866
1867 if (!HasDebugInfo) return;
1868
1869 // Tell MMI that we have debug info.
1870 MMI->setDebugInfoAvailability(true);
1871
Chris Lattner706afc02010-04-04 23:17:54 +00001872 // Emit initial sections.
Chris Lattnera52b6172010-04-05 02:19:28 +00001873 EmitSectionLabels();
Chris Lattner706afc02010-04-04 23:17:54 +00001874
Bill Wendlingf5839192009-05-20 23:19:06 +00001875 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001876 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1877 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001878 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001879
Devang Patel90a0fe32009-11-10 23:06:00 +00001880 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001881 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1882 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001883 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001884
Devang Patel1a8f9a82009-12-10 19:14:49 +00001885 // Create DIEs for each global variable.
1886 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1887 E = DbgFinder.global_variable_end(); I != E; ++I)
1888 constructGlobalVariableDIE(*I);
1889
Bill Wendlingf5839192009-05-20 23:19:06 +00001890 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001891 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001892
1893 // Print out .file directives to specify files for .loc directives. These are
1894 // printed out early so that they precede any .loc directives.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00001895 if (Asm->MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001896 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1897 // Remember source id starts at 1.
1898 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerad653482010-01-22 22:09:00 +00001899 // FIXME: don't use sys::path for this! This should not depend on the
1900 // host.
Bill Wendlingf5839192009-05-20 23:19:06 +00001901 sys::Path FullPath(getSourceDirectoryName(Id.first));
1902 bool AppendOk =
1903 FullPath.appendComponent(getSourceFileName(Id.second));
1904 assert(AppendOk && "Could not append filename to directory!");
1905 AppendOk = false;
Chris Lattner59be4ea2010-01-25 18:58:59 +00001906 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001907 }
1908 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001909}
1910
Devang Patelc50078e2009-11-21 02:48:08 +00001911/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001912///
Devang Patelc50078e2009-11-21 02:48:08 +00001913void DwarfDebug::endModule() {
Bill Wendlingce3c6252010-04-07 09:28:04 +00001914 if (!ModuleCU) return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001915
Devang Patel90a0fe32009-11-10 23:06:00 +00001916 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1917 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1918 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1919 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001920 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001921 }
1922
Devang Patel7d707f92010-01-19 06:19:05 +00001923 for (DenseMap<DIE *, MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel188c85d2009-12-03 19:11:07 +00001924 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1925 DIE *SPDie = CI->first;
1926 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1927 if (!N) continue;
1928 DIE *NDie = ModuleCU->getDIE(N);
1929 if (!NDie) continue;
1930 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel188c85d2009-12-03 19:11:07 +00001931 }
1932
Bill Wendlingf5839192009-05-20 23:19:06 +00001933 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001934 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattnerb93558d2010-04-04 19:25:43 +00001935 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Chris Lattner73266f92009-08-19 05:49:37 +00001936 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattnerb93558d2010-04-04 19:25:43 +00001937 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Bill Wendlingf5839192009-05-20 23:19:06 +00001938
1939 // End text sections.
1940 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001941 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattnerb93558d2010-04-04 19:25:43 +00001942 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Bill Wendlingf5839192009-05-20 23:19:06 +00001943 }
1944
1945 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001946 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001947
1948 // Emit function debug frame information
1949 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1950 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001951 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001952
1953 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001954 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001955
1956 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001957 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001958
1959 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00001960 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00001961
1962 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00001963 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00001964
1965 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00001966 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00001967
Devang Patelec13b4f2009-11-24 01:14:22 +00001968 // Emit info into a debug pubtypes section.
1969 emitDebugPubTypes();
1970
Bill Wendlingf5839192009-05-20 23:19:06 +00001971 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00001972 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00001973
1974 // Emit info into a debug aranges section.
1975 EmitDebugARanges();
1976
1977 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00001978 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00001979
1980 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00001981 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001982
1983 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00001984 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001985
Chris Lattner0f093f82010-03-13 02:17:42 +00001986 // Emit info into a debug str section.
1987 emitDebugStr();
1988
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001989 delete ModuleCU;
1990 ModuleCU = NULL; // Reset for the next Module, if any.
Bill Wendlingf5839192009-05-20 23:19:06 +00001991}
1992
Devang Patel90a0fe32009-11-10 23:06:00 +00001993/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00001994DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1995 unsigned FrameIdx,
Chris Lattnerb9692a72010-04-02 19:42:39 +00001996 DebugLoc ScopeLoc) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001997
1998 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1999 if (AbsDbgVariable)
2000 return AbsDbgVariable;
2001
Chris Lattnerb9692a72010-04-02 19:42:39 +00002002 LLVMContext &Ctx = Var.getNode()->getContext();
2003 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel90a0fe32009-11-10 23:06:00 +00002004 if (!Scope)
2005 return NULL;
2006
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002007 AbsDbgVariable = new DbgVariable(Var, FrameIdx,
2008 NULL /* No more-abstract variable*/);
Devang Patelc50078e2009-11-21 02:48:08 +00002009 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00002010 AbstractVariables[Var.getNode()] = AbsDbgVariable;
2011 return AbsDbgVariable;
2012}
2013
Devang Patel14da02f2010-03-15 18:33:46 +00002014/// findAbstractVariable - Find abstract variable, if any, associated with Var.
2015/// FIXME : Refactor findAbstractVariable.
2016DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2017 const MachineInstr *MI,
Chris Lattnerb9692a72010-04-02 19:42:39 +00002018 DebugLoc ScopeLoc) {
Devang Patel14da02f2010-03-15 18:33:46 +00002019
2020 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
2021 if (AbsDbgVariable)
2022 return AbsDbgVariable;
2023
Chris Lattnerb9692a72010-04-02 19:42:39 +00002024 LLVMContext &Ctx = Var.getNode()->getContext();
2025 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Devang Patel14da02f2010-03-15 18:33:46 +00002026 if (!Scope)
2027 return NULL;
2028
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002029 AbsDbgVariable = new DbgVariable(Var, MI,
Devang Patel14da02f2010-03-15 18:33:46 +00002030 NULL /* No more-abstract variable*/);
2031 Scope->addVariable(AbsDbgVariable);
2032 AbstractVariables[Var.getNode()] = AbsDbgVariable;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002033 DbgValueStartMap[MI] = AbsDbgVariable;
Devang Patel14da02f2010-03-15 18:33:46 +00002034 return AbsDbgVariable;
2035}
2036
Devang Patelc50078e2009-11-21 02:48:08 +00002037/// collectVariableInfo - Populate DbgScope entries with variables' info.
2038void DwarfDebug::collectVariableInfo() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002039 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Chris Lattnerb9692a72010-04-02 19:42:39 +00002040
Devang Patel84139992009-10-06 01:26:37 +00002041 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2042 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2043 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel0a0ea052010-01-22 22:52:10 +00002044 MDNode *Var = VI->first;
Devang Patel90a0fe32009-11-10 23:06:00 +00002045 if (!Var) continue;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002046 DIVariable DV(Var);
2047 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00002048
Chris Lattnerb9692a72010-04-02 19:42:39 +00002049 DbgScope *Scope = 0;
2050 if (MDNode *IA = VP.second.getInlinedAt(Ctx))
2051 Scope = ConcreteScopes.lookup(IA);
2052 if (Scope == 0)
2053 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2054
Devang Patelbad42262009-11-10 23:20:04 +00002055 // If variable scope is not found then skip this variable.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002056 if (Scope == 0)
Devang Patelbad42262009-11-10 23:20:04 +00002057 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00002058
Chris Lattnerb9692a72010-04-02 19:42:39 +00002059 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, VP.second);
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002060 DbgVariable *RegVar = new DbgVariable(DV, VP.first, AbsDbgVariable);
Devang Patelc50078e2009-11-21 02:48:08 +00002061 Scope->addVariable(RegVar);
Devang Patel84139992009-10-06 01:26:37 +00002062 }
Devang Patel14da02f2010-03-15 18:33:46 +00002063
2064 // Collect variable information from DBG_VALUE machine instructions;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002065 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel14da02f2010-03-15 18:33:46 +00002066 I != E; ++I) {
2067 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2068 II != IE; ++II) {
2069 const MachineInstr *MInsn = II;
Chris Lattner202f10b2010-03-31 05:39:57 +00002070 if (!MInsn->isDebugValue())
Devang Patel14da02f2010-03-15 18:33:46 +00002071 continue;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002072
Devang Patel14da02f2010-03-15 18:33:46 +00002073 // FIXME : Lift this restriction.
2074 if (MInsn->getNumOperands() != 3)
2075 continue;
Chris Lattner0b44e492010-03-31 03:34:40 +00002076 DIVariable DV((MDNode*)(MInsn->getOperand(MInsn->getNumOperands()
2077 - 1).getMetadata()));
Devang Patel14da02f2010-03-15 18:33:46 +00002078 if (DV.getTag() == dwarf::DW_TAG_arg_variable) {
2079 // FIXME Handle inlined subroutine arguments.
2080 DbgVariable *ArgVar = new DbgVariable(DV, MInsn, NULL);
2081 CurrentFnDbgScope->addVariable(ArgVar);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002082 DbgValueStartMap[MInsn] = ArgVar;
Devang Patel14da02f2010-03-15 18:33:46 +00002083 continue;
2084 }
2085
2086 DebugLoc DL = MInsn->getDebugLoc();
2087 if (DL.isUnknown()) continue;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002088 DbgScope *Scope = 0;
2089 if (MDNode *IA = DL.getInlinedAt(Ctx))
2090 Scope = ConcreteScopes.lookup(IA);
2091 if (Scope == 0)
2092 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2093
Devang Patel14da02f2010-03-15 18:33:46 +00002094 // If variable scope is not found then skip this variable.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002095 if (Scope == 0)
Devang Patel14da02f2010-03-15 18:33:46 +00002096 continue;
2097
Chris Lattnerb9692a72010-04-02 19:42:39 +00002098 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn, DL);
Devang Patel14da02f2010-03-15 18:33:46 +00002099 DbgVariable *RegVar = new DbgVariable(DV, MInsn, AbsDbgVariable);
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002100 DbgValueStartMap[MInsn] = RegVar;
Devang Patel14da02f2010-03-15 18:33:46 +00002101 Scope->addVariable(RegVar);
2102 }
2103 }
Devang Patel84139992009-10-06 01:26:37 +00002104}
2105
Devang Patelabc2b352010-03-29 17:20:31 +00002106/// beginScope - Process beginning of a scope.
2107void DwarfDebug::beginScope(const MachineInstr *MI) {
Devang Patelabc2b352010-03-29 17:20:31 +00002108 // Check location.
2109 DebugLoc DL = MI->getDebugLoc();
2110 if (DL.isUnknown())
2111 return;
Devang Patelabc2b352010-03-29 17:20:31 +00002112
2113 // Check and update last known location info.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002114 if (DL == PrevInstLoc)
2115 return;
2116
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002117 MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Chris Lattnerb9692a72010-04-02 19:42:39 +00002118
2119 // FIXME: Should only verify each scope once!
2120 if (!DIScope(Scope).Verify())
Devang Patelabc2b352010-03-29 17:20:31 +00002121 return;
Devang Patelabc2b352010-03-29 17:20:31 +00002122
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002123 // DBG_VALUE instruction establishes new value.
Chris Lattner202f10b2010-03-31 05:39:57 +00002124 if (MI->isDebugValue()) {
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002125 DenseMap<const MachineInstr *, DbgVariable *>::iterator DI
2126 = DbgValueStartMap.find(MI);
2127 if (DI != DbgValueStartMap.end()) {
Chris Lattnerb9692a72010-04-02 19:42:39 +00002128 MCSymbol *Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2129 PrevInstLoc = DL;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002130 DI->second->setDbgValueLabel(Label);
2131 }
Devang Patel98ca2372010-03-30 18:07:00 +00002132 return;
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002133 }
2134
Devang Patelabc2b352010-03-29 17:20:31 +00002135 // Emit a label to indicate location change. This is used for line
2136 // table even if this instruction does start a new scope.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002137 MCSymbol *Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2138 PrevInstLoc = DL;
Devang Patelabc2b352010-03-29 17:20:31 +00002139
2140 // update DbgScope if this instruction starts a new scope.
Devang Patel393a46d2009-10-06 01:50:42 +00002141 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
2142 if (I == DbgScopeBeginMap.end())
2143 return;
Devang Patelabc2b352010-03-29 17:20:31 +00002144
Dan Gohman8d34f972009-11-23 21:30:55 +00002145 ScopeVector &SD = I->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00002146 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00002147 SDI != SDE; ++SDI)
Chris Lattner597b0fc2010-03-09 04:54:43 +00002148 (*SDI)->setStartLabel(Label);
Devang Patel393a46d2009-10-06 01:50:42 +00002149}
2150
Devang Patelc50078e2009-11-21 02:48:08 +00002151/// endScope - Process end of a scope.
2152void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patelabc2b352010-03-29 17:20:31 +00002153 // Ignore DBG_VALUE instruction.
Chris Lattner202f10b2010-03-31 05:39:57 +00002154 if (MI->isDebugValue())
Devang Patelabc2b352010-03-29 17:20:31 +00002155 return;
2156
2157 // Check location.
2158 DebugLoc DL = MI->getDebugLoc();
2159 if (DL.isUnknown())
2160 return;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002161
Devang Patelabc2b352010-03-29 17:20:31 +00002162 // Emit a label and update DbgScope if this instruction ends a scope.
Devang Patel393a46d2009-10-06 01:50:42 +00002163 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patelf4348892009-10-06 03:15:38 +00002164 if (I == DbgScopeEndMap.end())
Devang Patel393a46d2009-10-06 01:50:42 +00002165 return;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002166
Chris Lattner54e56f22010-03-14 08:36:50 +00002167 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner855e8f52010-03-09 01:58:53 +00002168 Asm->OutStreamer.EmitLabel(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00002169
Chris Lattner855e8f52010-03-09 01:58:53 +00002170 SmallVector<DbgScope*, 2> &SD = I->second;
Devang Patel393a46d2009-10-06 01:50:42 +00002171 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00002172 SDI != SDE; ++SDI)
Chris Lattner855e8f52010-03-09 01:58:53 +00002173 (*SDI)->setEndLabel(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00002174 return;
2175}
2176
2177/// createDbgScope - Create DbgScope for the scope.
2178void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002179 if (!InlinedAt) {
2180 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2181 if (WScope)
2182 return;
2183 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2184 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach652b7432009-11-21 23:12:12 +00002185 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel53addbf2009-11-11 00:18:40 +00002186 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel90a0fe32009-11-10 23:06:00 +00002187 return;
2188 }
2189
2190 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2191 if (WScope)
2192 return;
2193
2194 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2195 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2196 DILocation DL(InlinedAt);
2197 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel393a46d2009-10-06 01:50:42 +00002198}
2199
Devang Patelc50078e2009-11-21 02:48:08 +00002200/// extractScopeInformation - Scan machine instructions in this function
Chris Lattner202f10b2010-03-31 05:39:57 +00002201/// and collect DbgScopes. Return true, if at least one scope was found.
Chris Lattner69a76972010-01-26 23:18:02 +00002202bool DwarfDebug::extractScopeInformation() {
Devang Patel6a260102009-10-01 20:31:14 +00002203 // If scope information was extracted using .dbg intrinsics then there is not
2204 // any need to extract these information by scanning each instruction.
2205 if (!DbgScopeMap.empty())
2206 return false;
2207
Devang Patel98e77302010-01-04 20:44:00 +00002208 DenseMap<const MachineInstr *, unsigned> MIIndexMap;
2209 unsigned MIIndex = 0;
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002210 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Chris Lattnerb9692a72010-04-02 19:42:39 +00002211
Devang Patel90a0fe32009-11-10 23:06:00 +00002212 // Scan each instruction and create scopes. First build working set of scopes.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002213 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel6a260102009-10-01 20:31:14 +00002214 I != E; ++I) {
2215 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2216 II != IE; ++II) {
2217 const MachineInstr *MInsn = II;
Devang Patel14da02f2010-03-15 18:33:46 +00002218 // FIXME : Remove DBG_VALUE check.
Chris Lattner202f10b2010-03-31 05:39:57 +00002219 if (MInsn->isDebugValue()) continue;
Devang Patel98e77302010-01-04 20:44:00 +00002220 MIIndexMap[MInsn] = MIIndex++;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002221
Devang Patel6a260102009-10-01 20:31:14 +00002222 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00002223 if (DL.isUnknown()) continue;
Chris Lattnerb9692a72010-04-02 19:42:39 +00002224
2225 MDNode *Scope = DL.getScope(Ctx);
2226
Devang Patel6a260102009-10-01 20:31:14 +00002227 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002228 // other scopes, create one DbgScope now. This will be translated
Devang Patel6a260102009-10-01 20:31:14 +00002229 // into a scope DIE at the end.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002230 if (DIScope(Scope).isCompileUnit()) continue;
2231 createDbgScope(Scope, DL.getInlinedAt(Ctx));
Devang Patel90a0fe32009-11-10 23:06:00 +00002232 }
2233 }
2234
Devang Patele8151122010-04-01 22:47:29 +00002235
Devang Patel90a0fe32009-11-10 23:06:00 +00002236 // Build scope hierarchy using working set of scopes.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002237 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Devang Patel90a0fe32009-11-10 23:06:00 +00002238 I != E; ++I) {
2239 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2240 II != IE; ++II) {
2241 const MachineInstr *MInsn = II;
Devang Patel14da02f2010-03-15 18:33:46 +00002242 // FIXME : Remove DBG_VALUE check.
Chris Lattner202f10b2010-03-31 05:39:57 +00002243 if (MInsn->isDebugValue()) continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00002244 DebugLoc DL = MInsn->getDebugLoc();
Chris Lattnerb9692a72010-04-02 19:42:39 +00002245 if (DL.isUnknown()) continue;
2246
2247 MDNode *Scope = DL.getScope(Ctx);
2248 if (Scope == 0) continue;
2249
Devang Patel90a0fe32009-11-10 23:06:00 +00002250 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002251 // other scopes, create one DbgScope now. This will be translated
Devang Patel90a0fe32009-11-10 23:06:00 +00002252 // into a scope DIE at the end.
Chris Lattnerb9692a72010-04-02 19:42:39 +00002253 if (DIScope(Scope).isCompileUnit()) continue;
2254 DbgScope *DScope = getUpdatedDbgScope(Scope, MInsn, DL.getInlinedAt(Ctx));
2255 DScope->setLastInsn(MInsn);
Devang Patel6a260102009-10-01 20:31:14 +00002256 }
2257 }
2258
Devang Patel98e77302010-01-04 20:44:00 +00002259 if (!CurrentFnDbgScope)
2260 return false;
2261
2262 CurrentFnDbgScope->fixInstructionMarkers(MIIndexMap);
Devang Patel6a260102009-10-01 20:31:14 +00002263
Devang Patel0d8f3b72010-04-08 15:37:09 +00002264 populateDbgScopeInverseMaps();
2265
2266 return !DbgScopeMap.empty();
2267}
2268
2269/// populateDbgScopeInverseMaps() - Populate DbgScopeBeginMap and
2270/// DbgScopeEndMap. This maps are used to indentify debug scope started
2271/// and ended by an instruction.
2272void DwarfDebug::populateDbgScopeInverseMaps() {
2273
Devang Patel6a260102009-10-01 20:31:14 +00002274 // Each scope has first instruction and last instruction to mark beginning
2275 // and end of a scope respectively. Create an inverse map that list scopes
2276 // starts (and ends) with an instruction. One instruction may start (or end)
Devang Patelfd311df2010-01-20 02:05:23 +00002277 // multiple scopes. Ignore scopes that are not reachable.
2278 SmallVector<DbgScope *, 4> WorkList;
2279 WorkList.push_back(CurrentFnDbgScope);
2280 while (!WorkList.empty()) {
Chris Lattner202f10b2010-03-31 05:39:57 +00002281 DbgScope *S = WorkList.pop_back_val();
Devang Patelfd311df2010-01-20 02:05:23 +00002282
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002283 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patelfd311df2010-01-20 02:05:23 +00002284 if (!Children.empty())
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002285 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patelfd311df2010-01-20 02:05:23 +00002286 SE = Children.end(); SI != SE; ++SI)
2287 WorkList.push_back(*SI);
2288
Devang Patel90a0fe32009-11-10 23:06:00 +00002289 if (S->isAbstractScope())
2290 continue;
Devang Patel6a260102009-10-01 20:31:14 +00002291 const MachineInstr *MI = S->getFirstInsn();
Chris Lattner8143ce82010-03-31 05:36:29 +00002292 assert(MI && "DbgScope does not have first instruction!");
Devang Patel6a260102009-10-01 20:31:14 +00002293
2294 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2295 if (IDI != DbgScopeBeginMap.end())
2296 IDI->second.push_back(S);
2297 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002298 DbgScopeBeginMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002299
2300 MI = S->getLastInsn();
Chris Lattner8143ce82010-03-31 05:36:29 +00002301 assert(MI && "DbgScope does not have last instruction!");
Devang Patel6a260102009-10-01 20:31:14 +00002302 IDI = DbgScopeEndMap.find(MI);
2303 if (IDI != DbgScopeEndMap.end())
2304 IDI->second.push_back(S);
2305 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002306 DbgScopeEndMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002307 }
Devang Patel6a260102009-10-01 20:31:14 +00002308}
2309
Devang Patelc50078e2009-11-21 02:48:08 +00002310/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002311/// emitted immediately after the function entry point.
Chris Lattner69a76972010-01-26 23:18:02 +00002312void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner2cb53302010-04-05 03:52:55 +00002313 if (!MMI->hasDebugInfo()) return;
Bill Wendlingce3c6252010-04-07 09:28:04 +00002314 if (!extractScopeInformation()) return;
Chris Lattnerb49c9f82010-03-29 20:38:20 +00002315
Devang Patelc50078e2009-11-21 02:48:08 +00002316 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002317
Bill Wendlingf5839192009-05-20 23:19:06 +00002318 // Assumes in correct section after the entry point.
Chris Lattnerb93558d2010-04-04 19:25:43 +00002319 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_begin",
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002320 Asm->getFunctionNumber()));
Bill Wendlingf5839192009-05-20 23:19:06 +00002321
2322 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2323 // function.
Devang Patel40c80212009-10-09 22:42:28 +00002324 DebugLoc FDL = MF->getDefaultDebugLoc();
Chris Lattnerb9692a72010-04-02 19:42:39 +00002325 if (FDL.isUnknown()) return;
2326
2327 MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
2328
2329 DISubprogram SP = getDISubprogram(Scope);
2330 unsigned Line, Col;
2331 if (SP.Verify()) {
2332 Line = SP.getLineNumber();
2333 Col = 0;
2334 } else {
2335 Line = FDL.getLine();
2336 Col = FDL.getCol();
Bill Wendlingf5839192009-05-20 23:19:06 +00002337 }
Chris Lattnerb9692a72010-04-02 19:42:39 +00002338
2339 recordSourceLine(Line, Col, Scope);
Bill Wendlingf5839192009-05-20 23:19:06 +00002340}
2341
Devang Patelc50078e2009-11-21 02:48:08 +00002342/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002343///
Chris Lattner69a76972010-01-26 23:18:02 +00002344void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingce3c6252010-04-07 09:28:04 +00002345 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002346
Devang Patel98e77302010-01-04 20:44:00 +00002347 if (CurrentFnDbgScope) {
2348 // Define end label for subprogram.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002349 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end",
2350 Asm->getFunctionNumber()));
Devang Patel98e77302010-01-04 20:44:00 +00002351
2352 // Get function line info.
2353 if (!Lines.empty()) {
2354 // Get section line info.
2355 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2356 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2357 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2358 // Append the function info to section info.
2359 SectionLineInfos.insert(SectionLineInfos.end(),
2360 Lines.begin(), Lines.end());
2361 }
2362
2363 // Construct abstract scopes.
2364 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2365 AE = AbstractScopesList.end(); AI != AE; ++AI)
2366 constructScopeDIE(*AI);
2367
2368 constructScopeDIE(CurrentFnDbgScope);
2369
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002370 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Devang Patel98e77302010-01-04 20:44:00 +00002371 MMI->getFrameMoves()));
Bill Wendlingf5839192009-05-20 23:19:06 +00002372 }
2373
Bill Wendlingf5839192009-05-20 23:19:06 +00002374 // Clear debug info
Devang Patel387e9c12010-01-19 01:26:02 +00002375 CurrentFnDbgScope = NULL;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002376 DeleteContainerSeconds(DbgScopeMap);
Devang Patel387e9c12010-01-19 01:26:02 +00002377 DbgScopeBeginMap.clear();
2378 DbgScopeEndMap.clear();
Devang Patel9a9a4ac2010-03-29 22:59:58 +00002379 DbgValueStartMap.clear();
Devang Patel387e9c12010-01-19 01:26:02 +00002380 ConcreteScopes.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002381 DeleteContainerSeconds(AbstractScopes);
Devang Patel387e9c12010-01-19 01:26:02 +00002382 AbstractScopesList.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002383 AbstractVariables.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002384 Lines.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002385}
2386
Chris Lattner597b0fc2010-03-09 04:54:43 +00002387/// recordSourceLine - Register a source line with debug info. Returns the
2388/// unique label that was emitted and which provides correspondence to
2389/// the source line list.
2390MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, MDNode *S) {
Devang Patel7f75bbe2009-11-25 17:36:49 +00002391 StringRef Dir;
2392 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002393
2394 DIDescriptor Scope(S);
2395 if (Scope.isCompileUnit()) {
2396 DICompileUnit CU(S);
2397 Dir = CU.getDirectory();
2398 Fn = CU.getFilename();
2399 } else if (Scope.isSubprogram()) {
2400 DISubprogram SP(S);
2401 Dir = SP.getDirectory();
2402 Fn = SP.getFilename();
2403 } else if (Scope.isLexicalBlock()) {
2404 DILexicalBlock DB(S);
2405 Dir = DB.getDirectory();
2406 Fn = DB.getFilename();
2407 } else
Chris Lattnerdd21da82010-03-13 07:26:18 +00002408 assert(0 && "Unexpected scope info");
Devang Patel946d0ae2009-10-05 18:03:19 +00002409
2410 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Chris Lattner54e56f22010-03-14 08:36:50 +00002411 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
Chris Lattner8d9d06a2010-03-14 08:15:55 +00002412 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf5839192009-05-20 23:19:06 +00002413
Chris Lattner597b0fc2010-03-09 04:54:43 +00002414 Asm->OutStreamer.EmitLabel(Label);
2415 return Label;
Bill Wendlingf5839192009-05-20 23:19:06 +00002416}
2417
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002418//===----------------------------------------------------------------------===//
2419// Emit Methods
2420//===----------------------------------------------------------------------===//
2421
Devang Patelc50078e2009-11-21 02:48:08 +00002422/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002423///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002424unsigned
2425DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002426 // Get the children.
2427 const std::vector<DIE *> &Children = Die->getChildren();
2428
2429 // If not last sibling and has children then add sibling offset attribute.
Jeffrey Yasskin2944ced2010-03-22 18:47:14 +00002430 if (!Last && !Children.empty())
Benjamin Kramer138c7ff2010-03-31 19:34:01 +00002431 Die->addSiblingOffset(DIEValueAllocator);
Bill Wendling55fccda2009-05-20 23:21:38 +00002432
2433 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002434 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002435
2436 // Get the abbreviation for this DIE.
2437 unsigned AbbrevNumber = Die->getAbbrevNumber();
2438 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2439
2440 // Set DIE offset
2441 Die->setOffset(Offset);
2442
2443 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002444 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002445
2446 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2447 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2448
2449 // Size the DIE attribute values.
2450 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2451 // Size attribute value.
Chris Lattner352c8e22010-04-05 00:18:22 +00002452 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling55fccda2009-05-20 23:21:38 +00002453
2454 // Size the DIE children if any.
2455 if (!Children.empty()) {
2456 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2457 "Children flag not set");
2458
2459 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002460 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002461
2462 // End of children marker.
2463 Offset += sizeof(int8_t);
2464 }
2465
2466 Die->setSize(Offset - Die->getOffset());
2467 return Offset;
2468}
2469
Devang Patelc50078e2009-11-21 02:48:08 +00002470/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002471///
Devang Patelc50078e2009-11-21 02:48:08 +00002472void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002473 // Compute size of compile unit header.
2474 static unsigned Offset =
2475 sizeof(int32_t) + // Length of Compilation Unit Info
2476 sizeof(int16_t) + // DWARF version number
2477 sizeof(int32_t) + // Offset Into Abbrev. Section
2478 sizeof(int8_t); // Pointer Size (in bytes)
2479
Devang Patelc50078e2009-11-21 02:48:08 +00002480 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel5a3d37f2009-06-29 20:45:18 +00002481 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling55fccda2009-05-20 23:21:38 +00002482}
2483
Chris Lattner733c69d2010-04-04 23:02:02 +00002484/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2485/// temporary label to it if SymbolStem is specified.
Chris Lattner66143d22010-04-04 22:59:04 +00002486static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Chris Lattner733c69d2010-04-04 23:02:02 +00002487 const char *SymbolStem = 0) {
Chris Lattner66143d22010-04-04 22:59:04 +00002488 Asm->OutStreamer.SwitchSection(Section);
Chris Lattner733c69d2010-04-04 23:02:02 +00002489 if (!SymbolStem) return 0;
2490
Chris Lattner66143d22010-04-04 22:59:04 +00002491 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2492 Asm->OutStreamer.EmitLabel(TmpSym);
2493 return TmpSym;
2494}
2495
2496/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2497/// the start of each one.
Chris Lattnerd91fd8c2010-04-04 22:33:59 +00002498void DwarfDebug::EmitSectionLabels() {
Chris Lattner73266f92009-08-19 05:49:37 +00002499 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002500
Bill Wendling55fccda2009-05-20 23:21:38 +00002501 // Dwarf sections base addresses.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002502 if (Asm->MAI->doesDwarfRequireFrameSection()) {
Chris Lattner66143d22010-04-04 22:59:04 +00002503 DwarfFrameSectionSym =
2504 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2505 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002506
Chris Lattner66143d22010-04-04 22:59:04 +00002507 DwarfInfoSectionSym =
2508 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2509 DwarfAbbrevSectionSym =
2510 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Chris Lattner733c69d2010-04-04 23:02:02 +00002511 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
Chris Lattner66143d22010-04-04 22:59:04 +00002512
2513 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Chris Lattner733c69d2010-04-04 23:02:02 +00002514 EmitSectionSym(Asm, MacroInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002515
Chris Lattner733c69d2010-04-04 23:02:02 +00002516 EmitSectionSym(Asm, TLOF.getDwarfLineSection());
2517 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2518 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2519 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Chris Lattner66143d22010-04-04 22:59:04 +00002520 DwarfStrSectionSym =
2521 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
Chris Lattner733c69d2010-04-04 23:02:02 +00002522 EmitSectionSym(Asm, TLOF.getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002523
Chris Lattner66143d22010-04-04 22:59:04 +00002524 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
Chris Lattner094932e2010-04-04 23:10:38 +00002525 EmitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002526}
2527
Devang Patelc50078e2009-11-21 02:48:08 +00002528/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002529///
Devang Patelc50078e2009-11-21 02:48:08 +00002530void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002531 // Get the abbreviation for this DIE.
2532 unsigned AbbrevNumber = Die->getAbbrevNumber();
2533 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2534
Bill Wendling55fccda2009-05-20 23:21:38 +00002535 // Emit the code (index) for the abbreviation.
Chris Lattner97c69b72010-04-04 18:52:31 +00002536 if (Asm->isVerbose())
Chris Lattnerbcc79432010-01-22 23:18:42 +00002537 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2538 Twine::utohexstr(Die->getOffset()) + ":0x" +
2539 Twine::utohexstr(Die->getSize()) + " " +
2540 dwarf::TagString(Abbrev->getTag()));
Chris Lattner26be1c12010-04-04 19:09:29 +00002541 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002542
Jeffrey Yasskin2944ced2010-03-22 18:47:14 +00002543 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Bill Wendling55fccda2009-05-20 23:21:38 +00002544 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2545
2546 // Emit the DIE attribute values.
2547 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2548 unsigned Attr = AbbrevData[i].getAttribute();
2549 unsigned Form = AbbrevData[i].getForm();
2550 assert(Form && "Too many attributes for DIE (check abbreviation)");
2551
Chris Lattner97c69b72010-04-04 18:52:31 +00002552 if (Asm->isVerbose())
Chris Lattner91e06b42010-01-24 18:54:17 +00002553 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2554
Bill Wendling55fccda2009-05-20 23:21:38 +00002555 switch (Attr) {
2556 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002557 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002558 break;
2559 case dwarf::DW_AT_abstract_origin: {
2560 DIEEntry *E = cast<DIEEntry>(Values[i]);
2561 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002562 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002563 Asm->EmitInt32(Addr);
2564 break;
2565 }
2566 default:
2567 // Emit an attribute using the defined form.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002568 Values[i]->EmitValue(Asm, Form);
Bill Wendling55fccda2009-05-20 23:21:38 +00002569 break;
2570 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002571 }
2572
2573 // Emit the DIE children if any.
2574 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2575 const std::vector<DIE *> &Children = Die->getChildren();
2576
2577 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002578 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002579
Chris Lattner97c69b72010-04-04 18:52:31 +00002580 if (Asm->isVerbose())
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002581 Asm->OutStreamer.AddComment("End Of Children Mark");
2582 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002583 }
2584}
2585
Devang Patelfe0be132009-12-09 18:24:21 +00002586/// emitDebugInfo - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002587///
Devang Patelfe0be132009-12-09 18:24:21 +00002588void DwarfDebug::emitDebugInfo() {
2589 // Start debug info section.
2590 Asm->OutStreamer.SwitchSection(
2591 Asm->getObjFileLowering().getDwarfInfoSection());
2592 DIE *Die = ModuleCU->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002593
2594 // Emit the compile units header.
Chris Lattnerb93558d2010-04-04 19:25:43 +00002595 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2596 ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002597
2598 // Emit size of content not including length itself
2599 unsigned ContentSize = Die->getSize() +
2600 sizeof(int16_t) + // DWARF version number
2601 sizeof(int32_t) + // Offset Into Abbrev. Section
2602 sizeof(int8_t) + // Pointer Size (in bytes)
2603 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2604
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002605 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2606 Asm->EmitInt32(ContentSize);
2607 Asm->OutStreamer.AddComment("DWARF version number");
2608 Asm->EmitInt16(dwarf::DWARF_VERSION);
2609 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Chris Lattnerc06b4742010-04-04 23:25:33 +00002610 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2611 DwarfAbbrevSectionSym);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002612 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002613 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00002614
Devang Patelc50078e2009-11-21 02:48:08 +00002615 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002616 // FIXME - extra padding for gdb bug.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002617 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2618 Asm->EmitInt8(0);
2619 Asm->EmitInt8(0);
2620 Asm->EmitInt8(0);
2621 Asm->EmitInt8(0);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002622 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002623}
2624
Devang Patelc50078e2009-11-21 02:48:08 +00002625/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002626///
Devang Patelc50078e2009-11-21 02:48:08 +00002627void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002628 // Check to see if it is worth the effort.
2629 if (!Abbreviations.empty()) {
2630 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002631 Asm->OutStreamer.SwitchSection(
2632 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002633
Chris Lattnerb93558d2010-04-04 19:25:43 +00002634 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002635
2636 // For each abbrevation.
2637 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2638 // Get abbreviation data
2639 const DIEAbbrev *Abbrev = Abbreviations[i];
2640
2641 // Emit the abbrevations code (base 1 index.)
Chris Lattner26be1c12010-04-04 19:09:29 +00002642 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling55fccda2009-05-20 23:21:38 +00002643
2644 // Emit the abbreviations data.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002645 Abbrev->Emit(Asm);
Bill Wendling55fccda2009-05-20 23:21:38 +00002646 }
2647
2648 // Mark end of abbreviations.
Chris Lattner26be1c12010-04-04 19:09:29 +00002649 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002650
Chris Lattnerb93558d2010-04-04 19:25:43 +00002651 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002652 }
2653}
2654
Devang Patelc50078e2009-11-21 02:48:08 +00002655/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002656/// the line matrix.
2657///
Devang Patelc50078e2009-11-21 02:48:08 +00002658void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002659 // Define last address of section.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002660 Asm->OutStreamer.AddComment("Extended Op");
2661 Asm->EmitInt8(0);
2662
2663 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002664 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002665 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2666 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2667
2668 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnereb159d62010-03-10 01:17:49 +00002669
Chris Lattnerb93558d2010-04-04 19:25:43 +00002670 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002671 Asm->getTargetData().getPointerSize(),
2672 0/*AddrSpace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00002673
2674 // Mark end of matrix.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002675 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2676 Asm->EmitInt8(0);
Chris Lattnerad653482010-01-22 22:09:00 +00002677 Asm->EmitInt8(1);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002678 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002679}
2680
Devang Patelc50078e2009-11-21 02:48:08 +00002681/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002682///
Devang Patelc50078e2009-11-21 02:48:08 +00002683void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002684 // If the target is using .loc/.file, the assembler will be emitting the
2685 // .debug_line table automatically.
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002686 if (Asm->MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002687 return;
2688
2689 // Minimum line delta, thus ranging from -10..(255-10).
2690 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2691 // Maximum line delta, thus ranging from -10..(255-10).
2692 const int MaxLineDelta = 255 + MinLineDelta;
2693
2694 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002695 Asm->OutStreamer.SwitchSection(
2696 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002697
2698 // Construct the section header.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002699 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002700 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
2701 Asm->GetTempSymbol("line_begin"), 4);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002702 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002703
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002704 Asm->OutStreamer.AddComment("DWARF version number");
2705 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00002706
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002707 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002708 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
2709 Asm->GetTempSymbol("line_prolog_begin"), 4);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002710 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002711
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002712 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2713 Asm->EmitInt8(1);
2714 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2715 Asm->EmitInt8(1);
2716 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2717 Asm->EmitInt8(MinLineDelta);
2718 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
2719 Asm->EmitInt8(MaxLineDelta);
2720 Asm->OutStreamer.AddComment("Special Opcode Base");
2721 Asm->EmitInt8(-MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00002722
2723 // Line number standard opcode encodings argument count
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002724 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
2725 Asm->EmitInt8(0);
2726 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
2727 Asm->EmitInt8(1);
2728 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
2729 Asm->EmitInt8(1);
2730 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
2731 Asm->EmitInt8(1);
2732 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
2733 Asm->EmitInt8(1);
2734 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
2735 Asm->EmitInt8(0);
2736 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
2737 Asm->EmitInt8(0);
2738 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
2739 Asm->EmitInt8(0);
2740 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
2741 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002742
2743 // Emit directories.
2744 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnercc564642010-01-23 03:11:46 +00002745 const std::string &Dir = getSourceDirectoryName(DI);
Chris Lattner97c69b72010-04-04 18:52:31 +00002746 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Chris Lattnercc564642010-01-23 03:11:46 +00002747 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002748 }
2749
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002750 Asm->OutStreamer.AddComment("End of directories");
2751 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002752
2753 // Emit files.
2754 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2755 // Remember source id starts at 1.
2756 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnercc564642010-01-23 03:11:46 +00002757 const std::string &FN = getSourceFileName(Id.second);
Chris Lattner97c69b72010-04-04 18:52:31 +00002758 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Chris Lattnercc564642010-01-23 03:11:46 +00002759 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2760
Chris Lattner26be1c12010-04-04 19:09:29 +00002761 Asm->EmitULEB128(Id.first, "Directory #");
2762 Asm->EmitULEB128(0, "Mod date");
2763 Asm->EmitULEB128(0, "File size");
Bill Wendling55fccda2009-05-20 23:21:38 +00002764 }
2765
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002766 Asm->OutStreamer.AddComment("End of files");
2767 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002768
Chris Lattnerb93558d2010-04-04 19:25:43 +00002769 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002770
2771 // A sequence for each text section.
2772 unsigned SecSrcLinesSize = SectionSourceLines.size();
2773
2774 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2775 // Isolate current sections line info.
2776 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2777
Bill Wendling55fccda2009-05-20 23:21:38 +00002778 // Dwarf assumes we start with first line of first source file.
2779 unsigned Source = 1;
2780 unsigned Line = 1;
2781
2782 // Construct rows of the address, source, line, column matrix.
2783 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2784 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner8d9d06a2010-03-14 08:15:55 +00002785 MCSymbol *Label = LineInfo.getLabel();
Chris Lattner31ae74d2010-03-14 02:20:58 +00002786 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling55fccda2009-05-20 23:21:38 +00002787
Caroline Tice9da96d82009-09-11 18:25:54 +00002788 if (LineInfo.getLine() == 0) continue;
2789
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00002790 if (Asm->isVerbose()) {
Chris Lattneree3b40f2010-03-10 01:04:13 +00002791 std::pair<unsigned, unsigned> SrcID =
Bill Wendling55fccda2009-05-20 23:21:38 +00002792 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattneree3b40f2010-03-10 01:04:13 +00002793 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner48fb0b12010-03-10 02:29:31 +00002794 "/" +
2795 Twine(getSourceFileName(SrcID.second)) +
Chris Lattneree3b40f2010-03-10 01:04:13 +00002796 ":" + Twine(LineInfo.getLine()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002797 }
2798
2799 // Define the line address.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002800 Asm->OutStreamer.AddComment("Extended Op");
2801 Asm->EmitInt8(0);
2802 Asm->OutStreamer.AddComment("Op size");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002803 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002804
2805 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2806 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2807
2808 Asm->OutStreamer.AddComment("Location label");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002809 Asm->OutStreamer.EmitSymbolValue(Label,
2810 Asm->getTargetData().getPointerSize(),
Chris Lattner31ae74d2010-03-14 02:20:58 +00002811 0/*AddrSpace*/);
Chris Lattnereb159d62010-03-10 01:17:49 +00002812
Bill Wendling55fccda2009-05-20 23:21:38 +00002813 // If change of source, then switch to the new source.
2814 if (Source != LineInfo.getSourceID()) {
2815 Source = LineInfo.getSourceID();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002816 Asm->OutStreamer.AddComment("DW_LNS_set_file");
2817 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattner26be1c12010-04-04 19:09:29 +00002818 Asm->EmitULEB128(Source, "New Source");
Bill Wendling55fccda2009-05-20 23:21:38 +00002819 }
2820
2821 // If change of line.
2822 if (Line != LineInfo.getLine()) {
2823 // Determine offset.
2824 int Offset = LineInfo.getLine() - Line;
2825 int Delta = Offset - MinLineDelta;
2826
2827 // Update line.
2828 Line = LineInfo.getLine();
2829
2830 // If delta is small enough and in range...
2831 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2832 // ... then use fast opcode.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002833 Asm->OutStreamer.AddComment("Line Delta");
2834 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00002835 } else {
2836 // ... otherwise use long hand.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002837 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling55fccda2009-05-20 23:21:38 +00002838 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner26be1c12010-04-04 19:09:29 +00002839 Asm->EmitSLEB128(Offset, "Line Offset");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002840 Asm->OutStreamer.AddComment("DW_LNS_copy");
2841 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00002842 }
2843 } else {
2844 // Copy the previous row (different address or source)
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002845 Asm->OutStreamer.AddComment("DW_LNS_copy");
2846 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00002847 }
2848 }
2849
Devang Patelc50078e2009-11-21 02:48:08 +00002850 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002851 }
2852
2853 if (SecSrcLinesSize == 0)
2854 // Because we're emitting a debug_line section, we still need a line
2855 // table. The linker and friends expect it to exist. If there's nothing to
2856 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00002857 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002858
Chris Lattnerb93558d2010-04-04 19:25:43 +00002859 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002860}
2861
Devang Patelc50078e2009-11-21 02:48:08 +00002862/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002863///
Devang Patelc50078e2009-11-21 02:48:08 +00002864void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002865 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002866 return;
2867
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002868 int stackGrowth = Asm->getTargetData().getPointerSize();
2869 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2870 TargetFrameInfo::StackGrowsDown)
2871 stackGrowth *= -1;
Bill Wendling55fccda2009-05-20 23:21:38 +00002872
2873 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002874 Asm->OutStreamer.SwitchSection(
2875 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002876
Chris Lattnerb93558d2010-04-04 19:25:43 +00002877 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002878 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002879 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
2880 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00002881
Chris Lattnerb93558d2010-04-04 19:25:43 +00002882 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002883 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling55fccda2009-05-20 23:21:38 +00002884 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002885 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling55fccda2009-05-20 23:21:38 +00002886 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002887 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnercc564642010-01-23 03:11:46 +00002888 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner26be1c12010-04-04 19:09:29 +00002889 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
2890 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002891 Asm->OutStreamer.AddComment("CIE RA Column");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002892 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Bill Wendling55fccda2009-05-20 23:21:38 +00002893 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling55fccda2009-05-20 23:21:38 +00002894
2895 std::vector<MachineMove> Moves;
2896 RI->getInitialFrameState(Moves);
2897
Chris Lattner193fec02010-04-04 23:41:46 +00002898 Asm->EmitFrameMoves(Moves, 0, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00002899
2900 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002901 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002902}
2903
Devang Patelc50078e2009-11-21 02:48:08 +00002904/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00002905/// section.
Chris Lattnerdd21da82010-03-13 07:26:18 +00002906void DwarfDebug::
2907emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002908 if (!Asm->MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002909 return;
2910
2911 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002912 Asm->OutStreamer.SwitchSection(
2913 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002914
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002915 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattnerdd21da82010-03-13 07:26:18 +00002916 MCSymbol *DebugFrameBegin =
Chris Lattnerb93558d2010-04-04 19:25:43 +00002917 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
Chris Lattnerdd21da82010-03-13 07:26:18 +00002918 MCSymbol *DebugFrameEnd =
Chris Lattnerb93558d2010-04-04 19:25:43 +00002919 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002920 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00002921
Chris Lattnerdd21da82010-03-13 07:26:18 +00002922 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling55fccda2009-05-20 23:21:38 +00002923
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002924 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattnerc06b4742010-04-04 23:25:33 +00002925 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
2926 DwarfFrameSectionSym);
Bill Wendling55fccda2009-05-20 23:21:38 +00002927
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002928 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattnerb93558d2010-04-04 19:25:43 +00002929 MCSymbol *FuncBeginSym =
2930 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
Chris Lattner314e3362010-03-13 07:40:56 +00002931 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002932 Asm->getTargetData().getPointerSize(),
2933 0/*AddrSpace*/);
Chris Lattnereb159d62010-03-10 01:17:49 +00002934
2935
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002936 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002937 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
Chris Lattnerbc0027b2010-04-05 00:13:49 +00002938 FuncBeginSym, Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00002939
Chris Lattner193fec02010-04-04 23:41:46 +00002940 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00002941
2942 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattnerdd21da82010-03-13 07:26:18 +00002943 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling55fccda2009-05-20 23:21:38 +00002944}
2945
Devang Patelfe0be132009-12-09 18:24:21 +00002946/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2947///
2948void DwarfDebug::emitDebugPubNames() {
2949 // Start the dwarf pubnames section.
2950 Asm->OutStreamer.SwitchSection(
2951 Asm->getObjFileLowering().getDwarfPubNamesSection());
2952
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002953 Asm->OutStreamer.AddComment("Length of Public Names Info");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002954 Asm->EmitLabelDifference(
2955 Asm->GetTempSymbol("pubnames_end", ModuleCU->getID()),
2956 Asm->GetTempSymbol("pubnames_begin", ModuleCU->getID()), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00002957
Chris Lattnerb93558d2010-04-04 19:25:43 +00002958 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
2959 ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002960
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002961 Asm->OutStreamer.AddComment("DWARF Version");
2962 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00002963
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002964 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Chris Lattnerc06b4742010-04-04 23:25:33 +00002965 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
2966 DwarfInfoSectionSym);
Bill Wendling55fccda2009-05-20 23:21:38 +00002967
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002968 Asm->OutStreamer.AddComment("Compilation Unit Length");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002969 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
2970 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
2971 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00002972
Devang Patelfe0be132009-12-09 18:24:21 +00002973 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling55fccda2009-05-20 23:21:38 +00002974 for (StringMap<DIE*>::const_iterator
2975 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2976 const char *Name = GI->getKeyData();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002977 DIE *Entity = GI->second;
Bill Wendling55fccda2009-05-20 23:21:38 +00002978
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002979 Asm->OutStreamer.AddComment("DIE offset");
2980 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00002981
Chris Lattner97c69b72010-04-04 18:52:31 +00002982 if (Asm->isVerbose())
Chris Lattnercc564642010-01-23 03:11:46 +00002983 Asm->OutStreamer.AddComment("External Name");
2984 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002985 }
2986
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002987 Asm->OutStreamer.AddComment("End Mark");
2988 Asm->EmitInt32(0);
Chris Lattnerb93558d2010-04-04 19:25:43 +00002989 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
2990 ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002991}
2992
Devang Patelec13b4f2009-11-24 01:14:22 +00002993void DwarfDebug::emitDebugPubTypes() {
Devang Patel6f2bdd52009-11-24 19:18:41 +00002994 // Start the dwarf pubnames section.
2995 Asm->OutStreamer.SwitchSection(
2996 Asm->getObjFileLowering().getDwarfPubTypesSection());
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002997 Asm->OutStreamer.AddComment("Length of Public Types Info");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00002998 Asm->EmitLabelDifference(
2999 Asm->GetTempSymbol("pubtypes_end", ModuleCU->getID()),
3000 Asm->GetTempSymbol("pubtypes_begin", ModuleCU->getID()), 4);
Devang Patelec13b4f2009-11-24 01:14:22 +00003001
Chris Lattnerb93558d2010-04-04 19:25:43 +00003002 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3003 ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00003004
Chris Lattner97c69b72010-04-04 18:52:31 +00003005 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
Devang Patel40b6cda2010-02-02 03:47:27 +00003006 Asm->EmitInt16(dwarf::DWARF_VERSION);
Devang Patelec13b4f2009-11-24 01:14:22 +00003007
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003008 Asm->OutStreamer.AddComment("Offset of Compilation ModuleCU Info");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003009 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3010 DwarfInfoSectionSym);
Devang Patelec13b4f2009-11-24 01:14:22 +00003011
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003012 Asm->OutStreamer.AddComment("Compilation ModuleCU Length");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003013 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
3014 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3015 4);
Devang Patelec13b4f2009-11-24 01:14:22 +00003016
3017 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
3018 for (StringMap<DIE*>::const_iterator
3019 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3020 const char *Name = GI->getKeyData();
3021 DIE * Entity = GI->second;
3022
Chris Lattner97c69b72010-04-04 18:52:31 +00003023 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Devang Patel40b6cda2010-02-02 03:47:27 +00003024 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00003025
Chris Lattner97c69b72010-04-04 18:52:31 +00003026 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Devang Patel0ceb4892010-02-02 03:37:03 +00003027 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00003028 }
3029
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003030 Asm->OutStreamer.AddComment("End Mark");
3031 Asm->EmitInt32(0);
Chris Lattnerb93558d2010-04-04 19:25:43 +00003032 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3033 ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00003034}
3035
Devang Patelc50078e2009-11-21 02:48:08 +00003036/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003037///
Devang Patelc50078e2009-11-21 02:48:08 +00003038void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003039 // Check to see if it is worth the effort.
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003040 if (StringPool.empty()) return;
3041
3042 // Start the dwarf str section.
3043 Asm->OutStreamer.SwitchSection(
Chris Lattner73266f92009-08-19 05:49:37 +00003044 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003045
Chris Lattner0f093f82010-03-13 02:17:42 +00003046 // Get all of the string pool entries and put them in an array by their ID so
3047 // we can sort them.
3048 SmallVector<std::pair<unsigned,
3049 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3050
3051 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3052 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3053 Entries.push_back(std::make_pair(I->second.second, &*I));
3054
3055 array_pod_sort(Entries.begin(), Entries.end());
3056
3057 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003058 // Emit a label for reference from debug information entries.
Chris Lattner0f093f82010-03-13 02:17:42 +00003059 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00003060
3061 // Emit the string itself.
Chris Lattner0f093f82010-03-13 02:17:42 +00003062 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00003063 }
3064}
3065
Devang Patelc50078e2009-11-21 02:48:08 +00003066/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003067///
Devang Patelc50078e2009-11-21 02:48:08 +00003068void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003069 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00003070 Asm->OutStreamer.SwitchSection(
3071 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003072}
3073
3074/// EmitDebugARanges - Emit visible names into a debug aranges section.
3075///
3076void DwarfDebug::EmitDebugARanges() {
3077 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00003078 Asm->OutStreamer.SwitchSection(
3079 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003080}
3081
Devang Patelc50078e2009-11-21 02:48:08 +00003082/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003083///
Devang Patelc50078e2009-11-21 02:48:08 +00003084void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00003085 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00003086 Asm->OutStreamer.SwitchSection(
3087 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00003088}
3089
Devang Patelc50078e2009-11-21 02:48:08 +00003090/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00003091///
Devang Patelc50078e2009-11-21 02:48:08 +00003092void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00003093 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00003094 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00003095 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00003096 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00003097 }
3098}
3099
Devang Patelc50078e2009-11-21 02:48:08 +00003100/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00003101/// Section Header:
3102/// 1. length of section
3103/// 2. Dwarf version number
3104/// 3. address size.
3105///
3106/// Entries (one "entry" for each function that was inlined):
3107///
3108/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3109/// otherwise offset into __debug_str for regular function name.
3110/// 2. offset into __debug_str section for regular function name.
3111/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3112/// instances for the function.
3113///
3114/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3115/// inlined instance; the die_offset points to the inlined_subroutine die in the
3116/// __debug_info section, and the low_pc is the starting address for the
3117/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00003118void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003119 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00003120 return;
3121
Devang Patel5a3d37f2009-06-29 20:45:18 +00003122 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00003123 return;
3124
Chris Lattner73266f92009-08-19 05:49:37 +00003125 Asm->OutStreamer.SwitchSection(
3126 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerad653482010-01-22 22:09:00 +00003127
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003128 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattnerfd2a3f62010-04-04 19:58:12 +00003129 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3130 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Bill Wendling55fccda2009-05-20 23:21:38 +00003131
Chris Lattnerb93558d2010-04-04 19:25:43 +00003132 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00003133
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003134 Asm->OutStreamer.AddComment("Dwarf Version");
3135 Asm->EmitInt16(dwarf::DWARF_VERSION);
3136 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003137 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00003138
Devang Patel90a0fe32009-11-10 23:06:00 +00003139 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
3140 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00003141
Devang Patel90a0fe32009-11-10 23:06:00 +00003142 MDNode *Node = *I;
Devang Patel7d707f92010-01-19 06:19:05 +00003143 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbachb23f2422009-11-22 19:20:36 +00003144 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00003145 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00003146 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00003147 StringRef LName = SP.getLinkageName();
3148 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00003149
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003150 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnercc564642010-01-23 03:11:46 +00003151 if (LName.empty()) {
3152 Asm->OutStreamer.EmitBytes(Name, 0);
3153 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3154 } else
Chris Lattnerc06b4742010-04-04 23:25:33 +00003155 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3156 DwarfStrSectionSym);
Devang Patel90a0fe32009-11-10 23:06:00 +00003157
Chris Lattner9ef8efd2010-03-09 23:52:58 +00003158 Asm->OutStreamer.AddComment("Function name");
Chris Lattnerc06b4742010-04-04 23:25:33 +00003159 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
Chris Lattner26be1c12010-04-04 19:09:29 +00003160 Asm->EmitULEB128(Labels.size(), "Inline count");
Bill Wendling55fccda2009-05-20 23:21:38 +00003161
Devang Patel90a0fe32009-11-10 23:06:00 +00003162 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00003163 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattner97c69b72010-04-04 18:52:31 +00003164 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Chris Lattneread58652010-03-09 00:31:02 +00003165 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00003166
Chris Lattner97c69b72010-04-04 18:52:31 +00003167 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
Chris Lattnerbc0027b2010-04-05 00:13:49 +00003168 Asm->OutStreamer.EmitSymbolValue(LI->first,
3169 Asm->getTargetData().getPointerSize(),0);
Bill Wendling55fccda2009-05-20 23:21:38 +00003170 }
3171 }
3172
Chris Lattnerb93558d2010-04-04 19:25:43 +00003173 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00003174}