blob: 37769421b44bceb4a2600f98afb619ceca5c21bc [file] [log] [blame]
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattner2ab0c442010-03-09 00:39:24 +000013
Devang Patel15e723d2009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000015#include "DwarfDebug.h"
16#include "llvm/Module.h"
David Greened87baff2009-08-19 21:52:55 +000017#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner855e8f52010-03-09 01:58:53 +000019#include "llvm/MC/MCAsmInfo.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000020#include "llvm/MC/MCSection.h"
Chris Lattner73266f92009-08-19 05:49:37 +000021#include "llvm/MC/MCStreamer.h"
Chris Lattner855e8f52010-03-09 01:58:53 +000022#include "llvm/MC/MCSymbol.h"
Chris Lattner31a54742010-01-16 21:57:06 +000023#include "llvm/Target/Mangler.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000024#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000026#include "llvm/Target/TargetLoweringObjectFile.h"
27#include "llvm/Target/TargetRegisterInfo.h"
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +000028#include "llvm/ADT/STLExtras.h"
Chris Lattnerf5377682009-08-24 03:52:50 +000029#include "llvm/ADT/StringExtras.h"
Daniel Dunbarc74255d2009-10-13 06:47:08 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/ErrorHandling.h"
Devang Patelae89d3b2010-01-26 21:39:14 +000032#include "llvm/Support/ValueHandle.h"
Chris Lattnerad653482010-01-22 22:09:00 +000033#include "llvm/Support/FormattedStream.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000034#include "llvm/Support/Timer.h"
35#include "llvm/System/Path.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000036using namespace llvm;
37
Bill Wendlingb12b3d72009-05-15 09:23:25 +000038//===----------------------------------------------------------------------===//
39
40/// Configuration values for initial hash set sizes (log2).
41///
Bill Wendlingb12b3d72009-05-15 09:23:25 +000042static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendlingb12b3d72009-05-15 09:23:25 +000043
44namespace llvm {
45
46//===----------------------------------------------------------------------===//
47/// CompileUnit - This dwarf writer support class manages information associate
48/// with a source file.
Nick Lewyckyee68f452009-11-17 08:11:44 +000049class CompileUnit {
Bill Wendlingb12b3d72009-05-15 09:23:25 +000050 /// ID - File identifier for source.
51 ///
52 unsigned ID;
53
54 /// Die - Compile unit debug information entry.
55 ///
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +000056 const OwningPtr<DIE> CUDie;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000057
Jeffrey Yasskinc3545712010-03-11 18:29:55 +000058 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Devang Patel1233f912009-11-21 00:31:03 +000059 DIE *IndexTyDie;
60
Bill Wendlingb12b3d72009-05-15 09:23:25 +000061 /// GVToDieMap - Tracks the mapping of unit level debug informaton
62 /// variables to debug information entries.
Devang Patel15e723d2009-08-28 23:24:31 +000063 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel7d707f92010-01-19 06:19:05 +000064 DenseMap<MDNode *, DIE *> GVToDieMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000065
66 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
67 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel15e723d2009-08-28 23:24:31 +000068 /// FIXME : Rename
Devang Patel7d707f92010-01-19 06:19:05 +000069 DenseMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000070
71 /// Globals - A map of globally visible named entities for this unit.
72 ///
73 StringMap<DIE*> Globals;
74
Devang Patelec13b4f2009-11-24 01:14:22 +000075 /// GlobalTypes - A map of globally visible types for this unit.
76 ///
77 StringMap<DIE*> GlobalTypes;
78
Bill Wendlingb12b3d72009-05-15 09:23:25 +000079public:
80 CompileUnit(unsigned I, DIE *D)
Devang Patelc50078e2009-11-21 02:48:08 +000081 : ID(I), CUDie(D), IndexTyDie(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +000082
83 // Accessors.
Devang Patelec13b4f2009-11-24 01:14:22 +000084 unsigned getID() const { return ID; }
Jeffrey Yasskinc3545712010-03-11 18:29:55 +000085 DIE* getCUDie() const { return CUDie.get(); }
Devang Patelec13b4f2009-11-24 01:14:22 +000086 const StringMap<DIE*> &getGlobals() const { return Globals; }
87 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000088
89 /// hasContent - Return true if this compile unit has something to write out.
90 ///
Devang Patelc50078e2009-11-21 02:48:08 +000091 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000092
Devang Patelc50078e2009-11-21 02:48:08 +000093 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +000094 ///
Devang Patelc50078e2009-11-21 02:48:08 +000095 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000096
Devang Patelec13b4f2009-11-24 01:14:22 +000097 /// addGlobalType - Add a new global type to the compile unit.
98 ///
99 void addGlobalType(const std::string &Name, DIE *Die) {
100 GlobalTypes[Name] = Die;
101 }
102
Devang Pateld90672c2009-11-20 21:37:22 +0000103 /// getDIE - Returns the debug information entry map slot for the
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000104 /// specified debug variable.
Devang Pateld90672c2009-11-20 21:37:22 +0000105 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach652b7432009-11-21 23:12:12 +0000106
Devang Pateld90672c2009-11-20 21:37:22 +0000107 /// insertDIE - Insert DIE into the map.
108 void insertDIE(MDNode *N, DIE *D) {
109 GVToDieMap.insert(std::make_pair(N, D));
110 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000111
Devang Pateld90672c2009-11-20 21:37:22 +0000112 /// getDIEEntry - Returns the debug information entry for the speciefied
113 /// debug variable.
Devang Patel8287d662009-12-15 19:16:48 +0000114 DIEEntry *getDIEEntry(MDNode *N) {
Devang Patel7d707f92010-01-19 06:19:05 +0000115 DenseMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel8287d662009-12-15 19:16:48 +0000116 if (I == GVToDIEEntryMap.end())
117 return NULL;
118 return I->second;
119 }
Devang Pateld90672c2009-11-20 21:37:22 +0000120
121 /// insertDIEEntry - Insert debug information entry into the map.
122 void insertDIEEntry(MDNode *N, DIEEntry *E) {
123 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000124 }
125
Devang Patelc50078e2009-11-21 02:48:08 +0000126 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000127 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000128 void addDie(DIE *Buffer) {
129 this->CUDie->addChild(Buffer);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000130 }
Devang Patel1233f912009-11-21 00:31:03 +0000131
132 // getIndexTyDie - Get an anonymous type for index type.
133 DIE *getIndexTyDie() {
134 return IndexTyDie;
135 }
136
Jim Grosbachb23f2422009-11-22 19:20:36 +0000137 // setIndexTyDie - Set D as anonymous type for index which can be reused
138 // later.
Devang Patel1233f912009-11-21 00:31:03 +0000139 void setIndexTyDie(DIE *D) {
140 IndexTyDie = D;
141 }
142
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000143};
144
145//===----------------------------------------------------------------------===//
146/// DbgVariable - This class is used to track local variable information.
147///
Devang Patel4cb32c32009-11-16 21:53:40 +0000148class DbgVariable {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000149 DIVariable Var; // Variable Descriptor.
150 unsigned FrameIndex; // Variable frame index.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000151 DbgVariable *const AbstractVar; // Abstract variable for this variable.
Devang Patel90a0fe32009-11-10 23:06:00 +0000152 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000153public:
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000154 // AbsVar may be NULL.
155 DbgVariable(DIVariable V, unsigned I, DbgVariable *AbsVar)
156 : Var(V), FrameIndex(I), AbstractVar(AbsVar), TheDIE(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000157
158 // Accessors.
Devang Patel90a0fe32009-11-10 23:06:00 +0000159 DIVariable getVariable() const { return Var; }
160 unsigned getFrameIndex() const { return FrameIndex; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000161 DbgVariable *getAbstractVariable() const { return AbstractVar; }
162 void setDIE(DIE *D) { TheDIE = D; }
163 DIE *getDIE() const { return TheDIE; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000164};
165
166//===----------------------------------------------------------------------===//
167/// DbgScope - This class is used to track scope information.
168///
Devang Patel4cb32c32009-11-16 21:53:40 +0000169class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000170 DbgScope *Parent; // Parent to this scope.
Jim Grosbach652b7432009-11-21 23:12:12 +0000171 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patelae89d3b2010-01-26 21:39:14 +0000172 // Location at which this scope is inlined.
173 AssertingVH<MDNode> InlinedAtLocation;
Devang Patel90a0fe32009-11-10 23:06:00 +0000174 bool AbstractScope; // Abstract Scope
Chris Lattner855e8f52010-03-09 01:58:53 +0000175 MCSymbol *StartLabel; // Label ID of the beginning of scope.
176 MCSymbol *EndLabel; // Label ID of the end of scope.
Devang Patel90ecd192009-10-01 18:25:23 +0000177 const MachineInstr *LastInsn; // Last instruction of this scope.
178 const MachineInstr *FirstInsn; // First instruction of this scope.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000179 // Scopes defined in scope. Contents not owned.
180 SmallVector<DbgScope *, 4> Scopes;
181 // Variables declared in scope. Contents owned.
182 SmallVector<DbgVariable *, 8> Variables;
Daniel Dunbar41716322009-09-19 20:40:05 +0000183
Owen Anderson696d4862009-06-24 22:53:20 +0000184 // Private state for dump()
185 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000186public:
Devang Pateldd7bb432009-10-14 21:08:09 +0000187 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000188 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Chris Lattner855e8f52010-03-09 01:58:53 +0000189 StartLabel(0), EndLabel(0),
Devang Pateldd7bb432009-10-14 21:08:09 +0000190 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000191 virtual ~DbgScope();
192
193 // Accessors.
194 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000195 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000196 DIDescriptor getDesc() const { return Desc; }
Chris Lattner0f093f82010-03-13 02:17:42 +0000197 MDNode *getInlinedAt() const { return InlinedAtLocation; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000198 MDNode *getScopeNode() const { return Desc.getNode(); }
Chris Lattner855e8f52010-03-09 01:58:53 +0000199 MCSymbol *getStartLabel() const { return StartLabel; }
200 MCSymbol *getEndLabel() const { return EndLabel; }
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000201 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
202 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Chris Lattner855e8f52010-03-09 01:58:53 +0000203 void setStartLabel(MCSymbol *S) { StartLabel = S; }
204 void setEndLabel(MCSymbol *E) { EndLabel = E; }
Devang Patel90ecd192009-10-01 18:25:23 +0000205 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
206 const MachineInstr *getLastInsn() { return LastInsn; }
207 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000208 void setAbstractScope() { AbstractScope = true; }
209 bool isAbstractScope() const { return AbstractScope; }
Devang Patel90ecd192009-10-01 18:25:23 +0000210 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000211
Devang Patelc50078e2009-11-21 02:48:08 +0000212 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000213 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000214 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000215
Devang Patelc50078e2009-11-21 02:48:08 +0000216 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000217 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000218 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000219
Devang Patel98e77302010-01-04 20:44:00 +0000220 void fixInstructionMarkers(DenseMap<const MachineInstr *,
221 unsigned> &MIIndexMap) {
Devang Patel6a260102009-10-01 20:31:14 +0000222 assert (getFirstInsn() && "First instruction is missing!");
Devang Patel98e77302010-01-04 20:44:00 +0000223
224 // Use the end of last child scope as end of this scope.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000225 const SmallVector<DbgScope *, 4> &Scopes = getScopes();
Devang Patel3b620bf2010-01-05 16:59:17 +0000226 const MachineInstr *LastInsn = getFirstInsn();
Devang Patel98e77302010-01-04 20:44:00 +0000227 unsigned LIndex = 0;
228 if (Scopes.empty()) {
229 assert (getLastInsn() && "Inner most scope does not have last insn!");
230 return;
231 }
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000232 for (SmallVector<DbgScope *, 4>::const_iterator SI = Scopes.begin(),
Devang Patel98e77302010-01-04 20:44:00 +0000233 SE = Scopes.end(); SI != SE; ++SI) {
234 DbgScope *DS = *SI;
235 DS->fixInstructionMarkers(MIIndexMap);
236 const MachineInstr *DSLastInsn = DS->getLastInsn();
237 unsigned DSI = MIIndexMap[DSLastInsn];
238 if (DSI > LIndex) {
239 LastInsn = DSLastInsn;
240 LIndex = DSI;
241 }
242 }
Devang Patel067e6d92010-02-17 02:20:34 +0000243
244 unsigned CurrentLastInsnIndex = 0;
245 if (const MachineInstr *CL = getLastInsn())
246 CurrentLastInsnIndex = MIIndexMap[CL];
247 unsigned FIndex = MIIndexMap[getFirstInsn()];
248
249 // Set LastInsn as the last instruction for this scope only if
250 // it follows
251 // 1) this scope's first instruction and
252 // 2) current last instruction for this scope, if any.
253 if (LIndex >= CurrentLastInsnIndex && LIndex >= FIndex)
254 setLastInsn(LastInsn);
Devang Patel6a260102009-10-01 20:31:14 +0000255 }
256
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000257#ifndef NDEBUG
258 void dump() const;
259#endif
260};
261
262#ifndef NDEBUG
263void DbgScope::dump() const {
David Greenedbc06132009-12-24 00:31:35 +0000264 raw_ostream &err = dbgs();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000265 err.indent(IndentLevel);
Devang Patel90a0fe32009-11-10 23:06:00 +0000266 MDNode *N = Desc.getNode();
267 N->dump();
Chris Lattner855e8f52010-03-09 01:58:53 +0000268 err << " [" << StartLabel << ", " << EndLabel << "]\n";
Devang Patel90a0fe32009-11-10 23:06:00 +0000269 if (AbstractScope)
270 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000271
272 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000273 if (!Scopes.empty())
274 err << "Children ...\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000275 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
276 if (Scopes[i] != this)
277 Scopes[i]->dump();
278
279 IndentLevel -= 2;
280}
281#endif
282
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000283DbgScope::~DbgScope() {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000284 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
285 delete Variables[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000286}
287
288} // end llvm namespace
289
Chris Lattner621c44d2009-08-22 20:48:53 +0000290DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Chris Lattner819669a2010-03-09 00:00:15 +0000291 : DwarfPrinter(OS, A, T), ModuleCU(0),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000292 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Chris Lattner0f093f82010-03-13 02:17:42 +0000293 DIEValues(), SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel90a0fe32009-11-10 23:06:00 +0000294 CurrentFnDbgScope(0), DebugTimer(0) {
Chris Lattner0f093f82010-03-13 02:17:42 +0000295 NextStringPoolNumber = 0;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000296 if (TimePassesIsEnabled)
Chris Lattner13bf2e52009-12-28 07:41:18 +0000297 DebugTimer = new Timer("Dwarf Debug Writer");
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000298}
299DwarfDebug::~DwarfDebug() {
Devang Patelc50078e2009-11-21 02:48:08 +0000300 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
301 delete DIEValues[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000302
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000303 delete DebugTimer;
304}
305
Chris Lattner0f093f82010-03-13 02:17:42 +0000306MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
307 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
308 if (Entry.first) return Entry.first;
309
310 Entry.second = NextStringPoolNumber++;
311 return Entry.first = getDWLabel("string", Entry.second);
312}
313
314
Devang Patelc50078e2009-11-21 02:48:08 +0000315/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000316///
Devang Patelc50078e2009-11-21 02:48:08 +0000317void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000318 // Profile the node so that we can make it unique.
319 FoldingSetNodeID ID;
320 Abbrev.Profile(ID);
321
322 // Check the set for priors.
323 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
324
325 // If it's newly added.
326 if (InSet == &Abbrev) {
327 // Add to abbreviation list.
328 Abbreviations.push_back(&Abbrev);
329
330 // Assign the vector position + 1 as its number.
331 Abbrev.setNumber(Abbreviations.size());
332 } else {
333 // Assign existing abbreviation number.
334 Abbrev.setNumber(InSet->getNumber());
335 }
336}
337
Devang Patelc50078e2009-11-21 02:48:08 +0000338/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000339/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000340DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel1233f912009-11-21 00:31:03 +0000341 DIEEntry *Value = new DIEEntry(Entry);
Devang Patelc50078e2009-11-21 02:48:08 +0000342 DIEValues.push_back(Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000343 return Value;
344}
345
Devang Patelc50078e2009-11-21 02:48:08 +0000346/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000347///
Devang Patelc50078e2009-11-21 02:48:08 +0000348void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000349 unsigned Form, uint64_t Integer) {
350 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000351 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000352 DIEValues.push_back(Value);
353 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000354}
355
Devang Patelc50078e2009-11-21 02:48:08 +0000356/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000357///
Devang Patelc50078e2009-11-21 02:48:08 +0000358void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000359 unsigned Form, int64_t Integer) {
360 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000361 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000362 DIEValues.push_back(Value);
363 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000364}
365
Devang Pateldf0f2152009-12-02 15:25:16 +0000366/// addString - Add a string attribute data and value. DIEString only
367/// keeps string reference.
Devang Patelc50078e2009-11-21 02:48:08 +0000368void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnercc564642010-01-23 03:11:46 +0000369 StringRef String) {
Devang Patel1233f912009-11-21 00:31:03 +0000370 DIEValue *Value = new DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000371 DIEValues.push_back(Value);
372 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000373}
374
Devang Patelc50078e2009-11-21 02:48:08 +0000375/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000376///
Devang Patelc50078e2009-11-21 02:48:08 +0000377void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000378 const MCSymbol *Label) {
Chris Lattner703d12e2010-03-08 22:31:46 +0000379 DIEValue *Value = new DIELabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000380 DIEValues.push_back(Value);
381 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000382}
383
Devang Patelc50078e2009-11-21 02:48:08 +0000384/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000385///
Devang Patelc50078e2009-11-21 02:48:08 +0000386void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000387 const MCSymbol *Label,const MCSymbol *Section,
Chris Lattnerf8bfa882010-03-08 23:23:25 +0000388 bool isEH) {
389 DIEValue *Value = new DIESectionOffset(Label, Section, isEH);
Devang Patelc50078e2009-11-21 02:48:08 +0000390 DIEValues.push_back(Value);
391 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000392}
393
Devang Patelc50078e2009-11-21 02:48:08 +0000394/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000395///
Devang Patelc50078e2009-11-21 02:48:08 +0000396void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +0000397 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel1233f912009-11-21 00:31:03 +0000398 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000399 DIEValues.push_back(Value);
400 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000401}
402
Devang Patelc50078e2009-11-21 02:48:08 +0000403/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000404///
Devang Patelc50078e2009-11-21 02:48:08 +0000405void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000406 DIEBlock *Block) {
407 Block->ComputeSize(TD);
Devang Patelc50078e2009-11-21 02:48:08 +0000408 DIEValues.push_back(Block);
409 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000410}
411
Devang Patelc50078e2009-11-21 02:48:08 +0000412/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000413/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000414void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000415 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000416 if (!V->getCompileUnit().Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000417 return;
418
419 unsigned Line = V->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000420 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
421 V->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000422 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000423 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
424 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000425}
426
Devang Patelc50078e2009-11-21 02:48:08 +0000427/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000428/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000429void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000430 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000431 if (!G->getCompileUnit().Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000432 return;
433
434 unsigned Line = G->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000435 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
436 G->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000437 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000438 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
439 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000440}
Devang Patel318d70d2009-08-31 22:47:13 +0000441
Devang Patelc50078e2009-11-21 02:48:08 +0000442/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000443/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000444void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel318d70d2009-08-31 22:47:13 +0000445 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000446 if (!SP->getCompileUnit().Verify())
Devang Patel318d70d2009-08-31 22:47:13 +0000447 return;
Caroline Tice9da96d82009-09-11 18:25:54 +0000448 // If the line number is 0, don't add it.
449 if (SP->getLineNumber() == 0)
450 return;
451
Devang Patel318d70d2009-08-31 22:47:13 +0000452 unsigned Line = SP->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000453 if (!SP->getContext().Verify())
454 return;
455 unsigned FileID = GetOrCreateSourceID(SP->getContext().getDirectory(),
456 SP->getContext().getFilename());
Devang Patel318d70d2009-08-31 22:47:13 +0000457 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000458 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
459 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel318d70d2009-08-31 22:47:13 +0000460}
461
Devang Patelc50078e2009-11-21 02:48:08 +0000462/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000463/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000464void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000465 // If there is no compile unit specified, don't add a line #.
466 DICompileUnit CU = Ty->getCompileUnit();
Devang Patel9e592492010-03-08 20:52:55 +0000467 if (!CU.Verify())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000468 return;
469
470 unsigned Line = Ty->getLineNumber();
Devang Patel7f9e0f32010-03-08 22:02:50 +0000471 if (!Ty->getContext().Verify())
472 return;
473 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
474 Ty->getContext().getFilename());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000475 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000476 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
477 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000478}
479
Devang Patel8287d662009-12-15 19:16:48 +0000480/// addSourceLine - Add location information to specified debug information
481/// entry.
482void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
483 // If there is no compile unit specified, don't add a line #.
Devang Patel9e592492010-03-08 20:52:55 +0000484 if (!NS->getCompileUnit().Verify())
Devang Patel8287d662009-12-15 19:16:48 +0000485 return;
486
487 unsigned Line = NS->getLineNumber();
488 StringRef FN = NS->getFilename();
489 StringRef Dir = NS->getDirectory();
490
491 unsigned FileID = GetOrCreateSourceID(Dir, FN);
492 assert(FileID && "Invalid file id");
493 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
494 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
495}
496
Caroline Tice248d5572009-08-31 21:19:37 +0000497/* Byref variables, in Blocks, are declared by the programmer as
498 "SomeType VarName;", but the compiler creates a
499 __Block_byref_x_VarName struct, and gives the variable VarName
500 either the struct, or a pointer to the struct, as its type. This
501 is necessary for various behind-the-scenes things the compiler
502 needs to do with by-reference variables in blocks.
503
504 However, as far as the original *programmer* is concerned, the
505 variable should still have type 'SomeType', as originally declared.
506
507 The following function dives into the __Block_byref_x_VarName
508 struct to find the original type of the variable. This will be
509 passed back to the code generating the type for the Debug
510 Information Entry for the variable 'VarName'. 'VarName' will then
511 have the original type 'SomeType' in its debug information.
512
513 The original type 'SomeType' will be the type of the field named
514 'VarName' inside the __Block_byref_x_VarName struct.
515
516 NOTE: In order for this to not completely fail on the debugger
517 side, the Debug Information Entry for the variable VarName needs to
518 have a DW_AT_location that tells the debugger how to unwind through
519 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patelc50078e2009-11-21 02:48:08 +0000520 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-08-31 21:19:37 +0000521
522/// Find the type the programmer originally declared the variable to be
523/// and return that type.
524///
Devang Patelc50078e2009-11-21 02:48:08 +0000525DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-08-31 21:19:37 +0000526
527 DIType subType = Ty;
528 unsigned tag = Ty.getTag();
529
530 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000531 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000532 subType = DTy.getTypeDerivedFrom();
533 }
534
535 DICompositeType blockStruct = DICompositeType(subType.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000536 DIArray Elements = blockStruct.getTypeArray();
537
Caroline Tice248d5572009-08-31 21:19:37 +0000538 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
539 DIDescriptor Element = Elements.getElement(i);
540 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000541 if (Name == DT.getName())
Caroline Tice248d5572009-08-31 21:19:37 +0000542 return (DT.getTypeDerivedFrom());
543 }
544
545 return Ty;
546}
547
Devang Patelc50078e2009-11-21 02:48:08 +0000548/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000549/// and generate the DWARF information necessary to find the actual variable
550/// given the extra address information encoded in the DIVariable, starting from
551/// the starting location. Add the DWARF information to the die.
552///
Devang Patelc50078e2009-11-21 02:48:08 +0000553void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000554 unsigned Attribute,
555 const MachineLocation &Location) {
556 const DIVariable &VD = DV->getVariable();
557 DIType Ty = VD.getType();
558
559 // Decode the original location, and use that as the start of the byref
560 // variable's location.
561 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
562 DIEBlock *Block = new DIEBlock();
563
564 if (Location.isReg()) {
565 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000566 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000567 } else {
568 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000569 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
570 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000571 }
572 } else {
573 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000574 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000575 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000576 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
577 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000578 }
579
Devang Patelc50078e2009-11-21 02:48:08 +0000580 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000581 }
582
583 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
584 uint64_t Element = VD.getAddrElement(i);
585
586 if (Element == DIFactory::OpPlus) {
Devang Patelc50078e2009-11-21 02:48:08 +0000587 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
588 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000589 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000590 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000591 } else llvm_unreachable("unknown DIFactory Opcode");
592 }
593
594 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000595 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000596}
597
Caroline Tice248d5572009-08-31 21:19:37 +0000598/* Byref variables, in Blocks, are declared by the programmer as "SomeType
599 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
600 gives the variable VarName either the struct, or a pointer to the struct, as
601 its type. This is necessary for various behind-the-scenes things the
602 compiler needs to do with by-reference variables in Blocks.
603
604 However, as far as the original *programmer* is concerned, the variable
605 should still have type 'SomeType', as originally declared.
606
Devang Patelc50078e2009-11-21 02:48:08 +0000607 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000608 struct to find the original type of the variable, which is then assigned to
609 the variable's Debug Information Entry as its real type. So far, so good.
610 However now the debugger will expect the variable VarName to have the type
611 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbar41716322009-09-19 20:40:05 +0000612 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-08-31 21:19:37 +0000613 pointers and struct to find the actual variable of type SomeType.
614
615 The following function does just that. We start by getting
616 the "normal" location for the variable. This will be the location
617 of either the struct __Block_byref_x_VarName or the pointer to the
618 struct __Block_byref_x_VarName.
619
620 The struct will look something like:
621
622 struct __Block_byref_x_VarName {
623 ... <various fields>
624 struct __Block_byref_x_VarName *forwarding;
625 ... <various other fields>
626 SomeType VarName;
627 ... <maybe more fields>
628 };
629
630 If we are given the struct directly (as our starting point) we
631 need to tell the debugger to:
632
633 1). Add the offset of the forwarding field.
634
Dan Gohmandf1a7ff2010-02-10 16:03:48 +0000635 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000636 struct to use (the real one may have been copied onto the heap).
637
638 3). Add the offset for the field VarName, to find the actual variable.
639
640 If we started with a pointer to the struct, then we need to
641 dereference that pointer first, before the other steps.
642 Translating this into DWARF ops, we will need to append the following
643 to the current location description for the variable:
644
645 DW_OP_deref -- optional, if we start with a pointer
646 DW_OP_plus_uconst <forward_fld_offset>
647 DW_OP_deref
648 DW_OP_plus_uconst <varName_fld_offset>
649
650 That is what this function does. */
651
Devang Patelc50078e2009-11-21 02:48:08 +0000652/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-08-31 21:19:37 +0000653/// provided, and generate the DWARF information necessary to find the
654/// actual Block variable (navigating the Block struct) based on the
655/// starting location. Add the DWARF information to the die. For
656/// more information, read large comment just above here.
657///
Devang Patelc50078e2009-11-21 02:48:08 +0000658void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000659 unsigned Attribute,
660 const MachineLocation &Location) {
Caroline Tice248d5572009-08-31 21:19:37 +0000661 const DIVariable &VD = DV->getVariable();
662 DIType Ty = VD.getType();
663 DIType TmpTy = Ty;
664 unsigned Tag = Ty.getTag();
665 bool isPointer = false;
666
Devang Patel7f75bbe2009-11-25 17:36:49 +0000667 StringRef varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000668
669 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000670 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000671 TmpTy = DTy.getTypeDerivedFrom();
672 isPointer = true;
673 }
674
675 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
676
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000677 // Find the __forwarding field and the variable field in the __Block_byref
678 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000679 DIArray Fields = blockStruct.getTypeArray();
680 DIDescriptor varField = DIDescriptor();
681 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000682
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000683 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
684 DIDescriptor Element = Fields.getElement(i);
685 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000686 StringRef fieldName = DT.getName();
687 if (fieldName == "__forwarding")
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000688 forwardingField = Element;
Devang Patel7f75bbe2009-11-25 17:36:49 +0000689 else if (fieldName == varName)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000690 varField = Element;
691 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000692
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000693 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000694 unsigned int forwardingFieldOffset =
695 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
696 unsigned int varFieldOffset =
697 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000698
Mike Stump2fd84e22009-09-24 23:21:26 +0000699 // Decode the original location, and use that as the start of the byref
700 // variable's location.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000701 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
702 DIEBlock *Block = new DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000703
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000704 if (Location.isReg()) {
705 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000706 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000707 else {
708 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000709 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
710 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000711 }
712 } else {
713 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000714 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000715 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000716 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
717 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000718 }
Caroline Tice248d5572009-08-31 21:19:37 +0000719
Devang Patelc50078e2009-11-21 02:48:08 +0000720 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000721 }
Caroline Tice248d5572009-08-31 21:19:37 +0000722
Mike Stump2fd84e22009-09-24 23:21:26 +0000723 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000724 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000725 if (isPointer)
Devang Patelc50078e2009-11-21 02:48:08 +0000726 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000727
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000728 // Next add the offset for the '__forwarding' field:
729 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
730 // adding the offset if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000731 if (forwardingFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000732 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
733 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000734 }
Caroline Tice248d5572009-08-31 21:19:37 +0000735
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000736 // Now dereference the __forwarding field to get to the real __Block_byref
737 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000738 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000739
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000740 // Now that we've got the real __Block_byref... struct, add the offset
741 // for the variable's field to get to the location of the actual variable:
742 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000743 if (varFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000744 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
745 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000746 }
Caroline Tice248d5572009-08-31 21:19:37 +0000747
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000748 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000749 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000750}
751
Devang Patelc50078e2009-11-21 02:48:08 +0000752/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000753/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000754void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000755 const MachineLocation &Location) {
756 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
757 DIEBlock *Block = new DIEBlock();
758
759 if (Location.isReg()) {
760 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000761 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000762 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000763 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
764 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000765 }
766 } else {
767 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000768 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000769 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000770 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
771 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000772 }
773
Devang Patelc50078e2009-11-21 02:48:08 +0000774 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000775 }
776
Devang Patelc50078e2009-11-21 02:48:08 +0000777 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000778}
779
Devang Patel1a8f9a82009-12-10 19:14:49 +0000780/// addToContextOwner - Add Die into the list of its context owner's children.
781void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Devang Patel9e592492010-03-08 20:52:55 +0000782 if (Context.isType()) {
Devang Patel1a8f9a82009-12-10 19:14:49 +0000783 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
784 ContextDIE->addChild(Die);
Devang Patel8287d662009-12-15 19:16:48 +0000785 } else if (Context.isNameSpace()) {
786 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode()));
787 ContextDIE->addChild(Die);
Devang Patel1a8f9a82009-12-10 19:14:49 +0000788 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
789 ContextDIE->addChild(Die);
790 else
791 ModuleCU->addDie(Die);
792}
793
Devang Patel7f139c12009-12-10 18:05:33 +0000794/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
795/// given DIType.
796DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
797 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
798 if (TyDIE)
799 return TyDIE;
800
801 // Create new type.
802 TyDIE = new DIE(dwarf::DW_TAG_base_type);
803 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
804 if (Ty.isBasicType())
805 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
806 else if (Ty.isCompositeType())
807 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
808 else {
809 assert(Ty.isDerivedType() && "Unknown kind of DIType");
810 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
811 }
812
Devang Patel1a8f9a82009-12-10 19:14:49 +0000813 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel7f139c12009-12-10 18:05:33 +0000814 return TyDIE;
815}
816
Devang Patelc50078e2009-11-21 02:48:08 +0000817/// addType - Add a new type attribute to the specified entity.
Devang Patelfe0be132009-12-09 18:24:21 +0000818void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Devang Patel9e592492010-03-08 20:52:55 +0000819 if (!Ty.isValid())
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000820 return;
821
822 // Check for pre-existence.
Devang Patelfe0be132009-12-09 18:24:21 +0000823 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000824 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000825 if (Entry) {
826 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000827 return;
828 }
829
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000830 // Construct type.
Devang Patel7f139c12009-12-10 18:05:33 +0000831 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000832
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +0000833 // Set up proxy.
834 Entry = createDIEEntry(Buffer);
835 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
836
Devang Patelc50078e2009-11-21 02:48:08 +0000837 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000838}
839
Devang Patelc50078e2009-11-21 02:48:08 +0000840/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patelfe0be132009-12-09 18:24:21 +0000841void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000842 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000843 StringRef Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000844 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000845 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000846 BTy.getEncoding());
847
848 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000849 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000850 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000851 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000852 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000853}
854
Devang Patelc50078e2009-11-21 02:48:08 +0000855/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patelfe0be132009-12-09 18:24:21 +0000856void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000857 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000858 StringRef Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000859 uint64_t Size = DTy.getSizeInBits() >> 3;
860 unsigned Tag = DTy.getTag();
861
862 // FIXME - Workaround for templates.
863 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
864
865 Buffer.setTag(Tag);
866
867 // Map to main type, void will not have a type.
868 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelfe0be132009-12-09 18:24:21 +0000869 addType(&Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000870
871 // Add name if not anonymous or intermediate type.
Devang Patel76b80672009-11-30 23:56:56 +0000872 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000873 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000874
875 // Add size if non-zero (derived types might be zero-sized.)
876 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000877 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000878
879 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000880 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000881 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000882}
883
Devang Patelc50078e2009-11-21 02:48:08 +0000884/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +0000885void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000886 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000887 StringRef Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000888
889 uint64_t Size = CTy.getSizeInBits() >> 3;
890 unsigned Tag = CTy.getTag();
891 Buffer.setTag(Tag);
892
893 switch (Tag) {
894 case dwarf::DW_TAG_vector_type:
895 case dwarf::DW_TAG_array_type:
Devang Patelfe0be132009-12-09 18:24:21 +0000896 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000897 break;
898 case dwarf::DW_TAG_enumeration_type: {
899 DIArray Elements = CTy.getTypeArray();
900
901 // Add enumerators to enumeration type.
902 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
903 DIE *ElemDie = NULL;
Devang Patel9e592492010-03-08 20:52:55 +0000904 DIDescriptor Enum(Elements.getElement(i).getNode());
905 if (Enum.isEnumerator()) {
906 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000907 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +0000908 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000909 }
910 }
911 break;
912 case dwarf::DW_TAG_subroutine_type: {
913 // Add return type.
914 DIArray Elements = CTy.getTypeArray();
915 DIDescriptor RTy = Elements.getElement(0);
Devang Patelfe0be132009-12-09 18:24:21 +0000916 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000917
918 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +0000919 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000920
921 // Add arguments.
922 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
923 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
924 DIDescriptor Ty = Elements.getElement(i);
Devang Patelfe0be132009-12-09 18:24:21 +0000925 addType(Arg, DIType(Ty.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000926 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000927 }
928 }
929 break;
930 case dwarf::DW_TAG_structure_type:
931 case dwarf::DW_TAG_union_type:
932 case dwarf::DW_TAG_class_type: {
933 // Add elements to structure type.
934 DIArray Elements = CTy.getTypeArray();
935
936 // A forward struct declared type may not have elements available.
Devang Patel9e592492010-03-08 20:52:55 +0000937 unsigned N = Elements.getNumElements();
938 if (N == 0)
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000939 break;
940
941 // Add elements to structure type.
Devang Patel9e592492010-03-08 20:52:55 +0000942 for (unsigned i = 0; i < N; ++i) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000943 DIDescriptor Element = Elements.getElement(i);
944 DIE *ElemDie = NULL;
Devang Patel9e592492010-03-08 20:52:55 +0000945 if (Element.isSubprogram())
Devang Patel814a12c2009-12-14 16:18:45 +0000946 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Devang Patel9e592492010-03-08 20:52:55 +0000947 else if (Element.isVariable()) {
Devang Patel423dfa02010-01-30 01:08:30 +0000948 DIVariable DV(Element.getNode());
949 ElemDie = new DIE(dwarf::DW_TAG_variable);
950 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
951 DV.getName());
952 addType(ElemDie, DV.getType());
953 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
954 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
955 addSourceLine(ElemDie, &DV);
Devang Patel9e592492010-03-08 20:52:55 +0000956 } else if (Element.isDerivedType())
Devang Patelfe0be132009-12-09 18:24:21 +0000957 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patel9e592492010-03-08 20:52:55 +0000958 else
959 continue;
Devang Patelc50078e2009-11-21 02:48:08 +0000960 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000961 }
962
Devang Patel20b32102009-08-27 23:51:51 +0000963 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +0000964 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000965
966 unsigned RLang = CTy.getRunTimeLang();
967 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +0000968 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000969 dwarf::DW_FORM_data1, RLang);
Devang Patel5ae52402010-01-26 21:16:06 +0000970
971 DICompositeType ContainingType = CTy.getContainingType();
Devang Patel9e592492010-03-08 20:52:55 +0000972 if (DIDescriptor(ContainingType.getNode()).isCompositeType())
Devang Patel5ae52402010-01-26 21:16:06 +0000973 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
974 getOrCreateTypeDIE(DIType(ContainingType.getNode())));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000975 break;
976 }
977 default:
978 break;
979 }
980
981 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000982 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000983 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000984
Devang Pateldc73c372010-01-29 18:34:58 +0000985 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000986 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
987 // Add size if non-zero (derived types might be zero-sized.)
988 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000989 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000990 else {
991 // Add zero size if it is not a forward declaration.
992 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000993 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000994 else
Devang Patelc50078e2009-11-21 02:48:08 +0000995 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000996 }
997
998 // Add source line info if available.
999 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001000 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001001 }
1002}
1003
Devang Patelc50078e2009-11-21 02:48:08 +00001004/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1005void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001006 int64_t L = SR.getLo();
1007 int64_t H = SR.getHi();
1008 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1009
Devang Patelc50078e2009-11-21 02:48:08 +00001010 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +00001011 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +00001012 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld3df6972009-12-04 23:10:24 +00001013 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001014
Devang Patelc50078e2009-11-21 02:48:08 +00001015 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001016}
1017
Devang Patelc50078e2009-11-21 02:48:08 +00001018/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +00001019void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001020 DICompositeType *CTy) {
1021 Buffer.setTag(dwarf::DW_TAG_array_type);
1022 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001023 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001024
1025 // Emit derived type.
Devang Patelfe0be132009-12-09 18:24:21 +00001026 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001027 DIArray Elements = CTy->getTypeArray();
1028
Devang Patel1233f912009-11-21 00:31:03 +00001029 // Get an anonymous type for index type.
Devang Patelfe0be132009-12-09 18:24:21 +00001030 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel1233f912009-11-21 00:31:03 +00001031 if (!IdxTy) {
1032 // Construct an anonymous type for index type.
1033 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +00001034 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1035 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +00001036 dwarf::DW_ATE_signed);
Devang Patelfe0be132009-12-09 18:24:21 +00001037 ModuleCU->addDie(IdxTy);
1038 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +00001039 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001040
1041 // Add subranges to array type.
1042 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1043 DIDescriptor Element = Elements.getElement(i);
1044 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001045 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001046 }
1047}
1048
Devang Patelc50078e2009-11-21 02:48:08 +00001049/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel9e592492010-03-08 20:52:55 +00001050DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001051 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel9e592492010-03-08 20:52:55 +00001052 StringRef Name = ETy.getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001053 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel9e592492010-03-08 20:52:55 +00001054 int64_t Value = ETy.getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +00001055 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001056 return Enumerator;
1057}
1058
Devang Patel141e1c42010-01-05 01:46:14 +00001059/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1060/// printer to not emit usual symbol prefix before the symbol name is used then
1061/// return linkage name after skipping this special LLVM prefix.
1062static StringRef getRealLinkageName(StringRef LinkageName) {
1063 char One = '\1';
1064 if (LinkageName.startswith(StringRef(&One, 1)))
1065 return LinkageName.substr(1);
1066 return LinkageName;
1067}
1068
Devang Patelc50078e2009-11-21 02:48:08 +00001069/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patelfe0be132009-12-09 18:24:21 +00001070DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001071 // If the global variable was optmized out then no need to create debug info
1072 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +00001073 if (!GV.getGlobal()) return NULL;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001074 if (GV.getDisplayName().empty()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +00001075
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001076 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +00001077 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001078 GV.getDisplayName());
1079
Devang Patel7f75bbe2009-11-25 17:36:49 +00001080 StringRef LinkageName = GV.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001081 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001082 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001083 getRealLinkageName(LinkageName));
1084
Devang Patelfe0be132009-12-09 18:24:21 +00001085 addType(GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001086 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001087 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1088 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001089
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001090 return GVDie;
1091}
1092
Devang Patelc50078e2009-11-21 02:48:08 +00001093/// createMemberDIE - Create new member DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001094DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001095 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel7f75bbe2009-11-25 17:36:49 +00001096 StringRef Name = DT.getName();
1097 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001098 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001099
Devang Patelfe0be132009-12-09 18:24:21 +00001100 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001101
Devang Patelc50078e2009-11-21 02:48:08 +00001102 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001103
Devang Patel7d9fe582009-11-04 22:06:12 +00001104 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001105 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001106
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001107 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001108 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001109
1110 if (Size != FieldSize) {
1111 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001112 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1113 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001114
1115 uint64_t Offset = DT.getOffsetInBits();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001116 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1117 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer631e3e32010-01-07 17:50:57 +00001118 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001119 Offset -= FieldOffset;
1120
1121 // Maybe we need to work from the other end.
1122 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001123 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001124
Devang Patel7d9fe582009-11-04 22:06:12 +00001125 // Here WD_AT_data_member_location points to the anonymous
1126 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001127 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001128
1129 } else
1130 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001131 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001132
Devang Patel84aba942010-02-03 20:08:48 +00001133 if (DT.getTag() == dwarf::DW_TAG_inheritance
1134 && DT.isVirtual()) {
1135
1136 // For C++, virtual base classes are not at fixed offset. Use following
1137 // expression to extract appropriate offset from vtable.
1138 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1139
1140 DIEBlock *VBaseLocationDie = new DIEBlock();
1141 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1142 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1143 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1144 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1145 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1146 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1147 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1148
1149 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1150 VBaseLocationDie);
1151 } else
1152 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001153
1154 if (DT.isProtected())
Devang Patel188c85d2009-12-03 19:11:07 +00001155 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001156 dwarf::DW_ACCESS_protected);
1157 else if (DT.isPrivate())
Devang Patel188c85d2009-12-03 19:11:07 +00001158 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001159 dwarf::DW_ACCESS_private);
Devang Patel188c85d2009-12-03 19:11:07 +00001160 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1161 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1162 dwarf::DW_ACCESS_public);
1163 if (DT.isVirtual())
1164 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1165 dwarf::DW_VIRTUALITY_virtual);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001166 return MemberDie;
1167}
1168
Devang Patel814a12c2009-12-14 16:18:45 +00001169/// createSubprogramDIE - Create new DIE using SP.
1170DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1171 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1172 if (SPDie)
1173 return SPDie;
1174
1175 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel36cd9f82010-03-02 17:58:15 +00001176 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel0ab194f2010-03-02 01:26:20 +00001177 if (!SP.getName().empty())
1178 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001179
Devang Patel7f75bbe2009-11-25 17:36:49 +00001180 StringRef LinkageName = SP.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001181 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001182 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001183 getRealLinkageName(LinkageName));
1184
Devang Patelc50078e2009-11-21 02:48:08 +00001185 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001186
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001187 // Add prototyped tag, if C or ObjC.
1188 unsigned Lang = SP.getCompileUnit().getLanguage();
1189 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1190 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001191 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001192
1193 // Add Return Type.
Devang Patelc1df8792009-12-03 01:25:38 +00001194 DICompositeType SPTy = SP.getType();
1195 DIArray Args = SPTy.getTypeArray();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001196 unsigned SPTag = SPTy.getTag();
Devang Patel188c85d2009-12-03 19:11:07 +00001197
Devang Patel9e592492010-03-08 20:52:55 +00001198 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelfe0be132009-12-09 18:24:21 +00001199 addType(SPDie, SPTy);
Devang Patelc1df8792009-12-03 01:25:38 +00001200 else
Devang Patelfe0be132009-12-09 18:24:21 +00001201 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patelc1df8792009-12-03 01:25:38 +00001202
Devang Patel188c85d2009-12-03 19:11:07 +00001203 unsigned VK = SP.getVirtuality();
1204 if (VK) {
1205 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1206 DIEBlock *Block = new DIEBlock();
1207 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1208 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1209 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel7d707f92010-01-19 06:19:05 +00001210 ContainingTypeMap.insert(std::make_pair(SPDie,
1211 SP.getContainingType().getNode()));
Devang Patel188c85d2009-12-03 19:11:07 +00001212 }
1213
Devang Patel814a12c2009-12-14 16:18:45 +00001214 if (MakeDecl || !SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001215 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001216
1217 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patelc1df8792009-12-03 01:25:38 +00001218 // be handled while processing variables.
1219 DICompositeType SPTy = SP.getType();
1220 DIArray Args = SPTy.getTypeArray();
1221 unsigned SPTag = SPTy.getTag();
1222
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001223 if (SPTag == dwarf::DW_TAG_subroutine_type)
1224 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1225 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc5dc7252010-02-06 01:02:37 +00001226 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1227 addType(Arg, ATy);
1228 if (ATy.isArtificial())
1229 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc50078e2009-11-21 02:48:08 +00001230 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001231 }
1232 }
1233
Devang Patelbf7d00a2010-02-03 19:57:19 +00001234 if (SP.isArtificial())
1235 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1236
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001237 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001238 ModuleCU->insertDIE(SP.getNode(), SPDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001239 return SPDie;
1240}
1241
Devang Patel90a0fe32009-11-10 23:06:00 +00001242/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1243/// Initialize scope and update scope hierarchy.
1244DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1245 MDNode *InlinedAt) {
1246 assert (N && "Invalid Scope encoding!");
1247 assert (MI && "Missing machine instruction!");
1248 bool GetConcreteScope = (MI && InlinedAt);
1249
1250 DbgScope *NScope = NULL;
1251
1252 if (InlinedAt)
1253 NScope = DbgScopeMap.lookup(InlinedAt);
1254 else
1255 NScope = DbgScopeMap.lookup(N);
1256 assert (NScope && "Unable to find working scope!");
1257
1258 if (NScope->getFirstInsn())
1259 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001260
1261 DbgScope *Parent = NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001262 if (GetConcreteScope) {
Devang Pateldd7bb432009-10-14 21:08:09 +00001263 DILocation IL(InlinedAt);
Jim Grosbach652b7432009-11-21 23:12:12 +00001264 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel90a0fe32009-11-10 23:06:00 +00001265 IL.getOrigLocation().getNode());
1266 assert (Parent && "Unable to find Parent scope!");
1267 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001268 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001269 } else if (DIDescriptor(N).isLexicalBlock()) {
1270 DILexicalBlock DB(N);
Devang Patel9e592492010-03-08 20:52:55 +00001271 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1272 NScope->setParent(Parent);
1273 Parent->addScope(NScope);
Devang Pateldd7bb432009-10-14 21:08:09 +00001274 }
Devang Patel6a260102009-10-01 20:31:14 +00001275
Devang Patelf5278f22009-10-27 20:47:17 +00001276 NScope->setFirstInsn(MI);
Devang Patel6a260102009-10-01 20:31:14 +00001277
Devang Patel90a0fe32009-11-10 23:06:00 +00001278 if (!Parent && !InlinedAt) {
Devang Patelce8986f2009-11-11 00:31:36 +00001279 StringRef SPName = DISubprogram(N).getLinkageName();
1280 if (SPName == MF->getFunction()->getName())
1281 CurrentFnDbgScope = NScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00001282 }
Devang Patel6a260102009-10-01 20:31:14 +00001283
Devang Patel90a0fe32009-11-10 23:06:00 +00001284 if (GetConcreteScope) {
1285 ConcreteScopes[InlinedAt] = NScope;
1286 getOrCreateAbstractScope(N);
1287 }
1288
Devang Patelf5278f22009-10-27 20:47:17 +00001289 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001290}
1291
Devang Patel90a0fe32009-11-10 23:06:00 +00001292DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1293 assert (N && "Invalid Scope encoding!");
1294
1295 DbgScope *AScope = AbstractScopes.lookup(N);
1296 if (AScope)
1297 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001298
Devang Patel90a0fe32009-11-10 23:06:00 +00001299 DbgScope *Parent = NULL;
1300
1301 DIDescriptor Scope(N);
1302 if (Scope.isLexicalBlock()) {
1303 DILexicalBlock DB(N);
1304 DIDescriptor ParentDesc = DB.getContext();
Devang Patel9e592492010-03-08 20:52:55 +00001305 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001306 }
1307
1308 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1309
1310 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001311 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001312 AScope->setAbstractScope();
1313 AbstractScopes[N] = AScope;
1314 if (DIDescriptor(N).isSubprogram())
1315 AbstractScopesList.push_back(AScope);
1316 return AScope;
1317}
Devang Patel6a260102009-10-01 20:31:14 +00001318
Jim Grosbach652b7432009-11-21 23:12:12 +00001319/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-11-21 02:48:08 +00001320/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1321/// If there are global variables in this scope then create and insert
1322/// DIEs for these variables.
1323DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Pateld90672c2009-11-20 21:37:22 +00001324 DIE *SPDie = ModuleCU->getDIE(SPNode);
Chris Lattnerdd21da82010-03-13 07:26:18 +00001325 assert(SPDie && "Unable to find subprogram DIE!");
Devang Patel814a12c2009-12-14 16:18:45 +00001326 DISubprogram SP(SPNode);
Chris Lattnerdd21da82010-03-13 07:26:18 +00001327
Devang Patel530a81e2010-02-05 23:09:20 +00001328 // There is not any need to generate specification DIE for a function
1329 // defined at compile unit level. If a function is defined inside another
1330 // function then gdb prefers the definition at top level and but does not
1331 // expect specification DIE in parent function. So avoid creating
1332 // specification DIE for a function defined inside a function.
Chris Lattnerdd21da82010-03-13 07:26:18 +00001333 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
1334 !SP.getContext().isFile() && !SP.getContext().isSubprogram()) {
Devang Patel814a12c2009-12-14 16:18:45 +00001335 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Chris Lattnerdd21da82010-03-13 07:26:18 +00001336
1337 // Add arguments.
Devang Patel814a12c2009-12-14 16:18:45 +00001338 DICompositeType SPTy = SP.getType();
1339 DIArray Args = SPTy.getTypeArray();
1340 unsigned SPTag = SPTy.getTag();
1341 if (SPTag == dwarf::DW_TAG_subroutine_type)
Chris Lattnerdd21da82010-03-13 07:26:18 +00001342 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Devang Patel814a12c2009-12-14 16:18:45 +00001343 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc5dc7252010-02-06 01:02:37 +00001344 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1345 addType(Arg, ATy);
1346 if (ATy.isArtificial())
1347 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel814a12c2009-12-14 16:18:45 +00001348 SPDie->addChild(Arg);
1349 }
1350 DIE *SPDeclDie = SPDie;
1351 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1352 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1353 SPDeclDie);
Devang Patel814a12c2009-12-14 16:18:45 +00001354 ModuleCU->addDie(SPDie);
1355 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001356
Devang Patelc50078e2009-11-21 02:48:08 +00001357 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001358 getDWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001359 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001360 getDWLabel("func_end", SubprogramCount));
Devang Patel90a0fe32009-11-10 23:06:00 +00001361 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patelc50078e2009-11-21 02:48:08 +00001362 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach652b7432009-11-21 23:12:12 +00001363
Devang Patel90a0fe32009-11-10 23:06:00 +00001364 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001365 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001366
Devang Patel90a0fe32009-11-10 23:06:00 +00001367 return SPDie;
1368}
1369
Jim Grosbach652b7432009-11-21 23:12:12 +00001370/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001371/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1372DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Chris Lattner855e8f52010-03-09 01:58:53 +00001373 MCSymbol *Start = Scope->getStartLabel();
1374 MCSymbol *End = Scope->getEndLabel();
Chris Lattner4ba1da92010-03-09 04:48:35 +00001375 if (Start == 0) return 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00001376
Chris Lattner855e8f52010-03-09 01:58:53 +00001377 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1378 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Chris Lattner5e423432010-03-09 01:51:43 +00001379
Devang Patel90a0fe32009-11-10 23:06:00 +00001380 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1381 if (Scope->isAbstractScope())
1382 return ScopeDIE;
1383
Devang Patelc50078e2009-11-21 02:48:08 +00001384 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Chris Lattner855e8f52010-03-09 01:58:53 +00001385 Start ? Start : getDWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001386 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Chris Lattner855e8f52010-03-09 01:58:53 +00001387 End ? End : getDWLabel("func_end", SubprogramCount));
Devang Patel90a0fe32009-11-10 23:06:00 +00001388
1389 return ScopeDIE;
1390}
1391
Devang Patelc50078e2009-11-21 02:48:08 +00001392/// constructInlinedScopeDIE - This scope represents inlined body of
1393/// a function. Construct DIE to represent this concrete inlined copy
1394/// of the function.
1395DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Chris Lattner855e8f52010-03-09 01:58:53 +00001396 MCSymbol *StartLabel = Scope->getStartLabel();
1397 MCSymbol *EndLabel = Scope->getEndLabel();
Chris Lattner4ba1da92010-03-09 04:48:35 +00001398 if (StartLabel == 0) return 0;
1399
Chris Lattner855e8f52010-03-09 01:58:53 +00001400 assert(StartLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001401 "Invalid starting label for an inlined scope!");
Chris Lattner855e8f52010-03-09 01:58:53 +00001402 assert(EndLabel->isDefined() &&
Chris Lattner5e423432010-03-09 01:51:43 +00001403 "Invalid end label for an inlined scope!");
Devang Patel9e592492010-03-08 20:52:55 +00001404 if (!Scope->getScopeNode())
Devang Patelbe149872010-03-08 19:20:38 +00001405 return NULL;
Devang Patel9e592492010-03-08 20:52:55 +00001406 DIScope DS(Scope->getScopeNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001407 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1408
1409 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001410 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001411 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001412 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001413 dwarf::DW_FORM_ref4, OriginDIE);
1414
Chris Lattneread58652010-03-09 00:31:02 +00001415 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
Chris Lattner855e8f52010-03-09 01:58:53 +00001416 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Devang Patel90a0fe32009-11-10 23:06:00 +00001417
1418 InlinedSubprogramDIEs.insert(OriginDIE);
1419
1420 // Track the start label for this inlined function.
Devang Patel7d707f92010-01-19 06:19:05 +00001421 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel90a0fe32009-11-10 23:06:00 +00001422 I = InlineInfo.find(InlinedSP.getNode());
1423
1424 if (I == InlineInfo.end()) {
Chris Lattneread58652010-03-09 00:31:02 +00001425 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartLabel,
Jim Grosbachb23f2422009-11-22 19:20:36 +00001426 ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001427 InlinedSPNodes.push_back(InlinedSP.getNode());
1428 } else
Chris Lattneread58652010-03-09 00:31:02 +00001429 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001430
Devang Patel90a0fe32009-11-10 23:06:00 +00001431 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001432 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1433 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001434
1435 return ScopeDIE;
1436}
1437
Devang Patelc50078e2009-11-21 02:48:08 +00001438
1439/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelfe0be132009-12-09 18:24:21 +00001440DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001441 // Get the descriptor.
1442 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001443 StringRef Name = VD.getName();
1444 if (Name.empty())
Devang Patelab9a0682009-11-13 02:25:26 +00001445 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001446
1447 // Translate tag to proper Dwarf tag. The result variable is dropped for
1448 // now.
1449 unsigned Tag;
1450 switch (VD.getTag()) {
1451 case dwarf::DW_TAG_return_variable:
1452 return NULL;
1453 case dwarf::DW_TAG_arg_variable:
1454 Tag = dwarf::DW_TAG_formal_parameter;
1455 break;
1456 case dwarf::DW_TAG_auto_variable: // fall thru
1457 default:
1458 Tag = dwarf::DW_TAG_variable;
1459 break;
1460 }
1461
1462 // Define variable debug information entry.
1463 DIE *VariableDie = new DIE(Tag);
1464
1465
1466 DIE *AbsDIE = NULL;
1467 if (DbgVariable *AV = DV->getAbstractVariable())
1468 AbsDIE = AV->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001469
Devang Patel90a0fe32009-11-10 23:06:00 +00001470 if (AbsDIE) {
1471 DIScope DS(Scope->getScopeNode());
1472 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001473 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001474 (void) OriginSPDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001475 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1476 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1477 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001478 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001479 dwarf::DW_FORM_ref4, AbsDIE);
1480 }
1481 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001482 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1483 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001484
1485 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001486 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001487 // addresses instead.
1488 if (VD.isBlockByrefVariable())
Devang Patelfe0be132009-12-09 18:24:21 +00001489 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001490 else
Devang Patelfe0be132009-12-09 18:24:21 +00001491 addType(VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001492 }
1493
1494 // Add variable address.
1495 if (!Scope->isAbstractScope()) {
1496 MachineLocation Location;
Jim Grosbach587d4882009-11-22 20:14:00 +00001497 unsigned FrameReg;
1498 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1499 Location.set(FrameReg, Offset);
Jim Grosbach652b7432009-11-21 23:12:12 +00001500
Devang Patel90a0fe32009-11-10 23:06:00 +00001501 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001502 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001503 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001504 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001505 else
Devang Patelc50078e2009-11-21 02:48:08 +00001506 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001507 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001508
1509 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1510 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001511 DV->setDIE(VariableDie);
1512 return VariableDie;
1513
1514}
Devang Patelc50078e2009-11-21 02:48:08 +00001515
Devang Patelec13b4f2009-11-24 01:14:22 +00001516void DwarfDebug::addPubTypes(DISubprogram SP) {
1517 DICompositeType SPTy = SP.getType();
1518 unsigned SPTag = SPTy.getTag();
1519 if (SPTag != dwarf::DW_TAG_subroutine_type)
1520 return;
1521
1522 DIArray Args = SPTy.getTypeArray();
Devang Patelec13b4f2009-11-24 01:14:22 +00001523 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1524 DIType ATy(Args.getElement(i).getNode());
Devang Patel9e592492010-03-08 20:52:55 +00001525 if (!ATy.isValid())
Devang Patelec13b4f2009-11-24 01:14:22 +00001526 continue;
1527 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel9e592492010-03-08 20:52:55 +00001528 if (DIDescriptor(CATy.getNode()).Verify() && !CATy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001529 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1530 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1531 }
1532 }
1533}
1534
Devang Patelc50078e2009-11-21 02:48:08 +00001535/// constructScopeDIE - Construct a DIE for this scope.
1536DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel9e592492010-03-08 20:52:55 +00001537 if (!Scope || !Scope->getScopeNode())
1538 return NULL;
1539
1540 DIScope DS(Scope->getScopeNode());
1541 DIE *ScopeDIE = NULL;
1542 if (Scope->getInlinedAt())
1543 ScopeDIE = constructInlinedScopeDIE(Scope);
1544 else if (DS.isSubprogram()) {
1545 if (Scope->isAbstractScope())
1546 ScopeDIE = ModuleCU->getDIE(DS.getNode());
1547 else
1548 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
1549 }
1550 else {
1551 ScopeDIE = constructLexicalScopeDIE(Scope);
1552 if (!ScopeDIE) return NULL;
1553 }
1554
Devang Patel90a0fe32009-11-10 23:06:00 +00001555 // Add variables to scope.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001556 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel90a0fe32009-11-10 23:06:00 +00001557 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelfe0be132009-12-09 18:24:21 +00001558 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach652b7432009-11-21 23:12:12 +00001559 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001560 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001561 }
1562
1563 // Add nested scopes.
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001564 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel90a0fe32009-11-10 23:06:00 +00001565 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1566 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001567 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001568 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001569 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001570 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001571
1572 if (DS.isSubprogram())
1573 addPubTypes(DISubprogram(DS.getNode()));
1574
1575 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001576}
1577
Bill Wendlingf5839192009-05-20 23:19:06 +00001578/// GetOrCreateSourceID - Look up the source id with the given directory and
1579/// source file names. If none currently exists, create a new id and insert it
1580/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1581/// maps as well.
Devang Patel7f75bbe2009-11-25 17:36:49 +00001582unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001583 unsigned DId;
1584 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1585 if (DI != DirectoryIdMap.end()) {
1586 DId = DI->getValue();
1587 } else {
1588 DId = DirectoryNames.size() + 1;
1589 DirectoryIdMap[DirName] = DId;
1590 DirectoryNames.push_back(DirName);
1591 }
1592
1593 unsigned FId;
1594 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1595 if (FI != SourceFileIdMap.end()) {
1596 FId = FI->getValue();
1597 } else {
1598 FId = SourceFileNames.size() + 1;
1599 SourceFileIdMap[FileName] = FId;
1600 SourceFileNames.push_back(FileName);
1601 }
1602
1603 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1604 SourceIdMap.find(std::make_pair(DId, FId));
1605 if (SI != SourceIdMap.end())
1606 return SI->second;
1607
1608 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1609 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1610 SourceIds.push_back(std::make_pair(DId, FId));
1611
1612 return SrcId;
1613}
1614
Devang Patel8287d662009-12-15 19:16:48 +00001615/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1616DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1617 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1618 if (NDie)
1619 return NDie;
1620 NDie = new DIE(dwarf::DW_TAG_namespace);
1621 ModuleCU->insertDIE(NS.getNode(), NDie);
1622 if (!NS.getName().empty())
1623 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1624 addSourceLine(NDie, &NS);
1625 addToContextOwner(NDie, NS.getContext());
1626 return NDie;
1627}
1628
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001629void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001630 DICompileUnit DIUnit(N);
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001631 // Use first compile unit marked as isMain as the compile unit for this
1632 // module.
1633 if (ModuleCU || !DIUnit.isMain())
1634 return;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001635 StringRef FN = DIUnit.getFilename();
1636 StringRef Dir = DIUnit.getDirectory();
Devang Patelaaf012e2009-09-29 18:40:58 +00001637 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001638
1639 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001640 // FIXME: Why getting the delta between two identical labels??
Devang Patelc50078e2009-11-21 02:48:08 +00001641 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00001642 getTempLabel("section_line"), getTempLabel("section_line"),
Bill Wendlingf5839192009-05-20 23:19:06 +00001643 false);
Devang Patelc50078e2009-11-21 02:48:08 +00001644 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001645 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001646 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001647 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001648 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001649
Devang Patel7f75bbe2009-11-25 17:36:49 +00001650 if (!Dir.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001651 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001652 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001653 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001654
Devang Patel7f75bbe2009-11-25 17:36:49 +00001655 StringRef Flags = DIUnit.getFlags();
1656 if (!Flags.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001657 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001658
1659 unsigned RVer = DIUnit.getRunTimeVersion();
1660 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001661 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001662 dwarf::DW_FORM_data1, RVer);
1663
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001664 assert(!ModuleCU &&
1665 "ModuleCU assigned since the top of constructCompileUnit");
1666 ModuleCU = new CompileUnit(ID, Die);
Bill Wendlingf5839192009-05-20 23:19:06 +00001667}
1668
Devang Patelc50078e2009-11-21 02:48:08 +00001669void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001670 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001671
Devang Patel0c03f062009-09-04 23:59:07 +00001672 // If debug information is malformed then ignore it.
1673 if (DI_GV.Verify() == false)
1674 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001675
1676 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001677 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel166f8432009-06-26 01:49:18 +00001678 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001679
Devang Patelfe0be132009-12-09 18:24:21 +00001680 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel6d479632009-12-10 23:25:41 +00001681 if (!VariableDie)
1682 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001683
Bill Wendlingf5839192009-05-20 23:19:06 +00001684 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001685 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001686
1687 // Add to context owner.
Devang Pateldcc0dce2010-01-15 01:12:22 +00001688 DIDescriptor GVContext = DI_GV.getContext();
1689 // Do not create specification DIE if context is either compile unit
1690 // or a subprogram.
1691 if (DI_GV.isDefinition() && !GVContext.isCompileUnit()
Devang Patel76667012010-03-11 23:44:52 +00001692 && !GVContext.isFile() && !GVContext.isSubprogram()) {
Devang Patel8287d662009-12-15 19:16:48 +00001693 // Create specification DIE.
1694 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1695 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1696 dwarf::DW_FORM_ref4, VariableDie);
1697 DIEBlock *Block = new DIEBlock();
1698 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001699 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001700 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001701 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patela0a98582010-02-09 01:58:33 +00001702 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel8287d662009-12-15 19:16:48 +00001703 ModuleCU->addDie(VariableSpecDIE);
1704 } else {
1705 DIEBlock *Block = new DIEBlock();
1706 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Chris Lattner703d12e2010-03-08 22:31:46 +00001707 addLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner90825792010-03-12 21:09:07 +00001708 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001709 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1710 }
Devang Pateldcc0dce2010-01-15 01:12:22 +00001711 addToContextOwner(VariableDie, GVContext);
Devang Patel1a8f9a82009-12-10 19:14:49 +00001712
Bill Wendlingf5839192009-05-20 23:19:06 +00001713 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001714 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001715
1716 DIType GTy = DI_GV.getType();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001717 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001718 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1719 assert (Entry && "Missing global type!");
1720 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1721 }
Devang Patel166f8432009-06-26 01:49:18 +00001722 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001723}
1724
Devang Patelc50078e2009-11-21 02:48:08 +00001725void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001726 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001727
1728 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001729 if (ModuleCU->getDIE(N))
Devang Patel166f8432009-06-26 01:49:18 +00001730 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001731
1732 if (!SP.isDefinition())
1733 // This is a method declaration which will be handled while constructing
1734 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001735 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001736
Devang Patelfe0be132009-12-09 18:24:21 +00001737 DIE *SubprogramDie = createSubprogramDIE(SP);
Bill Wendlingf5839192009-05-20 23:19:06 +00001738
1739 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001740 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001741
1742 // Add to context owner.
Devang Patel8287d662009-12-15 19:16:48 +00001743 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patelde2d3682009-12-08 23:21:45 +00001744
Bill Wendlingf5839192009-05-20 23:19:06 +00001745 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001746 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001747
Devang Patel166f8432009-06-26 01:49:18 +00001748 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001749}
1750
Devang Patelc50078e2009-11-21 02:48:08 +00001751/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001752/// content. Create global DIEs and emit initial debug info sections.
1753/// This is inovked by the target AsmPrinter.
Devang Patelc50078e2009-11-21 02:48:08 +00001754void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel59a1d422009-06-25 22:36:02 +00001755 this->M = M;
1756
Bill Wendlingf5839192009-05-20 23:19:06 +00001757 if (TimePassesIsEnabled)
1758 DebugTimer->startTimer();
1759
Devang Patel1b4d6832009-11-11 19:55:08 +00001760 if (!MAI->doesSupportDebugInformation())
1761 return;
1762
Devang Patelfda766d2009-07-30 18:56:46 +00001763 DebugInfoFinder DbgFinder;
1764 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001765
Bill Wendlingf5839192009-05-20 23:19:06 +00001766 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001767 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1768 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001769 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001770
Devang Patel5a3d37f2009-06-29 20:45:18 +00001771 if (!ModuleCU)
Devang Patel7f9e0f32010-03-08 22:02:50 +00001772 return;
Devang Patelf97a05a2009-06-29 20:38:13 +00001773
Devang Patel90a0fe32009-11-10 23:06:00 +00001774 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001775 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1776 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001777 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001778
Devang Patel1a8f9a82009-12-10 19:14:49 +00001779 // Create DIEs for each global variable.
1780 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1781 E = DbgFinder.global_variable_end(); I != E; ++I)
1782 constructGlobalVariableDIE(*I);
1783
Bill Wendlingf5839192009-05-20 23:19:06 +00001784 MMI = mmi;
1785 shouldEmit = true;
1786 MMI->setDebugInfoAvailability(true);
1787
1788 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001789 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001790
1791 // Print out .file directives to specify files for .loc directives. These are
1792 // printed out early so that they precede any .loc directives.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001793 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001794 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1795 // Remember source id starts at 1.
1796 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerad653482010-01-22 22:09:00 +00001797 // FIXME: don't use sys::path for this! This should not depend on the
1798 // host.
Bill Wendlingf5839192009-05-20 23:19:06 +00001799 sys::Path FullPath(getSourceDirectoryName(Id.first));
1800 bool AppendOk =
1801 FullPath.appendComponent(getSourceFileName(Id.second));
1802 assert(AppendOk && "Could not append filename to directory!");
1803 AppendOk = false;
Chris Lattner59be4ea2010-01-25 18:58:59 +00001804 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001805 }
1806 }
1807
1808 // Emit initial sections
Devang Patelc50078e2009-11-21 02:48:08 +00001809 emitInitial();
Bill Wendlingf5839192009-05-20 23:19:06 +00001810
1811 if (TimePassesIsEnabled)
1812 DebugTimer->stopTimer();
1813}
1814
Devang Patelc50078e2009-11-21 02:48:08 +00001815/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001816///
Devang Patelc50078e2009-11-21 02:48:08 +00001817void DwarfDebug::endModule() {
Devang Patel95d477e2009-10-06 00:03:14 +00001818 if (!ModuleCU)
Bill Wendlingf5839192009-05-20 23:19:06 +00001819 return;
1820
1821 if (TimePassesIsEnabled)
1822 DebugTimer->startTimer();
1823
Devang Patel90a0fe32009-11-10 23:06:00 +00001824 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1825 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1826 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1827 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001828 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001829 }
1830
Devang Patelc1df8792009-12-03 01:25:38 +00001831 // Insert top level DIEs.
1832 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1833 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1834 ModuleCU->getCUDie()->addChild(*TI);
1835
Devang Patel7d707f92010-01-19 06:19:05 +00001836 for (DenseMap<DIE *, MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel188c85d2009-12-03 19:11:07 +00001837 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1838 DIE *SPDie = CI->first;
1839 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1840 if (!N) continue;
1841 DIE *NDie = ModuleCU->getDIE(N);
1842 if (!NDie) continue;
1843 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel69aae432010-01-15 00:26:31 +00001844 // FIXME - This is not the correct approach.
1845 // addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel188c85d2009-12-03 19:11:07 +00001846 }
1847
Bill Wendlingf5839192009-05-20 23:19:06 +00001848 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001849 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00001850 Asm->OutStreamer.EmitLabel(getTempLabel("text_end"));
Chris Lattner73266f92009-08-19 05:49:37 +00001851 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00001852 Asm->OutStreamer.EmitLabel(getTempLabel("data_end"));
Bill Wendlingf5839192009-05-20 23:19:06 +00001853
1854 // End text sections.
1855 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001856 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Chris Lattner6d213e82010-03-08 22:44:40 +00001857 Asm->OutStreamer.EmitLabel(getDWLabel("section_end", i));
Bill Wendlingf5839192009-05-20 23:19:06 +00001858 }
1859
1860 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001861 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001862
1863 // Emit function debug frame information
1864 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1865 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001866 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001867
1868 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001869 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001870
1871 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001872 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001873
1874 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00001875 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00001876
1877 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00001878 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00001879
1880 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00001881 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00001882
Devang Patelec13b4f2009-11-24 01:14:22 +00001883 // Emit info into a debug pubtypes section.
1884 emitDebugPubTypes();
1885
Bill Wendlingf5839192009-05-20 23:19:06 +00001886 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00001887 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00001888
1889 // Emit info into a debug aranges section.
1890 EmitDebugARanges();
1891
1892 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00001893 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00001894
1895 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00001896 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001897
1898 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00001899 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001900
Chris Lattner0f093f82010-03-13 02:17:42 +00001901 // Emit info into a debug str section.
1902 emitDebugStr();
1903
Jeffrey Yasskinc3545712010-03-11 18:29:55 +00001904 delete ModuleCU;
1905 ModuleCU = NULL; // Reset for the next Module, if any.
1906
Bill Wendlingf5839192009-05-20 23:19:06 +00001907 if (TimePassesIsEnabled)
1908 DebugTimer->stopTimer();
1909}
1910
Devang Patel90a0fe32009-11-10 23:06:00 +00001911/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00001912DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1913 unsigned FrameIdx,
Devang Patel90a0fe32009-11-10 23:06:00 +00001914 DILocation &ScopeLoc) {
1915
1916 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1917 if (AbsDbgVariable)
1918 return AbsDbgVariable;
1919
1920 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1921 if (!Scope)
1922 return NULL;
1923
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001924 AbsDbgVariable = new DbgVariable(Var, FrameIdx,
1925 NULL /* No more-abstract variable*/);
Devang Patelc50078e2009-11-21 02:48:08 +00001926 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00001927 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1928 return AbsDbgVariable;
1929}
1930
Devang Patelc50078e2009-11-21 02:48:08 +00001931/// collectVariableInfo - Populate DbgScope entries with variables' info.
1932void DwarfDebug::collectVariableInfo() {
Devang Patel40c80212009-10-09 22:42:28 +00001933 if (!MMI) return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001934
Devang Patel84139992009-10-06 01:26:37 +00001935 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1936 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1937 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel0a0ea052010-01-22 22:52:10 +00001938 MDNode *Var = VI->first;
Devang Patel90a0fe32009-11-10 23:06:00 +00001939 if (!Var) continue;
Devang Patel6882dff2009-10-08 18:48:03 +00001940 DIVariable DV (Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00001941 std::pair< unsigned, MDNode *> VP = VI->second;
1942 DILocation ScopeLoc(VP.second);
1943
1944 DbgScope *Scope =
1945 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1946 if (!Scope)
Jim Grosbach652b7432009-11-21 23:12:12 +00001947 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelbad42262009-11-10 23:20:04 +00001948 // If variable scope is not found then skip this variable.
1949 if (!Scope)
1950 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00001951
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00001952 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, ScopeLoc);
1953 DbgVariable *RegVar = new DbgVariable(DV, VP.first, AbsDbgVariable);
Devang Patelc50078e2009-11-21 02:48:08 +00001954 Scope->addVariable(RegVar);
Devang Patel84139992009-10-06 01:26:37 +00001955 }
1956}
1957
Devang Patelc50078e2009-11-21 02:48:08 +00001958/// beginScope - Process beginning of a scope starting at Label.
Chris Lattner597b0fc2010-03-09 04:54:43 +00001959void DwarfDebug::beginScope(const MachineInstr *MI, MCSymbol *Label) {
Devang Patel393a46d2009-10-06 01:50:42 +00001960 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1961 if (I == DbgScopeBeginMap.end())
1962 return;
Dan Gohman8d34f972009-11-23 21:30:55 +00001963 ScopeVector &SD = I->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00001964 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001965 SDI != SDE; ++SDI)
Chris Lattner597b0fc2010-03-09 04:54:43 +00001966 (*SDI)->setStartLabel(Label);
Devang Patel393a46d2009-10-06 01:50:42 +00001967}
1968
Devang Patelc50078e2009-11-21 02:48:08 +00001969/// endScope - Process end of a scope.
1970void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel393a46d2009-10-06 01:50:42 +00001971 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patelf4348892009-10-06 03:15:38 +00001972 if (I == DbgScopeEndMap.end())
Devang Patel393a46d2009-10-06 01:50:42 +00001973 return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001974
Chris Lattnerd5060b82010-03-14 08:18:13 +00001975 MCSymbol *Label = MMI->getLabelSym(MMI->NextLabelID());
Chris Lattner855e8f52010-03-09 01:58:53 +00001976 Asm->OutStreamer.EmitLabel(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00001977
Chris Lattner855e8f52010-03-09 01:58:53 +00001978 SmallVector<DbgScope*, 2> &SD = I->second;
Devang Patel393a46d2009-10-06 01:50:42 +00001979 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001980 SDI != SDE; ++SDI)
Chris Lattner855e8f52010-03-09 01:58:53 +00001981 (*SDI)->setEndLabel(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00001982 return;
1983}
1984
1985/// createDbgScope - Create DbgScope for the scope.
1986void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1987
1988 if (!InlinedAt) {
1989 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1990 if (WScope)
1991 return;
1992 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1993 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach652b7432009-11-21 23:12:12 +00001994 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel53addbf2009-11-11 00:18:40 +00001995 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel90a0fe32009-11-10 23:06:00 +00001996 return;
1997 }
1998
1999 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2000 if (WScope)
2001 return;
2002
2003 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2004 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2005 DILocation DL(InlinedAt);
2006 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel393a46d2009-10-06 01:50:42 +00002007}
2008
Devang Patelc50078e2009-11-21 02:48:08 +00002009/// extractScopeInformation - Scan machine instructions in this function
Devang Patel6a260102009-10-01 20:31:14 +00002010/// and collect DbgScopes. Return true, if atleast one scope was found.
Chris Lattner69a76972010-01-26 23:18:02 +00002011bool DwarfDebug::extractScopeInformation() {
Devang Patel6a260102009-10-01 20:31:14 +00002012 // If scope information was extracted using .dbg intrinsics then there is not
2013 // any need to extract these information by scanning each instruction.
2014 if (!DbgScopeMap.empty())
2015 return false;
2016
Devang Patel98e77302010-01-04 20:44:00 +00002017 DenseMap<const MachineInstr *, unsigned> MIIndexMap;
2018 unsigned MIIndex = 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00002019 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6a260102009-10-01 20:31:14 +00002020 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2021 I != E; ++I) {
2022 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2023 II != IE; ++II) {
2024 const MachineInstr *MInsn = II;
Devang Patel98e77302010-01-04 20:44:00 +00002025 MIIndexMap[MInsn] = MIIndex++;
Devang Patel6a260102009-10-01 20:31:14 +00002026 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00002027 if (DL.isUnknown()) continue;
Devang Patel042945f2010-01-16 06:09:35 +00002028 DILocation DLT = MF->getDILocation(DL);
2029 DIScope DLTScope = DLT.getScope();
Devang Patel6a260102009-10-01 20:31:14 +00002030 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002031 // other scopes, create one DbgScope now. This will be translated
Devang Patel6a260102009-10-01 20:31:14 +00002032 // into a scope DIE at the end.
Devang Patel042945f2010-01-16 06:09:35 +00002033 if (DLTScope.isCompileUnit()) continue;
2034 createDbgScope(DLTScope.getNode(), DLT.getOrigLocation().getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00002035 }
2036 }
2037
2038
2039 // Build scope hierarchy using working set of scopes.
2040 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2041 I != E; ++I) {
2042 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2043 II != IE; ++II) {
2044 const MachineInstr *MInsn = II;
2045 DebugLoc DL = MInsn->getDebugLoc();
2046 if (DL.isUnknown()) continue;
Devang Patel042945f2010-01-16 06:09:35 +00002047 DILocation DLT = MF->getDILocation(DL);
2048 DIScope DLTScope = DLT.getScope();
Devang Patel90a0fe32009-11-10 23:06:00 +00002049 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002050 // other scopes, create one DbgScope now. This will be translated
Devang Patel90a0fe32009-11-10 23:06:00 +00002051 // into a scope DIE at the end.
Devang Patel042945f2010-01-16 06:09:35 +00002052 if (DLTScope.isCompileUnit()) continue;
2053 DbgScope *Scope = getUpdatedDbgScope(DLTScope.getNode(), MInsn,
2054 DLT.getOrigLocation().getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00002055 Scope->setLastInsn(MInsn);
Devang Patel6a260102009-10-01 20:31:14 +00002056 }
2057 }
2058
Devang Patel98e77302010-01-04 20:44:00 +00002059 if (!CurrentFnDbgScope)
2060 return false;
2061
2062 CurrentFnDbgScope->fixInstructionMarkers(MIIndexMap);
Devang Patel6a260102009-10-01 20:31:14 +00002063
2064 // Each scope has first instruction and last instruction to mark beginning
2065 // and end of a scope respectively. Create an inverse map that list scopes
2066 // starts (and ends) with an instruction. One instruction may start (or end)
Devang Patelfd311df2010-01-20 02:05:23 +00002067 // multiple scopes. Ignore scopes that are not reachable.
2068 SmallVector<DbgScope *, 4> WorkList;
2069 WorkList.push_back(CurrentFnDbgScope);
2070 while (!WorkList.empty()) {
2071 DbgScope *S = WorkList.back(); WorkList.pop_back();
2072
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002073 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patelfd311df2010-01-20 02:05:23 +00002074 if (!Children.empty())
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002075 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patelfd311df2010-01-20 02:05:23 +00002076 SE = Children.end(); SI != SE; ++SI)
2077 WorkList.push_back(*SI);
2078
Devang Patel90a0fe32009-11-10 23:06:00 +00002079 if (S->isAbstractScope())
2080 continue;
Devang Patel6a260102009-10-01 20:31:14 +00002081 const MachineInstr *MI = S->getFirstInsn();
2082 assert (MI && "DbgScope does not have first instruction!");
2083
2084 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2085 if (IDI != DbgScopeBeginMap.end())
2086 IDI->second.push_back(S);
2087 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002088 DbgScopeBeginMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002089
2090 MI = S->getLastInsn();
2091 assert (MI && "DbgScope does not have last instruction!");
2092 IDI = DbgScopeEndMap.find(MI);
2093 if (IDI != DbgScopeEndMap.end())
2094 IDI->second.push_back(S);
2095 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002096 DbgScopeEndMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002097 }
2098
2099 return !DbgScopeMap.empty();
2100}
2101
Devang Patelc50078e2009-11-21 02:48:08 +00002102/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002103/// emitted immediately after the function entry point.
Chris Lattner69a76972010-01-26 23:18:02 +00002104void DwarfDebug::beginFunction(const MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002105 this->MF = MF;
2106
2107 if (!ShouldEmitDwarfDebug()) return;
2108
2109 if (TimePassesIsEnabled)
2110 DebugTimer->startTimer();
2111
Chris Lattner69a76972010-01-26 23:18:02 +00002112 if (!extractScopeInformation())
Devang Patel0feae422009-10-06 18:37:31 +00002113 return;
Devang Patelc50078e2009-11-21 02:48:08 +00002114
2115 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002116
Bill Wendlingf5839192009-05-20 23:19:06 +00002117 // Assumes in correct section after the entry point.
Chris Lattner6d213e82010-03-08 22:44:40 +00002118 Asm->OutStreamer.EmitLabel(getDWLabel("func_begin", ++SubprogramCount));
Bill Wendlingf5839192009-05-20 23:19:06 +00002119
2120 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2121 // function.
Devang Patel40c80212009-10-09 22:42:28 +00002122 DebugLoc FDL = MF->getDefaultDebugLoc();
2123 if (!FDL.isUnknown()) {
Devang Patel042945f2010-01-16 06:09:35 +00002124 DILocation DLT = MF->getDILocation(FDL);
Devang Patel042945f2010-01-16 06:09:35 +00002125 DISubprogram SP = getDISubprogram(DLT.getScope().getNode());
Chris Lattner855e8f52010-03-09 01:58:53 +00002126 unsigned Line, Col;
2127 if (SP.Verify()) {
2128 Line = SP.getLineNumber();
2129 Col = 0;
2130 } else {
2131 Line = DLT.getLineNumber();
2132 Col = DLT.getColumnNumber();
2133 }
2134
Chris Lattner597b0fc2010-03-09 04:54:43 +00002135 recordSourceLine(Line, Col, DLT.getScope().getNode());
Bill Wendlingf5839192009-05-20 23:19:06 +00002136 }
Bill Wendlingf5839192009-05-20 23:19:06 +00002137 if (TimePassesIsEnabled)
2138 DebugTimer->stopTimer();
2139}
2140
Devang Patelc50078e2009-11-21 02:48:08 +00002141/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002142///
Chris Lattner69a76972010-01-26 23:18:02 +00002143void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002144 if (!ShouldEmitDwarfDebug()) return;
2145
2146 if (TimePassesIsEnabled)
2147 DebugTimer->startTimer();
2148
Devang Patel40c80212009-10-09 22:42:28 +00002149 if (DbgScopeMap.empty())
2150 return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002151
Devang Patel98e77302010-01-04 20:44:00 +00002152 if (CurrentFnDbgScope) {
2153 // Define end label for subprogram.
Chris Lattner6d213e82010-03-08 22:44:40 +00002154 Asm->OutStreamer.EmitLabel(getDWLabel("func_end", SubprogramCount));
Devang Patel98e77302010-01-04 20:44:00 +00002155
2156 // Get function line info.
2157 if (!Lines.empty()) {
2158 // Get section line info.
2159 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2160 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2161 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2162 // Append the function info to section info.
2163 SectionLineInfos.insert(SectionLineInfos.end(),
2164 Lines.begin(), Lines.end());
2165 }
2166
2167 // Construct abstract scopes.
2168 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2169 AE = AbstractScopesList.end(); AI != AE; ++AI)
2170 constructScopeDIE(*AI);
2171
2172 constructScopeDIE(CurrentFnDbgScope);
2173
2174 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2175 MMI->getFrameMoves()));
Bill Wendlingf5839192009-05-20 23:19:06 +00002176 }
2177
Bill Wendlingf5839192009-05-20 23:19:06 +00002178 // Clear debug info
Devang Patel387e9c12010-01-19 01:26:02 +00002179 CurrentFnDbgScope = NULL;
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002180 DeleteContainerSeconds(DbgScopeMap);
Devang Patel387e9c12010-01-19 01:26:02 +00002181 DbgScopeBeginMap.clear();
2182 DbgScopeEndMap.clear();
2183 ConcreteScopes.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002184 DeleteContainerSeconds(AbstractScopes);
Devang Patel387e9c12010-01-19 01:26:02 +00002185 AbstractScopesList.clear();
Jeffrey Yasskinecca2e62010-03-12 17:45:06 +00002186 AbstractVariables.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002187 Lines.clear();
Devang Patel67533ab2009-12-01 18:13:48 +00002188
Bill Wendlingf5839192009-05-20 23:19:06 +00002189 if (TimePassesIsEnabled)
2190 DebugTimer->stopTimer();
2191}
2192
Chris Lattner597b0fc2010-03-09 04:54:43 +00002193/// recordSourceLine - Register a source line with debug info. Returns the
2194/// unique label that was emitted and which provides correspondence to
2195/// the source line list.
2196MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, MDNode *S) {
Devang Patel15e723d2009-08-28 23:24:31 +00002197 if (!MMI)
2198 return 0;
2199
Bill Wendlingf5839192009-05-20 23:19:06 +00002200 if (TimePassesIsEnabled)
2201 DebugTimer->startTimer();
2202
Devang Patel7f75bbe2009-11-25 17:36:49 +00002203 StringRef Dir;
2204 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002205
2206 DIDescriptor Scope(S);
2207 if (Scope.isCompileUnit()) {
2208 DICompileUnit CU(S);
2209 Dir = CU.getDirectory();
2210 Fn = CU.getFilename();
2211 } else if (Scope.isSubprogram()) {
2212 DISubprogram SP(S);
2213 Dir = SP.getDirectory();
2214 Fn = SP.getFilename();
2215 } else if (Scope.isLexicalBlock()) {
2216 DILexicalBlock DB(S);
2217 Dir = DB.getDirectory();
2218 Fn = DB.getFilename();
2219 } else
Chris Lattnerdd21da82010-03-13 07:26:18 +00002220 assert(0 && "Unexpected scope info");
Devang Patel946d0ae2009-10-05 18:03:19 +00002221
2222 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Chris Lattnerd5060b82010-03-14 08:18:13 +00002223 MCSymbol *Label = MMI->getLabelSym(MMI->NextLabelID());
Chris Lattner8d9d06a2010-03-14 08:15:55 +00002224 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Bill Wendlingf5839192009-05-20 23:19:06 +00002225
2226 if (TimePassesIsEnabled)
2227 DebugTimer->stopTimer();
2228
Chris Lattner597b0fc2010-03-09 04:54:43 +00002229 Asm->OutStreamer.EmitLabel(Label);
2230 return Label;
Bill Wendlingf5839192009-05-20 23:19:06 +00002231}
2232
2233/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2234/// timed. Look up the source id with the given directory and source file
2235/// names. If none currently exists, create a new id and insert it in the
2236/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2237/// well.
2238unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2239 const std::string &FileName) {
2240 if (TimePassesIsEnabled)
2241 DebugTimer->startTimer();
2242
Devang Patelaaf012e2009-09-29 18:40:58 +00002243 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf5839192009-05-20 23:19:06 +00002244
2245 if (TimePassesIsEnabled)
2246 DebugTimer->stopTimer();
2247
2248 return SrcId;
2249}
2250
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002251//===----------------------------------------------------------------------===//
2252// Emit Methods
2253//===----------------------------------------------------------------------===//
2254
Devang Patelc50078e2009-11-21 02:48:08 +00002255/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002256///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002257unsigned
2258DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002259 // Get the children.
2260 const std::vector<DIE *> &Children = Die->getChildren();
2261
2262 // If not last sibling and has children then add sibling offset attribute.
Devang Patelc50078e2009-11-21 02:48:08 +00002263 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002264
2265 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002266 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002267
2268 // Get the abbreviation for this DIE.
2269 unsigned AbbrevNumber = Die->getAbbrevNumber();
2270 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2271
2272 // Set DIE offset
2273 Die->setOffset(Offset);
2274
2275 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002276 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002277
2278 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2279 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2280
2281 // Size the DIE attribute values.
2282 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2283 // Size attribute value.
2284 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2285
2286 // Size the DIE children if any.
2287 if (!Children.empty()) {
2288 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2289 "Children flag not set");
2290
2291 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002292 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002293
2294 // End of children marker.
2295 Offset += sizeof(int8_t);
2296 }
2297
2298 Die->setSize(Offset - Die->getOffset());
2299 return Offset;
2300}
2301
Devang Patelc50078e2009-11-21 02:48:08 +00002302/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002303///
Devang Patelc50078e2009-11-21 02:48:08 +00002304void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002305 // Compute size of compile unit header.
2306 static unsigned Offset =
2307 sizeof(int32_t) + // Length of Compilation Unit Info
2308 sizeof(int16_t) + // DWARF version number
2309 sizeof(int32_t) + // Offset Into Abbrev. Section
2310 sizeof(int8_t); // Pointer Size (in bytes)
2311
Devang Patelc50078e2009-11-21 02:48:08 +00002312 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel5a3d37f2009-06-29 20:45:18 +00002313 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling55fccda2009-05-20 23:21:38 +00002314}
2315
Devang Patelc50078e2009-11-21 02:48:08 +00002316/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling55fccda2009-05-20 23:21:38 +00002317/// tools to recognize the object file contains Dwarf information.
Devang Patelc50078e2009-11-21 02:48:08 +00002318void DwarfDebug::emitInitial() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002319 // Check to see if we already emitted intial headers.
2320 if (didInitial) return;
2321 didInitial = true;
2322
Chris Lattner73266f92009-08-19 05:49:37 +00002323 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002324
Bill Wendling55fccda2009-05-20 23:21:38 +00002325 // Dwarf sections base addresses.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002326 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner73266f92009-08-19 05:49:37 +00002327 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002328 Asm->OutStreamer.EmitLabel(getTempLabel("section_debug_frame"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002329 }
2330
Chris Lattner73266f92009-08-19 05:49:37 +00002331 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002332 Asm->OutStreamer.EmitLabel(getTempLabel("section_info"));
Chris Lattner73266f92009-08-19 05:49:37 +00002333 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002334 Asm->OutStreamer.EmitLabel(getTempLabel("section_abbrev"));
Chris Lattner73266f92009-08-19 05:49:37 +00002335 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002336 Asm->OutStreamer.EmitLabel(getTempLabel("section_aranges"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002337
Chris Lattner73266f92009-08-19 05:49:37 +00002338 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2339 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Chris Lattner6d213e82010-03-08 22:44:40 +00002340 Asm->OutStreamer.EmitLabel(getTempLabel("section_macinfo"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002341 }
2342
Chris Lattner73266f92009-08-19 05:49:37 +00002343 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002344 Asm->OutStreamer.EmitLabel(getTempLabel("section_line"));
Chris Lattner73266f92009-08-19 05:49:37 +00002345 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002346 Asm->OutStreamer.EmitLabel(getTempLabel("section_loc"));
Chris Lattner73266f92009-08-19 05:49:37 +00002347 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002348 Asm->OutStreamer.EmitLabel(getTempLabel("section_pubnames"));
Devang Patelec13b4f2009-11-24 01:14:22 +00002349 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002350 Asm->OutStreamer.EmitLabel(getTempLabel("section_pubtypes"));
Chris Lattner73266f92009-08-19 05:49:37 +00002351 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002352 Asm->OutStreamer.EmitLabel(getTempLabel("section_str"));
Chris Lattner73266f92009-08-19 05:49:37 +00002353 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002354 Asm->OutStreamer.EmitLabel(getTempLabel("section_ranges"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002355
Chris Lattner73266f92009-08-19 05:49:37 +00002356 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002357 Asm->OutStreamer.EmitLabel(getTempLabel("text_begin"));
Chris Lattner73266f92009-08-19 05:49:37 +00002358 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Chris Lattner6d213e82010-03-08 22:44:40 +00002359 Asm->OutStreamer.EmitLabel(getTempLabel("data_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002360}
2361
Devang Patelc50078e2009-11-21 02:48:08 +00002362/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002363///
Devang Patelc50078e2009-11-21 02:48:08 +00002364void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002365 // Get the abbreviation for this DIE.
2366 unsigned AbbrevNumber = Die->getAbbrevNumber();
2367 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2368
Bill Wendling55fccda2009-05-20 23:21:38 +00002369 // Emit the code (index) for the abbreviation.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002370 if (Asm->VerboseAsm)
2371 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2372 Twine::utohexstr(Die->getOffset()) + ":0x" +
2373 Twine::utohexstr(Die->getSize()) + " " +
2374 dwarf::TagString(Abbrev->getTag()));
2375 EmitULEB128(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002376
2377 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2378 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2379
2380 // Emit the DIE attribute values.
2381 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2382 unsigned Attr = AbbrevData[i].getAttribute();
2383 unsigned Form = AbbrevData[i].getForm();
2384 assert(Form && "Too many attributes for DIE (check abbreviation)");
2385
Chris Lattner91e06b42010-01-24 18:54:17 +00002386 if (Asm->VerboseAsm)
2387 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2388
Bill Wendling55fccda2009-05-20 23:21:38 +00002389 switch (Attr) {
2390 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002391 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002392 break;
2393 case dwarf::DW_AT_abstract_origin: {
2394 DIEEntry *E = cast<DIEEntry>(Values[i]);
2395 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002396 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002397 Asm->EmitInt32(Addr);
2398 break;
2399 }
2400 default:
2401 // Emit an attribute using the defined form.
2402 Values[i]->EmitValue(this, Form);
2403 break;
2404 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002405 }
2406
2407 // Emit the DIE children if any.
2408 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2409 const std::vector<DIE *> &Children = Die->getChildren();
2410
2411 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002412 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002413
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002414 if (Asm->VerboseAsm)
2415 Asm->OutStreamer.AddComment("End Of Children Mark");
2416 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002417 }
2418}
2419
Devang Patelfe0be132009-12-09 18:24:21 +00002420/// emitDebugInfo - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002421///
Devang Patelfe0be132009-12-09 18:24:21 +00002422void DwarfDebug::emitDebugInfo() {
2423 // Start debug info section.
2424 Asm->OutStreamer.SwitchSection(
2425 Asm->getObjFileLowering().getDwarfInfoSection());
2426 DIE *Die = ModuleCU->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002427
2428 // Emit the compile units header.
Chris Lattner6d213e82010-03-08 22:44:40 +00002429 Asm->OutStreamer.EmitLabel(getDWLabel("info_begin", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002430
2431 // Emit size of content not including length itself
2432 unsigned ContentSize = Die->getSize() +
2433 sizeof(int16_t) + // DWARF version number
2434 sizeof(int32_t) + // Offset Into Abbrev. Section
2435 sizeof(int8_t) + // Pointer Size (in bytes)
2436 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2437
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002438 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2439 Asm->EmitInt32(ContentSize);
2440 Asm->OutStreamer.AddComment("DWARF version number");
2441 Asm->EmitInt16(dwarf::DWARF_VERSION);
2442 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002443 EmitSectionOffset(getTempLabel("abbrev_begin"),getTempLabel("section_abbrev"),
2444 true, false);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002445 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2446 Asm->EmitInt8(TD->getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00002447
Devang Patelc50078e2009-11-21 02:48:08 +00002448 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002449 // FIXME - extra padding for gdb bug.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002450 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2451 Asm->EmitInt8(0);
2452 Asm->EmitInt8(0);
2453 Asm->EmitInt8(0);
2454 Asm->EmitInt8(0);
Chris Lattner6d213e82010-03-08 22:44:40 +00002455 Asm->OutStreamer.EmitLabel(getDWLabel("info_end", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002456}
2457
Devang Patelc50078e2009-11-21 02:48:08 +00002458/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002459///
Devang Patelc50078e2009-11-21 02:48:08 +00002460void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002461 // Check to see if it is worth the effort.
2462 if (!Abbreviations.empty()) {
2463 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002464 Asm->OutStreamer.SwitchSection(
2465 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002466
Chris Lattner6d213e82010-03-08 22:44:40 +00002467 Asm->OutStreamer.EmitLabel(getTempLabel("abbrev_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002468
2469 // For each abbrevation.
2470 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2471 // Get abbreviation data
2472 const DIEAbbrev *Abbrev = Abbreviations[i];
2473
2474 // Emit the abbrevations code (base 1 index.)
Chris Lattnerbcc79432010-01-22 23:18:42 +00002475 EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling55fccda2009-05-20 23:21:38 +00002476
2477 // Emit the abbreviations data.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002478 Abbrev->Emit(this);
Bill Wendling55fccda2009-05-20 23:21:38 +00002479 }
2480
2481 // Mark end of abbreviations.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002482 EmitULEB128(0, "EOM(3)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002483
Chris Lattner6d213e82010-03-08 22:44:40 +00002484 Asm->OutStreamer.EmitLabel(getTempLabel("abbrev_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002485 }
2486}
2487
Devang Patelc50078e2009-11-21 02:48:08 +00002488/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002489/// the line matrix.
2490///
Devang Patelc50078e2009-11-21 02:48:08 +00002491void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002492 // Define last address of section.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002493 Asm->OutStreamer.AddComment("Extended Op");
2494 Asm->EmitInt8(0);
2495
2496 Asm->OutStreamer.AddComment("Op size");
2497 Asm->EmitInt8(TD->getPointerSize() + 1);
2498 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2499 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2500
2501 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnereb159d62010-03-10 01:17:49 +00002502
2503 Asm->OutStreamer.EmitSymbolValue(getDWLabel("section_end", SectionEnd),
2504 TD->getPointerSize(), 0/*AddrSpace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00002505
2506 // Mark end of matrix.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002507 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2508 Asm->EmitInt8(0);
Chris Lattnerad653482010-01-22 22:09:00 +00002509 Asm->EmitInt8(1);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002510 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002511}
2512
Devang Patelc50078e2009-11-21 02:48:08 +00002513/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002514///
Devang Patelc50078e2009-11-21 02:48:08 +00002515void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002516 // If the target is using .loc/.file, the assembler will be emitting the
2517 // .debug_line table automatically.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002518 if (MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002519 return;
2520
2521 // Minimum line delta, thus ranging from -10..(255-10).
2522 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2523 // Maximum line delta, thus ranging from -10..(255-10).
2524 const int MaxLineDelta = 255 + MinLineDelta;
2525
2526 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002527 Asm->OutStreamer.SwitchSection(
2528 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002529
2530 // Construct the section header.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002531 Asm->OutStreamer.AddComment("Length of Source Line Info");
Chris Lattner6d213e82010-03-08 22:44:40 +00002532 EmitDifference(getTempLabel("line_end"), getTempLabel("line_begin"), true);
Chris Lattner6d213e82010-03-08 22:44:40 +00002533 Asm->OutStreamer.EmitLabel(getTempLabel("line_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002534
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002535 Asm->OutStreamer.AddComment("DWARF version number");
2536 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00002537
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002538 Asm->OutStreamer.AddComment("Prolog Length");
Chris Lattner6d213e82010-03-08 22:44:40 +00002539 EmitDifference(getTempLabel("line_prolog_end"),
2540 getTempLabel("line_prolog_begin"), true);
Chris Lattner6d213e82010-03-08 22:44:40 +00002541 Asm->OutStreamer.EmitLabel(getTempLabel("line_prolog_begin"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002542
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002543 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2544 Asm->EmitInt8(1);
2545 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2546 Asm->EmitInt8(1);
2547 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2548 Asm->EmitInt8(MinLineDelta);
2549 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
2550 Asm->EmitInt8(MaxLineDelta);
2551 Asm->OutStreamer.AddComment("Special Opcode Base");
2552 Asm->EmitInt8(-MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00002553
2554 // Line number standard opcode encodings argument count
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002555 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
2556 Asm->EmitInt8(0);
2557 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
2558 Asm->EmitInt8(1);
2559 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
2560 Asm->EmitInt8(1);
2561 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
2562 Asm->EmitInt8(1);
2563 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
2564 Asm->EmitInt8(1);
2565 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
2566 Asm->EmitInt8(0);
2567 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
2568 Asm->EmitInt8(0);
2569 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
2570 Asm->EmitInt8(0);
2571 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
2572 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002573
2574 // Emit directories.
2575 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnercc564642010-01-23 03:11:46 +00002576 const std::string &Dir = getSourceDirectoryName(DI);
2577 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Directory");
2578 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002579 }
2580
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002581 Asm->OutStreamer.AddComment("End of directories");
2582 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002583
2584 // Emit files.
2585 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2586 // Remember source id starts at 1.
2587 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnercc564642010-01-23 03:11:46 +00002588 const std::string &FN = getSourceFileName(Id.second);
2589 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Source");
2590 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2591
Chris Lattnerbcc79432010-01-22 23:18:42 +00002592 EmitULEB128(Id.first, "Directory #");
2593 EmitULEB128(0, "Mod date");
2594 EmitULEB128(0, "File size");
Bill Wendling55fccda2009-05-20 23:21:38 +00002595 }
2596
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002597 Asm->OutStreamer.AddComment("End of files");
2598 Asm->EmitInt8(0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002599
Chris Lattner6d213e82010-03-08 22:44:40 +00002600 Asm->OutStreamer.EmitLabel(getTempLabel("line_prolog_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002601
2602 // A sequence for each text section.
2603 unsigned SecSrcLinesSize = SectionSourceLines.size();
2604
2605 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2606 // Isolate current sections line info.
2607 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2608
Bill Wendling55fccda2009-05-20 23:21:38 +00002609 // Dwarf assumes we start with first line of first source file.
2610 unsigned Source = 1;
2611 unsigned Line = 1;
2612
2613 // Construct rows of the address, source, line, column matrix.
2614 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2615 const SrcLineInfo &LineInfo = LineInfos[i];
Chris Lattner8d9d06a2010-03-14 08:15:55 +00002616 MCSymbol *Label = LineInfo.getLabel();
Chris Lattner31ae74d2010-03-14 02:20:58 +00002617 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling55fccda2009-05-20 23:21:38 +00002618
Caroline Tice9da96d82009-09-11 18:25:54 +00002619 if (LineInfo.getLine() == 0) continue;
2620
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00002621 if (Asm->isVerbose()) {
Chris Lattneree3b40f2010-03-10 01:04:13 +00002622 std::pair<unsigned, unsigned> SrcID =
Bill Wendling55fccda2009-05-20 23:21:38 +00002623 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattneree3b40f2010-03-10 01:04:13 +00002624 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
Chris Lattner48fb0b12010-03-10 02:29:31 +00002625 "/" +
2626 Twine(getSourceFileName(SrcID.second)) +
Chris Lattneree3b40f2010-03-10 01:04:13 +00002627 ":" + Twine(LineInfo.getLine()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002628 }
2629
2630 // Define the line address.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002631 Asm->OutStreamer.AddComment("Extended Op");
2632 Asm->EmitInt8(0);
2633 Asm->OutStreamer.AddComment("Op size");
2634 Asm->EmitInt8(TD->getPointerSize() + 1);
2635
2636 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2637 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2638
2639 Asm->OutStreamer.AddComment("Location label");
Chris Lattner31ae74d2010-03-14 02:20:58 +00002640 Asm->OutStreamer.EmitSymbolValue(Label, TD->getPointerSize(),
2641 0/*AddrSpace*/);
Chris Lattnereb159d62010-03-10 01:17:49 +00002642
Bill Wendling55fccda2009-05-20 23:21:38 +00002643 // If change of source, then switch to the new source.
2644 if (Source != LineInfo.getSourceID()) {
2645 Source = LineInfo.getSourceID();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002646 Asm->OutStreamer.AddComment("DW_LNS_set_file");
2647 Asm->EmitInt8(dwarf::DW_LNS_set_file);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002648 EmitULEB128(Source, "New Source");
Bill Wendling55fccda2009-05-20 23:21:38 +00002649 }
2650
2651 // If change of line.
2652 if (Line != LineInfo.getLine()) {
2653 // Determine offset.
2654 int Offset = LineInfo.getLine() - Line;
2655 int Delta = Offset - MinLineDelta;
2656
2657 // Update line.
2658 Line = LineInfo.getLine();
2659
2660 // If delta is small enough and in range...
2661 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2662 // ... then use fast opcode.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002663 Asm->OutStreamer.AddComment("Line Delta");
2664 Asm->EmitInt8(Delta - MinLineDelta);
Bill Wendling55fccda2009-05-20 23:21:38 +00002665 } else {
2666 // ... otherwise use long hand.
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002667 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Bill Wendling55fccda2009-05-20 23:21:38 +00002668 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner20f334f2010-01-22 22:56:55 +00002669 EmitSLEB128(Offset, "Line Offset");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002670 Asm->OutStreamer.AddComment("DW_LNS_copy");
2671 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00002672 }
2673 } else {
2674 // Copy the previous row (different address or source)
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002675 Asm->OutStreamer.AddComment("DW_LNS_copy");
2676 Asm->EmitInt8(dwarf::DW_LNS_copy);
Bill Wendling55fccda2009-05-20 23:21:38 +00002677 }
2678 }
2679
Devang Patelc50078e2009-11-21 02:48:08 +00002680 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002681 }
2682
2683 if (SecSrcLinesSize == 0)
2684 // Because we're emitting a debug_line section, we still need a line
2685 // table. The linker and friends expect it to exist. If there's nothing to
2686 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00002687 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002688
Chris Lattner6d213e82010-03-08 22:44:40 +00002689 Asm->OutStreamer.EmitLabel(getTempLabel("line_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002690}
2691
Devang Patelc50078e2009-11-21 02:48:08 +00002692/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002693///
Devang Patelc50078e2009-11-21 02:48:08 +00002694void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002695 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002696 return;
2697
2698 int stackGrowth =
2699 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2700 TargetFrameInfo::StackGrowsUp ?
2701 TD->getPointerSize() : -TD->getPointerSize();
2702
2703 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002704 Asm->OutStreamer.SwitchSection(
2705 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002706
Chris Lattner6d213e82010-03-08 22:44:40 +00002707 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002708 Asm->OutStreamer.AddComment("Length of Common Information Entry");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002709 EmitDifference(getTempLabel("debug_frame_common_end"),
2710 getTempLabel("debug_frame_common_begin"), true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002711
Chris Lattner6d213e82010-03-08 22:44:40 +00002712 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common_begin"));
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002713 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Bill Wendling55fccda2009-05-20 23:21:38 +00002714 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002715 Asm->OutStreamer.AddComment("CIE Version");
Bill Wendling55fccda2009-05-20 23:21:38 +00002716 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002717 Asm->OutStreamer.AddComment("CIE Augmentation");
Chris Lattnercc564642010-01-23 03:11:46 +00002718 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002719 EmitULEB128(1, "CIE Code Alignment Factor");
Chris Lattner20f334f2010-01-22 22:56:55 +00002720 EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002721 Asm->OutStreamer.AddComment("CIE RA Column");
Bill Wendling55fccda2009-05-20 23:21:38 +00002722 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Bill Wendling55fccda2009-05-20 23:21:38 +00002723
2724 std::vector<MachineMove> Moves;
2725 RI->getInitialFrameState(Moves);
2726
Chris Lattnerdd4f4f82010-03-13 08:05:25 +00002727 EmitFrameMoves(0, Moves, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00002728
2729 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattner6d213e82010-03-08 22:44:40 +00002730 Asm->OutStreamer.EmitLabel(getTempLabel("debug_frame_common_end"));
Bill Wendling55fccda2009-05-20 23:21:38 +00002731}
2732
Devang Patelc50078e2009-11-21 02:48:08 +00002733/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00002734/// section.
Chris Lattnerdd21da82010-03-13 07:26:18 +00002735void DwarfDebug::
2736emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002737 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002738 return;
2739
2740 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002741 Asm->OutStreamer.SwitchSection(
2742 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002743
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002744 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
Chris Lattnerdd21da82010-03-13 07:26:18 +00002745 MCSymbol *DebugFrameBegin =
2746 getDWLabel("debug_frame_begin", DebugFrameInfo.Number);
2747 MCSymbol *DebugFrameEnd =
2748 getDWLabel("debug_frame_end", DebugFrameInfo.Number);
2749 EmitDifference(DebugFrameEnd, DebugFrameBegin, true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002750
Chris Lattnerdd21da82010-03-13 07:26:18 +00002751 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Bill Wendling55fccda2009-05-20 23:21:38 +00002752
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002753 Asm->OutStreamer.AddComment("FDE CIE offset");
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002754 EmitSectionOffset(getTempLabel("debug_frame_common"),
2755 getTempLabel("section_debug_frame"), true, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00002756
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002757 Asm->OutStreamer.AddComment("FDE initial location");
Chris Lattner314e3362010-03-13 07:40:56 +00002758 MCSymbol *FuncBeginSym = getDWLabel("func_begin", DebugFrameInfo.Number);
2759 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
Chris Lattnereb159d62010-03-10 01:17:49 +00002760 TD->getPointerSize(), 0/*AddrSpace*/);
2761
2762
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002763 Asm->OutStreamer.AddComment("FDE address range");
Chris Lattner314e3362010-03-13 07:40:56 +00002764 EmitDifference(getDWLabel("func_end", DebugFrameInfo.Number), FuncBeginSym);
Bill Wendling55fccda2009-05-20 23:21:38 +00002765
Chris Lattnerdd4f4f82010-03-13 08:05:25 +00002766 EmitFrameMoves(FuncBeginSym, DebugFrameInfo.Moves, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00002767
2768 Asm->EmitAlignment(2, 0, 0, false);
Chris Lattnerdd21da82010-03-13 07:26:18 +00002769 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Bill Wendling55fccda2009-05-20 23:21:38 +00002770}
2771
Devang Patelfe0be132009-12-09 18:24:21 +00002772/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2773///
2774void DwarfDebug::emitDebugPubNames() {
2775 // Start the dwarf pubnames section.
2776 Asm->OutStreamer.SwitchSection(
2777 Asm->getObjFileLowering().getDwarfPubNamesSection());
2778
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002779 Asm->OutStreamer.AddComment("Length of Public Names Info");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002780 EmitDifference(getDWLabel("pubnames_end", ModuleCU->getID()),
2781 getDWLabel("pubnames_begin", ModuleCU->getID()), true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002782
Chris Lattner6d213e82010-03-08 22:44:40 +00002783 Asm->OutStreamer.EmitLabel(getDWLabel("pubnames_begin", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002784
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002785 Asm->OutStreamer.AddComment("DWARF Version");
2786 Asm->EmitInt16(dwarf::DWARF_VERSION);
Bill Wendling55fccda2009-05-20 23:21:38 +00002787
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002788 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002789 EmitSectionOffset(getDWLabel("info_begin", ModuleCU->getID()),
2790 getTempLabel("section_info"),
2791 true, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00002792
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002793 Asm->OutStreamer.AddComment("Compilation Unit Length");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002794 EmitDifference(getDWLabel("info_end", ModuleCU->getID()),
2795 getDWLabel("info_begin", ModuleCU->getID()),
Bill Wendling55fccda2009-05-20 23:21:38 +00002796 true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002797
Devang Patelfe0be132009-12-09 18:24:21 +00002798 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling55fccda2009-05-20 23:21:38 +00002799 for (StringMap<DIE*>::const_iterator
2800 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2801 const char *Name = GI->getKeyData();
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002802 DIE *Entity = GI->second;
Bill Wendling55fccda2009-05-20 23:21:38 +00002803
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002804 Asm->OutStreamer.AddComment("DIE offset");
2805 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00002806
2807 if (Asm->VerboseAsm)
2808 Asm->OutStreamer.AddComment("External Name");
2809 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002810 }
2811
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002812 Asm->OutStreamer.AddComment("End Mark");
2813 Asm->EmitInt32(0);
Chris Lattner6d213e82010-03-08 22:44:40 +00002814 Asm->OutStreamer.EmitLabel(getDWLabel("pubnames_end", ModuleCU->getID()));
Bill Wendling55fccda2009-05-20 23:21:38 +00002815}
2816
Devang Patelec13b4f2009-11-24 01:14:22 +00002817void DwarfDebug::emitDebugPubTypes() {
Devang Patel6f2bdd52009-11-24 19:18:41 +00002818 // Start the dwarf pubnames section.
2819 Asm->OutStreamer.SwitchSection(
2820 Asm->getObjFileLowering().getDwarfPubTypesSection());
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002821 Asm->OutStreamer.AddComment("Length of Public Types Info");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002822 EmitDifference(getDWLabel("pubtypes_end", ModuleCU->getID()),
2823 getDWLabel("pubtypes_begin", ModuleCU->getID()), true);
Devang Patelec13b4f2009-11-24 01:14:22 +00002824
Chris Lattner6d213e82010-03-08 22:44:40 +00002825 Asm->OutStreamer.EmitLabel(getDWLabel("pubtypes_begin", ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00002826
Devang Patel40b6cda2010-02-02 03:47:27 +00002827 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DWARF Version");
2828 Asm->EmitInt16(dwarf::DWARF_VERSION);
Devang Patelec13b4f2009-11-24 01:14:22 +00002829
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002830 Asm->OutStreamer.AddComment("Offset of Compilation ModuleCU Info");
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002831 EmitSectionOffset(getDWLabel("info_begin", ModuleCU->getID()),
2832 getTempLabel("section_info"), true, false);
Devang Patelec13b4f2009-11-24 01:14:22 +00002833
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002834 Asm->OutStreamer.AddComment("Compilation ModuleCU Length");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002835 EmitDifference(getDWLabel("info_end", ModuleCU->getID()),
2836 getDWLabel("info_begin", ModuleCU->getID()),
Devang Patelec13b4f2009-11-24 01:14:22 +00002837 true);
Devang Patelec13b4f2009-11-24 01:14:22 +00002838
2839 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2840 for (StringMap<DIE*>::const_iterator
2841 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2842 const char *Name = GI->getKeyData();
2843 DIE * Entity = GI->second;
2844
Devang Patel40b6cda2010-02-02 03:47:27 +00002845 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DIE offset");
2846 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00002847
2848 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("External Name");
Devang Patel0ceb4892010-02-02 03:37:03 +00002849 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00002850 }
2851
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002852 Asm->OutStreamer.AddComment("End Mark");
2853 Asm->EmitInt32(0);
Chris Lattner6d213e82010-03-08 22:44:40 +00002854 Asm->OutStreamer.EmitLabel(getDWLabel("pubtypes_end", ModuleCU->getID()));
Devang Patelec13b4f2009-11-24 01:14:22 +00002855}
2856
Devang Patelc50078e2009-11-21 02:48:08 +00002857/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002858///
Devang Patelc50078e2009-11-21 02:48:08 +00002859void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002860 // Check to see if it is worth the effort.
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00002861 if (StringPool.empty()) return;
2862
2863 // Start the dwarf str section.
2864 Asm->OutStreamer.SwitchSection(
Chris Lattner73266f92009-08-19 05:49:37 +00002865 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002866
Chris Lattner0f093f82010-03-13 02:17:42 +00002867 // Get all of the string pool entries and put them in an array by their ID so
2868 // we can sort them.
2869 SmallVector<std::pair<unsigned,
2870 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
2871
2872 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2873 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2874 Entries.push_back(std::make_pair(I->second.second, &*I));
2875
2876 array_pod_sort(Entries.begin(), Entries.end());
2877
2878 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00002879 // Emit a label for reference from debug information entries.
Chris Lattner0f093f82010-03-13 02:17:42 +00002880 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Chris Lattner8bd2f8f2010-03-09 23:38:23 +00002881
2882 // Emit the string itself.
Chris Lattner0f093f82010-03-13 02:17:42 +00002883 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Bill Wendling55fccda2009-05-20 23:21:38 +00002884 }
2885}
2886
Devang Patelc50078e2009-11-21 02:48:08 +00002887/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002888///
Devang Patelc50078e2009-11-21 02:48:08 +00002889void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002890 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00002891 Asm->OutStreamer.SwitchSection(
2892 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002893}
2894
2895/// EmitDebugARanges - Emit visible names into a debug aranges section.
2896///
2897void DwarfDebug::EmitDebugARanges() {
2898 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002899 Asm->OutStreamer.SwitchSection(
2900 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002901}
2902
Devang Patelc50078e2009-11-21 02:48:08 +00002903/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002904///
Devang Patelc50078e2009-11-21 02:48:08 +00002905void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002906 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002907 Asm->OutStreamer.SwitchSection(
2908 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002909}
2910
Devang Patelc50078e2009-11-21 02:48:08 +00002911/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002912///
Devang Patelc50078e2009-11-21 02:48:08 +00002913void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00002914 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00002915 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002916 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00002917 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002918 }
2919}
2920
Devang Patelc50078e2009-11-21 02:48:08 +00002921/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00002922/// Section Header:
2923/// 1. length of section
2924/// 2. Dwarf version number
2925/// 3. address size.
2926///
2927/// Entries (one "entry" for each function that was inlined):
2928///
2929/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2930/// otherwise offset into __debug_str for regular function name.
2931/// 2. offset into __debug_str section for regular function name.
2932/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2933/// instances for the function.
2934///
2935/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2936/// inlined instance; the die_offset points to the inlined_subroutine die in the
2937/// __debug_info section, and the low_pc is the starting address for the
2938/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00002939void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002940 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002941 return;
2942
Devang Patel5a3d37f2009-06-29 20:45:18 +00002943 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00002944 return;
2945
Chris Lattner73266f92009-08-19 05:49:37 +00002946 Asm->OutStreamer.SwitchSection(
2947 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerad653482010-01-22 22:09:00 +00002948
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002949 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
Chris Lattner2a65fd42010-03-08 23:02:59 +00002950 EmitDifference(getDWLabel("debug_inlined_end", 1),
2951 getDWLabel("debug_inlined_begin", 1), true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002952
Chris Lattner6d213e82010-03-08 22:44:40 +00002953 Asm->OutStreamer.EmitLabel(getDWLabel("debug_inlined_begin", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00002954
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002955 Asm->OutStreamer.AddComment("Dwarf Version");
2956 Asm->EmitInt16(dwarf::DWARF_VERSION);
2957 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2958 Asm->EmitInt8(TD->getPointerSize());
Bill Wendling55fccda2009-05-20 23:21:38 +00002959
Devang Patel90a0fe32009-11-10 23:06:00 +00002960 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2961 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00002962
Devang Patel90a0fe32009-11-10 23:06:00 +00002963 MDNode *Node = *I;
Devang Patel7d707f92010-01-19 06:19:05 +00002964 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbachb23f2422009-11-22 19:20:36 +00002965 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00002966 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00002967 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00002968 StringRef LName = SP.getLinkageName();
2969 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00002970
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002971 Asm->OutStreamer.AddComment("MIPS linkage name");
Chris Lattnercc564642010-01-23 03:11:46 +00002972 if (LName.empty()) {
2973 Asm->OutStreamer.EmitBytes(Name, 0);
2974 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
2975 } else
Chris Lattner0f093f82010-03-13 02:17:42 +00002976 EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
Chris Lattnerbe4c2fc2010-03-08 22:23:36 +00002977 getTempLabel("section_str"), true);
Devang Patel90a0fe32009-11-10 23:06:00 +00002978
Chris Lattner9ef8efd2010-03-09 23:52:58 +00002979 Asm->OutStreamer.AddComment("Function name");
Chris Lattner0f093f82010-03-13 02:17:42 +00002980 EmitSectionOffset(getStringPoolEntry(Name), getTempLabel("section_str"),
2981 false, true);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002982 EmitULEB128(Labels.size(), "Inline count");
Bill Wendling55fccda2009-05-20 23:21:38 +00002983
Devang Patel90a0fe32009-11-10 23:06:00 +00002984 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00002985 LE = Labels.end(); LI != LE; ++LI) {
Chris Lattneread58652010-03-09 00:31:02 +00002986 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DIE offset");
2987 Asm->EmitInt32(LI->second->getOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002988
Chris Lattnere90630a2010-03-09 00:26:09 +00002989 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("low_pc");
Chris Lattner2ab0c442010-03-09 00:39:24 +00002990 Asm->OutStreamer.EmitSymbolValue(LI->first, TD->getPointerSize(), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002991 }
2992 }
2993
Chris Lattner6d213e82010-03-08 22:44:40 +00002994 Asm->OutStreamer.EmitLabel(getDWLabel("debug_inlined_end", 1));
Bill Wendling55fccda2009-05-20 23:21:38 +00002995}