blob: 8a3ceb631d03be2fab8896177e945d2fa2f23664 [file] [log] [blame]
Bill Wendling0310d762009-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 Patele4b27562009-08-28 23:24:31 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Bill Wendling0310d762009-05-15 09:23:25 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner23132b12009-08-24 03:52:50 +000025#include "llvm/ADT/StringExtras.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
Chris Lattner334fd1f2009-09-16 00:08:41 +000028#include "llvm/Support/Mangler.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000029#include "llvm/Support/Timer.h"
30#include "llvm/System/Path.h"
Bill Wendling0310d762009-05-15 09:23:25 +000031using namespace llvm;
32
Bill Wendling0310d762009-05-15 09:23:25 +000033//===----------------------------------------------------------------------===//
34
35/// Configuration values for initial hash set sizes (log2).
36///
Bill Wendling0310d762009-05-15 09:23:25 +000037static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +000038
39namespace llvm {
40
41//===----------------------------------------------------------------------===//
42/// CompileUnit - This dwarf writer support class manages information associate
43/// with a source file.
Nick Lewycky5f9843f2009-11-17 08:11:44 +000044class CompileUnit {
Bill Wendling0310d762009-05-15 09:23:25 +000045 /// ID - File identifier for source.
46 ///
47 unsigned ID;
48
49 /// Die - Compile unit debug information entry.
50 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +000051 DIE *CUDie;
Bill Wendling0310d762009-05-15 09:23:25 +000052
Devang Patel6f01d9c2009-11-21 00:31:03 +000053 /// IndexTyDie - An anonymous type for index type.
54 DIE *IndexTyDie;
55
Bill Wendling0310d762009-05-15 09:23:25 +000056 /// GVToDieMap - Tracks the mapping of unit level debug informaton
57 /// variables to debug information entries.
Devang Patele4b27562009-08-28 23:24:31 +000058 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patel017d1212009-11-20 21:37:22 +000059 ValueMap<MDNode *, DIE *> GVToDieMap;
Bill Wendling0310d762009-05-15 09:23:25 +000060
61 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
62 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patele4b27562009-08-28 23:24:31 +000063 /// FIXME : Rename
Devang Patel017d1212009-11-20 21:37:22 +000064 ValueMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling0310d762009-05-15 09:23:25 +000065
66 /// Globals - A map of globally visible named entities for this unit.
67 ///
68 StringMap<DIE*> Globals;
69
Devang Patel193f7202009-11-24 01:14:22 +000070 /// GlobalTypes - A map of globally visible types for this unit.
71 ///
72 StringMap<DIE*> GlobalTypes;
73
Bill Wendling0310d762009-05-15 09:23:25 +000074public:
75 CompileUnit(unsigned I, DIE *D)
Devang Patel2c4ceb12009-11-21 02:48:08 +000076 : ID(I), CUDie(D), IndexTyDie(0) {}
77 ~CompileUnit() { delete CUDie; delete IndexTyDie; }
Bill Wendling0310d762009-05-15 09:23:25 +000078
79 // Accessors.
Devang Patel193f7202009-11-24 01:14:22 +000080 unsigned getID() const { return ID; }
81 DIE* getCUDie() const { return CUDie; }
82 const StringMap<DIE*> &getGlobals() const { return Globals; }
83 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendling0310d762009-05-15 09:23:25 +000084
85 /// hasContent - Return true if this compile unit has something to write out.
86 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +000087 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling0310d762009-05-15 09:23:25 +000088
Devang Patel2c4ceb12009-11-21 02:48:08 +000089 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +000090 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +000091 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling0310d762009-05-15 09:23:25 +000092
Devang Patel193f7202009-11-24 01:14:22 +000093 /// addGlobalType - Add a new global type to the compile unit.
94 ///
95 void addGlobalType(const std::string &Name, DIE *Die) {
96 GlobalTypes[Name] = Die;
97 }
98
Devang Patel017d1212009-11-20 21:37:22 +000099 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling0310d762009-05-15 09:23:25 +0000100 /// specified debug variable.
Devang Patel017d1212009-11-20 21:37:22 +0000101 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000102
Devang Patel017d1212009-11-20 21:37:22 +0000103 /// insertDIE - Insert DIE into the map.
104 void insertDIE(MDNode *N, DIE *D) {
105 GVToDieMap.insert(std::make_pair(N, D));
106 }
Bill Wendling0310d762009-05-15 09:23:25 +0000107
Devang Patel017d1212009-11-20 21:37:22 +0000108 /// getDIEEntry - Returns the debug information entry for the speciefied
109 /// debug variable.
Devang Patel6404e4e2009-12-15 19:16:48 +0000110 DIEEntry *getDIEEntry(MDNode *N) {
111 ValueMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
112 if (I == GVToDIEEntryMap.end())
113 return NULL;
114 return I->second;
115 }
Devang Patel017d1212009-11-20 21:37:22 +0000116
117 /// insertDIEEntry - Insert debug information entry into the map.
118 void insertDIEEntry(MDNode *N, DIEEntry *E) {
119 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendling0310d762009-05-15 09:23:25 +0000120 }
121
Devang Patel2c4ceb12009-11-21 02:48:08 +0000122 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling0310d762009-05-15 09:23:25 +0000123 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000124 void addDie(DIE *Buffer) {
125 this->CUDie->addChild(Buffer);
Bill Wendling0310d762009-05-15 09:23:25 +0000126 }
Devang Patel6f01d9c2009-11-21 00:31:03 +0000127
128 // getIndexTyDie - Get an anonymous type for index type.
129 DIE *getIndexTyDie() {
130 return IndexTyDie;
131 }
132
Jim Grosbach7ab38df2009-11-22 19:20:36 +0000133 // setIndexTyDie - Set D as anonymous type for index which can be reused
134 // later.
Devang Patel6f01d9c2009-11-21 00:31:03 +0000135 void setIndexTyDie(DIE *D) {
136 IndexTyDie = D;
137 }
138
Bill Wendling0310d762009-05-15 09:23:25 +0000139};
140
141//===----------------------------------------------------------------------===//
142/// DbgVariable - This class is used to track local variable information.
143///
Devang Patelf76a3d62009-11-16 21:53:40 +0000144class DbgVariable {
Bill Wendling0310d762009-05-15 09:23:25 +0000145 DIVariable Var; // Variable Descriptor.
146 unsigned FrameIndex; // Variable frame index.
Devang Patel53bb5c92009-11-10 23:06:00 +0000147 DbgVariable *AbstractVar; // Abstract variable for this variable.
148 DIE *TheDIE;
Bill Wendling0310d762009-05-15 09:23:25 +0000149public:
Devang Patel53bb5c92009-11-10 23:06:00 +0000150 DbgVariable(DIVariable V, unsigned I)
151 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000152
153 // Accessors.
Devang Patel53bb5c92009-11-10 23:06:00 +0000154 DIVariable getVariable() const { return Var; }
155 unsigned getFrameIndex() const { return FrameIndex; }
156 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
157 DbgVariable *getAbstractVariable() const { return AbstractVar; }
158 void setDIE(DIE *D) { TheDIE = D; }
159 DIE *getDIE() const { return TheDIE; }
Bill Wendling0310d762009-05-15 09:23:25 +0000160};
161
162//===----------------------------------------------------------------------===//
163/// DbgScope - This class is used to track scope information.
164///
Devang Patelf76a3d62009-11-16 21:53:40 +0000165class DbgScope {
Bill Wendling0310d762009-05-15 09:23:25 +0000166 DbgScope *Parent; // Parent to this scope.
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000167 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel53bb5c92009-11-10 23:06:00 +0000168 WeakVH InlinedAtLocation; // Location at which scope is inlined.
169 bool AbstractScope; // Abstract Scope
Bill Wendling0310d762009-05-15 09:23:25 +0000170 unsigned StartLabelID; // Label ID of the beginning of scope.
171 unsigned EndLabelID; // Label ID of the end of scope.
Devang Pateld38dd112009-10-01 18:25:23 +0000172 const MachineInstr *LastInsn; // Last instruction of this scope.
173 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000174 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
175 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000176
Owen Anderson04c05f72009-06-24 22:53:20 +0000177 // Private state for dump()
178 mutable unsigned IndentLevel;
Bill Wendling0310d762009-05-15 09:23:25 +0000179public:
Devang Patelc90aefe2009-10-14 21:08:09 +0000180 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel53bb5c92009-11-10 23:06:00 +0000181 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000182 StartLabelID(0), EndLabelID(0),
Devang Patelc90aefe2009-10-14 21:08:09 +0000183 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendling0310d762009-05-15 09:23:25 +0000184 virtual ~DbgScope();
185
186 // Accessors.
187 DbgScope *getParent() const { return Parent; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000188 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling0310d762009-05-15 09:23:25 +0000189 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000190 MDNode *getInlinedAt() const {
Devang Patel53bb5c92009-11-10 23:06:00 +0000191 return dyn_cast_or_null<MDNode>(InlinedAtLocation);
Devang Patelc90aefe2009-10-14 21:08:09 +0000192 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000193 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendling0310d762009-05-15 09:23:25 +0000194 unsigned getStartLabelID() const { return StartLabelID; }
195 unsigned getEndLabelID() const { return EndLabelID; }
196 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
197 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendling0310d762009-05-15 09:23:25 +0000198 void setStartLabelID(unsigned S) { StartLabelID = S; }
199 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Pateld38dd112009-10-01 18:25:23 +0000200 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
201 const MachineInstr *getLastInsn() { return LastInsn; }
202 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000203 void setAbstractScope() { AbstractScope = true; }
204 bool isAbstractScope() const { return AbstractScope; }
Devang Pateld38dd112009-10-01 18:25:23 +0000205 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel53bb5c92009-11-10 23:06:00 +0000206
Devang Patel2c4ceb12009-11-21 02:48:08 +0000207 /// addScope - Add a scope to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000208 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000209 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling0310d762009-05-15 09:23:25 +0000210
Devang Patel2c4ceb12009-11-21 02:48:08 +0000211 /// addVariable - Add a variable to the scope.
Bill Wendling0310d762009-05-15 09:23:25 +0000212 ///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000213 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling0310d762009-05-15 09:23:25 +0000214
Devang Patel2c4ceb12009-11-21 02:48:08 +0000215 void fixInstructionMarkers() {
Devang Patelaf9e8472009-10-01 20:31:14 +0000216 assert (getFirstInsn() && "First instruction is missing!");
217 if (getLastInsn())
218 return;
Jim Grosbach31ef40e2009-11-21 23:12:12 +0000219
Devang Patelaf9e8472009-10-01 20:31:14 +0000220 // If a scope does not have an instruction to mark an end then use
221 // the end of last child scope.
222 SmallVector<DbgScope *, 4> &Scopes = getScopes();
223 assert (!Scopes.empty() && "Inner most scope does not have last insn!");
224 DbgScope *L = Scopes.back();
225 if (!L->getLastInsn())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000226 L->fixInstructionMarkers();
Devang Patelaf9e8472009-10-01 20:31:14 +0000227 setLastInsn(L->getLastInsn());
228 }
229
Bill Wendling0310d762009-05-15 09:23:25 +0000230#ifndef NDEBUG
231 void dump() const;
232#endif
233};
234
235#ifndef NDEBUG
236void DbgScope::dump() const {
David Greenef83adbc2009-12-24 00:31:35 +0000237 raw_ostream &err = dbgs();
Chris Lattnerc281de12009-08-23 00:51:00 +0000238 err.indent(IndentLevel);
Devang Patel53bb5c92009-11-10 23:06:00 +0000239 MDNode *N = Desc.getNode();
240 N->dump();
Chris Lattnerc281de12009-08-23 00:51:00 +0000241 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel53bb5c92009-11-10 23:06:00 +0000242 if (AbstractScope)
243 err << "Abstract Scope\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000244
245 IndentLevel += 2;
Devang Patel53bb5c92009-11-10 23:06:00 +0000246 if (!Scopes.empty())
247 err << "Children ...\n";
Bill Wendling0310d762009-05-15 09:23:25 +0000248 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
249 if (Scopes[i] != this)
250 Scopes[i]->dump();
251
252 IndentLevel -= 2;
253}
254#endif
255
Bill Wendling0310d762009-05-15 09:23:25 +0000256DbgScope::~DbgScope() {
257 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
258 delete Scopes[i];
259 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
260 delete Variables[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000261}
262
263} // end llvm namespace
264
Chris Lattneraf76e592009-08-22 20:48:53 +0000265DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel1dbc7712009-06-29 20:45:18 +0000266 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendling0310d762009-05-15 09:23:25 +0000267 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patel2c4ceb12009-11-21 02:48:08 +0000268 DIEValues(), StringPool(),
Bill Wendling0310d762009-05-15 09:23:25 +0000269 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel53bb5c92009-11-10 23:06:00 +0000270 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendling0310d762009-05-15 09:23:25 +0000271 if (TimePassesIsEnabled)
Chris Lattner0b86a6f2009-12-28 07:41:18 +0000272 DebugTimer = new Timer("Dwarf Debug Writer");
Bill Wendling0310d762009-05-15 09:23:25 +0000273}
274DwarfDebug::~DwarfDebug() {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000275 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
276 delete DIEValues[j];
Bill Wendling0310d762009-05-15 09:23:25 +0000277
Bill Wendling0310d762009-05-15 09:23:25 +0000278 delete DebugTimer;
279}
280
Devang Patel2c4ceb12009-11-21 02:48:08 +0000281/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling0310d762009-05-15 09:23:25 +0000282///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000283void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000284 // Profile the node so that we can make it unique.
285 FoldingSetNodeID ID;
286 Abbrev.Profile(ID);
287
288 // Check the set for priors.
289 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
290
291 // If it's newly added.
292 if (InSet == &Abbrev) {
293 // Add to abbreviation list.
294 Abbreviations.push_back(&Abbrev);
295
296 // Assign the vector position + 1 as its number.
297 Abbrev.setNumber(Abbreviations.size());
298 } else {
299 // Assign existing abbreviation number.
300 Abbrev.setNumber(InSet->getNumber());
301 }
302}
303
Devang Patel2c4ceb12009-11-21 02:48:08 +0000304/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling995f80a2009-05-20 23:24:48 +0000305/// information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000306DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000307 DIEEntry *Value = new DIEEntry(Entry);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000308 DIEValues.push_back(Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000309 return Value;
310}
311
Devang Patel2c4ceb12009-11-21 02:48:08 +0000312/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000313///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000314void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000315 unsigned Form, uint64_t Integer) {
316 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000317 DIEValue *Value = new DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000318 DIEValues.push_back(Value);
319 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000320}
321
Devang Patel2c4ceb12009-11-21 02:48:08 +0000322/// addSInt - Add an signed integer attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000323///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000324void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000325 unsigned Form, int64_t Integer) {
326 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel6f01d9c2009-11-21 00:31:03 +0000327 DIEValue *Value = new DIEInteger(Integer);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000328 DIEValues.push_back(Value);
329 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000330}
331
Devang Patel69f57b12009-12-02 15:25:16 +0000332/// addString - Add a string attribute data and value. DIEString only
333/// keeps string reference.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000334void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patele9a05972009-11-24 19:42:17 +0000335 const StringRef String) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000336 DIEValue *Value = new DIEString(String);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000337 DIEValues.push_back(Value);
338 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000339}
340
Devang Patel2c4ceb12009-11-21 02:48:08 +0000341/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000342///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000343void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000344 const DWLabel &Label) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000345 DIEValue *Value = new DIEDwarfLabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000346 DIEValues.push_back(Value);
347 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000348}
349
Devang Patel2c4ceb12009-11-21 02:48:08 +0000350/// addObjectLabel - Add an non-Dwarf label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000351///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000352void DwarfDebug::addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000353 const std::string &Label) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000354 DIEValue *Value = new DIEObjectLabel(Label);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000355 DIEValues.push_back(Value);
356 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000357}
358
Devang Patel2c4ceb12009-11-21 02:48:08 +0000359/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000360///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000361void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000362 const DWLabel &Label, const DWLabel &Section,
363 bool isEH, bool useSet) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000364 DIEValue *Value = new DIESectionOffset(Label, Section, isEH, useSet);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000365 DIEValues.push_back(Value);
366 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000367}
368
Devang Patel2c4ceb12009-11-21 02:48:08 +0000369/// addDelta - Add a label delta attribute data and value.
Bill Wendling0310d762009-05-15 09:23:25 +0000370///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000371void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000372 const DWLabel &Hi, const DWLabel &Lo) {
Devang Patel6f01d9c2009-11-21 00:31:03 +0000373 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000374 DIEValues.push_back(Value);
375 Die->addValue(Attribute, Form, Value);
Bill Wendling0310d762009-05-15 09:23:25 +0000376}
377
Devang Patel2c4ceb12009-11-21 02:48:08 +0000378/// addBlock - Add block data.
Bill Wendling0310d762009-05-15 09:23:25 +0000379///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000380void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling0310d762009-05-15 09:23:25 +0000381 DIEBlock *Block) {
382 Block->ComputeSize(TD);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000383 DIEValues.push_back(Block);
384 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000385}
386
Devang Patel2c4ceb12009-11-21 02:48:08 +0000387/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000388/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000389void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendling0310d762009-05-15 09:23:25 +0000390 // If there is no compile unit specified, don't add a line #.
391 if (V->getCompileUnit().isNull())
392 return;
393
394 unsigned Line = V->getLineNumber();
Devang Pateld037d7a2009-12-11 21:37:07 +0000395 unsigned FileID = findCompileUnit(V->getCompileUnit())->getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000396 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000397 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
398 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000399}
400
Devang Patel2c4ceb12009-11-21 02:48:08 +0000401/// addSourceLine - Add location information to specified debug information
Bill Wendling0310d762009-05-15 09:23:25 +0000402/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000403void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendling0310d762009-05-15 09:23:25 +0000404 // If there is no compile unit specified, don't add a line #.
405 if (G->getCompileUnit().isNull())
406 return;
407
408 unsigned Line = G->getLineNumber();
Devang Pateld037d7a2009-12-11 21:37:07 +0000409 unsigned FileID = findCompileUnit(G->getCompileUnit())->getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000410 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000411 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
412 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000413}
Devang Patel82dfc0c2009-08-31 22:47:13 +0000414
Devang Patel2c4ceb12009-11-21 02:48:08 +0000415/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000416/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000417void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel82dfc0c2009-08-31 22:47:13 +0000418 // If there is no compile unit specified, don't add a line #.
419 if (SP->getCompileUnit().isNull())
420 return;
Caroline Ticec6f9d622009-09-11 18:25:54 +0000421 // If the line number is 0, don't add it.
422 if (SP->getLineNumber() == 0)
423 return;
424
Devang Patel82dfc0c2009-08-31 22:47:13 +0000425
426 unsigned Line = SP->getLineNumber();
Devang Pateld037d7a2009-12-11 21:37:07 +0000427 unsigned FileID = findCompileUnit(SP->getCompileUnit())->getID();
Devang Patel82dfc0c2009-08-31 22:47:13 +0000428 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000429 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
430 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel82dfc0c2009-08-31 22:47:13 +0000431}
432
Devang Patel2c4ceb12009-11-21 02:48:08 +0000433/// addSourceLine - Add location information to specified debug information
Devang Patel82dfc0c2009-08-31 22:47:13 +0000434/// entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000435void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendling0310d762009-05-15 09:23:25 +0000436 // If there is no compile unit specified, don't add a line #.
437 DICompileUnit CU = Ty->getCompileUnit();
438 if (CU.isNull())
439 return;
440
441 unsigned Line = Ty->getLineNumber();
Devang Pateld037d7a2009-12-11 21:37:07 +0000442 unsigned FileID = findCompileUnit(CU)->getID();
Bill Wendling0310d762009-05-15 09:23:25 +0000443 assert(FileID && "Invalid file id");
Devang Patel2c4ceb12009-11-21 02:48:08 +0000444 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
445 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendling0310d762009-05-15 09:23:25 +0000446}
447
Devang Patel6404e4e2009-12-15 19:16:48 +0000448/// addSourceLine - Add location information to specified debug information
449/// entry.
450void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
451 // If there is no compile unit specified, don't add a line #.
452 if (NS->getCompileUnit().isNull())
453 return;
454
455 unsigned Line = NS->getLineNumber();
456 StringRef FN = NS->getFilename();
457 StringRef Dir = NS->getDirectory();
458
459 unsigned FileID = GetOrCreateSourceID(Dir, FN);
460 assert(FileID && "Invalid file id");
461 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
462 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
463}
464
Caroline Ticedc8f6042009-08-31 21:19:37 +0000465/* Byref variables, in Blocks, are declared by the programmer as
466 "SomeType VarName;", but the compiler creates a
467 __Block_byref_x_VarName struct, and gives the variable VarName
468 either the struct, or a pointer to the struct, as its type. This
469 is necessary for various behind-the-scenes things the compiler
470 needs to do with by-reference variables in blocks.
471
472 However, as far as the original *programmer* is concerned, the
473 variable should still have type 'SomeType', as originally declared.
474
475 The following function dives into the __Block_byref_x_VarName
476 struct to find the original type of the variable. This will be
477 passed back to the code generating the type for the Debug
478 Information Entry for the variable 'VarName'. 'VarName' will then
479 have the original type 'SomeType' in its debug information.
480
481 The original type 'SomeType' will be the type of the field named
482 'VarName' inside the __Block_byref_x_VarName struct.
483
484 NOTE: In order for this to not completely fail on the debugger
485 side, the Debug Information Entry for the variable VarName needs to
486 have a DW_AT_location that tells the debugger how to unwind through
487 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patel2c4ceb12009-11-21 02:48:08 +0000488 value of the variable. The function addBlockByrefType does this. */
Caroline Ticedc8f6042009-08-31 21:19:37 +0000489
490/// Find the type the programmer originally declared the variable to be
491/// and return that type.
492///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000493DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000494
495 DIType subType = Ty;
496 unsigned tag = Ty.getTag();
497
498 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000499 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000500 subType = DTy.getTypeDerivedFrom();
501 }
502
503 DICompositeType blockStruct = DICompositeType(subType.getNode());
504
505 DIArray Elements = blockStruct.getTypeArray();
506
507 if (Elements.isNull())
508 return Ty;
509
510 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
511 DIDescriptor Element = Elements.getElement(i);
512 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000513 if (Name == DT.getName())
Caroline Ticedc8f6042009-08-31 21:19:37 +0000514 return (DT.getTypeDerivedFrom());
515 }
516
517 return Ty;
518}
519
Devang Patel2c4ceb12009-11-21 02:48:08 +0000520/// addComplexAddress - Start with the address based on the location provided,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000521/// and generate the DWARF information necessary to find the actual variable
522/// given the extra address information encoded in the DIVariable, starting from
523/// the starting location. Add the DWARF information to the die.
524///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000525void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000526 unsigned Attribute,
527 const MachineLocation &Location) {
528 const DIVariable &VD = DV->getVariable();
529 DIType Ty = VD.getType();
530
531 // Decode the original location, and use that as the start of the byref
532 // variable's location.
533 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
534 DIEBlock *Block = new DIEBlock();
535
536 if (Location.isReg()) {
537 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000538 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000539 } else {
540 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000541 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
542 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000543 }
544 } else {
545 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000546 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000547 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000548 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
549 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000550 }
551
Devang Patel2c4ceb12009-11-21 02:48:08 +0000552 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000553 }
554
555 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
556 uint64_t Element = VD.getAddrElement(i);
557
558 if (Element == DIFactory::OpPlus) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000559 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
560 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000561 } else if (Element == DIFactory::OpDeref) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000562 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000563 } else llvm_unreachable("unknown DIFactory Opcode");
564 }
565
566 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000567 addBlock(Die, Attribute, 0, Block);
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000568}
569
Caroline Ticedc8f6042009-08-31 21:19:37 +0000570/* Byref variables, in Blocks, are declared by the programmer as "SomeType
571 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
572 gives the variable VarName either the struct, or a pointer to the struct, as
573 its type. This is necessary for various behind-the-scenes things the
574 compiler needs to do with by-reference variables in Blocks.
575
576 However, as far as the original *programmer* is concerned, the variable
577 should still have type 'SomeType', as originally declared.
578
Devang Patel2c4ceb12009-11-21 02:48:08 +0000579 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticedc8f6042009-08-31 21:19:37 +0000580 struct to find the original type of the variable, which is then assigned to
581 the variable's Debug Information Entry as its real type. So far, so good.
582 However now the debugger will expect the variable VarName to have the type
583 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000584 expression that explains to the debugger how to navigate through the
Caroline Ticedc8f6042009-08-31 21:19:37 +0000585 pointers and struct to find the actual variable of type SomeType.
586
587 The following function does just that. We start by getting
588 the "normal" location for the variable. This will be the location
589 of either the struct __Block_byref_x_VarName or the pointer to the
590 struct __Block_byref_x_VarName.
591
592 The struct will look something like:
593
594 struct __Block_byref_x_VarName {
595 ... <various fields>
596 struct __Block_byref_x_VarName *forwarding;
597 ... <various other fields>
598 SomeType VarName;
599 ... <maybe more fields>
600 };
601
602 If we are given the struct directly (as our starting point) we
603 need to tell the debugger to:
604
605 1). Add the offset of the forwarding field.
606
607 2). Follow that pointer to get the the real __Block_byref_x_VarName
608 struct to use (the real one may have been copied onto the heap).
609
610 3). Add the offset for the field VarName, to find the actual variable.
611
612 If we started with a pointer to the struct, then we need to
613 dereference that pointer first, before the other steps.
614 Translating this into DWARF ops, we will need to append the following
615 to the current location description for the variable:
616
617 DW_OP_deref -- optional, if we start with a pointer
618 DW_OP_plus_uconst <forward_fld_offset>
619 DW_OP_deref
620 DW_OP_plus_uconst <varName_fld_offset>
621
622 That is what this function does. */
623
Devang Patel2c4ceb12009-11-21 02:48:08 +0000624/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticedc8f6042009-08-31 21:19:37 +0000625/// provided, and generate the DWARF information necessary to find the
626/// actual Block variable (navigating the Block struct) based on the
627/// starting location. Add the DWARF information to the die. For
628/// more information, read large comment just above here.
629///
Devang Patel2c4ceb12009-11-21 02:48:08 +0000630void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar00564992009-09-19 20:40:14 +0000631 unsigned Attribute,
632 const MachineLocation &Location) {
Caroline Ticedc8f6042009-08-31 21:19:37 +0000633 const DIVariable &VD = DV->getVariable();
634 DIType Ty = VD.getType();
635 DIType TmpTy = Ty;
636 unsigned Tag = Ty.getTag();
637 bool isPointer = false;
638
Devang Patel65dbc902009-11-25 17:36:49 +0000639 StringRef varName = VD.getName();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000640
641 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000642 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticedc8f6042009-08-31 21:19:37 +0000643 TmpTy = DTy.getTypeDerivedFrom();
644 isPointer = true;
645 }
646
647 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
648
Daniel Dunbar00564992009-09-19 20:40:14 +0000649 // Find the __forwarding field and the variable field in the __Block_byref
650 // struct.
Daniel Dunbar00564992009-09-19 20:40:14 +0000651 DIArray Fields = blockStruct.getTypeArray();
652 DIDescriptor varField = DIDescriptor();
653 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000654
655
Daniel Dunbar00564992009-09-19 20:40:14 +0000656 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
657 DIDescriptor Element = Fields.getElement(i);
658 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel65dbc902009-11-25 17:36:49 +0000659 StringRef fieldName = DT.getName();
660 if (fieldName == "__forwarding")
Daniel Dunbar00564992009-09-19 20:40:14 +0000661 forwardingField = Element;
Devang Patel65dbc902009-11-25 17:36:49 +0000662 else if (fieldName == varName)
Daniel Dunbar00564992009-09-19 20:40:14 +0000663 varField = Element;
664 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000665
Mike Stump7e3720d2009-09-24 23:21:26 +0000666 assert(!varField.isNull() && "Can't find byref variable in Block struct");
667 assert(!forwardingField.isNull()
668 && "Can't find forwarding field in Block struct");
Caroline Ticedc8f6042009-08-31 21:19:37 +0000669
Daniel Dunbar00564992009-09-19 20:40:14 +0000670 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar00564992009-09-19 20:40:14 +0000671 unsigned int forwardingFieldOffset =
672 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
673 unsigned int varFieldOffset =
674 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Ticedc8f6042009-08-31 21:19:37 +0000675
Mike Stump7e3720d2009-09-24 23:21:26 +0000676 // Decode the original location, and use that as the start of the byref
677 // variable's location.
Daniel Dunbar00564992009-09-19 20:40:14 +0000678 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
679 DIEBlock *Block = new DIEBlock();
Caroline Ticedc8f6042009-08-31 21:19:37 +0000680
Daniel Dunbar00564992009-09-19 20:40:14 +0000681 if (Location.isReg()) {
682 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000683 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000684 else {
685 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000686 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
687 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000688 }
689 } else {
690 if (Reg < 32)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000691 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000692 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000693 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
694 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar00564992009-09-19 20:40:14 +0000695 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000696
Devang Patel2c4ceb12009-11-21 02:48:08 +0000697 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar00564992009-09-19 20:40:14 +0000698 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000699
Mike Stump7e3720d2009-09-24 23:21:26 +0000700 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar00564992009-09-19 20:40:14 +0000701 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar00564992009-09-19 20:40:14 +0000702 if (isPointer)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000703 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000704
Daniel Dunbar00564992009-09-19 20:40:14 +0000705 // Next add the offset for the '__forwarding' field:
706 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
707 // adding the offset if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000708 if (forwardingFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000709 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
710 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000711 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000712
Daniel Dunbar00564992009-09-19 20:40:14 +0000713 // Now dereference the __forwarding field to get to the real __Block_byref
714 // struct: DW_OP_deref.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000715 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000716
Daniel Dunbar00564992009-09-19 20:40:14 +0000717 // Now that we've got the real __Block_byref... struct, add the offset
718 // for the variable's field to get to the location of the actual variable:
719 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar00564992009-09-19 20:40:14 +0000720 if (varFieldOffset > 0) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000721 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
722 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar00564992009-09-19 20:40:14 +0000723 }
Caroline Ticedc8f6042009-08-31 21:19:37 +0000724
Daniel Dunbar00564992009-09-19 20:40:14 +0000725 // Now attach the location information to the DIE.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000726 addBlock(Die, Attribute, 0, Block);
Caroline Ticedc8f6042009-08-31 21:19:37 +0000727}
728
Devang Patel2c4ceb12009-11-21 02:48:08 +0000729/// addAddress - Add an address attribute to a die based on the location
Bill Wendling0310d762009-05-15 09:23:25 +0000730/// provided.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000731void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling0310d762009-05-15 09:23:25 +0000732 const MachineLocation &Location) {
733 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
734 DIEBlock *Block = new DIEBlock();
735
736 if (Location.isReg()) {
737 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000738 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000739 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000740 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
741 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000742 }
743 } else {
744 if (Reg < 32) {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000745 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000746 } else {
Devang Patel2c4ceb12009-11-21 02:48:08 +0000747 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
748 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendling0310d762009-05-15 09:23:25 +0000749 }
750
Devang Patel2c4ceb12009-11-21 02:48:08 +0000751 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling0310d762009-05-15 09:23:25 +0000752 }
753
Devang Patel2c4ceb12009-11-21 02:48:08 +0000754 addBlock(Die, Attribute, 0, Block);
Bill Wendling0310d762009-05-15 09:23:25 +0000755}
756
Devang Patelc366f832009-12-10 19:14:49 +0000757/// addToContextOwner - Add Die into the list of its context owner's children.
758void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
759 if (Context.isNull())
760 ModuleCU->addDie(Die);
761 else if (Context.isType()) {
762 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
763 ContextDIE->addChild(Die);
Devang Patel6404e4e2009-12-15 19:16:48 +0000764 } else if (Context.isNameSpace()) {
765 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode()));
766 ContextDIE->addChild(Die);
Devang Patelc366f832009-12-10 19:14:49 +0000767 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
768 ContextDIE->addChild(Die);
769 else
770 ModuleCU->addDie(Die);
771}
772
Devang Patel16ced732009-12-10 18:05:33 +0000773/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
774/// given DIType.
775DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
776 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
777 if (TyDIE)
778 return TyDIE;
779
780 // Create new type.
781 TyDIE = new DIE(dwarf::DW_TAG_base_type);
782 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
783 if (Ty.isBasicType())
784 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
785 else if (Ty.isCompositeType())
786 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
787 else {
788 assert(Ty.isDerivedType() && "Unknown kind of DIType");
789 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
790 }
791
Devang Patelc366f832009-12-10 19:14:49 +0000792 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel16ced732009-12-10 18:05:33 +0000793 return TyDIE;
794}
795
Devang Patel2c4ceb12009-11-21 02:48:08 +0000796/// addType - Add a new type attribute to the specified entity.
Devang Patel8a241142009-12-09 18:24:21 +0000797void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Bill Wendling0310d762009-05-15 09:23:25 +0000798 if (Ty.isNull())
799 return;
800
801 // Check for pre-existence.
Devang Patel8a241142009-12-09 18:24:21 +0000802 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendling0310d762009-05-15 09:23:25 +0000803 // If it exists then use the existing value.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000804 if (Entry) {
805 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000806 return;
807 }
808
809 // Set up proxy.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000810 Entry = createDIEEntry();
Devang Patel8a241142009-12-09 18:24:21 +0000811 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000812
813 // Construct type.
Devang Patel16ced732009-12-10 18:05:33 +0000814 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendling0310d762009-05-15 09:23:25 +0000815
Devang Patel2c4ceb12009-11-21 02:48:08 +0000816 Entry->setEntry(Buffer);
817 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling0310d762009-05-15 09:23:25 +0000818}
819
Devang Patel2c4ceb12009-11-21 02:48:08 +0000820/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patel8a241142009-12-09 18:24:21 +0000821void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000822 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000823 StringRef Name = BTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000824 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000825 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling0310d762009-05-15 09:23:25 +0000826 BTy.getEncoding());
827
828 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000829 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000830 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000831 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel2c4ceb12009-11-21 02:48:08 +0000832 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000833}
834
Devang Patel2c4ceb12009-11-21 02:48:08 +0000835/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patel8a241142009-12-09 18:24:21 +0000836void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000837 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000838 StringRef Name = DTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000839 uint64_t Size = DTy.getSizeInBits() >> 3;
840 unsigned Tag = DTy.getTag();
841
842 // FIXME - Workaround for templates.
843 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
844
845 Buffer.setTag(Tag);
846
847 // Map to main type, void will not have a type.
848 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel8a241142009-12-09 18:24:21 +0000849 addType(&Buffer, FromTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000850
851 // Add name if not anonymous or intermediate type.
Devang Pateldeea5642009-11-30 23:56:56 +0000852 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000853 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000854
855 // Add size if non-zero (derived types might be zero-sized.)
856 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000857 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000858
859 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel05f6fa82009-11-23 18:43:37 +0000860 if (!DTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000861 addSourceLine(&Buffer, &DTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000862}
863
Devang Patel2c4ceb12009-11-21 02:48:08 +0000864/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +0000865void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendling0310d762009-05-15 09:23:25 +0000866 // Get core information.
Devang Patel65dbc902009-11-25 17:36:49 +0000867 StringRef Name = CTy.getName();
Bill Wendling0310d762009-05-15 09:23:25 +0000868
869 uint64_t Size = CTy.getSizeInBits() >> 3;
870 unsigned Tag = CTy.getTag();
871 Buffer.setTag(Tag);
872
873 switch (Tag) {
874 case dwarf::DW_TAG_vector_type:
875 case dwarf::DW_TAG_array_type:
Devang Patel8a241142009-12-09 18:24:21 +0000876 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000877 break;
878 case dwarf::DW_TAG_enumeration_type: {
879 DIArray Elements = CTy.getTypeArray();
880
881 // Add enumerators to enumeration type.
882 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
883 DIE *ElemDie = NULL;
Devang Patele4b27562009-08-28 23:24:31 +0000884 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelc5254722009-10-09 17:51:49 +0000885 if (!Enum.isNull()) {
Devang Patel8a241142009-12-09 18:24:21 +0000886 ElemDie = constructEnumTypeDIE(&Enum);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000887 Buffer.addChild(ElemDie);
Devang Patelc5254722009-10-09 17:51:49 +0000888 }
Bill Wendling0310d762009-05-15 09:23:25 +0000889 }
890 }
891 break;
892 case dwarf::DW_TAG_subroutine_type: {
893 // Add return type.
894 DIArray Elements = CTy.getTypeArray();
895 DIDescriptor RTy = Elements.getElement(0);
Devang Patel8a241142009-12-09 18:24:21 +0000896 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000897
898 // Add prototype flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +0000899 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000900
901 // Add arguments.
902 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
903 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
904 DIDescriptor Ty = Elements.getElement(i);
Devang Patel8a241142009-12-09 18:24:21 +0000905 addType(Arg, DIType(Ty.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000906 Buffer.addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +0000907 }
908 }
909 break;
910 case dwarf::DW_TAG_structure_type:
911 case dwarf::DW_TAG_union_type:
912 case dwarf::DW_TAG_class_type: {
913 // Add elements to structure type.
914 DIArray Elements = CTy.getTypeArray();
915
916 // A forward struct declared type may not have elements available.
917 if (Elements.isNull())
918 break;
919
920 // Add elements to structure type.
921 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
922 DIDescriptor Element = Elements.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000923 if (Element.isNull())
924 continue;
Bill Wendling0310d762009-05-15 09:23:25 +0000925 DIE *ElemDie = NULL;
926 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patelffe966c2009-12-14 16:18:45 +0000927 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Bill Wendling0310d762009-05-15 09:23:25 +0000928 else
Devang Patel8a241142009-12-09 18:24:21 +0000929 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +0000930 Buffer.addChild(ElemDie);
Bill Wendling0310d762009-05-15 09:23:25 +0000931 }
932
Devang Patela1ba2692009-08-27 23:51:51 +0000933 if (CTy.isAppleBlockExtension())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000934 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000935
936 unsigned RLang = CTy.getRunTimeLang();
937 if (RLang)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000938 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling0310d762009-05-15 09:23:25 +0000939 dwarf::DW_FORM_data1, RLang);
940 break;
941 }
942 default:
943 break;
944 }
945
946 // Add name if not anonymous or intermediate type.
Devang Patel65dbc902009-11-25 17:36:49 +0000947 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000948 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +0000949
950 if (Tag == dwarf::DW_TAG_enumeration_type ||
951 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
952 // Add size if non-zero (derived types might be zero-sized.)
953 if (Size)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000954 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling0310d762009-05-15 09:23:25 +0000955 else {
956 // Add zero size if it is not a forward declaration.
957 if (CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000958 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000959 else
Devang Patel2c4ceb12009-11-21 02:48:08 +0000960 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling0310d762009-05-15 09:23:25 +0000961 }
962
963 // Add source line info if available.
964 if (!CTy.isForwardDecl())
Devang Patel2c4ceb12009-11-21 02:48:08 +0000965 addSourceLine(&Buffer, &CTy);
Bill Wendling0310d762009-05-15 09:23:25 +0000966 }
967}
968
Devang Patel2c4ceb12009-11-21 02:48:08 +0000969/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
970void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling0310d762009-05-15 09:23:25 +0000971 int64_t L = SR.getLo();
972 int64_t H = SR.getHi();
973 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
974
Devang Patel2c4ceb12009-11-21 02:48:08 +0000975 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel6325a532009-08-14 20:59:16 +0000976 if (L)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000977 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld55224c2009-12-04 23:10:24 +0000978 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling0310d762009-05-15 09:23:25 +0000979
Devang Patel2c4ceb12009-11-21 02:48:08 +0000980 Buffer.addChild(DW_Subrange);
Bill Wendling0310d762009-05-15 09:23:25 +0000981}
982
Devang Patel2c4ceb12009-11-21 02:48:08 +0000983/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patel8a241142009-12-09 18:24:21 +0000984void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendling0310d762009-05-15 09:23:25 +0000985 DICompositeType *CTy) {
986 Buffer.setTag(dwarf::DW_TAG_array_type);
987 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +0000988 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +0000989
990 // Emit derived type.
Devang Patel8a241142009-12-09 18:24:21 +0000991 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +0000992 DIArray Elements = CTy->getTypeArray();
993
Devang Patel6f01d9c2009-11-21 00:31:03 +0000994 // Get an anonymous type for index type.
Devang Patel8a241142009-12-09 18:24:21 +0000995 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel6f01d9c2009-11-21 00:31:03 +0000996 if (!IdxTy) {
997 // Construct an anonymous type for index type.
998 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patel2c4ceb12009-11-21 02:48:08 +0000999 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1000 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel6f01d9c2009-11-21 00:31:03 +00001001 dwarf::DW_ATE_signed);
Devang Patel8a241142009-12-09 18:24:21 +00001002 ModuleCU->addDie(IdxTy);
1003 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel6f01d9c2009-11-21 00:31:03 +00001004 }
Bill Wendling0310d762009-05-15 09:23:25 +00001005
1006 // Add subranges to array type.
1007 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1008 DIDescriptor Element = Elements.getElement(i);
1009 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001010 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendling0310d762009-05-15 09:23:25 +00001011 }
1012}
1013
Devang Patel2c4ceb12009-11-21 02:48:08 +00001014/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patel8a241142009-12-09 18:24:21 +00001015DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator *ETy) {
Bill Wendling0310d762009-05-15 09:23:25 +00001016 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel65dbc902009-11-25 17:36:49 +00001017 StringRef Name = ETy->getName();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001018 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling0310d762009-05-15 09:23:25 +00001019 int64_t Value = ETy->getEnumValue();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001020 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling0310d762009-05-15 09:23:25 +00001021 return Enumerator;
1022}
1023
Devang Patel2c4ceb12009-11-21 02:48:08 +00001024/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patel8a241142009-12-09 18:24:21 +00001025DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001026 // If the global variable was optmized out then no need to create debug info
1027 // entry.
Devang Patel84c73e92009-11-06 17:58:12 +00001028 if (!GV.getGlobal()) return NULL;
Devang Patel65dbc902009-11-25 17:36:49 +00001029 if (GV.getDisplayName().empty()) return NULL;
Devang Patel465c3be2009-11-06 01:30:04 +00001030
Bill Wendling0310d762009-05-15 09:23:25 +00001031 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001032 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001033 GV.getDisplayName());
1034
Devang Patel65dbc902009-11-25 17:36:49 +00001035 StringRef LinkageName = GV.getLinkageName();
1036 if (!LinkageName.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001037 // Skip special LLVM prefix that is used to inform the asm printer to not
1038 // emit usual symbol prefix before the symbol name. This happens for
1039 // Objective-C symbol names and symbol whose name is replaced using GCC's
1040 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001041 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001042 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001043 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001044 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001045 }
Devang Patel8a241142009-12-09 18:24:21 +00001046 addType(GVDie, GV.getType());
Bill Wendling0310d762009-05-15 09:23:25 +00001047 if (!GV.isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001048 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1049 addSourceLine(GVDie, &GV);
Devang Patelb71a16d2009-10-05 23:22:08 +00001050
Bill Wendling0310d762009-05-15 09:23:25 +00001051 return GVDie;
1052}
1053
Devang Patel2c4ceb12009-11-21 02:48:08 +00001054/// createMemberDIE - Create new member DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001055DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendling0310d762009-05-15 09:23:25 +00001056 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel65dbc902009-11-25 17:36:49 +00001057 StringRef Name = DT.getName();
1058 if (!Name.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001059 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel65dbc902009-11-25 17:36:49 +00001060
Devang Patel8a241142009-12-09 18:24:21 +00001061 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendling0310d762009-05-15 09:23:25 +00001062
Devang Patel2c4ceb12009-11-21 02:48:08 +00001063 addSourceLine(MemberDie, &DT);
Bill Wendling0310d762009-05-15 09:23:25 +00001064
Devang Patel33db5082009-11-04 22:06:12 +00001065 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patel2c4ceb12009-11-21 02:48:08 +00001066 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel33db5082009-11-04 22:06:12 +00001067
Bill Wendling0310d762009-05-15 09:23:25 +00001068 uint64_t Size = DT.getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +00001069 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling0310d762009-05-15 09:23:25 +00001070
1071 if (Size != FieldSize) {
1072 // Handle bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001073 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1074 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling0310d762009-05-15 09:23:25 +00001075
1076 uint64_t Offset = DT.getOffsetInBits();
1077 uint64_t FieldOffset = Offset;
1078 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1079 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1080 FieldOffset = (HiMark - FieldSize);
1081 Offset -= FieldOffset;
1082
1083 // Maybe we need to work from the other end.
1084 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001085 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling0310d762009-05-15 09:23:25 +00001086
Devang Patel33db5082009-11-04 22:06:12 +00001087 // Here WD_AT_data_member_location points to the anonymous
1088 // field that includes this bit field.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001089 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001090
1091 } else
1092 // This is not a bitfield.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001093 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel33db5082009-11-04 22:06:12 +00001094
Devang Patel2c4ceb12009-11-21 02:48:08 +00001095 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001096
1097 if (DT.isProtected())
Devang Patel5d11eb02009-12-03 19:11:07 +00001098 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001099 dwarf::DW_ACCESS_protected);
1100 else if (DT.isPrivate())
Devang Patel5d11eb02009-12-03 19:11:07 +00001101 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling0310d762009-05-15 09:23:25 +00001102 dwarf::DW_ACCESS_private);
Devang Patel5d11eb02009-12-03 19:11:07 +00001103 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1104 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1105 dwarf::DW_ACCESS_public);
1106 if (DT.isVirtual())
1107 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1108 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling0310d762009-05-15 09:23:25 +00001109 return MemberDie;
1110}
1111
Devang Patelffe966c2009-12-14 16:18:45 +00001112/// createSubprogramDIE - Create new DIE using SP.
1113DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1114 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1115 if (SPDie)
1116 return SPDie;
1117
1118 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel65dbc902009-11-25 17:36:49 +00001119 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling0310d762009-05-15 09:23:25 +00001120
Devang Patel65dbc902009-11-25 17:36:49 +00001121 StringRef LinkageName = SP.getLinkageName();
1122 if (!LinkageName.empty()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001123 // Skip special LLVM prefix that is used to inform the asm printer to not
1124 // emit usual symbol prefix before the symbol name. This happens for
1125 // Objective-C symbol names and symbol whose name is replaced using GCC's
1126 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00001127 if (LinkageName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00001128 LinkageName = LinkageName.substr(1);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001129 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patel1a8d2d22009-07-14 00:55:28 +00001130 LinkageName);
Devang Patel53cb17d2009-07-16 01:01:22 +00001131 }
Devang Patel2c4ceb12009-11-21 02:48:08 +00001132 addSourceLine(SPDie, &SP);
Bill Wendling0310d762009-05-15 09:23:25 +00001133
Bill Wendling0310d762009-05-15 09:23:25 +00001134 // Add prototyped tag, if C or ObjC.
1135 unsigned Lang = SP.getCompileUnit().getLanguage();
1136 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1137 Lang == dwarf::DW_LANG_ObjC)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001138 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001139
1140 // Add Return Type.
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001141 DICompositeType SPTy = SP.getType();
1142 DIArray Args = SPTy.getTypeArray();
Bill Wendling0310d762009-05-15 09:23:25 +00001143 unsigned SPTag = SPTy.getTag();
Devang Patel5d11eb02009-12-03 19:11:07 +00001144
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001145 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patel8a241142009-12-09 18:24:21 +00001146 addType(SPDie, SPTy);
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001147 else
Devang Patel8a241142009-12-09 18:24:21 +00001148 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001149
Devang Patel5d11eb02009-12-03 19:11:07 +00001150 unsigned VK = SP.getVirtuality();
1151 if (VK) {
1152 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1153 DIEBlock *Block = new DIEBlock();
1154 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1155 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1156 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1157 ContainingTypeMap.insert(std::make_pair(SPDie, WeakVH(SP.getContainingType().getNode())));
1158 }
1159
Devang Patelffe966c2009-12-14 16:18:45 +00001160 if (MakeDecl || !SP.isDefinition()) {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001161 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling0310d762009-05-15 09:23:25 +00001162
1163 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001164 // be handled while processing variables.
1165 DICompositeType SPTy = SP.getType();
1166 DIArray Args = SPTy.getTypeArray();
1167 unsigned SPTag = SPTy.getTag();
1168
Bill Wendling0310d762009-05-15 09:23:25 +00001169 if (SPTag == dwarf::DW_TAG_subroutine_type)
1170 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1171 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel8a241142009-12-09 18:24:21 +00001172 addType(Arg, DIType(Args.getElement(i).getNode()));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001173 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1174 SPDie->addChild(Arg);
Bill Wendling0310d762009-05-15 09:23:25 +00001175 }
1176 }
1177
Bill Wendling0310d762009-05-15 09:23:25 +00001178 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patel8a241142009-12-09 18:24:21 +00001179 ModuleCU->insertDIE(SP.getNode(), SPDie);
Bill Wendling0310d762009-05-15 09:23:25 +00001180 return SPDie;
1181}
1182
Devang Patel2c4ceb12009-11-21 02:48:08 +00001183/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendling0310d762009-05-15 09:23:25 +00001184///
Devang Pateld037d7a2009-12-11 21:37:07 +00001185CompileUnit *DwarfDebug::findCompileUnit(DICompileUnit Unit) {
Bill Wendling0310d762009-05-15 09:23:25 +00001186 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patele4b27562009-08-28 23:24:31 +00001187 CompileUnitMap.find(Unit.getNode());
Devang Pateld037d7a2009-12-11 21:37:07 +00001188 if (I == CompileUnitMap.end())
1189 return constructCompileUnit(Unit.getNode());
1190 return I->second;
Bill Wendling0310d762009-05-15 09:23:25 +00001191}
1192
Devang Patel53bb5c92009-11-10 23:06:00 +00001193/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1194/// Initialize scope and update scope hierarchy.
1195DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1196 MDNode *InlinedAt) {
1197 assert (N && "Invalid Scope encoding!");
1198 assert (MI && "Missing machine instruction!");
1199 bool GetConcreteScope = (MI && InlinedAt);
1200
1201 DbgScope *NScope = NULL;
1202
1203 if (InlinedAt)
1204 NScope = DbgScopeMap.lookup(InlinedAt);
1205 else
1206 NScope = DbgScopeMap.lookup(N);
1207 assert (NScope && "Unable to find working scope!");
1208
1209 if (NScope->getFirstInsn())
1210 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001211
1212 DbgScope *Parent = NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001213 if (GetConcreteScope) {
Devang Patelc90aefe2009-10-14 21:08:09 +00001214 DILocation IL(InlinedAt);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001215 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel53bb5c92009-11-10 23:06:00 +00001216 IL.getOrigLocation().getNode());
1217 assert (Parent && "Unable to find Parent scope!");
1218 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001219 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001220 } else if (DIDescriptor(N).isLexicalBlock()) {
1221 DILexicalBlock DB(N);
1222 if (!DB.getContext().isNull()) {
1223 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1224 NScope->setParent(Parent);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001225 Parent->addScope(NScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001226 }
Devang Patelc90aefe2009-10-14 21:08:09 +00001227 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001228
Devang Patelbdf45cb2009-10-27 20:47:17 +00001229 NScope->setFirstInsn(MI);
Devang Patelaf9e8472009-10-01 20:31:14 +00001230
Devang Patel53bb5c92009-11-10 23:06:00 +00001231 if (!Parent && !InlinedAt) {
Devang Patel39ae3ff2009-11-11 00:31:36 +00001232 StringRef SPName = DISubprogram(N).getLinkageName();
1233 if (SPName == MF->getFunction()->getName())
1234 CurrentFnDbgScope = NScope;
Devang Patel53bb5c92009-11-10 23:06:00 +00001235 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001236
Devang Patel53bb5c92009-11-10 23:06:00 +00001237 if (GetConcreteScope) {
1238 ConcreteScopes[InlinedAt] = NScope;
1239 getOrCreateAbstractScope(N);
1240 }
1241
Devang Patelbdf45cb2009-10-27 20:47:17 +00001242 return NScope;
Devang Patelaf9e8472009-10-01 20:31:14 +00001243}
1244
Devang Patel53bb5c92009-11-10 23:06:00 +00001245DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1246 assert (N && "Invalid Scope encoding!");
1247
1248 DbgScope *AScope = AbstractScopes.lookup(N);
1249 if (AScope)
1250 return AScope;
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001251
Devang Patel53bb5c92009-11-10 23:06:00 +00001252 DbgScope *Parent = NULL;
1253
1254 DIDescriptor Scope(N);
1255 if (Scope.isLexicalBlock()) {
1256 DILexicalBlock DB(N);
1257 DIDescriptor ParentDesc = DB.getContext();
1258 if (!ParentDesc.isNull())
1259 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1260 }
1261
1262 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1263
1264 if (Parent)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001265 Parent->addScope(AScope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001266 AScope->setAbstractScope();
1267 AbstractScopes[N] = AScope;
1268 if (DIDescriptor(N).isSubprogram())
1269 AbstractScopesList.push_back(AScope);
1270 return AScope;
1271}
Devang Patelaf9e8472009-10-01 20:31:14 +00001272
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001273/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel2c4ceb12009-11-21 02:48:08 +00001274/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1275/// If there are global variables in this scope then create and insert
1276/// DIEs for these variables.
1277DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001278
Devang Patel017d1212009-11-20 21:37:22 +00001279 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel53bb5c92009-11-10 23:06:00 +00001280 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patelffe966c2009-12-14 16:18:45 +00001281 DISubprogram SP(SPNode);
1282 if (SP.isDefinition() && !SP.getContext().isCompileUnit()) {
1283 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1284 // Add arguments.
1285 DICompositeType SPTy = SP.getType();
1286 DIArray Args = SPTy.getTypeArray();
1287 unsigned SPTag = SPTy.getTag();
1288 if (SPTag == dwarf::DW_TAG_subroutine_type)
1289 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1290 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1291 addType(Arg, DIType(Args.getElement(i).getNode()));
1292 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1293 SPDie->addChild(Arg);
1294 }
1295 DIE *SPDeclDie = SPDie;
1296 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1297 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1298 SPDeclDie);
Devang Patelffe966c2009-12-14 16:18:45 +00001299 ModuleCU->addDie(SPDie);
1300 }
1301
Devang Patel2c4ceb12009-11-21 02:48:08 +00001302 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001303 DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001304 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001305 DWLabel("func_end", SubprogramCount));
1306 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001307 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001308
Devang Patel53bb5c92009-11-10 23:06:00 +00001309 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001310 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel53bb5c92009-11-10 23:06:00 +00001311
Devang Patel53bb5c92009-11-10 23:06:00 +00001312 return SPDie;
1313}
1314
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001315/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel2c4ceb12009-11-21 02:48:08 +00001316/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1317DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001318 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1319 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1320
1321 // Ignore empty scopes.
1322 if (StartID == EndID && StartID != 0)
1323 return NULL;
1324
1325 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1326 if (Scope->isAbstractScope())
1327 return ScopeDIE;
1328
Devang Patel2c4ceb12009-11-21 02:48:08 +00001329 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001330 StartID ?
1331 DWLabel("label", StartID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001332 : DWLabel("func_begin", SubprogramCount));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001333 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001334 EndID ?
1335 DWLabel("label", EndID)
Devang Patel53bb5c92009-11-10 23:06:00 +00001336 : DWLabel("func_end", SubprogramCount));
1337
1338
1339
1340 return ScopeDIE;
1341}
1342
Devang Patel2c4ceb12009-11-21 02:48:08 +00001343/// constructInlinedScopeDIE - This scope represents inlined body of
1344/// a function. Construct DIE to represent this concrete inlined copy
1345/// of the function.
1346DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001347 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1348 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1349 assert (StartID && "Invalid starting label for an inlined scope!");
1350 assert (EndID && "Invalid end label for an inlined scope!");
1351 // Ignore empty scopes.
1352 if (StartID == EndID && StartID != 0)
1353 return NULL;
1354
1355 DIScope DS(Scope->getScopeNode());
1356 if (DS.isNull())
1357 return NULL;
1358 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1359
1360 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001361 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001362 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001363 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001364 dwarf::DW_FORM_ref4, OriginDIE);
1365
Devang Patel2c4ceb12009-11-21 02:48:08 +00001366 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001367 DWLabel("label", StartID));
Devang Patel2c4ceb12009-11-21 02:48:08 +00001368 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel53bb5c92009-11-10 23:06:00 +00001369 DWLabel("label", EndID));
1370
1371 InlinedSubprogramDIEs.insert(OriginDIE);
1372
1373 // Track the start label for this inlined function.
1374 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1375 I = InlineInfo.find(InlinedSP.getNode());
1376
1377 if (I == InlineInfo.end()) {
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001378 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1379 ScopeDIE));
Devang Patel53bb5c92009-11-10 23:06:00 +00001380 InlinedSPNodes.push_back(InlinedSP.getNode());
1381 } else
1382 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1383
1384 StringPool.insert(InlinedSP.getName());
1385 StringPool.insert(InlinedSP.getLinkageName());
1386 DILocation DL(Scope->getInlinedAt());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001387 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1388 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +00001389
1390 return ScopeDIE;
1391}
1392
Devang Patel2c4ceb12009-11-21 02:48:08 +00001393
1394/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patel8a241142009-12-09 18:24:21 +00001395DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001396 // Get the descriptor.
1397 const DIVariable &VD = DV->getVariable();
Devang Patel65dbc902009-11-25 17:36:49 +00001398 StringRef Name = VD.getName();
1399 if (Name.empty())
Devang Patel3fb6bd62009-11-13 02:25:26 +00001400 return NULL;
Devang Patel53bb5c92009-11-10 23:06:00 +00001401
1402 // Translate tag to proper Dwarf tag. The result variable is dropped for
1403 // now.
1404 unsigned Tag;
1405 switch (VD.getTag()) {
1406 case dwarf::DW_TAG_return_variable:
1407 return NULL;
1408 case dwarf::DW_TAG_arg_variable:
1409 Tag = dwarf::DW_TAG_formal_parameter;
1410 break;
1411 case dwarf::DW_TAG_auto_variable: // fall thru
1412 default:
1413 Tag = dwarf::DW_TAG_variable;
1414 break;
1415 }
1416
1417 // Define variable debug information entry.
1418 DIE *VariableDie = new DIE(Tag);
1419
1420
1421 DIE *AbsDIE = NULL;
1422 if (DbgVariable *AV = DV->getAbstractVariable())
1423 AbsDIE = AV->getDIE();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001424
Devang Patel53bb5c92009-11-10 23:06:00 +00001425 if (AbsDIE) {
1426 DIScope DS(Scope->getScopeNode());
1427 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patel017d1212009-11-20 21:37:22 +00001428 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc0326792009-11-11 03:09:50 +00001429 (void) OriginSPDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001430 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1431 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1432 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00001433 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel53bb5c92009-11-10 23:06:00 +00001434 dwarf::DW_FORM_ref4, AbsDIE);
1435 }
1436 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001437 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1438 addSourceLine(VariableDie, &VD);
Devang Patel53bb5c92009-11-10 23:06:00 +00001439
1440 // Add variable type.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001441 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel53bb5c92009-11-10 23:06:00 +00001442 // addresses instead.
1443 if (VD.isBlockByrefVariable())
Devang Patel8a241142009-12-09 18:24:21 +00001444 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel53bb5c92009-11-10 23:06:00 +00001445 else
Devang Patel8a241142009-12-09 18:24:21 +00001446 addType(VariableDie, VD.getType());
Devang Patel53bb5c92009-11-10 23:06:00 +00001447 }
1448
1449 // Add variable address.
1450 if (!Scope->isAbstractScope()) {
1451 MachineLocation Location;
Jim Grosbacha2f20b22009-11-22 20:14:00 +00001452 unsigned FrameReg;
1453 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1454 Location.set(FrameReg, Offset);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001455
Devang Patel53bb5c92009-11-10 23:06:00 +00001456 if (VD.hasComplexAddress())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001457 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001458 else if (VD.isBlockByrefVariable())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001459 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001460 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001461 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel53bb5c92009-11-10 23:06:00 +00001462 }
1463 DV->setDIE(VariableDie);
1464 return VariableDie;
1465
1466}
Devang Patel2c4ceb12009-11-21 02:48:08 +00001467
Devang Patel193f7202009-11-24 01:14:22 +00001468void DwarfDebug::addPubTypes(DISubprogram SP) {
1469 DICompositeType SPTy = SP.getType();
1470 unsigned SPTag = SPTy.getTag();
1471 if (SPTag != dwarf::DW_TAG_subroutine_type)
1472 return;
1473
1474 DIArray Args = SPTy.getTypeArray();
1475 if (Args.isNull())
1476 return;
1477
1478 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1479 DIType ATy(Args.getElement(i).getNode());
1480 if (ATy.isNull())
1481 continue;
1482 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel65dbc902009-11-25 17:36:49 +00001483 if (!CATy.isNull() && !CATy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001484 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1485 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1486 }
1487 }
1488}
1489
Devang Patel2c4ceb12009-11-21 02:48:08 +00001490/// constructScopeDIE - Construct a DIE for this scope.
1491DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001492 if (!Scope)
1493 return NULL;
1494 DIScope DS(Scope->getScopeNode());
1495 if (DS.isNull())
1496 return NULL;
1497
1498 DIE *ScopeDIE = NULL;
1499 if (Scope->getInlinedAt())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001500 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001501 else if (DS.isSubprogram()) {
1502 if (Scope->isAbstractScope())
Devang Patel017d1212009-11-20 21:37:22 +00001503 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001504 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00001505 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001506 }
1507 else {
Devang Patel2c4ceb12009-11-21 02:48:08 +00001508 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +00001509 if (!ScopeDIE) return NULL;
1510 }
1511
1512 // Add variables to scope.
1513 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1514 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel8a241142009-12-09 18:24:21 +00001515 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001516 if (VariableDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001517 ScopeDIE->addChild(VariableDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001518 }
1519
1520 // Add nested scopes.
1521 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1522 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1523 // Define the Scope debug information entry.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001524 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001525 if (NestedDIE)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001526 ScopeDIE->addChild(NestedDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +00001527 }
Devang Patel193f7202009-11-24 01:14:22 +00001528
1529 if (DS.isSubprogram())
1530 addPubTypes(DISubprogram(DS.getNode()));
1531
1532 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +00001533}
1534
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001535/// GetOrCreateSourceID - Look up the source id with the given directory and
1536/// source file names. If none currently exists, create a new id and insert it
1537/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1538/// maps as well.
Devang Patel65dbc902009-11-25 17:36:49 +00001539unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001540 unsigned DId;
1541 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1542 if (DI != DirectoryIdMap.end()) {
1543 DId = DI->getValue();
1544 } else {
1545 DId = DirectoryNames.size() + 1;
1546 DirectoryIdMap[DirName] = DId;
1547 DirectoryNames.push_back(DirName);
1548 }
1549
1550 unsigned FId;
1551 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1552 if (FI != SourceFileIdMap.end()) {
1553 FId = FI->getValue();
1554 } else {
1555 FId = SourceFileNames.size() + 1;
1556 SourceFileIdMap[FileName] = FId;
1557 SourceFileNames.push_back(FileName);
1558 }
1559
1560 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1561 SourceIdMap.find(std::make_pair(DId, FId));
1562 if (SI != SourceIdMap.end())
1563 return SI->second;
1564
1565 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1566 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1567 SourceIds.push_back(std::make_pair(DId, FId));
1568
1569 return SrcId;
1570}
1571
Devang Patel6404e4e2009-12-15 19:16:48 +00001572/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1573DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1574 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1575 if (NDie)
1576 return NDie;
1577 NDie = new DIE(dwarf::DW_TAG_namespace);
1578 ModuleCU->insertDIE(NS.getNode(), NDie);
1579 if (!NS.getName().empty())
1580 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1581 addSourceLine(NDie, &NS);
1582 addToContextOwner(NDie, NS.getContext());
1583 return NDie;
1584}
1585
Devang Pateld037d7a2009-12-11 21:37:07 +00001586CompileUnit *DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001587 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +00001588 StringRef FN = DIUnit.getFilename();
1589 StringRef Dir = DIUnit.getDirectory();
Devang Patel5ccdd102009-09-29 18:40:58 +00001590 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001591
1592 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001593 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001594 DWLabel("section_line", 0), DWLabel("section_line", 0),
1595 false);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001596 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patel5ccdd102009-09-29 18:40:58 +00001597 DIUnit.getProducer());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001598 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001599 DIUnit.getLanguage());
Devang Patel2c4ceb12009-11-21 02:48:08 +00001600 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001601
Devang Patel65dbc902009-11-25 17:36:49 +00001602 if (!Dir.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001603 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001604 if (DIUnit.isOptimized())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001605 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001606
Devang Patel65dbc902009-11-25 17:36:49 +00001607 StringRef Flags = DIUnit.getFlags();
1608 if (!Flags.empty())
Devang Patel2c4ceb12009-11-21 02:48:08 +00001609 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001610
1611 unsigned RVer = DIUnit.getRunTimeVersion();
1612 if (RVer)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001613 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001614 dwarf::DW_FORM_data1, RVer);
1615
1616 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel1dbc7712009-06-29 20:45:18 +00001617 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel70f44262009-06-29 20:38:13 +00001618 // Use first compile unit marked as isMain as the compile unit
1619 // for this module.
Devang Patel1dbc7712009-06-29 20:45:18 +00001620 ModuleCU = Unit;
Devang Patel70f44262009-06-29 20:38:13 +00001621 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001622
Devang Patele4b27562009-08-28 23:24:31 +00001623 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001624 CompileUnits.push_back(Unit);
Devang Pateld037d7a2009-12-11 21:37:07 +00001625 return Unit;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001626}
1627
Devang Patel2c4ceb12009-11-21 02:48:08 +00001628void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001629 DIGlobalVariable DI_GV(N);
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001630
Devang Patel905cf5e2009-09-04 23:59:07 +00001631 // If debug information is malformed then ignore it.
1632 if (DI_GV.Verify() == false)
1633 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001634
1635 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001636 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel13e16b62009-06-26 01:49:18 +00001637 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001638
Devang Patel8a241142009-12-09 18:24:21 +00001639 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Pateledb45632009-12-10 23:25:41 +00001640 if (!VariableDie)
1641 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001642
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001643 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001644 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001645
1646 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00001647 if (DI_GV.isDefinition()
1648 && !DI_GV.getContext().isCompileUnit()) {
1649 // Create specification DIE.
1650 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1651 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1652 dwarf::DW_FORM_ref4, VariableDie);
1653 DIEBlock *Block = new DIEBlock();
1654 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1655 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
1656 Asm->Mang->getMangledName(DI_GV.getGlobal()));
1657 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1658 ModuleCU->addDie(VariableSpecDIE);
1659 } else {
1660 DIEBlock *Block = new DIEBlock();
1661 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1662 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
1663 Asm->Mang->getMangledName(DI_GV.getGlobal()));
1664 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1665 }
Devang Patelc366f832009-12-10 19:14:49 +00001666 addToContextOwner(VariableDie, DI_GV.getContext());
1667
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001668 // Expose as global. FIXME - need to check external flag.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001669 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel193f7202009-11-24 01:14:22 +00001670
1671 DIType GTy = DI_GV.getType();
Devang Patel65dbc902009-11-25 17:36:49 +00001672 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patel193f7202009-11-24 01:14:22 +00001673 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1674 assert (Entry && "Missing global type!");
1675 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1676 }
Devang Patel13e16b62009-06-26 01:49:18 +00001677 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001678}
1679
Devang Patel2c4ceb12009-11-21 02:48:08 +00001680void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +00001681 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001682
1683 // Check for pre-existence.
Devang Patel017d1212009-11-20 21:37:22 +00001684 if (ModuleCU->getDIE(N))
Devang Patel13e16b62009-06-26 01:49:18 +00001685 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001686
1687 if (!SP.isDefinition())
1688 // This is a method declaration which will be handled while constructing
1689 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +00001690 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001691
Devang Patel8a241142009-12-09 18:24:21 +00001692 DIE *SubprogramDie = createSubprogramDIE(SP);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001693
1694 // Add to map.
Devang Patel017d1212009-11-20 21:37:22 +00001695 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001696
1697 // Add to context owner.
Devang Patel6404e4e2009-12-15 19:16:48 +00001698 addToContextOwner(SubprogramDie, SP.getContext());
Devang Patel0000fad2009-12-08 23:21:45 +00001699
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001700 // Expose as global.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001701 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel193f7202009-11-24 01:14:22 +00001702
Devang Patel13e16b62009-06-26 01:49:18 +00001703 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001704}
1705
Devang Patel2c4ceb12009-11-21 02:48:08 +00001706/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar00564992009-09-19 20:40:14 +00001707/// content. Create global DIEs and emit initial debug info sections.
1708/// This is inovked by the target AsmPrinter.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001709void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel208622d2009-06-25 22:36:02 +00001710 this->M = M;
1711
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001712 if (TimePassesIsEnabled)
1713 DebugTimer->startTimer();
1714
Devang Patel3380cc52009-11-11 19:55:08 +00001715 if (!MAI->doesSupportDebugInformation())
1716 return;
1717
Devang Patel78ab9e22009-07-30 18:56:46 +00001718 DebugInfoFinder DbgFinder;
1719 DbgFinder.processModule(*M);
Devang Patel13e16b62009-06-26 01:49:18 +00001720
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001721 // Create all the compile unit DIEs.
Devang Patel78ab9e22009-07-30 18:56:46 +00001722 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1723 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001724 constructCompileUnit(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001725
1726 if (CompileUnits.empty()) {
1727 if (TimePassesIsEnabled)
1728 DebugTimer->stopTimer();
1729
1730 return;
1731 }
1732
Devang Patel70f44262009-06-29 20:38:13 +00001733 // If main compile unit for this module is not seen than randomly
1734 // select first compile unit.
Devang Patel1dbc7712009-06-29 20:45:18 +00001735 if (!ModuleCU)
1736 ModuleCU = CompileUnits[0];
Devang Patel70f44262009-06-29 20:38:13 +00001737
Devang Patel53bb5c92009-11-10 23:06:00 +00001738 // Create DIEs for each subprogram.
Devang Patel78ab9e22009-07-30 18:56:46 +00001739 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1740 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001741 constructSubprogramDIE(*I);
Devang Patel13e16b62009-06-26 01:49:18 +00001742
Devang Patelc366f832009-12-10 19:14:49 +00001743 // Create DIEs for each global variable.
1744 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1745 E = DbgFinder.global_variable_end(); I != E; ++I)
1746 constructGlobalVariableDIE(*I);
1747
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001748 MMI = mmi;
1749 shouldEmit = true;
1750 MMI->setDebugInfoAvailability(true);
1751
1752 // Prime section data.
Chris Lattnerf0144122009-07-28 03:13:23 +00001753 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001754
1755 // Print out .file directives to specify files for .loc directives. These are
1756 // printed out early so that they precede any .loc directives.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001757 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001758 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1759 // Remember source id starts at 1.
1760 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1761 sys::Path FullPath(getSourceDirectoryName(Id.first));
1762 bool AppendOk =
1763 FullPath.appendComponent(getSourceFileName(Id.second));
1764 assert(AppendOk && "Could not append filename to directory!");
1765 AppendOk = false;
Chris Lattner74382b72009-08-23 22:45:37 +00001766 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001767 Asm->EOL();
1768 }
1769 }
1770
1771 // Emit initial sections
Devang Patel2c4ceb12009-11-21 02:48:08 +00001772 emitInitial();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001773
1774 if (TimePassesIsEnabled)
1775 DebugTimer->stopTimer();
1776}
1777
Devang Patel2c4ceb12009-11-21 02:48:08 +00001778/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001779///
Devang Patel2c4ceb12009-11-21 02:48:08 +00001780void DwarfDebug::endModule() {
Devang Patel6f3dc922009-10-06 00:03:14 +00001781 if (!ModuleCU)
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001782 return;
1783
1784 if (TimePassesIsEnabled)
1785 DebugTimer->startTimer();
1786
Devang Patel53bb5c92009-11-10 23:06:00 +00001787 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1788 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1789 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1790 DIE *ISP = *AI;
Devang Patel2c4ceb12009-11-21 02:48:08 +00001791 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +00001792 }
1793
Devang Patel1d5cc1d2009-12-03 01:25:38 +00001794 // Insert top level DIEs.
1795 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1796 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1797 ModuleCU->getCUDie()->addChild(*TI);
1798
Devang Patel5d11eb02009-12-03 19:11:07 +00001799 for (DenseMap<DIE *, WeakVH>::iterator CI = ContainingTypeMap.begin(),
1800 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1801 DIE *SPDie = CI->first;
1802 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1803 if (!N) continue;
1804 DIE *NDie = ModuleCU->getDIE(N);
1805 if (!NDie) continue;
1806 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1807 addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1808 }
1809
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001810 // Standard sections final addresses.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001811 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001812 EmitLabel("text_end", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001813 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001814 EmitLabel("data_end", 0);
1815
1816 // End text sections.
1817 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001818 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001819 EmitLabel("section_end", i);
1820 }
1821
1822 // Emit common frame information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001823 emitCommonDebugFrame();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001824
1825 // Emit function debug frame information
1826 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1827 E = DebugFrames.end(); I != E; ++I)
Devang Patel2c4ceb12009-11-21 02:48:08 +00001828 emitFunctionDebugFrame(*I);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001829
1830 // Compute DIE offsets and sizes.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001831 computeSizeAndOffsets();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001832
1833 // Emit all the DIEs into a debug info section
Devang Patel2c4ceb12009-11-21 02:48:08 +00001834 emitDebugInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001835
1836 // Corresponding abbreviations into a abbrev section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001837 emitAbbreviations();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001838
1839 // Emit source line correspondence into a debug line section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001840 emitDebugLines();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001841
1842 // Emit info into a debug pubnames section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001843 emitDebugPubNames();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001844
Devang Patel193f7202009-11-24 01:14:22 +00001845 // Emit info into a debug pubtypes section.
1846 emitDebugPubTypes();
1847
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001848 // Emit info into a debug str section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001849 emitDebugStr();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001850
1851 // Emit info into a debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001852 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001853
1854 // Emit info into a debug aranges section.
1855 EmitDebugARanges();
1856
1857 // Emit info into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001858 emitDebugRanges();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001859
1860 // Emit info into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001861 emitDebugMacInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001862
1863 // Emit inline info.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001864 emitDebugInlineInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001865
1866 if (TimePassesIsEnabled)
1867 DebugTimer->stopTimer();
1868}
1869
Devang Patel53bb5c92009-11-10 23:06:00 +00001870/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001871DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1872 unsigned FrameIdx,
Devang Patel53bb5c92009-11-10 23:06:00 +00001873 DILocation &ScopeLoc) {
1874
1875 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1876 if (AbsDbgVariable)
1877 return AbsDbgVariable;
1878
1879 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1880 if (!Scope)
1881 return NULL;
1882
1883 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001884 Scope->addVariable(AbsDbgVariable);
Devang Patel53bb5c92009-11-10 23:06:00 +00001885 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1886 return AbsDbgVariable;
1887}
1888
Devang Patel2c4ceb12009-11-21 02:48:08 +00001889/// collectVariableInfo - Populate DbgScope entries with variables' info.
1890void DwarfDebug::collectVariableInfo() {
Devang Patelac1ceb32009-10-09 22:42:28 +00001891 if (!MMI) return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001892
Devang Patele717faa2009-10-06 01:26:37 +00001893 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1894 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1895 VE = VMap.end(); VI != VE; ++VI) {
Devang Patelac1ceb32009-10-09 22:42:28 +00001896 MetadataBase *MB = VI->first;
1897 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel53bb5c92009-11-10 23:06:00 +00001898 if (!Var) continue;
Devang Pateleda31212009-10-08 18:48:03 +00001899 DIVariable DV (Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001900 std::pair< unsigned, MDNode *> VP = VI->second;
1901 DILocation ScopeLoc(VP.second);
1902
1903 DbgScope *Scope =
1904 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1905 if (!Scope)
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001906 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelfb0ee432009-11-10 23:20:04 +00001907 // If variable scope is not found then skip this variable.
1908 if (!Scope)
1909 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001910
1911 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patel2c4ceb12009-11-21 02:48:08 +00001912 Scope->addVariable(RegVar);
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001913 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1914 ScopeLoc))
Devang Patel53bb5c92009-11-10 23:06:00 +00001915 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patele717faa2009-10-06 01:26:37 +00001916 }
1917}
1918
Devang Patel2c4ceb12009-11-21 02:48:08 +00001919/// beginScope - Process beginning of a scope starting at Label.
1920void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001921 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1922 if (I == DbgScopeBeginMap.end())
1923 return;
Dan Gohman277207e2009-11-23 21:30:55 +00001924 ScopeVector &SD = I->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001925 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001926 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001927 (*SDI)->setStartLabelID(Label);
1928}
1929
Devang Patel2c4ceb12009-11-21 02:48:08 +00001930/// endScope - Process end of a scope.
1931void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel0d20ac82009-10-06 01:50:42 +00001932 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel8a4087d2009-10-06 03:15:38 +00001933 if (I == DbgScopeEndMap.end())
Devang Patel0d20ac82009-10-06 01:50:42 +00001934 return;
Devang Patel53bb5c92009-11-10 23:06:00 +00001935
1936 unsigned Label = MMI->NextLabelID();
1937 Asm->printLabel(Label);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001938 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001939
Devang Patel0d20ac82009-10-06 01:50:42 +00001940 SmallVector<DbgScope *, 2> &SD = I->second;
1941 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001942 SDI != SDE; ++SDI)
Devang Patel0d20ac82009-10-06 01:50:42 +00001943 (*SDI)->setEndLabelID(Label);
Devang Patel53bb5c92009-11-10 23:06:00 +00001944 return;
1945}
1946
1947/// createDbgScope - Create DbgScope for the scope.
1948void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1949
1950 if (!InlinedAt) {
1951 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1952 if (WScope)
1953 return;
1954 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1955 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001956 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel2f105c62009-11-11 00:18:40 +00001957 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel53bb5c92009-11-10 23:06:00 +00001958 return;
1959 }
1960
1961 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1962 if (WScope)
1963 return;
1964
1965 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1966 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
1967 DILocation DL(InlinedAt);
1968 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel0d20ac82009-10-06 01:50:42 +00001969}
1970
Devang Patel2c4ceb12009-11-21 02:48:08 +00001971/// extractScopeInformation - Scan machine instructions in this function
Devang Patelaf9e8472009-10-01 20:31:14 +00001972/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001973bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patelaf9e8472009-10-01 20:31:14 +00001974 // If scope information was extracted using .dbg intrinsics then there is not
1975 // any need to extract these information by scanning each instruction.
1976 if (!DbgScopeMap.empty())
1977 return false;
1978
Devang Patel53bb5c92009-11-10 23:06:00 +00001979 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patelaf9e8472009-10-01 20:31:14 +00001980 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1981 I != E; ++I) {
1982 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1983 II != IE; ++II) {
1984 const MachineInstr *MInsn = II;
1985 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001986 if (DL.isUnknown()) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00001987 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel53bb5c92009-11-10 23:06:00 +00001988 if (!DLT.Scope) continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00001989 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00001990 // other scopes, create one DbgScope now. This will be translated
Devang Patelaf9e8472009-10-01 20:31:14 +00001991 // into a scope DIE at the end.
Devang Patel53bb5c92009-11-10 23:06:00 +00001992 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1993 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
1994 }
1995 }
1996
1997
1998 // Build scope hierarchy using working set of scopes.
1999 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2000 I != E; ++I) {
2001 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2002 II != IE; ++II) {
2003 const MachineInstr *MInsn = II;
2004 DebugLoc DL = MInsn->getDebugLoc();
2005 if (DL.isUnknown()) continue;
2006 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
2007 if (!DLT.Scope) continue;
2008 // There is no need to create another DIE for compile unit. For all
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002009 // other scopes, create one DbgScope now. This will be translated
Devang Patel53bb5c92009-11-10 23:06:00 +00002010 // into a scope DIE at the end.
2011 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2012 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
2013 Scope->setLastInsn(MInsn);
Devang Patelaf9e8472009-10-01 20:31:14 +00002014 }
2015 }
2016
2017 // If a scope's last instruction is not set then use its child scope's
2018 // last instruction as this scope's last instrunction.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002019 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002020 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002021 if (DI->second->isAbstractScope())
2022 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002023 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patel2c4ceb12009-11-21 02:48:08 +00002024 DI->second->fixInstructionMarkers();
Devang Patelaf9e8472009-10-01 20:31:14 +00002025 assert (DI->second->getLastInsn() && "Invalid last instruction!");
2026 }
2027
2028 // Each scope has first instruction and last instruction to mark beginning
2029 // and end of a scope respectively. Create an inverse map that list scopes
2030 // starts (and ends) with an instruction. One instruction may start (or end)
2031 // multiple scopes.
Devang Patelbdf45cb2009-10-27 20:47:17 +00002032 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patelaf9e8472009-10-01 20:31:14 +00002033 DE = DbgScopeMap.end(); DI != DE; ++DI) {
2034 DbgScope *S = DI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00002035 if (S->isAbstractScope())
2036 continue;
Devang Patelaf9e8472009-10-01 20:31:14 +00002037 const MachineInstr *MI = S->getFirstInsn();
2038 assert (MI && "DbgScope does not have first instruction!");
2039
2040 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2041 if (IDI != DbgScopeBeginMap.end())
2042 IDI->second.push_back(S);
2043 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002044 DbgScopeBeginMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002045
2046 MI = S->getLastInsn();
2047 assert (MI && "DbgScope does not have last instruction!");
2048 IDI = DbgScopeEndMap.find(MI);
2049 if (IDI != DbgScopeEndMap.end())
2050 IDI->second.push_back(S);
2051 else
Devang Patel53bb5c92009-11-10 23:06:00 +00002052 DbgScopeEndMap[MI].push_back(S);
Devang Patelaf9e8472009-10-01 20:31:14 +00002053 }
2054
2055 return !DbgScopeMap.empty();
2056}
2057
Devang Patel2c4ceb12009-11-21 02:48:08 +00002058/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002059/// emitted immediately after the function entry point.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002060void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002061 this->MF = MF;
2062
2063 if (!ShouldEmitDwarfDebug()) return;
2064
2065 if (TimePassesIsEnabled)
2066 DebugTimer->startTimer();
2067
Devang Patel2c4ceb12009-11-21 02:48:08 +00002068 if (!extractScopeInformation(MF))
Devang Patel60b35bd2009-10-06 18:37:31 +00002069 return;
Devang Patel2c4ceb12009-11-21 02:48:08 +00002070
2071 collectVariableInfo();
Devang Patel60b35bd2009-10-06 18:37:31 +00002072
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002073 // Begin accumulating function debug information.
2074 MMI->BeginFunction(MF);
2075
2076 // Assumes in correct section after the entry point.
2077 EmitLabel("func_begin", ++SubprogramCount);
2078
2079 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2080 // function.
Devang Patelac1ceb32009-10-09 22:42:28 +00002081 DebugLoc FDL = MF->getDefaultDebugLoc();
2082 if (!FDL.isUnknown()) {
2083 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2084 unsigned LabelID = 0;
Devang Patel1619dc32009-10-13 23:28:53 +00002085 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002086 if (!SP.isNull())
Devang Patel2c4ceb12009-11-21 02:48:08 +00002087 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002088 else
Devang Patel2c4ceb12009-11-21 02:48:08 +00002089 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patelac1ceb32009-10-09 22:42:28 +00002090 Asm->printLabel(LabelID);
2091 O << '\n';
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002092 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002093 if (TimePassesIsEnabled)
2094 DebugTimer->stopTimer();
2095}
2096
Devang Patel2c4ceb12009-11-21 02:48:08 +00002097/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002098///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002099void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002100 if (!ShouldEmitDwarfDebug()) return;
2101
2102 if (TimePassesIsEnabled)
2103 DebugTimer->startTimer();
2104
Devang Patelac1ceb32009-10-09 22:42:28 +00002105 if (DbgScopeMap.empty())
2106 return;
Devang Patel70d75ca2009-11-12 19:02:56 +00002107
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002108 // Define end label for subprogram.
2109 EmitLabel("func_end", SubprogramCount);
2110
2111 // Get function line info.
2112 if (!Lines.empty()) {
2113 // Get section line info.
Chris Lattner290c2f52009-08-03 23:20:21 +00002114 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002115 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2116 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2117 // Append the function info to section info.
2118 SectionLineInfos.insert(SectionLineInfos.end(),
2119 Lines.begin(), Lines.end());
2120 }
2121
Devang Patel53bb5c92009-11-10 23:06:00 +00002122 // Construct abstract scopes.
2123 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002124 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002125 constructScopeDIE(*AI);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002126
Devang Patel2c4ceb12009-11-21 02:48:08 +00002127 constructScopeDIE(CurrentFnDbgScope);
Devang Patel70d75ca2009-11-12 19:02:56 +00002128
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002129 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2130 MMI->getFrameMoves()));
2131
2132 // Clear debug info
Devang Patelc09ddc12009-12-01 18:13:48 +00002133 CurrentFnDbgScope = NULL;
2134 DbgScopeMap.clear();
2135 DbgScopeBeginMap.clear();
2136 DbgScopeEndMap.clear();
2137 ConcreteScopes.clear();
2138 AbstractScopesList.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002139
2140 Lines.clear();
Devang Patelc09ddc12009-12-01 18:13:48 +00002141
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002142 if (TimePassesIsEnabled)
2143 DebugTimer->stopTimer();
2144}
2145
Devang Patel2c4ceb12009-11-21 02:48:08 +00002146/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002147/// label. Returns a unique label ID used to generate a label and provide
2148/// correspondence to the source line list.
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002149unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patelf84548d2009-10-05 18:03:19 +00002150 MDNode *S) {
Devang Patele4b27562009-08-28 23:24:31 +00002151 if (!MMI)
2152 return 0;
2153
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002154 if (TimePassesIsEnabled)
2155 DebugTimer->startTimer();
2156
Devang Patel65dbc902009-11-25 17:36:49 +00002157 StringRef Dir;
2158 StringRef Fn;
Devang Patelf84548d2009-10-05 18:03:19 +00002159
2160 DIDescriptor Scope(S);
2161 if (Scope.isCompileUnit()) {
2162 DICompileUnit CU(S);
2163 Dir = CU.getDirectory();
2164 Fn = CU.getFilename();
2165 } else if (Scope.isSubprogram()) {
2166 DISubprogram SP(S);
2167 Dir = SP.getDirectory();
2168 Fn = SP.getFilename();
2169 } else if (Scope.isLexicalBlock()) {
2170 DILexicalBlock DB(S);
2171 Dir = DB.getDirectory();
2172 Fn = DB.getFilename();
2173 } else
2174 assert (0 && "Unexpected scope info");
2175
2176 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002177 unsigned ID = MMI->NextLabelID();
2178 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2179
2180 if (TimePassesIsEnabled)
2181 DebugTimer->stopTimer();
2182
2183 return ID;
2184}
2185
2186/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2187/// timed. Look up the source id with the given directory and source file
2188/// names. If none currently exists, create a new id and insert it in the
2189/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2190/// well.
2191unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2192 const std::string &FileName) {
2193 if (TimePassesIsEnabled)
2194 DebugTimer->startTimer();
2195
Devang Patel5ccdd102009-09-29 18:40:58 +00002196 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00002197
2198 if (TimePassesIsEnabled)
2199 DebugTimer->stopTimer();
2200
2201 return SrcId;
2202}
2203
Bill Wendling829e67b2009-05-20 23:22:40 +00002204//===----------------------------------------------------------------------===//
2205// Emit Methods
2206//===----------------------------------------------------------------------===//
2207
Devang Patel2c4ceb12009-11-21 02:48:08 +00002208/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling94d04b82009-05-20 23:21:38 +00002209///
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002210unsigned
2211DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002212 // Get the children.
2213 const std::vector<DIE *> &Children = Die->getChildren();
2214
2215 // If not last sibling and has children then add sibling offset attribute.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002216 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002217
2218 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002219 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00002220
2221 // Get the abbreviation for this DIE.
2222 unsigned AbbrevNumber = Die->getAbbrevNumber();
2223 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2224
2225 // Set DIE offset
2226 Die->setOffset(Offset);
2227
2228 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00002229 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002230
2231 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2232 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2233
2234 // Size the DIE attribute values.
2235 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2236 // Size attribute value.
2237 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2238
2239 // Size the DIE children if any.
2240 if (!Children.empty()) {
2241 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2242 "Children flag not set");
2243
2244 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002245 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling94d04b82009-05-20 23:21:38 +00002246
2247 // End of children marker.
2248 Offset += sizeof(int8_t);
2249 }
2250
2251 Die->setSize(Offset - Die->getOffset());
2252 return Offset;
2253}
2254
Devang Patel2c4ceb12009-11-21 02:48:08 +00002255/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling94d04b82009-05-20 23:21:38 +00002256///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002257void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002258 // Compute size of compile unit header.
2259 static unsigned Offset =
2260 sizeof(int32_t) + // Length of Compilation Unit Info
2261 sizeof(int16_t) + // DWARF version number
2262 sizeof(int32_t) + // Offset Into Abbrev. Section
2263 sizeof(int8_t); // Pointer Size (in bytes)
2264
Devang Patel2c4ceb12009-11-21 02:48:08 +00002265 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel1dbc7712009-06-29 20:45:18 +00002266 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling94d04b82009-05-20 23:21:38 +00002267}
2268
Devang Patel2c4ceb12009-11-21 02:48:08 +00002269/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling94d04b82009-05-20 23:21:38 +00002270/// tools to recognize the object file contains Dwarf information.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002271void DwarfDebug::emitInitial() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002272 // Check to see if we already emitted intial headers.
2273 if (didInitial) return;
2274 didInitial = true;
2275
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002276 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002277
Bill Wendling94d04b82009-05-20 23:21:38 +00002278 // Dwarf sections base addresses.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002279 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002280 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002281 EmitLabel("section_debug_frame", 0);
2282 }
2283
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002284 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002285 EmitLabel("section_info", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002286 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002287 EmitLabel("section_abbrev", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002288 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002289 EmitLabel("section_aranges", 0);
2290
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002291 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2292 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling94d04b82009-05-20 23:21:38 +00002293 EmitLabel("section_macinfo", 0);
2294 }
2295
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002296 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002297 EmitLabel("section_line", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002298 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002299 EmitLabel("section_loc", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002300 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002301 EmitLabel("section_pubnames", 0);
Devang Patel193f7202009-11-24 01:14:22 +00002302 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2303 EmitLabel("section_pubtypes", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002304 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002305 EmitLabel("section_str", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002306 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002307 EmitLabel("section_ranges", 0);
2308
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002309 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002310 EmitLabel("text_begin", 0);
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002311 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002312 EmitLabel("data_begin", 0);
2313}
2314
Devang Patel2c4ceb12009-11-21 02:48:08 +00002315/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling94d04b82009-05-20 23:21:38 +00002316///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002317void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002318 // Get the abbreviation for this DIE.
2319 unsigned AbbrevNumber = Die->getAbbrevNumber();
2320 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2321
2322 Asm->EOL();
2323
2324 // Emit the code (index) for the abbreviation.
2325 Asm->EmitULEB128Bytes(AbbrevNumber);
2326
2327 if (Asm->isVerbose())
2328 Asm->EOL(std::string("Abbrev [" +
2329 utostr(AbbrevNumber) +
2330 "] 0x" + utohexstr(Die->getOffset()) +
2331 ":0x" + utohexstr(Die->getSize()) + " " +
2332 dwarf::TagString(Abbrev->getTag())));
2333 else
2334 Asm->EOL();
2335
2336 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2337 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2338
2339 // Emit the DIE attribute values.
2340 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2341 unsigned Attr = AbbrevData[i].getAttribute();
2342 unsigned Form = AbbrevData[i].getForm();
2343 assert(Form && "Too many attributes for DIE (check abbreviation)");
2344
2345 switch (Attr) {
2346 case dwarf::DW_AT_sibling:
Devang Patel2c4ceb12009-11-21 02:48:08 +00002347 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling94d04b82009-05-20 23:21:38 +00002348 break;
2349 case dwarf::DW_AT_abstract_origin: {
2350 DIEEntry *E = cast<DIEEntry>(Values[i]);
2351 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002352 unsigned Addr = Origin->getOffset();
Bill Wendling94d04b82009-05-20 23:21:38 +00002353 Asm->EmitInt32(Addr);
2354 break;
2355 }
2356 default:
2357 // Emit an attribute using the defined form.
2358 Values[i]->EmitValue(this, Form);
2359 break;
2360 }
2361
2362 Asm->EOL(dwarf::AttributeString(Attr));
2363 }
2364
2365 // Emit the DIE children if any.
2366 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2367 const std::vector<DIE *> &Children = Die->getChildren();
2368
2369 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel2c4ceb12009-11-21 02:48:08 +00002370 emitDIE(Children[j]);
Bill Wendling94d04b82009-05-20 23:21:38 +00002371
2372 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2373 }
2374}
2375
Devang Patel8a241142009-12-09 18:24:21 +00002376/// emitDebugInfo - Emit the debug info section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002377///
Devang Patel8a241142009-12-09 18:24:21 +00002378void DwarfDebug::emitDebugInfo() {
2379 // Start debug info section.
2380 Asm->OutStreamer.SwitchSection(
2381 Asm->getObjFileLowering().getDwarfInfoSection());
2382 DIE *Die = ModuleCU->getCUDie();
Bill Wendling94d04b82009-05-20 23:21:38 +00002383
2384 // Emit the compile units header.
Devang Patel8a241142009-12-09 18:24:21 +00002385 EmitLabel("info_begin", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002386
2387 // Emit size of content not including length itself
2388 unsigned ContentSize = Die->getSize() +
2389 sizeof(int16_t) + // DWARF version number
2390 sizeof(int32_t) + // Offset Into Abbrev. Section
2391 sizeof(int8_t) + // Pointer Size (in bytes)
2392 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2393
2394 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2395 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2396 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2397 Asm->EOL("Offset Into Abbrev. Section");
2398 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2399
Devang Patel2c4ceb12009-11-21 02:48:08 +00002400 emitDIE(Die);
Bill Wendling94d04b82009-05-20 23:21:38 +00002401 // FIXME - extra padding for gdb bug.
2402 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2403 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2404 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2405 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Devang Patel8a241142009-12-09 18:24:21 +00002406 EmitLabel("info_end", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002407
2408 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002409}
2410
Devang Patel2c4ceb12009-11-21 02:48:08 +00002411/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002412///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002413void DwarfDebug::emitAbbreviations() const {
Bill Wendling94d04b82009-05-20 23:21:38 +00002414 // Check to see if it is worth the effort.
2415 if (!Abbreviations.empty()) {
2416 // Start the debug abbrev section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002417 Asm->OutStreamer.SwitchSection(
2418 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002419
2420 EmitLabel("abbrev_begin", 0);
2421
2422 // For each abbrevation.
2423 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2424 // Get abbreviation data
2425 const DIEAbbrev *Abbrev = Abbreviations[i];
2426
2427 // Emit the abbrevations code (base 1 index.)
2428 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2429 Asm->EOL("Abbreviation Code");
2430
2431 // Emit the abbreviations data.
2432 Abbrev->Emit(Asm);
2433
2434 Asm->EOL();
2435 }
2436
2437 // Mark end of abbreviations.
2438 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2439
2440 EmitLabel("abbrev_end", 0);
2441 Asm->EOL();
2442 }
2443}
2444
Devang Patel2c4ceb12009-11-21 02:48:08 +00002445/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling94d04b82009-05-20 23:21:38 +00002446/// the line matrix.
2447///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002448void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002449 // Define last address of section.
2450 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2451 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2452 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2453 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2454
2455 // Mark end of matrix.
2456 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2457 Asm->EmitULEB128Bytes(1); Asm->EOL();
2458 Asm->EmitInt8(1); Asm->EOL();
2459}
2460
Devang Patel2c4ceb12009-11-21 02:48:08 +00002461/// emitDebugLines - Emit source line information.
Bill Wendling94d04b82009-05-20 23:21:38 +00002462///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002463void DwarfDebug::emitDebugLines() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002464 // If the target is using .loc/.file, the assembler will be emitting the
2465 // .debug_line table automatically.
Chris Lattner33adcfb2009-08-22 21:43:10 +00002466 if (MAI->hasDotLocAndDotFile())
Bill Wendling94d04b82009-05-20 23:21:38 +00002467 return;
2468
2469 // Minimum line delta, thus ranging from -10..(255-10).
2470 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2471 // Maximum line delta, thus ranging from -10..(255-10).
2472 const int MaxLineDelta = 255 + MinLineDelta;
2473
2474 // Start the dwarf line section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002475 Asm->OutStreamer.SwitchSection(
2476 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002477
2478 // Construct the section header.
2479 EmitDifference("line_end", 0, "line_begin", 0, true);
2480 Asm->EOL("Length of Source Line Info");
2481 EmitLabel("line_begin", 0);
2482
2483 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2484
2485 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2486 Asm->EOL("Prolog Length");
2487 EmitLabel("line_prolog_begin", 0);
2488
2489 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2490
2491 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2492
2493 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2494
2495 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2496
2497 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2498
2499 // Line number standard opcode encodings argument count
2500 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2501 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2502 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2503 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2504 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2505 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2506 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2507 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2508 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2509
2510 // Emit directories.
2511 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2512 Asm->EmitString(getSourceDirectoryName(DI));
2513 Asm->EOL("Directory");
2514 }
2515
2516 Asm->EmitInt8(0); Asm->EOL("End of directories");
2517
2518 // Emit files.
2519 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2520 // Remember source id starts at 1.
2521 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2522 Asm->EmitString(getSourceFileName(Id.second));
2523 Asm->EOL("Source");
2524 Asm->EmitULEB128Bytes(Id.first);
2525 Asm->EOL("Directory #");
2526 Asm->EmitULEB128Bytes(0);
2527 Asm->EOL("Mod date");
2528 Asm->EmitULEB128Bytes(0);
2529 Asm->EOL("File size");
2530 }
2531
2532 Asm->EmitInt8(0); Asm->EOL("End of files");
2533
2534 EmitLabel("line_prolog_end", 0);
2535
2536 // A sequence for each text section.
2537 unsigned SecSrcLinesSize = SectionSourceLines.size();
2538
2539 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2540 // Isolate current sections line info.
2541 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2542
Chris Lattner93b6db32009-08-08 23:39:42 +00002543 /*if (Asm->isVerbose()) {
Chris Lattnera87dea42009-07-31 18:48:30 +00002544 const MCSection *S = SectionMap[j + 1];
Chris Lattner33adcfb2009-08-22 21:43:10 +00002545 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling94d04b82009-05-20 23:21:38 +00002546 << S->getName() << '\n';
Chris Lattner93b6db32009-08-08 23:39:42 +00002547 }*/
2548 Asm->EOL();
Bill Wendling94d04b82009-05-20 23:21:38 +00002549
2550 // Dwarf assumes we start with first line of first source file.
2551 unsigned Source = 1;
2552 unsigned Line = 1;
2553
2554 // Construct rows of the address, source, line, column matrix.
2555 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2556 const SrcLineInfo &LineInfo = LineInfos[i];
2557 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2558 if (!LabelID) continue;
2559
Caroline Ticec6f9d622009-09-11 18:25:54 +00002560 if (LineInfo.getLine() == 0) continue;
2561
Bill Wendling94d04b82009-05-20 23:21:38 +00002562 if (!Asm->isVerbose())
2563 Asm->EOL();
2564 else {
2565 std::pair<unsigned, unsigned> SourceID =
2566 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattner33adcfb2009-08-22 21:43:10 +00002567 O << '\t' << MAI->getCommentString() << ' '
Dan Gohmanb3b98212009-12-05 02:00:34 +00002568 << getSourceDirectoryName(SourceID.first) << '/'
Bill Wendling94d04b82009-05-20 23:21:38 +00002569 << getSourceFileName(SourceID.second)
Dan Gohmanb3b98212009-12-05 02:00:34 +00002570 << ':' << utostr_32(LineInfo.getLine()) << '\n';
Bill Wendling94d04b82009-05-20 23:21:38 +00002571 }
2572
2573 // Define the line address.
2574 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2575 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2576 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2577 EmitReference("label", LabelID); Asm->EOL("Location label");
2578
2579 // If change of source, then switch to the new source.
2580 if (Source != LineInfo.getSourceID()) {
2581 Source = LineInfo.getSourceID();
2582 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2583 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2584 }
2585
2586 // If change of line.
2587 if (Line != LineInfo.getLine()) {
2588 // Determine offset.
2589 int Offset = LineInfo.getLine() - Line;
2590 int Delta = Offset - MinLineDelta;
2591
2592 // Update line.
2593 Line = LineInfo.getLine();
2594
2595 // If delta is small enough and in range...
2596 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2597 // ... then use fast opcode.
2598 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2599 } else {
2600 // ... otherwise use long hand.
2601 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2602 Asm->EOL("DW_LNS_advance_line");
2603 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2604 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2605 }
2606 } else {
2607 // Copy the previous row (different address or source)
2608 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2609 }
2610 }
2611
Devang Patel2c4ceb12009-11-21 02:48:08 +00002612 emitEndOfLineMatrix(j + 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002613 }
2614
2615 if (SecSrcLinesSize == 0)
2616 // Because we're emitting a debug_line section, we still need a line
2617 // table. The linker and friends expect it to exist. If there's nothing to
2618 // put into it, emit an empty table.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002619 emitEndOfLineMatrix(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002620
2621 EmitLabel("line_end", 0);
2622 Asm->EOL();
2623}
2624
Devang Patel2c4ceb12009-11-21 02:48:08 +00002625/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002626///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002627void DwarfDebug::emitCommonDebugFrame() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002628 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002629 return;
2630
2631 int stackGrowth =
2632 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2633 TargetFrameInfo::StackGrowsUp ?
2634 TD->getPointerSize() : -TD->getPointerSize();
2635
2636 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002637 Asm->OutStreamer.SwitchSection(
2638 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002639
2640 EmitLabel("debug_frame_common", 0);
2641 EmitDifference("debug_frame_common_end", 0,
2642 "debug_frame_common_begin", 0, true);
2643 Asm->EOL("Length of Common Information Entry");
2644
2645 EmitLabel("debug_frame_common_begin", 0);
2646 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2647 Asm->EOL("CIE Identifier Tag");
2648 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2649 Asm->EOL("CIE Version");
2650 Asm->EmitString("");
2651 Asm->EOL("CIE Augmentation");
2652 Asm->EmitULEB128Bytes(1);
2653 Asm->EOL("CIE Code Alignment Factor");
2654 Asm->EmitSLEB128Bytes(stackGrowth);
2655 Asm->EOL("CIE Data Alignment Factor");
2656 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2657 Asm->EOL("CIE RA Column");
2658
2659 std::vector<MachineMove> Moves;
2660 RI->getInitialFrameState(Moves);
2661
2662 EmitFrameMoves(NULL, 0, Moves, false);
2663
2664 Asm->EmitAlignment(2, 0, 0, false);
2665 EmitLabel("debug_frame_common_end", 0);
2666
2667 Asm->EOL();
2668}
2669
Devang Patel2c4ceb12009-11-21 02:48:08 +00002670/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling94d04b82009-05-20 23:21:38 +00002671/// section.
2672void
Devang Patel2c4ceb12009-11-21 02:48:08 +00002673DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattner33adcfb2009-08-22 21:43:10 +00002674 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002675 return;
2676
2677 // Start the dwarf frame section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002678 Asm->OutStreamer.SwitchSection(
2679 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002680
2681 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2682 "debug_frame_begin", DebugFrameInfo.Number, true);
2683 Asm->EOL("Length of Frame Information Entry");
2684
2685 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2686
2687 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2688 0, 0, true, false);
2689 Asm->EOL("FDE CIE offset");
2690
2691 EmitReference("func_begin", DebugFrameInfo.Number);
2692 Asm->EOL("FDE initial location");
2693 EmitDifference("func_end", DebugFrameInfo.Number,
2694 "func_begin", DebugFrameInfo.Number);
2695 Asm->EOL("FDE address range");
2696
2697 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2698 false);
2699
2700 Asm->EmitAlignment(2, 0, 0, false);
2701 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2702
2703 Asm->EOL();
2704}
2705
Devang Patel8a241142009-12-09 18:24:21 +00002706/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2707///
2708void DwarfDebug::emitDebugPubNames() {
2709 // Start the dwarf pubnames section.
2710 Asm->OutStreamer.SwitchSection(
2711 Asm->getObjFileLowering().getDwarfPubNamesSection());
2712
2713 EmitDifference("pubnames_end", ModuleCU->getID(),
2714 "pubnames_begin", ModuleCU->getID(), true);
Bill Wendling94d04b82009-05-20 23:21:38 +00002715 Asm->EOL("Length of Public Names Info");
2716
Devang Patel8a241142009-12-09 18:24:21 +00002717 EmitLabel("pubnames_begin", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002718
2719 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2720
2721 EmitSectionOffset("info_begin", "section_info",
Devang Patel8a241142009-12-09 18:24:21 +00002722 ModuleCU->getID(), 0, true, false);
Bill Wendling94d04b82009-05-20 23:21:38 +00002723 Asm->EOL("Offset of Compilation Unit Info");
2724
Devang Patel8a241142009-12-09 18:24:21 +00002725 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002726 true);
2727 Asm->EOL("Compilation Unit Length");
2728
Devang Patel8a241142009-12-09 18:24:21 +00002729 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling94d04b82009-05-20 23:21:38 +00002730 for (StringMap<DIE*>::const_iterator
2731 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2732 const char *Name = GI->getKeyData();
2733 DIE * Entity = GI->second;
2734
2735 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2736 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2737 }
2738
2739 Asm->EmitInt32(0); Asm->EOL("End Mark");
Devang Patel8a241142009-12-09 18:24:21 +00002740 EmitLabel("pubnames_end", ModuleCU->getID());
Bill Wendling94d04b82009-05-20 23:21:38 +00002741
2742 Asm->EOL();
2743}
2744
Devang Patel193f7202009-11-24 01:14:22 +00002745void DwarfDebug::emitDebugPubTypes() {
Devang Patelf3a03762009-11-24 19:18:41 +00002746 // Start the dwarf pubnames section.
2747 Asm->OutStreamer.SwitchSection(
2748 Asm->getObjFileLowering().getDwarfPubTypesSection());
Devang Patel193f7202009-11-24 01:14:22 +00002749 EmitDifference("pubtypes_end", ModuleCU->getID(),
2750 "pubtypes_begin", ModuleCU->getID(), true);
2751 Asm->EOL("Length of Public Types Info");
2752
2753 EmitLabel("pubtypes_begin", ModuleCU->getID());
2754
2755 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2756
2757 EmitSectionOffset("info_begin", "section_info",
2758 ModuleCU->getID(), 0, true, false);
2759 Asm->EOL("Offset of Compilation ModuleCU Info");
2760
2761 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2762 true);
2763 Asm->EOL("Compilation ModuleCU Length");
2764
2765 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2766 for (StringMap<DIE*>::const_iterator
2767 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2768 const char *Name = GI->getKeyData();
2769 DIE * Entity = GI->second;
2770
2771 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2772 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2773 }
2774
2775 Asm->EmitInt32(0); Asm->EOL("End Mark");
2776 EmitLabel("pubtypes_end", ModuleCU->getID());
2777
2778 Asm->EOL();
2779}
2780
Devang Patel2c4ceb12009-11-21 02:48:08 +00002781/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002782///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002783void DwarfDebug::emitDebugStr() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002784 // Check to see if it is worth the effort.
2785 if (!StringPool.empty()) {
2786 // Start the dwarf str section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002787 Asm->OutStreamer.SwitchSection(
2788 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002789
2790 // For each of strings in the string pool.
2791 for (unsigned StringID = 1, N = StringPool.size();
2792 StringID <= N; ++StringID) {
2793 // Emit a label for reference from debug information entries.
2794 EmitLabel("string", StringID);
2795
2796 // Emit the string itself.
2797 const std::string &String = StringPool[StringID];
2798 Asm->EmitString(String); Asm->EOL();
2799 }
2800
2801 Asm->EOL();
2802 }
2803}
2804
Devang Patel2c4ceb12009-11-21 02:48:08 +00002805/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002806///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002807void DwarfDebug::emitDebugLoc() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002808 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002809 Asm->OutStreamer.SwitchSection(
2810 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002811 Asm->EOL();
2812}
2813
2814/// EmitDebugARanges - Emit visible names into a debug aranges section.
2815///
2816void DwarfDebug::EmitDebugARanges() {
2817 // Start the dwarf aranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002818 Asm->OutStreamer.SwitchSection(
2819 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002820
2821 // FIXME - Mock up
2822#if 0
2823 CompileUnit *Unit = GetBaseCompileUnit();
2824
2825 // Don't include size of length
2826 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2827
2828 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2829
2830 EmitReference("info_begin", Unit->getID());
2831 Asm->EOL("Offset of Compilation Unit Info");
2832
2833 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2834
2835 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2836
2837 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2838 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2839
2840 // Range 1
2841 EmitReference("text_begin", 0); Asm->EOL("Address");
2842 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2843
2844 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2845 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2846#endif
2847
2848 Asm->EOL();
2849}
2850
Devang Patel2c4ceb12009-11-21 02:48:08 +00002851/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002852///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002853void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002854 // Start the dwarf ranges section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002855 Asm->OutStreamer.SwitchSection(
2856 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002857 Asm->EOL();
2858}
2859
Devang Patel2c4ceb12009-11-21 02:48:08 +00002860/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling94d04b82009-05-20 23:21:38 +00002861///
Devang Patel2c4ceb12009-11-21 02:48:08 +00002862void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002863 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002864 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002865 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002866 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002867 Asm->EOL();
2868 }
2869}
2870
Devang Patel2c4ceb12009-11-21 02:48:08 +00002871/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling94d04b82009-05-20 23:21:38 +00002872/// Section Header:
2873/// 1. length of section
2874/// 2. Dwarf version number
2875/// 3. address size.
2876///
2877/// Entries (one "entry" for each function that was inlined):
2878///
2879/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2880/// otherwise offset into __debug_str for regular function name.
2881/// 2. offset into __debug_str section for regular function name.
2882/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2883/// instances for the function.
2884///
2885/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2886/// inlined instance; the die_offset points to the inlined_subroutine die in the
2887/// __debug_info section, and the low_pc is the starting address for the
2888/// inlining instance.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002889void DwarfDebug::emitDebugInlineInfo() {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002890 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling94d04b82009-05-20 23:21:38 +00002891 return;
2892
Devang Patel1dbc7712009-06-29 20:45:18 +00002893 if (!ModuleCU)
Bill Wendling94d04b82009-05-20 23:21:38 +00002894 return;
2895
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002896 Asm->OutStreamer.SwitchSection(
2897 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002898 Asm->EOL();
2899 EmitDifference("debug_inlined_end", 1,
2900 "debug_inlined_begin", 1, true);
2901 Asm->EOL("Length of Debug Inlined Information Entry");
2902
2903 EmitLabel("debug_inlined_begin", 1);
2904
2905 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2906 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2907
Devang Patel53bb5c92009-11-10 23:06:00 +00002908 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2909 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002910
Devang Patel53bb5c92009-11-10 23:06:00 +00002911// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2912 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2913 MDNode *Node = *I;
Jim Grosbach7ab38df2009-11-22 19:20:36 +00002914 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
2915 = InlineInfo.find(Node);
Devang Patel53bb5c92009-11-10 23:06:00 +00002916 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patele4b27562009-08-28 23:24:31 +00002917 DISubprogram SP(Node);
Devang Patel65dbc902009-11-25 17:36:49 +00002918 StringRef LName = SP.getLinkageName();
2919 StringRef Name = SP.getName();
Bill Wendling94d04b82009-05-20 23:21:38 +00002920
Devang Patel65dbc902009-11-25 17:36:49 +00002921 if (LName.empty())
Devang Patel53cb17d2009-07-16 01:01:22 +00002922 Asm->EmitString(Name);
2923 else {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002924 // Skip special LLVM prefix that is used to inform the asm printer to not
2925 // emit usual symbol prefix before the symbol name. This happens for
2926 // Objective-C symbol names and symbol whose name is replaced using GCC's
2927 // __asm__ attribute.
Devang Patel53cb17d2009-07-16 01:01:22 +00002928 if (LName[0] == 1)
Benjamin Kramer1c3451f2009-11-25 18:26:09 +00002929 LName = LName.substr(1);
Devang Patel53bb5c92009-11-10 23:06:00 +00002930// Asm->EmitString(LName);
2931 EmitSectionOffset("string", "section_str",
2932 StringPool.idFor(LName), false, true);
2933
Devang Patel53cb17d2009-07-16 01:01:22 +00002934 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002935 Asm->EOL("MIPS linkage name");
Jim Grosbach31ef40e2009-11-21 23:12:12 +00002936// Asm->EmitString(Name);
Devang Patel53bb5c92009-11-10 23:06:00 +00002937 EmitSectionOffset("string", "section_str",
2938 StringPool.idFor(Name), false, true);
2939 Asm->EOL("Function name");
Bill Wendling94d04b82009-05-20 23:21:38 +00002940 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2941
Devang Patel53bb5c92009-11-10 23:06:00 +00002942 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling94d04b82009-05-20 23:21:38 +00002943 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel53bb5c92009-11-10 23:06:00 +00002944 DIE *SP = LI->second;
Bill Wendling94d04b82009-05-20 23:21:38 +00002945 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2946
2947 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattner33adcfb2009-08-22 21:43:10 +00002948 O << MAI->getData32bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002949 else
Chris Lattner33adcfb2009-08-22 21:43:10 +00002950 O << MAI->getData64bitsDirective();
Bill Wendling94d04b82009-05-20 23:21:38 +00002951
Devang Patel53bb5c92009-11-10 23:06:00 +00002952 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling94d04b82009-05-20 23:21:38 +00002953 }
2954 }
2955
2956 EmitLabel("debug_inlined_end", 1);
2957 Asm->EOL();
2958}