blob: d83cbf21d24547fcaa344c5c0050b1601598203d [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//===----------------------------------------------------------------------===//
Devang Patel15e723d2009-08-28 23:24:31 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greened87baff2009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner73266f92009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Chris Lattner31a54742010-01-16 21:57:06 +000021#include "llvm/Target/Mangler.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000022#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000024#include "llvm/Target/TargetLoweringObjectFile.h"
25#include "llvm/Target/TargetRegisterInfo.h"
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +000026#include "llvm/ADT/STLExtras.h"
Chris Lattnerf5377682009-08-24 03:52:50 +000027#include "llvm/ADT/StringExtras.h"
Daniel Dunbarc74255d2009-10-13 06:47:08 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
Devang Patelae89d3b2010-01-26 21:39:14 +000030#include "llvm/Support/ValueHandle.h"
Chris Lattnerad653482010-01-22 22:09:00 +000031#include "llvm/Support/FormattedStream.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000032#include "llvm/Support/Timer.h"
33#include "llvm/System/Path.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000034using namespace llvm;
35
Bill Wendlingb12b3d72009-05-15 09:23:25 +000036//===----------------------------------------------------------------------===//
37
38/// Configuration values for initial hash set sizes (log2).
39///
Bill Wendlingb12b3d72009-05-15 09:23:25 +000040static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendlingb12b3d72009-05-15 09:23:25 +000041
42namespace llvm {
43
44//===----------------------------------------------------------------------===//
45/// CompileUnit - This dwarf writer support class manages information associate
46/// with a source file.
Nick Lewyckyee68f452009-11-17 08:11:44 +000047class CompileUnit {
Bill Wendlingb12b3d72009-05-15 09:23:25 +000048 /// ID - File identifier for source.
49 ///
50 unsigned ID;
51
52 /// Die - Compile unit debug information entry.
53 ///
Devang Patelc50078e2009-11-21 02:48:08 +000054 DIE *CUDie;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000055
Devang Patel1233f912009-11-21 00:31:03 +000056 /// IndexTyDie - An anonymous type for index type.
57 DIE *IndexTyDie;
58
Bill Wendlingb12b3d72009-05-15 09:23:25 +000059 /// GVToDieMap - Tracks the mapping of unit level debug informaton
60 /// variables to debug information entries.
Devang Patel15e723d2009-08-28 23:24:31 +000061 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel7d707f92010-01-19 06:19:05 +000062 DenseMap<MDNode *, DIE *> GVToDieMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000063
64 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
65 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel15e723d2009-08-28 23:24:31 +000066 /// FIXME : Rename
Devang Patel7d707f92010-01-19 06:19:05 +000067 DenseMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000068
69 /// Globals - A map of globally visible named entities for this unit.
70 ///
71 StringMap<DIE*> Globals;
72
Devang Patelec13b4f2009-11-24 01:14:22 +000073 /// GlobalTypes - A map of globally visible types for this unit.
74 ///
75 StringMap<DIE*> GlobalTypes;
76
Bill Wendlingb12b3d72009-05-15 09:23:25 +000077public:
78 CompileUnit(unsigned I, DIE *D)
Devang Patelc50078e2009-11-21 02:48:08 +000079 : ID(I), CUDie(D), IndexTyDie(0) {}
80 ~CompileUnit() { delete CUDie; delete IndexTyDie; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000081
82 // Accessors.
Devang Patelec13b4f2009-11-24 01:14:22 +000083 unsigned getID() const { return ID; }
84 DIE* getCUDie() const { return CUDie; }
85 const StringMap<DIE*> &getGlobals() const { return Globals; }
86 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000087
88 /// hasContent - Return true if this compile unit has something to write out.
89 ///
Devang Patelc50078e2009-11-21 02:48:08 +000090 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000091
Devang Patelc50078e2009-11-21 02:48:08 +000092 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +000093 ///
Devang Patelc50078e2009-11-21 02:48:08 +000094 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000095
Devang Patelec13b4f2009-11-24 01:14:22 +000096 /// addGlobalType - Add a new global type to the compile unit.
97 ///
98 void addGlobalType(const std::string &Name, DIE *Die) {
99 GlobalTypes[Name] = Die;
100 }
101
Devang Pateld90672c2009-11-20 21:37:22 +0000102 /// getDIE - Returns the debug information entry map slot for the
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000103 /// specified debug variable.
Devang Pateld90672c2009-11-20 21:37:22 +0000104 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach652b7432009-11-21 23:12:12 +0000105
Devang Pateld90672c2009-11-20 21:37:22 +0000106 /// insertDIE - Insert DIE into the map.
107 void insertDIE(MDNode *N, DIE *D) {
108 GVToDieMap.insert(std::make_pair(N, D));
109 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000110
Devang Pateld90672c2009-11-20 21:37:22 +0000111 /// getDIEEntry - Returns the debug information entry for the speciefied
112 /// debug variable.
Devang Patel8287d662009-12-15 19:16:48 +0000113 DIEEntry *getDIEEntry(MDNode *N) {
Devang Patel7d707f92010-01-19 06:19:05 +0000114 DenseMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
Devang Patel8287d662009-12-15 19:16:48 +0000115 if (I == GVToDIEEntryMap.end())
116 return NULL;
117 return I->second;
118 }
Devang Pateld90672c2009-11-20 21:37:22 +0000119
120 /// insertDIEEntry - Insert debug information entry into the map.
121 void insertDIEEntry(MDNode *N, DIEEntry *E) {
122 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000123 }
124
Devang Patelc50078e2009-11-21 02:48:08 +0000125 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000126 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000127 void addDie(DIE *Buffer) {
128 this->CUDie->addChild(Buffer);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000129 }
Devang Patel1233f912009-11-21 00:31:03 +0000130
131 // getIndexTyDie - Get an anonymous type for index type.
132 DIE *getIndexTyDie() {
133 return IndexTyDie;
134 }
135
Jim Grosbachb23f2422009-11-22 19:20:36 +0000136 // setIndexTyDie - Set D as anonymous type for index which can be reused
137 // later.
Devang Patel1233f912009-11-21 00:31:03 +0000138 void setIndexTyDie(DIE *D) {
139 IndexTyDie = D;
140 }
141
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000142};
143
144//===----------------------------------------------------------------------===//
145/// DbgVariable - This class is used to track local variable information.
146///
Devang Patel4cb32c32009-11-16 21:53:40 +0000147class DbgVariable {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000148 DIVariable Var; // Variable Descriptor.
149 unsigned FrameIndex; // Variable frame index.
Devang Patel90a0fe32009-11-10 23:06:00 +0000150 DbgVariable *AbstractVar; // Abstract variable for this variable.
151 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000152public:
Devang Patel90a0fe32009-11-10 23:06:00 +0000153 DbgVariable(DIVariable V, unsigned I)
154 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000155
156 // Accessors.
Devang Patel90a0fe32009-11-10 23:06:00 +0000157 DIVariable getVariable() const { return Var; }
158 unsigned getFrameIndex() const { return FrameIndex; }
159 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
160 DbgVariable *getAbstractVariable() const { return AbstractVar; }
161 void setDIE(DIE *D) { TheDIE = D; }
162 DIE *getDIE() const { return TheDIE; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000163};
164
165//===----------------------------------------------------------------------===//
166/// DbgScope - This class is used to track scope information.
167///
Devang Patel4cb32c32009-11-16 21:53:40 +0000168class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000169 DbgScope *Parent; // Parent to this scope.
Jim Grosbach652b7432009-11-21 23:12:12 +0000170 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patelae89d3b2010-01-26 21:39:14 +0000171 // Location at which this scope is inlined.
172 AssertingVH<MDNode> InlinedAtLocation;
Devang Patel90a0fe32009-11-10 23:06:00 +0000173 bool AbstractScope; // Abstract Scope
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000174 unsigned StartLabelID; // Label ID of the beginning of scope.
175 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel90ecd192009-10-01 18:25:23 +0000176 const MachineInstr *LastInsn; // Last instruction of this scope.
177 const MachineInstr *FirstInsn; // First instruction of this scope.
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +0000178 // Scopes defined in scope. Contents not owned.
179 SmallVector<DbgScope *, 4> Scopes;
180 // Variables declared in scope. Contents owned.
181 SmallVector<DbgVariable *, 8> Variables;
Daniel Dunbar41716322009-09-19 20:40:05 +0000182
Owen Anderson696d4862009-06-24 22:53:20 +0000183 // Private state for dump()
184 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000185public:
Devang Pateldd7bb432009-10-14 21:08:09 +0000186 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000187 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Jim Grosbach652b7432009-11-21 23:12:12 +0000188 StartLabelID(0), EndLabelID(0),
Devang Pateldd7bb432009-10-14 21:08:09 +0000189 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000190 virtual ~DbgScope();
191
192 // Accessors.
193 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000194 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000195 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach652b7432009-11-21 23:12:12 +0000196 MDNode *getInlinedAt() const {
Devang Patelae89d3b2010-01-26 21:39:14 +0000197 return InlinedAtLocation;
Devang Pateldd7bb432009-10-14 21:08:09 +0000198 }
Devang Patel90a0fe32009-11-10 23:06:00 +0000199 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000200 unsigned getStartLabelID() const { return StartLabelID; }
201 unsigned getEndLabelID() const { return EndLabelID; }
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +0000202 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
203 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000204 void setStartLabelID(unsigned S) { StartLabelID = S; }
205 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Patel90ecd192009-10-01 18:25:23 +0000206 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
207 const MachineInstr *getLastInsn() { return LastInsn; }
208 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000209 void setAbstractScope() { AbstractScope = true; }
210 bool isAbstractScope() const { return AbstractScope; }
Devang Patel90ecd192009-10-01 18:25:23 +0000211 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000212
Devang Patelc50078e2009-11-21 02:48:08 +0000213 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000214 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000215 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000216
Devang Patelc50078e2009-11-21 02:48:08 +0000217 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000218 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000219 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000220
Devang Patel98e77302010-01-04 20:44:00 +0000221 void fixInstructionMarkers(DenseMap<const MachineInstr *,
222 unsigned> &MIIndexMap) {
Devang Patel6a260102009-10-01 20:31:14 +0000223 assert (getFirstInsn() && "First instruction is missing!");
Devang Patel98e77302010-01-04 20:44:00 +0000224
225 // Use the end of last child scope as end of this scope.
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +0000226 const SmallVector<DbgScope *, 4> &Scopes = getScopes();
Devang Patel3b620bf2010-01-05 16:59:17 +0000227 const MachineInstr *LastInsn = getFirstInsn();
Devang Patel98e77302010-01-04 20:44:00 +0000228 unsigned LIndex = 0;
229 if (Scopes.empty()) {
230 assert (getLastInsn() && "Inner most scope does not have last insn!");
231 return;
232 }
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +0000233 for (SmallVector<DbgScope *, 4>::const_iterator SI = Scopes.begin(),
Devang Patel98e77302010-01-04 20:44:00 +0000234 SE = Scopes.end(); SI != SE; ++SI) {
235 DbgScope *DS = *SI;
236 DS->fixInstructionMarkers(MIIndexMap);
237 const MachineInstr *DSLastInsn = DS->getLastInsn();
238 unsigned DSI = MIIndexMap[DSLastInsn];
239 if (DSI > LIndex) {
240 LastInsn = DSLastInsn;
241 LIndex = DSI;
242 }
243 }
Devang Patel067e6d92010-02-17 02:20:34 +0000244
245 unsigned CurrentLastInsnIndex = 0;
246 if (const MachineInstr *CL = getLastInsn())
247 CurrentLastInsnIndex = MIIndexMap[CL];
248 unsigned FIndex = MIIndexMap[getFirstInsn()];
249
250 // Set LastInsn as the last instruction for this scope only if
251 // it follows
252 // 1) this scope's first instruction and
253 // 2) current last instruction for this scope, if any.
254 if (LIndex >= CurrentLastInsnIndex && LIndex >= FIndex)
255 setLastInsn(LastInsn);
Devang Patel6a260102009-10-01 20:31:14 +0000256 }
257
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000258#ifndef NDEBUG
259 void dump() const;
260#endif
261};
262
263#ifndef NDEBUG
264void DbgScope::dump() const {
David Greenedbc06132009-12-24 00:31:35 +0000265 raw_ostream &err = dbgs();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000266 err.indent(IndentLevel);
Devang Patel90a0fe32009-11-10 23:06:00 +0000267 MDNode *N = Desc.getNode();
268 N->dump();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000269 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel90a0fe32009-11-10 23:06:00 +0000270 if (AbstractScope)
271 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000272
273 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000274 if (!Scopes.empty())
275 err << "Children ...\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000276 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
277 if (Scopes[i] != this)
278 Scopes[i]->dump();
279
280 IndentLevel -= 2;
281}
282#endif
283
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000284DbgScope::~DbgScope() {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000285 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
286 delete Variables[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000287}
288
289} // end llvm namespace
290
Chris Lattner621c44d2009-08-22 20:48:53 +0000291DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Chris Lattnerb21f1042010-01-22 22:23:57 +0000292 : DwarfPrinter(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000293 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patelc50078e2009-11-21 02:48:08 +0000294 DIEValues(), StringPool(),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000295 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel90a0fe32009-11-10 23:06:00 +0000296 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000297 if (TimePassesIsEnabled)
Chris Lattner13bf2e52009-12-28 07:41:18 +0000298 DebugTimer = new Timer("Dwarf Debug Writer");
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000299}
300DwarfDebug::~DwarfDebug() {
Devang Patelc50078e2009-11-21 02:48:08 +0000301 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
302 delete DIEValues[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000303
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000304 delete DebugTimer;
305}
306
Devang Patelc50078e2009-11-21 02:48:08 +0000307/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000308///
Devang Patelc50078e2009-11-21 02:48:08 +0000309void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000310 // Profile the node so that we can make it unique.
311 FoldingSetNodeID ID;
312 Abbrev.Profile(ID);
313
314 // Check the set for priors.
315 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
316
317 // If it's newly added.
318 if (InSet == &Abbrev) {
319 // Add to abbreviation list.
320 Abbreviations.push_back(&Abbrev);
321
322 // Assign the vector position + 1 as its number.
323 Abbrev.setNumber(Abbreviations.size());
324 } else {
325 // Assign existing abbreviation number.
326 Abbrev.setNumber(InSet->getNumber());
327 }
328}
329
Devang Patelc50078e2009-11-21 02:48:08 +0000330/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000331/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000332DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel1233f912009-11-21 00:31:03 +0000333 DIEEntry *Value = new DIEEntry(Entry);
Devang Patelc50078e2009-11-21 02:48:08 +0000334 DIEValues.push_back(Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000335 return Value;
336}
337
Devang Patelc50078e2009-11-21 02:48:08 +0000338/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000339///
Devang Patelc50078e2009-11-21 02:48:08 +0000340void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000341 unsigned Form, uint64_t Integer) {
342 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000343 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000344 DIEValues.push_back(Value);
345 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000346}
347
Devang Patelc50078e2009-11-21 02:48:08 +0000348/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000349///
Devang Patelc50078e2009-11-21 02:48:08 +0000350void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000351 unsigned Form, int64_t Integer) {
352 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000353 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000354 DIEValues.push_back(Value);
355 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000356}
357
Devang Pateldf0f2152009-12-02 15:25:16 +0000358/// addString - Add a string attribute data and value. DIEString only
359/// keeps string reference.
Devang Patelc50078e2009-11-21 02:48:08 +0000360void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattnercc564642010-01-23 03:11:46 +0000361 StringRef String) {
Devang Patel1233f912009-11-21 00:31:03 +0000362 DIEValue *Value = new DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000363 DIEValues.push_back(Value);
364 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000365}
366
Devang Patelc50078e2009-11-21 02:48:08 +0000367/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000368///
Devang Patelc50078e2009-11-21 02:48:08 +0000369void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000370 const DWLabel &Label) {
Devang Patel1233f912009-11-21 00:31:03 +0000371 DIEValue *Value = new DIEDwarfLabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000372 DIEValues.push_back(Value);
373 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000374}
375
Devang Patelc50078e2009-11-21 02:48:08 +0000376/// addObjectLabel - Add an non-Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000377///
Devang Patelc50078e2009-11-21 02:48:08 +0000378void DwarfDebug::addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
Chris Lattner41e330f2010-01-16 18:50:28 +0000379 const MCSymbol *Sym) {
380 DIEValue *Value = new DIEObjectLabel(Sym);
Devang Patelc50078e2009-11-21 02:48:08 +0000381 DIEValues.push_back(Value);
382 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000383}
384
Devang Patelc50078e2009-11-21 02:48:08 +0000385/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000386///
Devang Patelc50078e2009-11-21 02:48:08 +0000387void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000388 const DWLabel &Label, const DWLabel &Section,
389 bool isEH, bool useSet) {
Devang Patel1233f912009-11-21 00:31:03 +0000390 DIEValue *Value = new DIESectionOffset(Label, Section, isEH, useSet);
Devang Patelc50078e2009-11-21 02:48:08 +0000391 DIEValues.push_back(Value);
392 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000393}
394
Devang Patelc50078e2009-11-21 02:48:08 +0000395/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000396///
Devang Patelc50078e2009-11-21 02:48:08 +0000397void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000398 const DWLabel &Hi, const DWLabel &Lo) {
Devang Patel1233f912009-11-21 00:31:03 +0000399 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000400 DIEValues.push_back(Value);
401 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000402}
403
Devang Patelc50078e2009-11-21 02:48:08 +0000404/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000405///
Devang Patelc50078e2009-11-21 02:48:08 +0000406void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000407 DIEBlock *Block) {
408 Block->ComputeSize(TD);
Devang Patelc50078e2009-11-21 02:48:08 +0000409 DIEValues.push_back(Block);
410 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000411}
412
Devang Patelc50078e2009-11-21 02:48:08 +0000413/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000414/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000415void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000416 // If there is no compile unit specified, don't add a line #.
417 if (V->getCompileUnit().isNull())
418 return;
419
420 unsigned Line = V->getLineNumber();
Devang Patelb9f2c6b2009-12-11 21:37:07 +0000421 unsigned FileID = findCompileUnit(V->getCompileUnit())->getID();
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 #.
431 if (G->getCompileUnit().isNull())
432 return;
433
434 unsigned Line = G->getLineNumber();
Devang Patelb9f2c6b2009-12-11 21:37:07 +0000435 unsigned FileID = findCompileUnit(G->getCompileUnit())->getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000436 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000437 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
438 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000439}
Devang Patel318d70d2009-08-31 22:47:13 +0000440
Devang Patelc50078e2009-11-21 02:48:08 +0000441/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000442/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000443void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel318d70d2009-08-31 22:47:13 +0000444 // If there is no compile unit specified, don't add a line #.
445 if (SP->getCompileUnit().isNull())
446 return;
Caroline Tice9da96d82009-09-11 18:25:54 +0000447 // If the line number is 0, don't add it.
448 if (SP->getLineNumber() == 0)
449 return;
450
Devang Patel318d70d2009-08-31 22:47:13 +0000451
452 unsigned Line = SP->getLineNumber();
Devang Patelb9f2c6b2009-12-11 21:37:07 +0000453 unsigned FileID = findCompileUnit(SP->getCompileUnit())->getID();
Devang Patel318d70d2009-08-31 22:47:13 +0000454 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000455 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
456 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel318d70d2009-08-31 22:47:13 +0000457}
458
Devang Patelc50078e2009-11-21 02:48:08 +0000459/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000460/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000461void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000462 // If there is no compile unit specified, don't add a line #.
463 DICompileUnit CU = Ty->getCompileUnit();
464 if (CU.isNull())
465 return;
466
467 unsigned Line = Ty->getLineNumber();
Devang Patelb9f2c6b2009-12-11 21:37:07 +0000468 unsigned FileID = findCompileUnit(CU)->getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000469 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000470 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
471 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000472}
473
Devang Patel8287d662009-12-15 19:16:48 +0000474/// addSourceLine - Add location information to specified debug information
475/// entry.
476void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
477 // If there is no compile unit specified, don't add a line #.
478 if (NS->getCompileUnit().isNull())
479 return;
480
481 unsigned Line = NS->getLineNumber();
482 StringRef FN = NS->getFilename();
483 StringRef Dir = NS->getDirectory();
484
485 unsigned FileID = GetOrCreateSourceID(Dir, FN);
486 assert(FileID && "Invalid file id");
487 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
488 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
489}
490
Caroline Tice248d5572009-08-31 21:19:37 +0000491/* Byref variables, in Blocks, are declared by the programmer as
492 "SomeType VarName;", but the compiler creates a
493 __Block_byref_x_VarName struct, and gives the variable VarName
494 either the struct, or a pointer to the struct, as its type. This
495 is necessary for various behind-the-scenes things the compiler
496 needs to do with by-reference variables in blocks.
497
498 However, as far as the original *programmer* is concerned, the
499 variable should still have type 'SomeType', as originally declared.
500
501 The following function dives into the __Block_byref_x_VarName
502 struct to find the original type of the variable. This will be
503 passed back to the code generating the type for the Debug
504 Information Entry for the variable 'VarName'. 'VarName' will then
505 have the original type 'SomeType' in its debug information.
506
507 The original type 'SomeType' will be the type of the field named
508 'VarName' inside the __Block_byref_x_VarName struct.
509
510 NOTE: In order for this to not completely fail on the debugger
511 side, the Debug Information Entry for the variable VarName needs to
512 have a DW_AT_location that tells the debugger how to unwind through
513 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patelc50078e2009-11-21 02:48:08 +0000514 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-08-31 21:19:37 +0000515
516/// Find the type the programmer originally declared the variable to be
517/// and return that type.
518///
Devang Patelc50078e2009-11-21 02:48:08 +0000519DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-08-31 21:19:37 +0000520
521 DIType subType = Ty;
522 unsigned tag = Ty.getTag();
523
524 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000525 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000526 subType = DTy.getTypeDerivedFrom();
527 }
528
529 DICompositeType blockStruct = DICompositeType(subType.getNode());
530
531 DIArray Elements = blockStruct.getTypeArray();
532
533 if (Elements.isNull())
534 return Ty;
535
536 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
537 DIDescriptor Element = Elements.getElement(i);
538 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000539 if (Name == DT.getName())
Caroline Tice248d5572009-08-31 21:19:37 +0000540 return (DT.getTypeDerivedFrom());
541 }
542
543 return Ty;
544}
545
Devang Patelc50078e2009-11-21 02:48:08 +0000546/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000547/// and generate the DWARF information necessary to find the actual variable
548/// given the extra address information encoded in the DIVariable, starting from
549/// the starting location. Add the DWARF information to the die.
550///
Devang Patelc50078e2009-11-21 02:48:08 +0000551void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000552 unsigned Attribute,
553 const MachineLocation &Location) {
554 const DIVariable &VD = DV->getVariable();
555 DIType Ty = VD.getType();
556
557 // Decode the original location, and use that as the start of the byref
558 // variable's location.
559 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
560 DIEBlock *Block = new DIEBlock();
561
562 if (Location.isReg()) {
563 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000564 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000565 } else {
566 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000567 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
568 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000569 }
570 } else {
571 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000572 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000573 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000574 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
575 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000576 }
577
Devang Patelc50078e2009-11-21 02:48:08 +0000578 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000579 }
580
581 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
582 uint64_t Element = VD.getAddrElement(i);
583
584 if (Element == DIFactory::OpPlus) {
Devang Patelc50078e2009-11-21 02:48:08 +0000585 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
586 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000587 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000588 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000589 } else llvm_unreachable("unknown DIFactory Opcode");
590 }
591
592 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000593 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000594}
595
Caroline Tice248d5572009-08-31 21:19:37 +0000596/* Byref variables, in Blocks, are declared by the programmer as "SomeType
597 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
598 gives the variable VarName either the struct, or a pointer to the struct, as
599 its type. This is necessary for various behind-the-scenes things the
600 compiler needs to do with by-reference variables in Blocks.
601
602 However, as far as the original *programmer* is concerned, the variable
603 should still have type 'SomeType', as originally declared.
604
Devang Patelc50078e2009-11-21 02:48:08 +0000605 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000606 struct to find the original type of the variable, which is then assigned to
607 the variable's Debug Information Entry as its real type. So far, so good.
608 However now the debugger will expect the variable VarName to have the type
609 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbar41716322009-09-19 20:40:05 +0000610 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-08-31 21:19:37 +0000611 pointers and struct to find the actual variable of type SomeType.
612
613 The following function does just that. We start by getting
614 the "normal" location for the variable. This will be the location
615 of either the struct __Block_byref_x_VarName or the pointer to the
616 struct __Block_byref_x_VarName.
617
618 The struct will look something like:
619
620 struct __Block_byref_x_VarName {
621 ... <various fields>
622 struct __Block_byref_x_VarName *forwarding;
623 ... <various other fields>
624 SomeType VarName;
625 ... <maybe more fields>
626 };
627
628 If we are given the struct directly (as our starting point) we
629 need to tell the debugger to:
630
631 1). Add the offset of the forwarding field.
632
Dan Gohmandf1a7ff2010-02-10 16:03:48 +0000633 2). Follow that pointer to get the real __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000634 struct to use (the real one may have been copied onto the heap).
635
636 3). Add the offset for the field VarName, to find the actual variable.
637
638 If we started with a pointer to the struct, then we need to
639 dereference that pointer first, before the other steps.
640 Translating this into DWARF ops, we will need to append the following
641 to the current location description for the variable:
642
643 DW_OP_deref -- optional, if we start with a pointer
644 DW_OP_plus_uconst <forward_fld_offset>
645 DW_OP_deref
646 DW_OP_plus_uconst <varName_fld_offset>
647
648 That is what this function does. */
649
Devang Patelc50078e2009-11-21 02:48:08 +0000650/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-08-31 21:19:37 +0000651/// provided, and generate the DWARF information necessary to find the
652/// actual Block variable (navigating the Block struct) based on the
653/// starting location. Add the DWARF information to the die. For
654/// more information, read large comment just above here.
655///
Devang Patelc50078e2009-11-21 02:48:08 +0000656void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000657 unsigned Attribute,
658 const MachineLocation &Location) {
Caroline Tice248d5572009-08-31 21:19:37 +0000659 const DIVariable &VD = DV->getVariable();
660 DIType Ty = VD.getType();
661 DIType TmpTy = Ty;
662 unsigned Tag = Ty.getTag();
663 bool isPointer = false;
664
Devang Patel7f75bbe2009-11-25 17:36:49 +0000665 StringRef varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000666
667 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000668 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000669 TmpTy = DTy.getTypeDerivedFrom();
670 isPointer = true;
671 }
672
673 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
674
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000675 // Find the __forwarding field and the variable field in the __Block_byref
676 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000677 DIArray Fields = blockStruct.getTypeArray();
678 DIDescriptor varField = DIDescriptor();
679 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000680
681
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000682 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
683 DIDescriptor Element = Fields.getElement(i);
684 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000685 StringRef fieldName = DT.getName();
686 if (fieldName == "__forwarding")
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000687 forwardingField = Element;
Devang Patel7f75bbe2009-11-25 17:36:49 +0000688 else if (fieldName == varName)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000689 varField = Element;
690 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000691
Mike Stump2fd84e22009-09-24 23:21:26 +0000692 assert(!varField.isNull() && "Can't find byref variable in Block struct");
693 assert(!forwardingField.isNull()
694 && "Can't find forwarding field in Block struct");
Caroline Tice248d5572009-08-31 21:19:37 +0000695
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000696 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000697 unsigned int forwardingFieldOffset =
698 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
699 unsigned int varFieldOffset =
700 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000701
Mike Stump2fd84e22009-09-24 23:21:26 +0000702 // Decode the original location, and use that as the start of the byref
703 // variable's location.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000704 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
705 DIEBlock *Block = new DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000706
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000707 if (Location.isReg()) {
708 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000709 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000710 else {
711 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000712 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
713 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000714 }
715 } else {
716 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000717 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000718 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000719 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
720 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000721 }
Caroline Tice248d5572009-08-31 21:19:37 +0000722
Devang Patelc50078e2009-11-21 02:48:08 +0000723 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000724 }
Caroline Tice248d5572009-08-31 21:19:37 +0000725
Mike Stump2fd84e22009-09-24 23:21:26 +0000726 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000727 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000728 if (isPointer)
Devang Patelc50078e2009-11-21 02:48:08 +0000729 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000730
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000731 // Next add the offset for the '__forwarding' field:
732 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
733 // adding the offset if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000734 if (forwardingFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000735 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
736 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000737 }
Caroline Tice248d5572009-08-31 21:19:37 +0000738
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000739 // Now dereference the __forwarding field to get to the real __Block_byref
740 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000741 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000742
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000743 // Now that we've got the real __Block_byref... struct, add the offset
744 // for the variable's field to get to the location of the actual variable:
745 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000746 if (varFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000747 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
748 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000749 }
Caroline Tice248d5572009-08-31 21:19:37 +0000750
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000751 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000752 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000753}
754
Devang Patelc50078e2009-11-21 02:48:08 +0000755/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000756/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000757void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000758 const MachineLocation &Location) {
759 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
760 DIEBlock *Block = new DIEBlock();
761
762 if (Location.isReg()) {
763 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000764 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000765 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000766 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
767 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000768 }
769 } else {
770 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000771 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000772 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000773 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
774 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000775 }
776
Devang Patelc50078e2009-11-21 02:48:08 +0000777 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000778 }
779
Devang Patelc50078e2009-11-21 02:48:08 +0000780 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000781}
782
Devang Patel1a8f9a82009-12-10 19:14:49 +0000783/// addToContextOwner - Add Die into the list of its context owner's children.
784void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
785 if (Context.isNull())
786 ModuleCU->addDie(Die);
787 else if (Context.isType()) {
788 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
789 ContextDIE->addChild(Die);
Devang Patel8287d662009-12-15 19:16:48 +0000790 } else if (Context.isNameSpace()) {
791 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode()));
792 ContextDIE->addChild(Die);
Devang Patel1a8f9a82009-12-10 19:14:49 +0000793 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
794 ContextDIE->addChild(Die);
795 else
796 ModuleCU->addDie(Die);
797}
798
Devang Patel7f139c12009-12-10 18:05:33 +0000799/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
800/// given DIType.
801DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
802 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
803 if (TyDIE)
804 return TyDIE;
805
806 // Create new type.
807 TyDIE = new DIE(dwarf::DW_TAG_base_type);
808 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
809 if (Ty.isBasicType())
810 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
811 else if (Ty.isCompositeType())
812 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
813 else {
814 assert(Ty.isDerivedType() && "Unknown kind of DIType");
815 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
816 }
817
Devang Patel1a8f9a82009-12-10 19:14:49 +0000818 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel7f139c12009-12-10 18:05:33 +0000819 return TyDIE;
820}
821
Devang Patelc50078e2009-11-21 02:48:08 +0000822/// addType - Add a new type attribute to the specified entity.
Devang Patelfe0be132009-12-09 18:24:21 +0000823void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000824 if (Ty.isNull())
825 return;
826
827 // Check for pre-existence.
Devang Patelfe0be132009-12-09 18:24:21 +0000828 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000829 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000830 if (Entry) {
831 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000832 return;
833 }
834
835 // Set up proxy.
Devang Patelc50078e2009-11-21 02:48:08 +0000836 Entry = createDIEEntry();
Devang Patelfe0be132009-12-09 18:24:21 +0000837 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000838
839 // Construct type.
Devang Patel7f139c12009-12-10 18:05:33 +0000840 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000841
Devang Patelc50078e2009-11-21 02:48:08 +0000842 Entry->setEntry(Buffer);
843 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000844}
845
Devang Patelc50078e2009-11-21 02:48:08 +0000846/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patelfe0be132009-12-09 18:24:21 +0000847void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000848 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000849 StringRef Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000850 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000851 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000852 BTy.getEncoding());
853
854 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000855 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000856 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000857 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000858 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000859}
860
Devang Patelc50078e2009-11-21 02:48:08 +0000861/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patelfe0be132009-12-09 18:24:21 +0000862void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000863 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000864 StringRef Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000865 uint64_t Size = DTy.getSizeInBits() >> 3;
866 unsigned Tag = DTy.getTag();
867
868 // FIXME - Workaround for templates.
869 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
870
871 Buffer.setTag(Tag);
872
873 // Map to main type, void will not have a type.
874 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelfe0be132009-12-09 18:24:21 +0000875 addType(&Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000876
877 // Add name if not anonymous or intermediate type.
Devang Patel76b80672009-11-30 23:56:56 +0000878 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000879 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000880
881 // Add size if non-zero (derived types might be zero-sized.)
882 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000883 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000884
885 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000886 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000887 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000888}
889
Devang Patelc50078e2009-11-21 02:48:08 +0000890/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +0000891void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000892 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000893 StringRef Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000894
895 uint64_t Size = CTy.getSizeInBits() >> 3;
896 unsigned Tag = CTy.getTag();
897 Buffer.setTag(Tag);
898
899 switch (Tag) {
900 case dwarf::DW_TAG_vector_type:
901 case dwarf::DW_TAG_array_type:
Devang Patelfe0be132009-12-09 18:24:21 +0000902 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000903 break;
904 case dwarf::DW_TAG_enumeration_type: {
905 DIArray Elements = CTy.getTypeArray();
906
907 // Add enumerators to enumeration type.
908 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
909 DIE *ElemDie = NULL;
Devang Patel15e723d2009-08-28 23:24:31 +0000910 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelfb812752009-10-09 17:51:49 +0000911 if (!Enum.isNull()) {
Devang Patelfe0be132009-12-09 18:24:21 +0000912 ElemDie = constructEnumTypeDIE(&Enum);
Devang Patelc50078e2009-11-21 02:48:08 +0000913 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +0000914 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000915 }
916 }
917 break;
918 case dwarf::DW_TAG_subroutine_type: {
919 // Add return type.
920 DIArray Elements = CTy.getTypeArray();
921 DIDescriptor RTy = Elements.getElement(0);
Devang Patelfe0be132009-12-09 18:24:21 +0000922 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000923
924 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +0000925 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000926
927 // Add arguments.
928 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
929 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
930 DIDescriptor Ty = Elements.getElement(i);
Devang Patelfe0be132009-12-09 18:24:21 +0000931 addType(Arg, DIType(Ty.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000932 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000933 }
934 }
935 break;
936 case dwarf::DW_TAG_structure_type:
937 case dwarf::DW_TAG_union_type:
938 case dwarf::DW_TAG_class_type: {
939 // Add elements to structure type.
940 DIArray Elements = CTy.getTypeArray();
941
942 // A forward struct declared type may not have elements available.
943 if (Elements.isNull())
944 break;
945
946 // Add elements to structure type.
947 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
948 DIDescriptor Element = Elements.getElement(i);
Devang Patel15e723d2009-08-28 23:24:31 +0000949 if (Element.isNull())
950 continue;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000951 DIE *ElemDie = NULL;
952 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel814a12c2009-12-14 16:18:45 +0000953 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Devang Patel423dfa02010-01-30 01:08:30 +0000954 else if (Element.getTag() == dwarf::DW_TAG_auto_variable) {
955 DIVariable DV(Element.getNode());
956 ElemDie = new DIE(dwarf::DW_TAG_variable);
957 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
958 DV.getName());
959 addType(ElemDie, DV.getType());
960 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
961 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
962 addSourceLine(ElemDie, &DV);
963 } else
Devang Patelfe0be132009-12-09 18:24:21 +0000964 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000965 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000966 }
967
Devang Patel20b32102009-08-27 23:51:51 +0000968 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +0000969 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000970
971 unsigned RLang = CTy.getRunTimeLang();
972 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +0000973 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000974 dwarf::DW_FORM_data1, RLang);
Devang Patel5ae52402010-01-26 21:16:06 +0000975
976 DICompositeType ContainingType = CTy.getContainingType();
977 if (!ContainingType.isNull())
978 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
979 getOrCreateTypeDIE(DIType(ContainingType.getNode())));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000980 break;
981 }
982 default:
983 break;
984 }
985
986 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000987 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000988 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000989
Devang Pateldc73c372010-01-29 18:34:58 +0000990 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000991 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
992 // Add size if non-zero (derived types might be zero-sized.)
993 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000994 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000995 else {
996 // Add zero size if it is not a forward declaration.
997 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000998 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000999 else
Devang Patelc50078e2009-11-21 02:48:08 +00001000 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001001 }
1002
1003 // Add source line info if available.
1004 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +00001005 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001006 }
1007}
1008
Devang Patelc50078e2009-11-21 02:48:08 +00001009/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1010void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001011 int64_t L = SR.getLo();
1012 int64_t H = SR.getHi();
1013 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1014
Devang Patelc50078e2009-11-21 02:48:08 +00001015 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +00001016 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +00001017 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld3df6972009-12-04 23:10:24 +00001018 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001019
Devang Patelc50078e2009-11-21 02:48:08 +00001020 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001021}
1022
Devang Patelc50078e2009-11-21 02:48:08 +00001023/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +00001024void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001025 DICompositeType *CTy) {
1026 Buffer.setTag(dwarf::DW_TAG_array_type);
1027 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001028 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001029
1030 // Emit derived type.
Devang Patelfe0be132009-12-09 18:24:21 +00001031 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001032 DIArray Elements = CTy->getTypeArray();
1033
Devang Patel1233f912009-11-21 00:31:03 +00001034 // Get an anonymous type for index type.
Devang Patelfe0be132009-12-09 18:24:21 +00001035 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel1233f912009-11-21 00:31:03 +00001036 if (!IdxTy) {
1037 // Construct an anonymous type for index type.
1038 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +00001039 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1040 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +00001041 dwarf::DW_ATE_signed);
Devang Patelfe0be132009-12-09 18:24:21 +00001042 ModuleCU->addDie(IdxTy);
1043 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +00001044 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001045
1046 // Add subranges to array type.
1047 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1048 DIDescriptor Element = Elements.getElement(i);
1049 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001050 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001051 }
1052}
1053
Devang Patelc50078e2009-11-21 02:48:08 +00001054/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patelfe0be132009-12-09 18:24:21 +00001055DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator *ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001056 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001057 StringRef Name = ETy->getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001058 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001059 int64_t Value = ETy->getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +00001060 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001061 return Enumerator;
1062}
1063
Devang Patel141e1c42010-01-05 01:46:14 +00001064/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1065/// printer to not emit usual symbol prefix before the symbol name is used then
1066/// return linkage name after skipping this special LLVM prefix.
1067static StringRef getRealLinkageName(StringRef LinkageName) {
1068 char One = '\1';
1069 if (LinkageName.startswith(StringRef(&One, 1)))
1070 return LinkageName.substr(1);
1071 return LinkageName;
1072}
1073
Devang Patelc50078e2009-11-21 02:48:08 +00001074/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patelfe0be132009-12-09 18:24:21 +00001075DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001076 // If the global variable was optmized out then no need to create debug info
1077 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +00001078 if (!GV.getGlobal()) return NULL;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001079 if (GV.getDisplayName().empty()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +00001080
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001081 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +00001082 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001083 GV.getDisplayName());
1084
Devang Patel7f75bbe2009-11-25 17:36:49 +00001085 StringRef LinkageName = GV.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001086 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001087 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001088 getRealLinkageName(LinkageName));
1089
Devang Patelfe0be132009-12-09 18:24:21 +00001090 addType(GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001091 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001092 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1093 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001094
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001095 return GVDie;
1096}
1097
Devang Patelc50078e2009-11-21 02:48:08 +00001098/// createMemberDIE - Create new member DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001099DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001100 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel7f75bbe2009-11-25 17:36:49 +00001101 StringRef Name = DT.getName();
1102 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001103 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001104
Devang Patelfe0be132009-12-09 18:24:21 +00001105 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001106
Devang Patelc50078e2009-11-21 02:48:08 +00001107 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001108
Devang Patel7d9fe582009-11-04 22:06:12 +00001109 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001110 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001111
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001112 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001113 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001114
1115 if (Size != FieldSize) {
1116 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001117 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1118 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001119
1120 uint64_t Offset = DT.getOffsetInBits();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001121 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1122 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
Benjamin Kramer631e3e32010-01-07 17:50:57 +00001123 uint64_t FieldOffset = (HiMark - FieldSize);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001124 Offset -= FieldOffset;
1125
1126 // Maybe we need to work from the other end.
1127 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001128 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001129
Devang Patel7d9fe582009-11-04 22:06:12 +00001130 // Here WD_AT_data_member_location points to the anonymous
1131 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001132 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001133
1134 } else
1135 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001136 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001137
Devang Patel84aba942010-02-03 20:08:48 +00001138 if (DT.getTag() == dwarf::DW_TAG_inheritance
1139 && DT.isVirtual()) {
1140
1141 // For C++, virtual base classes are not at fixed offset. Use following
1142 // expression to extract appropriate offset from vtable.
1143 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1144
1145 DIEBlock *VBaseLocationDie = new DIEBlock();
1146 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1147 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1148 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1149 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1150 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1151 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1152 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1153
1154 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1155 VBaseLocationDie);
1156 } else
1157 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001158
1159 if (DT.isProtected())
Devang Patel188c85d2009-12-03 19:11:07 +00001160 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001161 dwarf::DW_ACCESS_protected);
1162 else if (DT.isPrivate())
Devang Patel188c85d2009-12-03 19:11:07 +00001163 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001164 dwarf::DW_ACCESS_private);
Devang Patel188c85d2009-12-03 19:11:07 +00001165 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1166 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1167 dwarf::DW_ACCESS_public);
1168 if (DT.isVirtual())
1169 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1170 dwarf::DW_VIRTUALITY_virtual);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001171 return MemberDie;
1172}
1173
Devang Patel814a12c2009-12-14 16:18:45 +00001174/// createSubprogramDIE - Create new DIE using SP.
1175DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1176 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1177 if (SPDie)
1178 return SPDie;
1179
1180 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel36cd9f82010-03-02 17:58:15 +00001181 // Constructors and operators for anonymous aggregates do not have names.
Devang Patel0ab194f2010-03-02 01:26:20 +00001182 if (!SP.getName().empty())
1183 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001184
Devang Patel7f75bbe2009-11-25 17:36:49 +00001185 StringRef LinkageName = SP.getLinkageName();
Devang Patel141e1c42010-01-05 01:46:14 +00001186 if (!LinkageName.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001187 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel141e1c42010-01-05 01:46:14 +00001188 getRealLinkageName(LinkageName));
1189
Devang Patelc50078e2009-11-21 02:48:08 +00001190 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001191
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001192 // Add prototyped tag, if C or ObjC.
1193 unsigned Lang = SP.getCompileUnit().getLanguage();
1194 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1195 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001196 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001197
1198 // Add Return Type.
Devang Patelc1df8792009-12-03 01:25:38 +00001199 DICompositeType SPTy = SP.getType();
1200 DIArray Args = SPTy.getTypeArray();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001201 unsigned SPTag = SPTy.getTag();
Devang Patel188c85d2009-12-03 19:11:07 +00001202
Devang Patelc1df8792009-12-03 01:25:38 +00001203 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelfe0be132009-12-09 18:24:21 +00001204 addType(SPDie, SPTy);
Devang Patelc1df8792009-12-03 01:25:38 +00001205 else
Devang Patelfe0be132009-12-09 18:24:21 +00001206 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patelc1df8792009-12-03 01:25:38 +00001207
Devang Patel188c85d2009-12-03 19:11:07 +00001208 unsigned VK = SP.getVirtuality();
1209 if (VK) {
1210 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1211 DIEBlock *Block = new DIEBlock();
1212 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1213 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1214 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
Devang Patel7d707f92010-01-19 06:19:05 +00001215 ContainingTypeMap.insert(std::make_pair(SPDie,
1216 SP.getContainingType().getNode()));
Devang Patel188c85d2009-12-03 19:11:07 +00001217 }
1218
Devang Patel814a12c2009-12-14 16:18:45 +00001219 if (MakeDecl || !SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001220 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001221
1222 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patelc1df8792009-12-03 01:25:38 +00001223 // be handled while processing variables.
1224 DICompositeType SPTy = SP.getType();
1225 DIArray Args = SPTy.getTypeArray();
1226 unsigned SPTag = SPTy.getTag();
1227
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001228 if (SPTag == dwarf::DW_TAG_subroutine_type)
1229 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1230 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc5dc7252010-02-06 01:02:37 +00001231 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1232 addType(Arg, ATy);
1233 if (ATy.isArtificial())
1234 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patelc50078e2009-11-21 02:48:08 +00001235 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001236 }
1237 }
1238
Devang Patelbf7d00a2010-02-03 19:57:19 +00001239 if (SP.isArtificial())
1240 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1241
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001242 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001243 ModuleCU->insertDIE(SP.getNode(), SPDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001244 return SPDie;
1245}
1246
Devang Patelc50078e2009-11-21 02:48:08 +00001247/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001248///
Devang Patelb9f2c6b2009-12-11 21:37:07 +00001249CompileUnit *DwarfDebug::findCompileUnit(DICompileUnit Unit) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001250 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patel15e723d2009-08-28 23:24:31 +00001251 CompileUnitMap.find(Unit.getNode());
Devang Patelb9f2c6b2009-12-11 21:37:07 +00001252 if (I == CompileUnitMap.end())
1253 return constructCompileUnit(Unit.getNode());
1254 return I->second;
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001255}
1256
Devang Patel90a0fe32009-11-10 23:06:00 +00001257/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1258/// Initialize scope and update scope hierarchy.
1259DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1260 MDNode *InlinedAt) {
1261 assert (N && "Invalid Scope encoding!");
1262 assert (MI && "Missing machine instruction!");
1263 bool GetConcreteScope = (MI && InlinedAt);
1264
1265 DbgScope *NScope = NULL;
1266
1267 if (InlinedAt)
1268 NScope = DbgScopeMap.lookup(InlinedAt);
1269 else
1270 NScope = DbgScopeMap.lookup(N);
1271 assert (NScope && "Unable to find working scope!");
1272
1273 if (NScope->getFirstInsn())
1274 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001275
1276 DbgScope *Parent = NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001277 if (GetConcreteScope) {
Devang Pateldd7bb432009-10-14 21:08:09 +00001278 DILocation IL(InlinedAt);
Jim Grosbach652b7432009-11-21 23:12:12 +00001279 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel90a0fe32009-11-10 23:06:00 +00001280 IL.getOrigLocation().getNode());
1281 assert (Parent && "Unable to find Parent scope!");
1282 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001283 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001284 } else if (DIDescriptor(N).isLexicalBlock()) {
1285 DILexicalBlock DB(N);
1286 if (!DB.getContext().isNull()) {
1287 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1288 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001289 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001290 }
Devang Pateldd7bb432009-10-14 21:08:09 +00001291 }
Devang Patel6a260102009-10-01 20:31:14 +00001292
Devang Patelf5278f22009-10-27 20:47:17 +00001293 NScope->setFirstInsn(MI);
Devang Patel6a260102009-10-01 20:31:14 +00001294
Devang Patel90a0fe32009-11-10 23:06:00 +00001295 if (!Parent && !InlinedAt) {
Devang Patelce8986f2009-11-11 00:31:36 +00001296 StringRef SPName = DISubprogram(N).getLinkageName();
1297 if (SPName == MF->getFunction()->getName())
1298 CurrentFnDbgScope = NScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00001299 }
Devang Patel6a260102009-10-01 20:31:14 +00001300
Devang Patel90a0fe32009-11-10 23:06:00 +00001301 if (GetConcreteScope) {
1302 ConcreteScopes[InlinedAt] = NScope;
1303 getOrCreateAbstractScope(N);
1304 }
1305
Devang Patelf5278f22009-10-27 20:47:17 +00001306 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001307}
1308
Devang Patel90a0fe32009-11-10 23:06:00 +00001309DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1310 assert (N && "Invalid Scope encoding!");
1311
1312 DbgScope *AScope = AbstractScopes.lookup(N);
1313 if (AScope)
1314 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001315
Devang Patel90a0fe32009-11-10 23:06:00 +00001316 DbgScope *Parent = NULL;
1317
1318 DIDescriptor Scope(N);
1319 if (Scope.isLexicalBlock()) {
1320 DILexicalBlock DB(N);
1321 DIDescriptor ParentDesc = DB.getContext();
1322 if (!ParentDesc.isNull())
1323 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1324 }
1325
1326 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1327
1328 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001329 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001330 AScope->setAbstractScope();
1331 AbstractScopes[N] = AScope;
1332 if (DIDescriptor(N).isSubprogram())
1333 AbstractScopesList.push_back(AScope);
1334 return AScope;
1335}
Devang Patel6a260102009-10-01 20:31:14 +00001336
Jim Grosbach652b7432009-11-21 23:12:12 +00001337/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-11-21 02:48:08 +00001338/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1339/// If there are global variables in this scope then create and insert
1340/// DIEs for these variables.
1341DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001342
Devang Pateld90672c2009-11-20 21:37:22 +00001343 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel90a0fe32009-11-10 23:06:00 +00001344 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patel814a12c2009-12-14 16:18:45 +00001345 DISubprogram SP(SPNode);
Devang Patel530a81e2010-02-05 23:09:20 +00001346 // There is not any need to generate specification DIE for a function
1347 // defined at compile unit level. If a function is defined inside another
1348 // function then gdb prefers the definition at top level and but does not
1349 // expect specification DIE in parent function. So avoid creating
1350 // specification DIE for a function defined inside a function.
1351 if (SP.isDefinition() && !SP.getContext().isCompileUnit()
1352 && !SP.getContext().isSubprogram()) {
Devang Patel814a12c2009-12-14 16:18:45 +00001353 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1354 // Add arguments.
1355 DICompositeType SPTy = SP.getType();
1356 DIArray Args = SPTy.getTypeArray();
1357 unsigned SPTag = SPTy.getTag();
1358 if (SPTag == dwarf::DW_TAG_subroutine_type)
1359 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1360 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc5dc7252010-02-06 01:02:37 +00001361 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1362 addType(Arg, ATy);
1363 if (ATy.isArtificial())
1364 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel814a12c2009-12-14 16:18:45 +00001365 SPDie->addChild(Arg);
1366 }
1367 DIE *SPDeclDie = SPDie;
1368 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1369 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1370 SPDeclDie);
Devang Patel814a12c2009-12-14 16:18:45 +00001371 ModuleCU->addDie(SPDie);
1372 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001373
Devang Patelc50078e2009-11-21 02:48:08 +00001374 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001375 DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001376 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001377 DWLabel("func_end", SubprogramCount));
1378 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patelc50078e2009-11-21 02:48:08 +00001379 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach652b7432009-11-21 23:12:12 +00001380
Devang Patel90a0fe32009-11-10 23:06:00 +00001381 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001382 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001383
Devang Patel90a0fe32009-11-10 23:06:00 +00001384 return SPDie;
1385}
1386
Jim Grosbach652b7432009-11-21 23:12:12 +00001387/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001388/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1389DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001390 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1391 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1392
1393 // Ignore empty scopes.
1394 if (StartID == EndID && StartID != 0)
1395 return NULL;
1396
1397 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1398 if (Scope->isAbstractScope())
1399 return ScopeDIE;
1400
Devang Patelc50078e2009-11-21 02:48:08 +00001401 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach652b7432009-11-21 23:12:12 +00001402 StartID ?
1403 DWLabel("label", StartID)
Devang Patel90a0fe32009-11-10 23:06:00 +00001404 : DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001405 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach652b7432009-11-21 23:12:12 +00001406 EndID ?
1407 DWLabel("label", EndID)
Devang Patel90a0fe32009-11-10 23:06:00 +00001408 : DWLabel("func_end", SubprogramCount));
1409
1410
1411
1412 return ScopeDIE;
1413}
1414
Devang Patelc50078e2009-11-21 02:48:08 +00001415/// constructInlinedScopeDIE - This scope represents inlined body of
1416/// a function. Construct DIE to represent this concrete inlined copy
1417/// of the function.
1418DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001419 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1420 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1421 assert (StartID && "Invalid starting label for an inlined scope!");
1422 assert (EndID && "Invalid end label for an inlined scope!");
1423 // Ignore empty scopes.
1424 if (StartID == EndID && StartID != 0)
1425 return NULL;
1426
1427 DIScope DS(Scope->getScopeNode());
1428 if (DS.isNull())
1429 return NULL;
1430 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1431
1432 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001433 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001434 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001435 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001436 dwarf::DW_FORM_ref4, OriginDIE);
1437
Devang Patelc50078e2009-11-21 02:48:08 +00001438 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001439 DWLabel("label", StartID));
Devang Patelc50078e2009-11-21 02:48:08 +00001440 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001441 DWLabel("label", EndID));
1442
1443 InlinedSubprogramDIEs.insert(OriginDIE);
1444
1445 // Track the start label for this inlined function.
Devang Patel7d707f92010-01-19 06:19:05 +00001446 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
Devang Patel90a0fe32009-11-10 23:06:00 +00001447 I = InlineInfo.find(InlinedSP.getNode());
1448
1449 if (I == InlineInfo.end()) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001450 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1451 ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001452 InlinedSPNodes.push_back(InlinedSP.getNode());
1453 } else
1454 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1455
1456 StringPool.insert(InlinedSP.getName());
Devang Patel141e1c42010-01-05 01:46:14 +00001457 StringPool.insert(getRealLinkageName(InlinedSP.getLinkageName()));
1458
Devang Patel90a0fe32009-11-10 23:06:00 +00001459 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001460 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1461 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001462
1463 return ScopeDIE;
1464}
1465
Devang Patelc50078e2009-11-21 02:48:08 +00001466
1467/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelfe0be132009-12-09 18:24:21 +00001468DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001469 // Get the descriptor.
1470 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001471 StringRef Name = VD.getName();
1472 if (Name.empty())
Devang Patelab9a0682009-11-13 02:25:26 +00001473 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001474
1475 // Translate tag to proper Dwarf tag. The result variable is dropped for
1476 // now.
1477 unsigned Tag;
1478 switch (VD.getTag()) {
1479 case dwarf::DW_TAG_return_variable:
1480 return NULL;
1481 case dwarf::DW_TAG_arg_variable:
1482 Tag = dwarf::DW_TAG_formal_parameter;
1483 break;
1484 case dwarf::DW_TAG_auto_variable: // fall thru
1485 default:
1486 Tag = dwarf::DW_TAG_variable;
1487 break;
1488 }
1489
1490 // Define variable debug information entry.
1491 DIE *VariableDie = new DIE(Tag);
1492
1493
1494 DIE *AbsDIE = NULL;
1495 if (DbgVariable *AV = DV->getAbstractVariable())
1496 AbsDIE = AV->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001497
Devang Patel90a0fe32009-11-10 23:06:00 +00001498 if (AbsDIE) {
1499 DIScope DS(Scope->getScopeNode());
1500 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001501 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001502 (void) OriginSPDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001503 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1504 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1505 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001506 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001507 dwarf::DW_FORM_ref4, AbsDIE);
1508 }
1509 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001510 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1511 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001512
1513 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001514 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001515 // addresses instead.
1516 if (VD.isBlockByrefVariable())
Devang Patelfe0be132009-12-09 18:24:21 +00001517 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001518 else
Devang Patelfe0be132009-12-09 18:24:21 +00001519 addType(VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001520 }
1521
1522 // Add variable address.
1523 if (!Scope->isAbstractScope()) {
1524 MachineLocation Location;
Jim Grosbach587d4882009-11-22 20:14:00 +00001525 unsigned FrameReg;
1526 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1527 Location.set(FrameReg, Offset);
Jim Grosbach652b7432009-11-21 23:12:12 +00001528
Devang Patel90a0fe32009-11-10 23:06:00 +00001529 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001530 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001531 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001532 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001533 else
Devang Patelc50078e2009-11-21 02:48:08 +00001534 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001535 }
Devang Patelc5dc7252010-02-06 01:02:37 +00001536
1537 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1538 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001539 DV->setDIE(VariableDie);
1540 return VariableDie;
1541
1542}
Devang Patelc50078e2009-11-21 02:48:08 +00001543
Devang Patelec13b4f2009-11-24 01:14:22 +00001544void DwarfDebug::addPubTypes(DISubprogram SP) {
1545 DICompositeType SPTy = SP.getType();
1546 unsigned SPTag = SPTy.getTag();
1547 if (SPTag != dwarf::DW_TAG_subroutine_type)
1548 return;
1549
1550 DIArray Args = SPTy.getTypeArray();
1551 if (Args.isNull())
1552 return;
1553
1554 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1555 DIType ATy(Args.getElement(i).getNode());
1556 if (ATy.isNull())
1557 continue;
1558 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001559 if (!CATy.isNull() && !CATy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001560 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1561 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1562 }
1563 }
1564}
1565
Devang Patelc50078e2009-11-21 02:48:08 +00001566/// constructScopeDIE - Construct a DIE for this scope.
1567DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001568 if (!Scope)
1569 return NULL;
1570 DIScope DS(Scope->getScopeNode());
1571 if (DS.isNull())
1572 return NULL;
1573
1574 DIE *ScopeDIE = NULL;
1575 if (Scope->getInlinedAt())
Devang Patelc50078e2009-11-21 02:48:08 +00001576 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001577 else if (DS.isSubprogram()) {
1578 if (Scope->isAbstractScope())
Devang Pateld90672c2009-11-20 21:37:22 +00001579 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001580 else
Devang Patelc50078e2009-11-21 02:48:08 +00001581 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001582 }
1583 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001584 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001585 if (!ScopeDIE) return NULL;
1586 }
1587
1588 // Add variables to scope.
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +00001589 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Devang Patel90a0fe32009-11-10 23:06:00 +00001590 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelfe0be132009-12-09 18:24:21 +00001591 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach652b7432009-11-21 23:12:12 +00001592 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001593 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001594 }
1595
1596 // Add nested scopes.
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +00001597 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Devang Patel90a0fe32009-11-10 23:06:00 +00001598 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1599 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001600 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001601 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001602 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001603 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001604
1605 if (DS.isSubprogram())
1606 addPubTypes(DISubprogram(DS.getNode()));
1607
1608 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001609}
1610
Bill Wendlingf5839192009-05-20 23:19:06 +00001611/// GetOrCreateSourceID - Look up the source id with the given directory and
1612/// source file names. If none currently exists, create a new id and insert it
1613/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1614/// maps as well.
Devang Patel7f75bbe2009-11-25 17:36:49 +00001615unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001616 unsigned DId;
1617 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1618 if (DI != DirectoryIdMap.end()) {
1619 DId = DI->getValue();
1620 } else {
1621 DId = DirectoryNames.size() + 1;
1622 DirectoryIdMap[DirName] = DId;
1623 DirectoryNames.push_back(DirName);
1624 }
1625
1626 unsigned FId;
1627 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1628 if (FI != SourceFileIdMap.end()) {
1629 FId = FI->getValue();
1630 } else {
1631 FId = SourceFileNames.size() + 1;
1632 SourceFileIdMap[FileName] = FId;
1633 SourceFileNames.push_back(FileName);
1634 }
1635
1636 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1637 SourceIdMap.find(std::make_pair(DId, FId));
1638 if (SI != SourceIdMap.end())
1639 return SI->second;
1640
1641 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1642 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1643 SourceIds.push_back(std::make_pair(DId, FId));
1644
1645 return SrcId;
1646}
1647
Devang Patel8287d662009-12-15 19:16:48 +00001648/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1649DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1650 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1651 if (NDie)
1652 return NDie;
1653 NDie = new DIE(dwarf::DW_TAG_namespace);
1654 ModuleCU->insertDIE(NS.getNode(), NDie);
1655 if (!NS.getName().empty())
1656 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1657 addSourceLine(NDie, &NS);
1658 addToContextOwner(NDie, NS.getContext());
1659 return NDie;
1660}
1661
Devang Patelb9f2c6b2009-12-11 21:37:07 +00001662CompileUnit *DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001663 DICompileUnit DIUnit(N);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001664 StringRef FN = DIUnit.getFilename();
1665 StringRef Dir = DIUnit.getDirectory();
Devang Patelaaf012e2009-09-29 18:40:58 +00001666 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001667
1668 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelc50078e2009-11-21 02:48:08 +00001669 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf5839192009-05-20 23:19:06 +00001670 DWLabel("section_line", 0), DWLabel("section_line", 0),
1671 false);
Devang Patelc50078e2009-11-21 02:48:08 +00001672 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001673 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001674 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001675 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001676 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001677
Devang Patel7f75bbe2009-11-25 17:36:49 +00001678 if (!Dir.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001679 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001680 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001681 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001682
Devang Patel7f75bbe2009-11-25 17:36:49 +00001683 StringRef Flags = DIUnit.getFlags();
1684 if (!Flags.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001685 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001686
1687 unsigned RVer = DIUnit.getRunTimeVersion();
1688 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001689 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001690 dwarf::DW_FORM_data1, RVer);
1691
1692 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel5a3d37f2009-06-29 20:45:18 +00001693 if (!ModuleCU && DIUnit.isMain()) {
Devang Patelf97a05a2009-06-29 20:38:13 +00001694 // Use first compile unit marked as isMain as the compile unit
1695 // for this module.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001696 ModuleCU = Unit;
Devang Patelf97a05a2009-06-29 20:38:13 +00001697 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001698
Devang Patel15e723d2009-08-28 23:24:31 +00001699 CompileUnitMap[DIUnit.getNode()] = Unit;
Devang Patelb9f2c6b2009-12-11 21:37:07 +00001700 return Unit;
Bill Wendlingf5839192009-05-20 23:19:06 +00001701}
1702
Devang Patelc50078e2009-11-21 02:48:08 +00001703void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001704 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001705
Devang Patel0c03f062009-09-04 23:59:07 +00001706 // If debug information is malformed then ignore it.
1707 if (DI_GV.Verify() == false)
1708 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001709
1710 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001711 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel166f8432009-06-26 01:49:18 +00001712 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001713
Devang Patelfe0be132009-12-09 18:24:21 +00001714 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel6d479632009-12-10 23:25:41 +00001715 if (!VariableDie)
1716 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001717
Bill Wendlingf5839192009-05-20 23:19:06 +00001718 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001719 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001720
1721 // Add to context owner.
Devang Pateldcc0dce2010-01-15 01:12:22 +00001722 DIDescriptor GVContext = DI_GV.getContext();
1723 // Do not create specification DIE if context is either compile unit
1724 // or a subprogram.
1725 if (DI_GV.isDefinition() && !GVContext.isCompileUnit()
1726 && !GVContext.isSubprogram()) {
Devang Patel8287d662009-12-15 19:16:48 +00001727 // Create specification DIE.
1728 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1729 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1730 dwarf::DW_FORM_ref4, VariableDie);
1731 DIEBlock *Block = new DIEBlock();
1732 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1733 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner41e330f2010-01-16 18:50:28 +00001734 Asm->GetGlobalValueSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001735 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Devang Patela0a98582010-02-09 01:58:33 +00001736 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Devang Patel8287d662009-12-15 19:16:48 +00001737 ModuleCU->addDie(VariableSpecDIE);
1738 } else {
1739 DIEBlock *Block = new DIEBlock();
1740 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1741 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Chris Lattner41e330f2010-01-16 18:50:28 +00001742 Asm->GetGlobalValueSymbol(DI_GV.getGlobal()));
Devang Patel8287d662009-12-15 19:16:48 +00001743 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1744 }
Devang Pateldcc0dce2010-01-15 01:12:22 +00001745 addToContextOwner(VariableDie, GVContext);
Devang Patel1a8f9a82009-12-10 19:14:49 +00001746
Bill Wendlingf5839192009-05-20 23:19:06 +00001747 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001748 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001749
1750 DIType GTy = DI_GV.getType();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001751 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001752 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1753 assert (Entry && "Missing global type!");
1754 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1755 }
Devang Patel166f8432009-06-26 01:49:18 +00001756 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001757}
1758
Devang Patelc50078e2009-11-21 02:48:08 +00001759void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001760 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001761
1762 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001763 if (ModuleCU->getDIE(N))
Devang Patel166f8432009-06-26 01:49:18 +00001764 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001765
1766 if (!SP.isDefinition())
1767 // This is a method declaration which will be handled while constructing
1768 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001769 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001770
Devang Patelfe0be132009-12-09 18:24:21 +00001771 DIE *SubprogramDie = createSubprogramDIE(SP);
Bill Wendlingf5839192009-05-20 23:19:06 +00001772
1773 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001774 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001775
1776 // Add to context owner.
Devang Patel8287d662009-12-15 19:16:48 +00001777 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patelde2d3682009-12-08 23:21:45 +00001778
Bill Wendlingf5839192009-05-20 23:19:06 +00001779 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001780 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001781
Devang Patel166f8432009-06-26 01:49:18 +00001782 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001783}
1784
Devang Patelc50078e2009-11-21 02:48:08 +00001785/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001786/// content. Create global DIEs and emit initial debug info sections.
1787/// This is inovked by the target AsmPrinter.
Devang Patelc50078e2009-11-21 02:48:08 +00001788void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel59a1d422009-06-25 22:36:02 +00001789 this->M = M;
1790
Bill Wendlingf5839192009-05-20 23:19:06 +00001791 if (TimePassesIsEnabled)
1792 DebugTimer->startTimer();
1793
Devang Patel1b4d6832009-11-11 19:55:08 +00001794 if (!MAI->doesSupportDebugInformation())
1795 return;
1796
Devang Patelfda766d2009-07-30 18:56:46 +00001797 DebugInfoFinder DbgFinder;
1798 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001799
Bill Wendlingf5839192009-05-20 23:19:06 +00001800 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001801 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1802 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001803 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001804
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +00001805 if (CompileUnitMap.empty()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001806 if (TimePassesIsEnabled)
1807 DebugTimer->stopTimer();
1808
1809 return;
1810 }
1811
Devang Patelf97a05a2009-06-29 20:38:13 +00001812 // If main compile unit for this module is not seen than randomly
1813 // select first compile unit.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001814 if (!ModuleCU)
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +00001815 ModuleCU = CompileUnitMap.begin()->second;
Devang Patelf97a05a2009-06-29 20:38:13 +00001816
Devang Patel90a0fe32009-11-10 23:06:00 +00001817 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001818 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1819 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001820 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001821
Devang Patel1a8f9a82009-12-10 19:14:49 +00001822 // Create DIEs for each global variable.
1823 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1824 E = DbgFinder.global_variable_end(); I != E; ++I)
1825 constructGlobalVariableDIE(*I);
1826
Bill Wendlingf5839192009-05-20 23:19:06 +00001827 MMI = mmi;
1828 shouldEmit = true;
1829 MMI->setDebugInfoAvailability(true);
1830
1831 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001832 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001833
1834 // Print out .file directives to specify files for .loc directives. These are
1835 // printed out early so that they precede any .loc directives.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001836 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001837 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1838 // Remember source id starts at 1.
1839 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
Chris Lattnerad653482010-01-22 22:09:00 +00001840 // FIXME: don't use sys::path for this! This should not depend on the
1841 // host.
Bill Wendlingf5839192009-05-20 23:19:06 +00001842 sys::Path FullPath(getSourceDirectoryName(Id.first));
1843 bool AppendOk =
1844 FullPath.appendComponent(getSourceFileName(Id.second));
1845 assert(AppendOk && "Could not append filename to directory!");
1846 AppendOk = false;
Chris Lattner59be4ea2010-01-25 18:58:59 +00001847 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001848 }
1849 }
1850
1851 // Emit initial sections
Devang Patelc50078e2009-11-21 02:48:08 +00001852 emitInitial();
Bill Wendlingf5839192009-05-20 23:19:06 +00001853
1854 if (TimePassesIsEnabled)
1855 DebugTimer->stopTimer();
1856}
1857
Devang Patelc50078e2009-11-21 02:48:08 +00001858/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001859///
Devang Patelc50078e2009-11-21 02:48:08 +00001860void DwarfDebug::endModule() {
Devang Patel95d477e2009-10-06 00:03:14 +00001861 if (!ModuleCU)
Bill Wendlingf5839192009-05-20 23:19:06 +00001862 return;
1863
1864 if (TimePassesIsEnabled)
1865 DebugTimer->startTimer();
1866
Devang Patel90a0fe32009-11-10 23:06:00 +00001867 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1868 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1869 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1870 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001871 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001872 }
1873
Devang Patelc1df8792009-12-03 01:25:38 +00001874 // Insert top level DIEs.
1875 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1876 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1877 ModuleCU->getCUDie()->addChild(*TI);
1878
Devang Patel7d707f92010-01-19 06:19:05 +00001879 for (DenseMap<DIE *, MDNode *>::iterator CI = ContainingTypeMap.begin(),
Devang Patel188c85d2009-12-03 19:11:07 +00001880 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1881 DIE *SPDie = CI->first;
1882 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1883 if (!N) continue;
1884 DIE *NDie = ModuleCU->getDIE(N);
1885 if (!NDie) continue;
1886 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel69aae432010-01-15 00:26:31 +00001887 // FIXME - This is not the correct approach.
1888 // addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Devang Patel188c85d2009-12-03 19:11:07 +00001889 }
1890
Bill Wendlingf5839192009-05-20 23:19:06 +00001891 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001892 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001893 EmitLabel("text_end", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00001894 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001895 EmitLabel("data_end", 0);
1896
1897 // End text sections.
1898 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001899 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf5839192009-05-20 23:19:06 +00001900 EmitLabel("section_end", i);
1901 }
1902
1903 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001904 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001905
1906 // Emit function debug frame information
1907 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1908 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001909 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001910
1911 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001912 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001913
1914 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001915 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001916
1917 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00001918 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00001919
1920 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00001921 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00001922
1923 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00001924 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00001925
Devang Patelec13b4f2009-11-24 01:14:22 +00001926 // Emit info into a debug pubtypes section.
1927 emitDebugPubTypes();
1928
Bill Wendlingf5839192009-05-20 23:19:06 +00001929 // Emit info into a debug str section.
Devang Patelc50078e2009-11-21 02:48:08 +00001930 emitDebugStr();
Bill Wendlingf5839192009-05-20 23:19:06 +00001931
1932 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00001933 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00001934
1935 // Emit info into a debug aranges section.
1936 EmitDebugARanges();
1937
1938 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00001939 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00001940
1941 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00001942 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001943
1944 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00001945 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001946
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +00001947 // Clear debug info in preparation for the next Module.
1948 ModuleCU = NULL;
1949 DeleteContainerSeconds(CompileUnitMap);
1950
Bill Wendlingf5839192009-05-20 23:19:06 +00001951 if (TimePassesIsEnabled)
1952 DebugTimer->stopTimer();
1953}
1954
Devang Patel90a0fe32009-11-10 23:06:00 +00001955/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00001956DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1957 unsigned FrameIdx,
Devang Patel90a0fe32009-11-10 23:06:00 +00001958 DILocation &ScopeLoc) {
1959
1960 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1961 if (AbsDbgVariable)
1962 return AbsDbgVariable;
1963
1964 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1965 if (!Scope)
1966 return NULL;
1967
1968 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patelc50078e2009-11-21 02:48:08 +00001969 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00001970 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1971 return AbsDbgVariable;
1972}
1973
Devang Patelc50078e2009-11-21 02:48:08 +00001974/// collectVariableInfo - Populate DbgScope entries with variables' info.
1975void DwarfDebug::collectVariableInfo() {
Devang Patel40c80212009-10-09 22:42:28 +00001976 if (!MMI) return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001977
Devang Patel84139992009-10-06 01:26:37 +00001978 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1979 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1980 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel0a0ea052010-01-22 22:52:10 +00001981 MDNode *Var = VI->first;
Devang Patel90a0fe32009-11-10 23:06:00 +00001982 if (!Var) continue;
Devang Patel6882dff2009-10-08 18:48:03 +00001983 DIVariable DV (Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00001984 std::pair< unsigned, MDNode *> VP = VI->second;
1985 DILocation ScopeLoc(VP.second);
1986
1987 DbgScope *Scope =
1988 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1989 if (!Scope)
Jim Grosbach652b7432009-11-21 23:12:12 +00001990 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelbad42262009-11-10 23:20:04 +00001991 // If variable scope is not found then skip this variable.
1992 if (!Scope)
1993 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00001994
1995 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patelc50078e2009-11-21 02:48:08 +00001996 Scope->addVariable(RegVar);
Jim Grosbachb23f2422009-11-22 19:20:36 +00001997 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1998 ScopeLoc))
Devang Patel90a0fe32009-11-10 23:06:00 +00001999 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patel84139992009-10-06 01:26:37 +00002000 }
2001}
2002
Devang Patelc50078e2009-11-21 02:48:08 +00002003/// beginScope - Process beginning of a scope starting at Label.
2004void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel393a46d2009-10-06 01:50:42 +00002005 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
2006 if (I == DbgScopeBeginMap.end())
2007 return;
Dan Gohman8d34f972009-11-23 21:30:55 +00002008 ScopeVector &SD = I->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00002009 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00002010 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00002011 (*SDI)->setStartLabelID(Label);
2012}
2013
Devang Patelc50078e2009-11-21 02:48:08 +00002014/// endScope - Process end of a scope.
2015void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel393a46d2009-10-06 01:50:42 +00002016 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patelf4348892009-10-06 03:15:38 +00002017 if (I == DbgScopeEndMap.end())
Devang Patel393a46d2009-10-06 01:50:42 +00002018 return;
Devang Patel90a0fe32009-11-10 23:06:00 +00002019
2020 unsigned Label = MMI->NextLabelID();
2021 Asm->printLabel(Label);
Dan Gohmancfca6e32009-12-05 01:42:34 +00002022 O << '\n';
Devang Patel90a0fe32009-11-10 23:06:00 +00002023
Devang Patel393a46d2009-10-06 01:50:42 +00002024 SmallVector<DbgScope *, 2> &SD = I->second;
2025 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00002026 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00002027 (*SDI)->setEndLabelID(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00002028 return;
2029}
2030
2031/// createDbgScope - Create DbgScope for the scope.
2032void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
2033
2034 if (!InlinedAt) {
2035 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2036 if (WScope)
2037 return;
2038 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2039 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach652b7432009-11-21 23:12:12 +00002040 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel53addbf2009-11-11 00:18:40 +00002041 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel90a0fe32009-11-10 23:06:00 +00002042 return;
2043 }
2044
2045 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2046 if (WScope)
2047 return;
2048
2049 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2050 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2051 DILocation DL(InlinedAt);
2052 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel393a46d2009-10-06 01:50:42 +00002053}
2054
Devang Patelc50078e2009-11-21 02:48:08 +00002055/// extractScopeInformation - Scan machine instructions in this function
Devang Patel6a260102009-10-01 20:31:14 +00002056/// and collect DbgScopes. Return true, if atleast one scope was found.
Chris Lattner69a76972010-01-26 23:18:02 +00002057bool DwarfDebug::extractScopeInformation() {
Devang Patel6a260102009-10-01 20:31:14 +00002058 // If scope information was extracted using .dbg intrinsics then there is not
2059 // any need to extract these information by scanning each instruction.
2060 if (!DbgScopeMap.empty())
2061 return false;
2062
Devang Patel98e77302010-01-04 20:44:00 +00002063 DenseMap<const MachineInstr *, unsigned> MIIndexMap;
2064 unsigned MIIndex = 0;
Devang Patel90a0fe32009-11-10 23:06:00 +00002065 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6a260102009-10-01 20:31:14 +00002066 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2067 I != E; ++I) {
2068 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2069 II != IE; ++II) {
2070 const MachineInstr *MInsn = II;
Devang Patel98e77302010-01-04 20:44:00 +00002071 MIIndexMap[MInsn] = MIIndex++;
Devang Patel6a260102009-10-01 20:31:14 +00002072 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00002073 if (DL.isUnknown()) continue;
Devang Patel042945f2010-01-16 06:09:35 +00002074 DILocation DLT = MF->getDILocation(DL);
2075 DIScope DLTScope = DLT.getScope();
2076 if (DLTScope.isNull()) continue;
Devang Patel6a260102009-10-01 20:31:14 +00002077 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002078 // other scopes, create one DbgScope now. This will be translated
Devang Patel6a260102009-10-01 20:31:14 +00002079 // into a scope DIE at the end.
Devang Patel042945f2010-01-16 06:09:35 +00002080 if (DLTScope.isCompileUnit()) continue;
2081 createDbgScope(DLTScope.getNode(), DLT.getOrigLocation().getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00002082 }
2083 }
2084
2085
2086 // Build scope hierarchy using working set of scopes.
2087 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2088 I != E; ++I) {
2089 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2090 II != IE; ++II) {
2091 const MachineInstr *MInsn = II;
2092 DebugLoc DL = MInsn->getDebugLoc();
2093 if (DL.isUnknown()) continue;
Devang Patel042945f2010-01-16 06:09:35 +00002094 DILocation DLT = MF->getDILocation(DL);
2095 DIScope DLTScope = DLT.getScope();
2096 if (DLTScope.isNull()) continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00002097 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00002098 // other scopes, create one DbgScope now. This will be translated
Devang Patel90a0fe32009-11-10 23:06:00 +00002099 // into a scope DIE at the end.
Devang Patel042945f2010-01-16 06:09:35 +00002100 if (DLTScope.isCompileUnit()) continue;
2101 DbgScope *Scope = getUpdatedDbgScope(DLTScope.getNode(), MInsn,
2102 DLT.getOrigLocation().getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00002103 Scope->setLastInsn(MInsn);
Devang Patel6a260102009-10-01 20:31:14 +00002104 }
2105 }
2106
Devang Patel98e77302010-01-04 20:44:00 +00002107 if (!CurrentFnDbgScope)
2108 return false;
2109
2110 CurrentFnDbgScope->fixInstructionMarkers(MIIndexMap);
Devang Patel6a260102009-10-01 20:31:14 +00002111
2112 // Each scope has first instruction and last instruction to mark beginning
2113 // and end of a scope respectively. Create an inverse map that list scopes
2114 // starts (and ends) with an instruction. One instruction may start (or end)
Devang Patelfd311df2010-01-20 02:05:23 +00002115 // multiple scopes. Ignore scopes that are not reachable.
2116 SmallVector<DbgScope *, 4> WorkList;
2117 WorkList.push_back(CurrentFnDbgScope);
2118 while (!WorkList.empty()) {
2119 DbgScope *S = WorkList.back(); WorkList.pop_back();
2120
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +00002121 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Devang Patelfd311df2010-01-20 02:05:23 +00002122 if (!Children.empty())
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +00002123 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Devang Patelfd311df2010-01-20 02:05:23 +00002124 SE = Children.end(); SI != SE; ++SI)
2125 WorkList.push_back(*SI);
2126
Devang Patel90a0fe32009-11-10 23:06:00 +00002127 if (S->isAbstractScope())
2128 continue;
Devang Patel6a260102009-10-01 20:31:14 +00002129 const MachineInstr *MI = S->getFirstInsn();
2130 assert (MI && "DbgScope does not have first instruction!");
2131
2132 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2133 if (IDI != DbgScopeBeginMap.end())
2134 IDI->second.push_back(S);
2135 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002136 DbgScopeBeginMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002137
2138 MI = S->getLastInsn();
2139 assert (MI && "DbgScope does not have last instruction!");
2140 IDI = DbgScopeEndMap.find(MI);
2141 if (IDI != DbgScopeEndMap.end())
2142 IDI->second.push_back(S);
2143 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002144 DbgScopeEndMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002145 }
2146
2147 return !DbgScopeMap.empty();
2148}
2149
Devang Patelc50078e2009-11-21 02:48:08 +00002150/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002151/// emitted immediately after the function entry point.
Chris Lattner69a76972010-01-26 23:18:02 +00002152void DwarfDebug::beginFunction(const MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002153 this->MF = MF;
2154
2155 if (!ShouldEmitDwarfDebug()) return;
2156
2157 if (TimePassesIsEnabled)
2158 DebugTimer->startTimer();
2159
Chris Lattner69a76972010-01-26 23:18:02 +00002160 if (!extractScopeInformation())
Devang Patel0feae422009-10-06 18:37:31 +00002161 return;
Devang Patelc50078e2009-11-21 02:48:08 +00002162
2163 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002164
Bill Wendlingf5839192009-05-20 23:19:06 +00002165 // Assumes in correct section after the entry point.
2166 EmitLabel("func_begin", ++SubprogramCount);
2167
2168 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2169 // function.
Devang Patel40c80212009-10-09 22:42:28 +00002170 DebugLoc FDL = MF->getDefaultDebugLoc();
2171 if (!FDL.isUnknown()) {
Devang Patel042945f2010-01-16 06:09:35 +00002172 DILocation DLT = MF->getDILocation(FDL);
Devang Patel40c80212009-10-09 22:42:28 +00002173 unsigned LabelID = 0;
Devang Patel042945f2010-01-16 06:09:35 +00002174 DISubprogram SP = getDISubprogram(DLT.getScope().getNode());
Devang Patel40c80212009-10-09 22:42:28 +00002175 if (!SP.isNull())
Devang Patel042945f2010-01-16 06:09:35 +00002176 LabelID = recordSourceLine(SP.getLineNumber(), 0,
2177 DLT.getScope().getNode());
Devang Patel40c80212009-10-09 22:42:28 +00002178 else
Devang Patel042945f2010-01-16 06:09:35 +00002179 LabelID = recordSourceLine(DLT.getLineNumber(),
2180 DLT.getColumnNumber(),
2181 DLT.getScope().getNode());
Devang Patel40c80212009-10-09 22:42:28 +00002182 Asm->printLabel(LabelID);
2183 O << '\n';
Bill Wendlingf5839192009-05-20 23:19:06 +00002184 }
Bill Wendlingf5839192009-05-20 23:19:06 +00002185 if (TimePassesIsEnabled)
2186 DebugTimer->stopTimer();
2187}
2188
Devang Patelc50078e2009-11-21 02:48:08 +00002189/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002190///
Chris Lattner69a76972010-01-26 23:18:02 +00002191void DwarfDebug::endFunction(const MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002192 if (!ShouldEmitDwarfDebug()) return;
2193
2194 if (TimePassesIsEnabled)
2195 DebugTimer->startTimer();
2196
Devang Patel40c80212009-10-09 22:42:28 +00002197 if (DbgScopeMap.empty())
2198 return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002199
Devang Patel98e77302010-01-04 20:44:00 +00002200 if (CurrentFnDbgScope) {
2201 // Define end label for subprogram.
2202 EmitLabel("func_end", SubprogramCount);
2203
2204 // Get function line info.
2205 if (!Lines.empty()) {
2206 // Get section line info.
2207 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2208 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2209 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2210 // Append the function info to section info.
2211 SectionLineInfos.insert(SectionLineInfos.end(),
2212 Lines.begin(), Lines.end());
2213 }
2214
2215 // Construct abstract scopes.
2216 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2217 AE = AbstractScopesList.end(); AI != AE; ++AI)
2218 constructScopeDIE(*AI);
2219
2220 constructScopeDIE(CurrentFnDbgScope);
2221
2222 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2223 MMI->getFrameMoves()));
Bill Wendlingf5839192009-05-20 23:19:06 +00002224 }
2225
Bill Wendlingf5839192009-05-20 23:19:06 +00002226 // Clear debug info
Devang Patel387e9c12010-01-19 01:26:02 +00002227 CurrentFnDbgScope = NULL;
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +00002228 DeleteContainerSeconds(DbgScopeMap);
Devang Patel387e9c12010-01-19 01:26:02 +00002229 DbgScopeBeginMap.clear();
2230 DbgScopeEndMap.clear();
2231 ConcreteScopes.clear();
Jeffrey Yasskin3116cb12010-03-07 06:55:35 +00002232 DeleteContainerSeconds(AbstractScopes);
Devang Patel387e9c12010-01-19 01:26:02 +00002233 AbstractScopesList.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002234 Lines.clear();
Devang Patel67533ab2009-12-01 18:13:48 +00002235
Bill Wendlingf5839192009-05-20 23:19:06 +00002236 if (TimePassesIsEnabled)
2237 DebugTimer->stopTimer();
2238}
2239
Devang Patelc50078e2009-11-21 02:48:08 +00002240/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf5839192009-05-20 23:19:06 +00002241/// label. Returns a unique label ID used to generate a label and provide
2242/// correspondence to the source line list.
Jim Grosbach652b7432009-11-21 23:12:12 +00002243unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel946d0ae2009-10-05 18:03:19 +00002244 MDNode *S) {
Devang Patel15e723d2009-08-28 23:24:31 +00002245 if (!MMI)
2246 return 0;
2247
Bill Wendlingf5839192009-05-20 23:19:06 +00002248 if (TimePassesIsEnabled)
2249 DebugTimer->startTimer();
2250
Devang Patel7f75bbe2009-11-25 17:36:49 +00002251 StringRef Dir;
2252 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002253
2254 DIDescriptor Scope(S);
2255 if (Scope.isCompileUnit()) {
2256 DICompileUnit CU(S);
2257 Dir = CU.getDirectory();
2258 Fn = CU.getFilename();
2259 } else if (Scope.isSubprogram()) {
2260 DISubprogram SP(S);
2261 Dir = SP.getDirectory();
2262 Fn = SP.getFilename();
2263 } else if (Scope.isLexicalBlock()) {
2264 DILexicalBlock DB(S);
2265 Dir = DB.getDirectory();
2266 Fn = DB.getFilename();
2267 } else
2268 assert (0 && "Unexpected scope info");
2269
2270 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf5839192009-05-20 23:19:06 +00002271 unsigned ID = MMI->NextLabelID();
2272 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2273
2274 if (TimePassesIsEnabled)
2275 DebugTimer->stopTimer();
2276
2277 return ID;
2278}
2279
2280/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2281/// timed. Look up the source id with the given directory and source file
2282/// names. If none currently exists, create a new id and insert it in the
2283/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2284/// well.
2285unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2286 const std::string &FileName) {
2287 if (TimePassesIsEnabled)
2288 DebugTimer->startTimer();
2289
Devang Patelaaf012e2009-09-29 18:40:58 +00002290 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf5839192009-05-20 23:19:06 +00002291
2292 if (TimePassesIsEnabled)
2293 DebugTimer->stopTimer();
2294
2295 return SrcId;
2296}
2297
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002298//===----------------------------------------------------------------------===//
2299// Emit Methods
2300//===----------------------------------------------------------------------===//
2301
Devang Patelc50078e2009-11-21 02:48:08 +00002302/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002303///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002304unsigned
2305DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002306 // Get the children.
2307 const std::vector<DIE *> &Children = Die->getChildren();
2308
2309 // If not last sibling and has children then add sibling offset attribute.
Devang Patelc50078e2009-11-21 02:48:08 +00002310 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002311
2312 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002313 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002314
2315 // Get the abbreviation for this DIE.
2316 unsigned AbbrevNumber = Die->getAbbrevNumber();
2317 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2318
2319 // Set DIE offset
2320 Die->setOffset(Offset);
2321
2322 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002323 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002324
2325 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2326 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2327
2328 // Size the DIE attribute values.
2329 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2330 // Size attribute value.
2331 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2332
2333 // Size the DIE children if any.
2334 if (!Children.empty()) {
2335 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2336 "Children flag not set");
2337
2338 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002339 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002340
2341 // End of children marker.
2342 Offset += sizeof(int8_t);
2343 }
2344
2345 Die->setSize(Offset - Die->getOffset());
2346 return Offset;
2347}
2348
Devang Patelc50078e2009-11-21 02:48:08 +00002349/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002350///
Devang Patelc50078e2009-11-21 02:48:08 +00002351void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002352 // Compute size of compile unit header.
2353 static unsigned Offset =
2354 sizeof(int32_t) + // Length of Compilation Unit Info
2355 sizeof(int16_t) + // DWARF version number
2356 sizeof(int32_t) + // Offset Into Abbrev. Section
2357 sizeof(int8_t); // Pointer Size (in bytes)
2358
Devang Patelc50078e2009-11-21 02:48:08 +00002359 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel5a3d37f2009-06-29 20:45:18 +00002360 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling55fccda2009-05-20 23:21:38 +00002361}
2362
Devang Patelc50078e2009-11-21 02:48:08 +00002363/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling55fccda2009-05-20 23:21:38 +00002364/// tools to recognize the object file contains Dwarf information.
Devang Patelc50078e2009-11-21 02:48:08 +00002365void DwarfDebug::emitInitial() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002366 // Check to see if we already emitted intial headers.
2367 if (didInitial) return;
2368 didInitial = true;
2369
Chris Lattner73266f92009-08-19 05:49:37 +00002370 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002371
Bill Wendling55fccda2009-05-20 23:21:38 +00002372 // Dwarf sections base addresses.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002373 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner73266f92009-08-19 05:49:37 +00002374 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002375 EmitLabel("section_debug_frame", 0);
2376 }
2377
Chris Lattner73266f92009-08-19 05:49:37 +00002378 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002379 EmitLabel("section_info", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002380 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002381 EmitLabel("section_abbrev", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002382 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002383 EmitLabel("section_aranges", 0);
2384
Chris Lattner73266f92009-08-19 05:49:37 +00002385 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2386 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling55fccda2009-05-20 23:21:38 +00002387 EmitLabel("section_macinfo", 0);
2388 }
2389
Chris Lattner73266f92009-08-19 05:49:37 +00002390 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002391 EmitLabel("section_line", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002392 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002393 EmitLabel("section_loc", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002394 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002395 EmitLabel("section_pubnames", 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00002396 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2397 EmitLabel("section_pubtypes", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002398 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002399 EmitLabel("section_str", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002400 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002401 EmitLabel("section_ranges", 0);
2402
Chris Lattner73266f92009-08-19 05:49:37 +00002403 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002404 EmitLabel("text_begin", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002405 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002406 EmitLabel("data_begin", 0);
2407}
2408
Devang Patelc50078e2009-11-21 02:48:08 +00002409/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002410///
Devang Patelc50078e2009-11-21 02:48:08 +00002411void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002412 // Get the abbreviation for this DIE.
2413 unsigned AbbrevNumber = Die->getAbbrevNumber();
2414 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2415
Chris Lattnerad653482010-01-22 22:09:00 +00002416 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002417
2418 // Emit the code (index) for the abbreviation.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002419 if (Asm->VerboseAsm)
2420 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2421 Twine::utohexstr(Die->getOffset()) + ":0x" +
2422 Twine::utohexstr(Die->getSize()) + " " +
2423 dwarf::TagString(Abbrev->getTag()));
2424 EmitULEB128(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002425
2426 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2427 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2428
2429 // Emit the DIE attribute values.
2430 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2431 unsigned Attr = AbbrevData[i].getAttribute();
2432 unsigned Form = AbbrevData[i].getForm();
2433 assert(Form && "Too many attributes for DIE (check abbreviation)");
2434
Chris Lattner91e06b42010-01-24 18:54:17 +00002435 if (Asm->VerboseAsm)
2436 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2437
Bill Wendling55fccda2009-05-20 23:21:38 +00002438 switch (Attr) {
2439 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002440 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002441 break;
2442 case dwarf::DW_AT_abstract_origin: {
2443 DIEEntry *E = cast<DIEEntry>(Values[i]);
2444 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002445 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002446 Asm->EmitInt32(Addr);
2447 break;
2448 }
2449 default:
2450 // Emit an attribute using the defined form.
2451 Values[i]->EmitValue(this, Form);
Chris Lattner2d2f1002010-01-24 19:01:06 +00002452 O << "\n"; // REMOVE This once all EmitValue impls emit their own newline.
Bill Wendling55fccda2009-05-20 23:21:38 +00002453 break;
2454 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002455 }
2456
2457 // Emit the DIE children if any.
2458 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2459 const std::vector<DIE *> &Children = Die->getChildren();
2460
2461 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002462 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002463
Chris Lattner05ae1d22010-01-22 23:47:11 +00002464 Asm->EmitInt8(0); EOL("End Of Children Mark");
Bill Wendling55fccda2009-05-20 23:21:38 +00002465 }
2466}
2467
Devang Patelfe0be132009-12-09 18:24:21 +00002468/// emitDebugInfo - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002469///
Devang Patelfe0be132009-12-09 18:24:21 +00002470void DwarfDebug::emitDebugInfo() {
2471 // Start debug info section.
2472 Asm->OutStreamer.SwitchSection(
2473 Asm->getObjFileLowering().getDwarfInfoSection());
2474 DIE *Die = ModuleCU->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002475
2476 // Emit the compile units header.
Devang Patelfe0be132009-12-09 18:24:21 +00002477 EmitLabel("info_begin", ModuleCU->getID());
Bill Wendling55fccda2009-05-20 23:21:38 +00002478
2479 // Emit size of content not including length itself
2480 unsigned ContentSize = Die->getSize() +
2481 sizeof(int16_t) + // DWARF version number
2482 sizeof(int32_t) + // Offset Into Abbrev. Section
2483 sizeof(int8_t) + // Pointer Size (in bytes)
2484 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2485
Chris Lattner05ae1d22010-01-22 23:47:11 +00002486 Asm->EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2487 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF version number");
Bill Wendling55fccda2009-05-20 23:21:38 +00002488 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002489 EOL("Offset Into Abbrev. Section");
2490 Asm->EmitInt8(TD->getPointerSize()); EOL("Address Size (in bytes)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002491
Devang Patelc50078e2009-11-21 02:48:08 +00002492 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002493 // FIXME - extra padding for gdb bug.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002494 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2495 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2496 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
2497 Asm->EmitInt8(0); EOL("Extra Pad For GDB");
Devang Patelfe0be132009-12-09 18:24:21 +00002498 EmitLabel("info_end", ModuleCU->getID());
Chris Lattnerad653482010-01-22 22:09:00 +00002499 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002500}
2501
Devang Patelc50078e2009-11-21 02:48:08 +00002502/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002503///
Devang Patelc50078e2009-11-21 02:48:08 +00002504void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002505 // Check to see if it is worth the effort.
2506 if (!Abbreviations.empty()) {
2507 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002508 Asm->OutStreamer.SwitchSection(
2509 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002510
2511 EmitLabel("abbrev_begin", 0);
2512
2513 // For each abbrevation.
2514 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2515 // Get abbreviation data
2516 const DIEAbbrev *Abbrev = Abbreviations[i];
2517
2518 // Emit the abbrevations code (base 1 index.)
Chris Lattnerbcc79432010-01-22 23:18:42 +00002519 EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling55fccda2009-05-20 23:21:38 +00002520
2521 // Emit the abbreviations data.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002522 Abbrev->Emit(this);
Chris Lattnerad653482010-01-22 22:09:00 +00002523 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002524 }
2525
2526 // Mark end of abbreviations.
Chris Lattnerbcc79432010-01-22 23:18:42 +00002527 EmitULEB128(0, "EOM(3)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002528
2529 EmitLabel("abbrev_end", 0);
Chris Lattnerad653482010-01-22 22:09:00 +00002530 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002531 }
2532}
2533
Devang Patelc50078e2009-11-21 02:48:08 +00002534/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002535/// the line matrix.
2536///
Devang Patelc50078e2009-11-21 02:48:08 +00002537void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002538 // Define last address of section.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002539 Asm->EmitInt8(0); EOL("Extended Op");
2540 Asm->EmitInt8(TD->getPointerSize() + 1); EOL("Op size");
2541 Asm->EmitInt8(dwarf::DW_LNE_set_address); EOL("DW_LNE_set_address");
2542 EmitReference("section_end", SectionEnd); EOL("Section end label");
Bill Wendling55fccda2009-05-20 23:21:38 +00002543
2544 // Mark end of matrix.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002545 Asm->EmitInt8(0); EOL("DW_LNE_end_sequence");
Chris Lattnerad653482010-01-22 22:09:00 +00002546 Asm->EmitInt8(1);
Chris Lattnerbcc79432010-01-22 23:18:42 +00002547 Asm->EmitInt8(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002548}
2549
Devang Patelc50078e2009-11-21 02:48:08 +00002550/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002551///
Devang Patelc50078e2009-11-21 02:48:08 +00002552void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002553 // If the target is using .loc/.file, the assembler will be emitting the
2554 // .debug_line table automatically.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002555 if (MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002556 return;
2557
2558 // Minimum line delta, thus ranging from -10..(255-10).
2559 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2560 // Maximum line delta, thus ranging from -10..(255-10).
2561 const int MaxLineDelta = 255 + MinLineDelta;
2562
2563 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002564 Asm->OutStreamer.SwitchSection(
2565 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002566
2567 // Construct the section header.
2568 EmitDifference("line_end", 0, "line_begin", 0, true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002569 EOL("Length of Source Line Info");
Bill Wendling55fccda2009-05-20 23:21:38 +00002570 EmitLabel("line_begin", 0);
2571
Chris Lattner05ae1d22010-01-22 23:47:11 +00002572 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF version number");
Bill Wendling55fccda2009-05-20 23:21:38 +00002573
2574 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002575 EOL("Prolog Length");
Bill Wendling55fccda2009-05-20 23:21:38 +00002576 EmitLabel("line_prolog_begin", 0);
2577
Chris Lattner05ae1d22010-01-22 23:47:11 +00002578 Asm->EmitInt8(1); EOL("Minimum Instruction Length");
2579 Asm->EmitInt8(1); EOL("Default is_stmt_start flag");
2580 Asm->EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2581 Asm->EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2582 Asm->EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
Bill Wendling55fccda2009-05-20 23:21:38 +00002583
2584 // Line number standard opcode encodings argument count
Chris Lattner05ae1d22010-01-22 23:47:11 +00002585 Asm->EmitInt8(0); EOL("DW_LNS_copy arg count");
2586 Asm->EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2587 Asm->EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2588 Asm->EmitInt8(1); EOL("DW_LNS_set_file arg count");
2589 Asm->EmitInt8(1); EOL("DW_LNS_set_column arg count");
2590 Asm->EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2591 Asm->EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2592 Asm->EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2593 Asm->EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
Bill Wendling55fccda2009-05-20 23:21:38 +00002594
2595 // Emit directories.
2596 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
Chris Lattnercc564642010-01-23 03:11:46 +00002597 const std::string &Dir = getSourceDirectoryName(DI);
2598 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Directory");
2599 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002600 }
2601
Chris Lattner05ae1d22010-01-22 23:47:11 +00002602 Asm->EmitInt8(0); EOL("End of directories");
Bill Wendling55fccda2009-05-20 23:21:38 +00002603
2604 // Emit files.
2605 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2606 // Remember source id starts at 1.
2607 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
Chris Lattnercc564642010-01-23 03:11:46 +00002608 const std::string &FN = getSourceFileName(Id.second);
2609 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("Source");
2610 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2611
Chris Lattnerbcc79432010-01-22 23:18:42 +00002612 EmitULEB128(Id.first, "Directory #");
2613 EmitULEB128(0, "Mod date");
2614 EmitULEB128(0, "File size");
Bill Wendling55fccda2009-05-20 23:21:38 +00002615 }
2616
Chris Lattner05ae1d22010-01-22 23:47:11 +00002617 Asm->EmitInt8(0); EOL("End of files");
Bill Wendling55fccda2009-05-20 23:21:38 +00002618
2619 EmitLabel("line_prolog_end", 0);
2620
2621 // A sequence for each text section.
2622 unsigned SecSrcLinesSize = SectionSourceLines.size();
2623
2624 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2625 // Isolate current sections line info.
2626 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2627
Chris Lattner26aabb92009-08-08 23:39:42 +00002628 /*if (Asm->isVerbose()) {
Chris Lattnere6ad12f2009-07-31 18:48:30 +00002629 const MCSection *S = SectionMap[j + 1];
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002630 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling55fccda2009-05-20 23:21:38 +00002631 << S->getName() << '\n';
Chris Lattner26aabb92009-08-08 23:39:42 +00002632 }*/
Chris Lattnerad653482010-01-22 22:09:00 +00002633 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002634
2635 // Dwarf assumes we start with first line of first source file.
2636 unsigned Source = 1;
2637 unsigned Line = 1;
2638
2639 // Construct rows of the address, source, line, column matrix.
2640 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2641 const SrcLineInfo &LineInfo = LineInfos[i];
2642 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2643 if (!LabelID) continue;
2644
Caroline Tice9da96d82009-09-11 18:25:54 +00002645 if (LineInfo.getLine() == 0) continue;
2646
Bill Wendling55fccda2009-05-20 23:21:38 +00002647 if (!Asm->isVerbose())
Chris Lattnerad653482010-01-22 22:09:00 +00002648 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002649 else {
2650 std::pair<unsigned, unsigned> SourceID =
2651 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002652 O << '\t' << MAI->getCommentString() << ' '
Dan Gohman1792bc62009-12-05 02:00:34 +00002653 << getSourceDirectoryName(SourceID.first) << '/'
Bill Wendling55fccda2009-05-20 23:21:38 +00002654 << getSourceFileName(SourceID.second)
Dan Gohman1792bc62009-12-05 02:00:34 +00002655 << ':' << utostr_32(LineInfo.getLine()) << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002656 }
2657
2658 // Define the line address.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002659 Asm->EmitInt8(0); EOL("Extended Op");
2660 Asm->EmitInt8(TD->getPointerSize() + 1); EOL("Op size");
2661 Asm->EmitInt8(dwarf::DW_LNE_set_address); EOL("DW_LNE_set_address");
2662 EmitReference("label", LabelID); EOL("Location label");
Bill Wendling55fccda2009-05-20 23:21:38 +00002663
2664 // If change of source, then switch to the new source.
2665 if (Source != LineInfo.getSourceID()) {
2666 Source = LineInfo.getSourceID();
Chris Lattner05ae1d22010-01-22 23:47:11 +00002667 Asm->EmitInt8(dwarf::DW_LNS_set_file); EOL("DW_LNS_set_file");
Chris Lattnerbcc79432010-01-22 23:18:42 +00002668 EmitULEB128(Source, "New Source");
Bill Wendling55fccda2009-05-20 23:21:38 +00002669 }
2670
2671 // If change of line.
2672 if (Line != LineInfo.getLine()) {
2673 // Determine offset.
2674 int Offset = LineInfo.getLine() - Line;
2675 int Delta = Offset - MinLineDelta;
2676
2677 // Update line.
2678 Line = LineInfo.getLine();
2679
2680 // If delta is small enough and in range...
2681 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2682 // ... then use fast opcode.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002683 Asm->EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
Bill Wendling55fccda2009-05-20 23:21:38 +00002684 } else {
2685 // ... otherwise use long hand.
2686 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002687 EOL("DW_LNS_advance_line");
Chris Lattner20f334f2010-01-22 22:56:55 +00002688 EmitSLEB128(Offset, "Line Offset");
Chris Lattner05ae1d22010-01-22 23:47:11 +00002689 Asm->EmitInt8(dwarf::DW_LNS_copy); EOL("DW_LNS_copy");
Bill Wendling55fccda2009-05-20 23:21:38 +00002690 }
2691 } else {
2692 // Copy the previous row (different address or source)
Chris Lattner05ae1d22010-01-22 23:47:11 +00002693 Asm->EmitInt8(dwarf::DW_LNS_copy); EOL("DW_LNS_copy");
Bill Wendling55fccda2009-05-20 23:21:38 +00002694 }
2695 }
2696
Devang Patelc50078e2009-11-21 02:48:08 +00002697 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002698 }
2699
2700 if (SecSrcLinesSize == 0)
2701 // Because we're emitting a debug_line section, we still need a line
2702 // table. The linker and friends expect it to exist. If there's nothing to
2703 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00002704 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002705
2706 EmitLabel("line_end", 0);
Chris Lattnerad653482010-01-22 22:09:00 +00002707 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002708}
2709
Devang Patelc50078e2009-11-21 02:48:08 +00002710/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002711///
Devang Patelc50078e2009-11-21 02:48:08 +00002712void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002713 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002714 return;
2715
2716 int stackGrowth =
2717 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2718 TargetFrameInfo::StackGrowsUp ?
2719 TD->getPointerSize() : -TD->getPointerSize();
2720
2721 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002722 Asm->OutStreamer.SwitchSection(
2723 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002724
2725 EmitLabel("debug_frame_common", 0);
2726 EmitDifference("debug_frame_common_end", 0,
2727 "debug_frame_common_begin", 0, true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002728 EOL("Length of Common Information Entry");
Bill Wendling55fccda2009-05-20 23:21:38 +00002729
2730 EmitLabel("debug_frame_common_begin", 0);
2731 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002732 EOL("CIE Identifier Tag");
Bill Wendling55fccda2009-05-20 23:21:38 +00002733 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002734 EOL("CIE Version");
Chris Lattnercc564642010-01-23 03:11:46 +00002735 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Chris Lattner05ae1d22010-01-22 23:47:11 +00002736 EOL("CIE Augmentation");
Chris Lattnerbcc79432010-01-22 23:18:42 +00002737 EmitULEB128(1, "CIE Code Alignment Factor");
Chris Lattner20f334f2010-01-22 22:56:55 +00002738 EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
Bill Wendling55fccda2009-05-20 23:21:38 +00002739 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Chris Lattner05ae1d22010-01-22 23:47:11 +00002740 EOL("CIE RA Column");
Bill Wendling55fccda2009-05-20 23:21:38 +00002741
2742 std::vector<MachineMove> Moves;
2743 RI->getInitialFrameState(Moves);
2744
2745 EmitFrameMoves(NULL, 0, Moves, false);
2746
2747 Asm->EmitAlignment(2, 0, 0, false);
2748 EmitLabel("debug_frame_common_end", 0);
Chris Lattnerad653482010-01-22 22:09:00 +00002749 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002750}
2751
Devang Patelc50078e2009-11-21 02:48:08 +00002752/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00002753/// section.
2754void
Devang Patelc50078e2009-11-21 02:48:08 +00002755DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002756 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002757 return;
2758
2759 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002760 Asm->OutStreamer.SwitchSection(
2761 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002762
2763 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2764 "debug_frame_begin", DebugFrameInfo.Number, true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002765 EOL("Length of Frame Information Entry");
Bill Wendling55fccda2009-05-20 23:21:38 +00002766
2767 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2768
2769 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2770 0, 0, true, false);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002771 EOL("FDE CIE offset");
Bill Wendling55fccda2009-05-20 23:21:38 +00002772
2773 EmitReference("func_begin", DebugFrameInfo.Number);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002774 EOL("FDE initial location");
Bill Wendling55fccda2009-05-20 23:21:38 +00002775 EmitDifference("func_end", DebugFrameInfo.Number,
2776 "func_begin", DebugFrameInfo.Number);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002777 EOL("FDE address range");
Bill Wendling55fccda2009-05-20 23:21:38 +00002778
2779 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2780 false);
2781
2782 Asm->EmitAlignment(2, 0, 0, false);
2783 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
Chris Lattnerad653482010-01-22 22:09:00 +00002784 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002785}
2786
Devang Patelfe0be132009-12-09 18:24:21 +00002787/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2788///
2789void DwarfDebug::emitDebugPubNames() {
2790 // Start the dwarf pubnames section.
2791 Asm->OutStreamer.SwitchSection(
2792 Asm->getObjFileLowering().getDwarfPubNamesSection());
2793
2794 EmitDifference("pubnames_end", ModuleCU->getID(),
2795 "pubnames_begin", ModuleCU->getID(), true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002796 EOL("Length of Public Names Info");
Bill Wendling55fccda2009-05-20 23:21:38 +00002797
Devang Patelfe0be132009-12-09 18:24:21 +00002798 EmitLabel("pubnames_begin", ModuleCU->getID());
Bill Wendling55fccda2009-05-20 23:21:38 +00002799
Chris Lattner05ae1d22010-01-22 23:47:11 +00002800 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("DWARF Version");
Bill Wendling55fccda2009-05-20 23:21:38 +00002801
2802 EmitSectionOffset("info_begin", "section_info",
Devang Patelfe0be132009-12-09 18:24:21 +00002803 ModuleCU->getID(), 0, true, false);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002804 EOL("Offset of Compilation Unit Info");
Bill Wendling55fccda2009-05-20 23:21:38 +00002805
Devang Patelfe0be132009-12-09 18:24:21 +00002806 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
Bill Wendling55fccda2009-05-20 23:21:38 +00002807 true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002808 EOL("Compilation Unit Length");
Bill Wendling55fccda2009-05-20 23:21:38 +00002809
Devang Patelfe0be132009-12-09 18:24:21 +00002810 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling55fccda2009-05-20 23:21:38 +00002811 for (StringMap<DIE*>::const_iterator
2812 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2813 const char *Name = GI->getKeyData();
2814 DIE * Entity = GI->second;
2815
Chris Lattner05ae1d22010-01-22 23:47:11 +00002816 Asm->EmitInt32(Entity->getOffset()); EOL("DIE offset");
Chris Lattnercc564642010-01-23 03:11:46 +00002817
2818 if (Asm->VerboseAsm)
2819 Asm->OutStreamer.AddComment("External Name");
2820 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002821 }
2822
Chris Lattner05ae1d22010-01-22 23:47:11 +00002823 Asm->EmitInt32(0); EOL("End Mark");
Devang Patelfe0be132009-12-09 18:24:21 +00002824 EmitLabel("pubnames_end", ModuleCU->getID());
Chris Lattnerad653482010-01-22 22:09:00 +00002825 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002826}
2827
Devang Patelec13b4f2009-11-24 01:14:22 +00002828void DwarfDebug::emitDebugPubTypes() {
Devang Patel6f2bdd52009-11-24 19:18:41 +00002829 // Start the dwarf pubnames section.
2830 Asm->OutStreamer.SwitchSection(
2831 Asm->getObjFileLowering().getDwarfPubTypesSection());
Devang Patelec13b4f2009-11-24 01:14:22 +00002832 EmitDifference("pubtypes_end", ModuleCU->getID(),
2833 "pubtypes_begin", ModuleCU->getID(), true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002834 EOL("Length of Public Types Info");
Devang Patelec13b4f2009-11-24 01:14:22 +00002835
2836 EmitLabel("pubtypes_begin", ModuleCU->getID());
2837
Devang Patel40b6cda2010-02-02 03:47:27 +00002838 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DWARF Version");
2839 Asm->EmitInt16(dwarf::DWARF_VERSION);
Devang Patelec13b4f2009-11-24 01:14:22 +00002840
2841 EmitSectionOffset("info_begin", "section_info",
2842 ModuleCU->getID(), 0, true, false);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002843 EOL("Offset of Compilation ModuleCU Info");
Devang Patelec13b4f2009-11-24 01:14:22 +00002844
2845 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2846 true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002847 EOL("Compilation ModuleCU Length");
Devang Patelec13b4f2009-11-24 01:14:22 +00002848
2849 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2850 for (StringMap<DIE*>::const_iterator
2851 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2852 const char *Name = GI->getKeyData();
2853 DIE * Entity = GI->second;
2854
Devang Patel40b6cda2010-02-02 03:47:27 +00002855 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("DIE offset");
2856 Asm->EmitInt32(Entity->getOffset());
Chris Lattnercc564642010-01-23 03:11:46 +00002857
2858 if (Asm->VerboseAsm) Asm->OutStreamer.AddComment("External Name");
Devang Patel0ceb4892010-02-02 03:37:03 +00002859 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00002860 }
2861
Chris Lattner05ae1d22010-01-22 23:47:11 +00002862 Asm->EmitInt32(0); EOL("End Mark");
Devang Patelec13b4f2009-11-24 01:14:22 +00002863 EmitLabel("pubtypes_end", ModuleCU->getID());
Chris Lattnerad653482010-01-22 22:09:00 +00002864 Asm->O << '\n';
Devang Patelec13b4f2009-11-24 01:14:22 +00002865}
2866
Devang Patelc50078e2009-11-21 02:48:08 +00002867/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002868///
Devang Patelc50078e2009-11-21 02:48:08 +00002869void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002870 // Check to see if it is worth the effort.
2871 if (!StringPool.empty()) {
2872 // Start the dwarf str section.
Chris Lattner73266f92009-08-19 05:49:37 +00002873 Asm->OutStreamer.SwitchSection(
2874 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002875
2876 // For each of strings in the string pool.
2877 for (unsigned StringID = 1, N = StringPool.size();
2878 StringID <= N; ++StringID) {
2879 // Emit a label for reference from debug information entries.
2880 EmitLabel("string", StringID);
2881
2882 // Emit the string itself.
2883 const std::string &String = StringPool[StringID];
Chris Lattnercc564642010-01-23 03:11:46 +00002884 Asm->OutStreamer.EmitBytes(StringRef(String.c_str(), String.size()+1), 0);
Bill Wendling55fccda2009-05-20 23:21:38 +00002885 }
2886
Chris Lattnerad653482010-01-22 22:09:00 +00002887 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002888 }
2889}
2890
Devang Patelc50078e2009-11-21 02:48:08 +00002891/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002892///
Devang Patelc50078e2009-11-21 02:48:08 +00002893void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002894 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00002895 Asm->OutStreamer.SwitchSection(
2896 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002897}
2898
2899/// EmitDebugARanges - Emit visible names into a debug aranges section.
2900///
2901void DwarfDebug::EmitDebugARanges() {
2902 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002903 Asm->OutStreamer.SwitchSection(
2904 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002905
2906 // FIXME - Mock up
2907#if 0
2908 CompileUnit *Unit = GetBaseCompileUnit();
2909
2910 // Don't include size of length
Chris Lattner05ae1d22010-01-22 23:47:11 +00002911 Asm->EmitInt32(0x1c); EOL("Length of Address Ranges Info");
Bill Wendling55fccda2009-05-20 23:21:38 +00002912
Chris Lattner05ae1d22010-01-22 23:47:11 +00002913 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("Dwarf Version");
Bill Wendling55fccda2009-05-20 23:21:38 +00002914
2915 EmitReference("info_begin", Unit->getID());
Chris Lattner05ae1d22010-01-22 23:47:11 +00002916 EOL("Offset of Compilation Unit Info");
Bill Wendling55fccda2009-05-20 23:21:38 +00002917
Chris Lattner05ae1d22010-01-22 23:47:11 +00002918 Asm->EmitInt8(TD->getPointerSize()); EOL("Size of Address");
Bill Wendling55fccda2009-05-20 23:21:38 +00002919
Chris Lattner05ae1d22010-01-22 23:47:11 +00002920 Asm->EmitInt8(0); EOL("Size of Segment Descriptor");
Bill Wendling55fccda2009-05-20 23:21:38 +00002921
Chris Lattner05ae1d22010-01-22 23:47:11 +00002922 Asm->EmitInt16(0); EOL("Pad (1)");
2923 Asm->EmitInt16(0); EOL("Pad (2)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002924
2925 // Range 1
Chris Lattner05ae1d22010-01-22 23:47:11 +00002926 EmitReference("text_begin", 0); EOL("Address");
2927 EmitDifference("text_end", 0, "text_begin", 0, true); EOL("Length");
Bill Wendling55fccda2009-05-20 23:21:38 +00002928
Chris Lattner05ae1d22010-01-22 23:47:11 +00002929 Asm->EmitInt32(0); EOL("EOM (1)");
2930 Asm->EmitInt32(0); EOL("EOM (2)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002931#endif
Bill Wendling55fccda2009-05-20 23:21:38 +00002932}
2933
Devang Patelc50078e2009-11-21 02:48:08 +00002934/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002935///
Devang Patelc50078e2009-11-21 02:48:08 +00002936void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002937 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002938 Asm->OutStreamer.SwitchSection(
2939 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002940}
2941
Devang Patelc50078e2009-11-21 02:48:08 +00002942/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002943///
Devang Patelc50078e2009-11-21 02:48:08 +00002944void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00002945 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00002946 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002947 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00002948 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002949 }
2950}
2951
Devang Patelc50078e2009-11-21 02:48:08 +00002952/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00002953/// Section Header:
2954/// 1. length of section
2955/// 2. Dwarf version number
2956/// 3. address size.
2957///
2958/// Entries (one "entry" for each function that was inlined):
2959///
2960/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2961/// otherwise offset into __debug_str for regular function name.
2962/// 2. offset into __debug_str section for regular function name.
2963/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2964/// instances for the function.
2965///
2966/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2967/// inlined instance; the die_offset points to the inlined_subroutine die in the
2968/// __debug_info section, and the low_pc is the starting address for the
2969/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00002970void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002971 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002972 return;
2973
Devang Patel5a3d37f2009-06-29 20:45:18 +00002974 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00002975 return;
2976
Chris Lattner73266f92009-08-19 05:49:37 +00002977 Asm->OutStreamer.SwitchSection(
2978 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Chris Lattnerad653482010-01-22 22:09:00 +00002979
Bill Wendling55fccda2009-05-20 23:21:38 +00002980 EmitDifference("debug_inlined_end", 1,
2981 "debug_inlined_begin", 1, true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00002982 EOL("Length of Debug Inlined Information Entry");
Bill Wendling55fccda2009-05-20 23:21:38 +00002983
2984 EmitLabel("debug_inlined_begin", 1);
2985
Chris Lattner05ae1d22010-01-22 23:47:11 +00002986 Asm->EmitInt16(dwarf::DWARF_VERSION); EOL("Dwarf Version");
2987 Asm->EmitInt8(TD->getPointerSize()); EOL("Address Size (in bytes)");
Bill Wendling55fccda2009-05-20 23:21:38 +00002988
Devang Patel90a0fe32009-11-10 23:06:00 +00002989 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2990 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00002991
Devang Patel90a0fe32009-11-10 23:06:00 +00002992 MDNode *Node = *I;
Devang Patel7d707f92010-01-19 06:19:05 +00002993 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
Jim Grosbachb23f2422009-11-22 19:20:36 +00002994 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00002995 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00002996 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00002997 StringRef LName = SP.getLinkageName();
2998 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00002999
Chris Lattnercc564642010-01-23 03:11:46 +00003000 if (LName.empty()) {
3001 Asm->OutStreamer.EmitBytes(Name, 0);
3002 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3003 } else
Devang Patel90a0fe32009-11-10 23:06:00 +00003004 EmitSectionOffset("string", "section_str",
Chris Lattnercc564642010-01-23 03:11:46 +00003005 StringPool.idFor(getRealLinkageName(LName)), false, true);
Devang Patel90a0fe32009-11-10 23:06:00 +00003006
Chris Lattner05ae1d22010-01-22 23:47:11 +00003007 EOL("MIPS linkage name");
Devang Patel90a0fe32009-11-10 23:06:00 +00003008 EmitSectionOffset("string", "section_str",
3009 StringPool.idFor(Name), false, true);
Chris Lattner05ae1d22010-01-22 23:47:11 +00003010 EOL("Function name");
Chris Lattnerbcc79432010-01-22 23:18:42 +00003011 EmitULEB128(Labels.size(), "Inline count");
Bill Wendling55fccda2009-05-20 23:21:38 +00003012
Devang Patel90a0fe32009-11-10 23:06:00 +00003013 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00003014 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00003015 DIE *SP = LI->second;
Chris Lattner05ae1d22010-01-22 23:47:11 +00003016 Asm->EmitInt32(SP->getOffset()); EOL("DIE offset");
Bill Wendling55fccda2009-05-20 23:21:38 +00003017
3018 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattnera5ef4d32009-08-22 21:43:10 +00003019 O << MAI->getData32bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00003020 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +00003021 O << MAI->getData64bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00003022
Chris Lattner05ae1d22010-01-22 23:47:11 +00003023 PrintLabelName("label", LI->first); EOL("low_pc");
Bill Wendling55fccda2009-05-20 23:21:38 +00003024 }
3025 }
3026
3027 EmitLabel("debug_inlined_end", 1);
Chris Lattnerad653482010-01-22 22:09:00 +00003028 Asm->O << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00003029}