blob: f1b9c8ac8fa8ebb5bff5c67941367c7e7c263c3b [file] [log] [blame]
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Devang Patel15e723d2009-08-28 23:24:31 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greened87baff2009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner73266f92009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattnerf5377682009-08-24 03:52:50 +000025#include "llvm/ADT/StringExtras.h"
Daniel Dunbarc74255d2009-10-13 06:47:08 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
Chris Lattner5632fab2009-09-16 00:08:41 +000028#include "llvm/Support/Mangler.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000029#include "llvm/Support/Timer.h"
30#include "llvm/System/Path.h"
Bill Wendlingb12b3d72009-05-15 09:23:25 +000031using namespace llvm;
32
33static TimerGroup &getDwarfTimerGroup() {
34 static TimerGroup DwarfTimerGroup("Dwarf Debugging");
35 return DwarfTimerGroup;
36}
37
38//===----------------------------------------------------------------------===//
39
40/// Configuration values for initial hash set sizes (log2).
41///
Bill Wendlingb12b3d72009-05-15 09:23:25 +000042static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendlingb12b3d72009-05-15 09:23:25 +000043
44namespace llvm {
45
46//===----------------------------------------------------------------------===//
47/// CompileUnit - This dwarf writer support class manages information associate
48/// with a source file.
Nick Lewyckyee68f452009-11-17 08:11:44 +000049class CompileUnit {
Bill Wendlingb12b3d72009-05-15 09:23:25 +000050 /// ID - File identifier for source.
51 ///
52 unsigned ID;
53
54 /// Die - Compile unit debug information entry.
55 ///
Devang Patelc50078e2009-11-21 02:48:08 +000056 DIE *CUDie;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000057
Devang Patel1233f912009-11-21 00:31:03 +000058 /// IndexTyDie - An anonymous type for index type.
59 DIE *IndexTyDie;
60
Bill Wendlingb12b3d72009-05-15 09:23:25 +000061 /// GVToDieMap - Tracks the mapping of unit level debug informaton
62 /// variables to debug information entries.
Devang Patel15e723d2009-08-28 23:24:31 +000063 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Pateld90672c2009-11-20 21:37:22 +000064 ValueMap<MDNode *, DIE *> GVToDieMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000065
66 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
67 /// descriptors to debug information entries using a DIEEntry proxy.
Devang Patel15e723d2009-08-28 23:24:31 +000068 /// FIXME : Rename
Devang Pateld90672c2009-11-20 21:37:22 +000069 ValueMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendlingb12b3d72009-05-15 09:23:25 +000070
71 /// Globals - A map of globally visible named entities for this unit.
72 ///
73 StringMap<DIE*> Globals;
74
Bill Wendlingb12b3d72009-05-15 09:23:25 +000075public:
76 CompileUnit(unsigned I, DIE *D)
Devang Patelc50078e2009-11-21 02:48:08 +000077 : ID(I), CUDie(D), IndexTyDie(0) {}
78 ~CompileUnit() { delete CUDie; delete IndexTyDie; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000079
80 // Accessors.
Bill Wendling4ca8dfd2009-05-20 23:31:45 +000081 unsigned getID() const { return ID; }
Devang Patelc50078e2009-11-21 02:48:08 +000082 DIE* getCUDie() const { return CUDie; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000083 StringMap<DIE*> &getGlobals() { return Globals; }
84
85 /// hasContent - Return true if this compile unit has something to write out.
86 ///
Devang Patelc50078e2009-11-21 02:48:08 +000087 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000088
Devang Patelc50078e2009-11-21 02:48:08 +000089 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +000090 ///
Devang Patelc50078e2009-11-21 02:48:08 +000091 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000092
Devang Pateld90672c2009-11-20 21:37:22 +000093 /// getDIE - Returns the debug information entry map slot for the
Bill Wendlingb12b3d72009-05-15 09:23:25 +000094 /// specified debug variable.
Devang Pateld90672c2009-11-20 21:37:22 +000095 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach652b7432009-11-21 23:12:12 +000096
Devang Pateld90672c2009-11-20 21:37:22 +000097 /// insertDIE - Insert DIE into the map.
98 void insertDIE(MDNode *N, DIE *D) {
99 GVToDieMap.insert(std::make_pair(N, D));
100 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000101
Devang Pateld90672c2009-11-20 21:37:22 +0000102 /// getDIEEntry - Returns the debug information entry for the speciefied
103 /// debug variable.
104 DIEEntry *getDIEEntry(MDNode *N) { return GVToDIEEntryMap.lookup(N); }
105
106 /// insertDIEEntry - Insert debug information entry into the map.
107 void insertDIEEntry(MDNode *N, DIEEntry *E) {
108 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000109 }
110
Devang Patelc50078e2009-11-21 02:48:08 +0000111 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000112 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000113 void addDie(DIE *Buffer) {
114 this->CUDie->addChild(Buffer);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000115 }
Devang Patel1233f912009-11-21 00:31:03 +0000116
117 // getIndexTyDie - Get an anonymous type for index type.
118 DIE *getIndexTyDie() {
119 return IndexTyDie;
120 }
121
Jim Grosbachb23f2422009-11-22 19:20:36 +0000122 // setIndexTyDie - Set D as anonymous type for index which can be reused
123 // later.
Devang Patel1233f912009-11-21 00:31:03 +0000124 void setIndexTyDie(DIE *D) {
125 IndexTyDie = D;
126 }
127
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000128};
129
130//===----------------------------------------------------------------------===//
131/// DbgVariable - This class is used to track local variable information.
132///
Devang Patel4cb32c32009-11-16 21:53:40 +0000133class DbgVariable {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000134 DIVariable Var; // Variable Descriptor.
135 unsigned FrameIndex; // Variable frame index.
Devang Patel90a0fe32009-11-10 23:06:00 +0000136 DbgVariable *AbstractVar; // Abstract variable for this variable.
137 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000138public:
Devang Patel90a0fe32009-11-10 23:06:00 +0000139 DbgVariable(DIVariable V, unsigned I)
140 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000141
142 // Accessors.
Devang Patel90a0fe32009-11-10 23:06:00 +0000143 DIVariable getVariable() const { return Var; }
144 unsigned getFrameIndex() const { return FrameIndex; }
145 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
146 DbgVariable *getAbstractVariable() const { return AbstractVar; }
147 void setDIE(DIE *D) { TheDIE = D; }
148 DIE *getDIE() const { return TheDIE; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000149};
150
151//===----------------------------------------------------------------------===//
152/// DbgScope - This class is used to track scope information.
153///
Devang Patel4cb32c32009-11-16 21:53:40 +0000154class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000155 DbgScope *Parent; // Parent to this scope.
Jim Grosbach652b7432009-11-21 23:12:12 +0000156 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel90a0fe32009-11-10 23:06:00 +0000157 WeakVH InlinedAtLocation; // Location at which scope is inlined.
158 bool AbstractScope; // Abstract Scope
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000159 unsigned StartLabelID; // Label ID of the beginning of scope.
160 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel90ecd192009-10-01 18:25:23 +0000161 const MachineInstr *LastInsn; // Last instruction of this scope.
162 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000163 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
164 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbar41716322009-09-19 20:40:05 +0000165
Owen Anderson696d4862009-06-24 22:53:20 +0000166 // Private state for dump()
167 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000168public:
Devang Pateldd7bb432009-10-14 21:08:09 +0000169 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000170 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Jim Grosbach652b7432009-11-21 23:12:12 +0000171 StartLabelID(0), EndLabelID(0),
Devang Pateldd7bb432009-10-14 21:08:09 +0000172 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000173 virtual ~DbgScope();
174
175 // Accessors.
176 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000177 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000178 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach652b7432009-11-21 23:12:12 +0000179 MDNode *getInlinedAt() const {
Devang Patel90a0fe32009-11-10 23:06:00 +0000180 return dyn_cast_or_null<MDNode>(InlinedAtLocation);
Devang Pateldd7bb432009-10-14 21:08:09 +0000181 }
Devang Patel90a0fe32009-11-10 23:06:00 +0000182 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000183 unsigned getStartLabelID() const { return StartLabelID; }
184 unsigned getEndLabelID() const { return EndLabelID; }
185 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
186 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000187 void setStartLabelID(unsigned S) { StartLabelID = S; }
188 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Patel90ecd192009-10-01 18:25:23 +0000189 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
190 const MachineInstr *getLastInsn() { return LastInsn; }
191 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000192 void setAbstractScope() { AbstractScope = true; }
193 bool isAbstractScope() const { return AbstractScope; }
Devang Patel90ecd192009-10-01 18:25:23 +0000194 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000195
Devang Patelc50078e2009-11-21 02:48:08 +0000196 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000197 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000198 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000199
Devang Patelc50078e2009-11-21 02:48:08 +0000200 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000201 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000202 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000203
Devang Patelc50078e2009-11-21 02:48:08 +0000204 void fixInstructionMarkers() {
Devang Patel6a260102009-10-01 20:31:14 +0000205 assert (getFirstInsn() && "First instruction is missing!");
206 if (getLastInsn())
207 return;
Jim Grosbach652b7432009-11-21 23:12:12 +0000208
Devang Patel6a260102009-10-01 20:31:14 +0000209 // If a scope does not have an instruction to mark an end then use
210 // the end of last child scope.
211 SmallVector<DbgScope *, 4> &Scopes = getScopes();
212 assert (!Scopes.empty() && "Inner most scope does not have last insn!");
213 DbgScope *L = Scopes.back();
214 if (!L->getLastInsn())
Devang Patelc50078e2009-11-21 02:48:08 +0000215 L->fixInstructionMarkers();
Devang Patel6a260102009-10-01 20:31:14 +0000216 setLastInsn(L->getLastInsn());
217 }
218
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000219#ifndef NDEBUG
220 void dump() const;
221#endif
222};
223
224#ifndef NDEBUG
225void DbgScope::dump() const {
Chris Lattnerebb8c082009-08-23 00:51:00 +0000226 raw_ostream &err = errs();
227 err.indent(IndentLevel);
Devang Patel90a0fe32009-11-10 23:06:00 +0000228 MDNode *N = Desc.getNode();
229 N->dump();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000230 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel90a0fe32009-11-10 23:06:00 +0000231 if (AbstractScope)
232 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000233
234 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000235 if (!Scopes.empty())
236 err << "Children ...\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000237 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
238 if (Scopes[i] != this)
239 Scopes[i]->dump();
240
241 IndentLevel -= 2;
242}
243#endif
244
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000245DbgScope::~DbgScope() {
246 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
247 delete Scopes[i];
248 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
249 delete Variables[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000250}
251
252} // end llvm namespace
253
Chris Lattner621c44d2009-08-22 20:48:53 +0000254DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel5a3d37f2009-06-29 20:45:18 +0000255 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000256 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patelc50078e2009-11-21 02:48:08 +0000257 DIEValues(), StringPool(),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000258 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel90a0fe32009-11-10 23:06:00 +0000259 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000260 if (TimePassesIsEnabled)
261 DebugTimer = new Timer("Dwarf Debug Writer",
262 getDwarfTimerGroup());
263}
264DwarfDebug::~DwarfDebug() {
Devang Patelc50078e2009-11-21 02:48:08 +0000265 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
266 delete DIEValues[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000267
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000268 delete DebugTimer;
269}
270
Devang Patelc50078e2009-11-21 02:48:08 +0000271/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000272///
Devang Patelc50078e2009-11-21 02:48:08 +0000273void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000274 // Profile the node so that we can make it unique.
275 FoldingSetNodeID ID;
276 Abbrev.Profile(ID);
277
278 // Check the set for priors.
279 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
280
281 // If it's newly added.
282 if (InSet == &Abbrev) {
283 // Add to abbreviation list.
284 Abbreviations.push_back(&Abbrev);
285
286 // Assign the vector position + 1 as its number.
287 Abbrev.setNumber(Abbreviations.size());
288 } else {
289 // Assign existing abbreviation number.
290 Abbrev.setNumber(InSet->getNumber());
291 }
292}
293
Devang Patelc50078e2009-11-21 02:48:08 +0000294/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000295/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000296DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel1233f912009-11-21 00:31:03 +0000297 DIEEntry *Value = new DIEEntry(Entry);
Devang Patelc50078e2009-11-21 02:48:08 +0000298 DIEValues.push_back(Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000299 return Value;
300}
301
Devang Patelc50078e2009-11-21 02:48:08 +0000302/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000303///
Devang Patelc50078e2009-11-21 02:48:08 +0000304void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000305 unsigned Form, uint64_t Integer) {
306 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000307 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000308 DIEValues.push_back(Value);
309 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000310}
311
Devang Patelc50078e2009-11-21 02:48:08 +0000312/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000313///
Devang Patelc50078e2009-11-21 02:48:08 +0000314void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000315 unsigned Form, int64_t Integer) {
316 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000317 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000318 DIEValues.push_back(Value);
319 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000320}
321
Devang Patelc50078e2009-11-21 02:48:08 +0000322/// addString - Add a string attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000323///
Devang Patelc50078e2009-11-21 02:48:08 +0000324void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000325 const std::string &String) {
Devang Patel1233f912009-11-21 00:31:03 +0000326 DIEValue *Value = new DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000327 DIEValues.push_back(Value);
328 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000329}
330
Devang Patelc50078e2009-11-21 02:48:08 +0000331/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000332///
Devang Patelc50078e2009-11-21 02:48:08 +0000333void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000334 const DWLabel &Label) {
Devang Patel1233f912009-11-21 00:31:03 +0000335 DIEValue *Value = new DIEDwarfLabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000336 DIEValues.push_back(Value);
337 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000338}
339
Devang Patelc50078e2009-11-21 02:48:08 +0000340/// addObjectLabel - Add an non-Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000341///
Devang Patelc50078e2009-11-21 02:48:08 +0000342void DwarfDebug::addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000343 const std::string &Label) {
Devang Patel1233f912009-11-21 00:31:03 +0000344 DIEValue *Value = new DIEObjectLabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000345 DIEValues.push_back(Value);
346 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000347}
348
Devang Patelc50078e2009-11-21 02:48:08 +0000349/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000350///
Devang Patelc50078e2009-11-21 02:48:08 +0000351void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000352 const DWLabel &Label, const DWLabel &Section,
353 bool isEH, bool useSet) {
Devang Patel1233f912009-11-21 00:31:03 +0000354 DIEValue *Value = new DIESectionOffset(Label, Section, isEH, useSet);
Devang Patelc50078e2009-11-21 02:48:08 +0000355 DIEValues.push_back(Value);
356 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000357}
358
Devang Patelc50078e2009-11-21 02:48:08 +0000359/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000360///
Devang Patelc50078e2009-11-21 02:48:08 +0000361void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000362 const DWLabel &Hi, const DWLabel &Lo) {
Devang Patel1233f912009-11-21 00:31:03 +0000363 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000364 DIEValues.push_back(Value);
365 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000366}
367
Devang Patelc50078e2009-11-21 02:48:08 +0000368/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000369///
Devang Patelc50078e2009-11-21 02:48:08 +0000370void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000371 DIEBlock *Block) {
372 Block->ComputeSize(TD);
Devang Patelc50078e2009-11-21 02:48:08 +0000373 DIEValues.push_back(Block);
374 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000375}
376
Devang Patelc50078e2009-11-21 02:48:08 +0000377/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000378/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000379void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000380 // If there is no compile unit specified, don't add a line #.
381 if (V->getCompileUnit().isNull())
382 return;
383
384 unsigned Line = V->getLineNumber();
Devang Patelc50078e2009-11-21 02:48:08 +0000385 unsigned FileID = findCompileUnit(V->getCompileUnit()).getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000386 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000387 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
388 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000389}
390
Devang Patelc50078e2009-11-21 02:48:08 +0000391/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000392/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000393void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000394 // If there is no compile unit specified, don't add a line #.
395 if (G->getCompileUnit().isNull())
396 return;
397
398 unsigned Line = G->getLineNumber();
Devang Patelc50078e2009-11-21 02:48:08 +0000399 unsigned FileID = findCompileUnit(G->getCompileUnit()).getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000400 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000401 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
402 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000403}
Devang Patel318d70d2009-08-31 22:47:13 +0000404
Devang Patelc50078e2009-11-21 02:48:08 +0000405/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000406/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000407void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel318d70d2009-08-31 22:47:13 +0000408 // If there is no compile unit specified, don't add a line #.
409 if (SP->getCompileUnit().isNull())
410 return;
Caroline Tice9da96d82009-09-11 18:25:54 +0000411 // If the line number is 0, don't add it.
412 if (SP->getLineNumber() == 0)
413 return;
414
Devang Patel318d70d2009-08-31 22:47:13 +0000415
416 unsigned Line = SP->getLineNumber();
Devang Patelc50078e2009-11-21 02:48:08 +0000417 unsigned FileID = findCompileUnit(SP->getCompileUnit()).getID();
Devang Patel318d70d2009-08-31 22:47:13 +0000418 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000419 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
420 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel318d70d2009-08-31 22:47:13 +0000421}
422
Devang Patelc50078e2009-11-21 02:48:08 +0000423/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000424/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000425void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000426 // If there is no compile unit specified, don't add a line #.
427 DICompileUnit CU = Ty->getCompileUnit();
428 if (CU.isNull())
429 return;
430
431 unsigned Line = Ty->getLineNumber();
Devang Patelc50078e2009-11-21 02:48:08 +0000432 unsigned FileID = findCompileUnit(CU).getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000433 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000434 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
435 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000436}
437
Caroline Tice248d5572009-08-31 21:19:37 +0000438/* Byref variables, in Blocks, are declared by the programmer as
439 "SomeType VarName;", but the compiler creates a
440 __Block_byref_x_VarName struct, and gives the variable VarName
441 either the struct, or a pointer to the struct, as its type. This
442 is necessary for various behind-the-scenes things the compiler
443 needs to do with by-reference variables in blocks.
444
445 However, as far as the original *programmer* is concerned, the
446 variable should still have type 'SomeType', as originally declared.
447
448 The following function dives into the __Block_byref_x_VarName
449 struct to find the original type of the variable. This will be
450 passed back to the code generating the type for the Debug
451 Information Entry for the variable 'VarName'. 'VarName' will then
452 have the original type 'SomeType' in its debug information.
453
454 The original type 'SomeType' will be the type of the field named
455 'VarName' inside the __Block_byref_x_VarName struct.
456
457 NOTE: In order for this to not completely fail on the debugger
458 side, the Debug Information Entry for the variable VarName needs to
459 have a DW_AT_location that tells the debugger how to unwind through
460 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patelc50078e2009-11-21 02:48:08 +0000461 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-08-31 21:19:37 +0000462
463/// Find the type the programmer originally declared the variable to be
464/// and return that type.
465///
Devang Patelc50078e2009-11-21 02:48:08 +0000466DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-08-31 21:19:37 +0000467
468 DIType subType = Ty;
469 unsigned tag = Ty.getTag();
470
471 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000472 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000473 subType = DTy.getTypeDerivedFrom();
474 }
475
476 DICompositeType blockStruct = DICompositeType(subType.getNode());
477
478 DIArray Elements = blockStruct.getTypeArray();
479
480 if (Elements.isNull())
481 return Ty;
482
483 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
484 DIDescriptor Element = Elements.getElement(i);
485 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patelaaf012e2009-09-29 18:40:58 +0000486 if (strcmp(Name.c_str(), DT.getName()) == 0)
Caroline Tice248d5572009-08-31 21:19:37 +0000487 return (DT.getTypeDerivedFrom());
488 }
489
490 return Ty;
491}
492
Devang Patelc50078e2009-11-21 02:48:08 +0000493/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000494/// and generate the DWARF information necessary to find the actual variable
495/// given the extra address information encoded in the DIVariable, starting from
496/// the starting location. Add the DWARF information to the die.
497///
Devang Patelc50078e2009-11-21 02:48:08 +0000498void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000499 unsigned Attribute,
500 const MachineLocation &Location) {
501 const DIVariable &VD = DV->getVariable();
502 DIType Ty = VD.getType();
503
504 // Decode the original location, and use that as the start of the byref
505 // variable's location.
506 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
507 DIEBlock *Block = new DIEBlock();
508
509 if (Location.isReg()) {
510 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000511 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000512 } else {
513 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000514 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
515 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000516 }
517 } else {
518 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000519 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000520 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000521 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
522 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000523 }
524
Devang Patelc50078e2009-11-21 02:48:08 +0000525 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000526 }
527
528 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
529 uint64_t Element = VD.getAddrElement(i);
530
531 if (Element == DIFactory::OpPlus) {
Devang Patelc50078e2009-11-21 02:48:08 +0000532 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
533 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000534 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000535 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000536 } else llvm_unreachable("unknown DIFactory Opcode");
537 }
538
539 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000540 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000541}
542
Caroline Tice248d5572009-08-31 21:19:37 +0000543/* Byref variables, in Blocks, are declared by the programmer as "SomeType
544 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
545 gives the variable VarName either the struct, or a pointer to the struct, as
546 its type. This is necessary for various behind-the-scenes things the
547 compiler needs to do with by-reference variables in Blocks.
548
549 However, as far as the original *programmer* is concerned, the variable
550 should still have type 'SomeType', as originally declared.
551
Devang Patelc50078e2009-11-21 02:48:08 +0000552 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000553 struct to find the original type of the variable, which is then assigned to
554 the variable's Debug Information Entry as its real type. So far, so good.
555 However now the debugger will expect the variable VarName to have the type
556 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbar41716322009-09-19 20:40:05 +0000557 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-08-31 21:19:37 +0000558 pointers and struct to find the actual variable of type SomeType.
559
560 The following function does just that. We start by getting
561 the "normal" location for the variable. This will be the location
562 of either the struct __Block_byref_x_VarName or the pointer to the
563 struct __Block_byref_x_VarName.
564
565 The struct will look something like:
566
567 struct __Block_byref_x_VarName {
568 ... <various fields>
569 struct __Block_byref_x_VarName *forwarding;
570 ... <various other fields>
571 SomeType VarName;
572 ... <maybe more fields>
573 };
574
575 If we are given the struct directly (as our starting point) we
576 need to tell the debugger to:
577
578 1). Add the offset of the forwarding field.
579
580 2). Follow that pointer to get the the real __Block_byref_x_VarName
581 struct to use (the real one may have been copied onto the heap).
582
583 3). Add the offset for the field VarName, to find the actual variable.
584
585 If we started with a pointer to the struct, then we need to
586 dereference that pointer first, before the other steps.
587 Translating this into DWARF ops, we will need to append the following
588 to the current location description for the variable:
589
590 DW_OP_deref -- optional, if we start with a pointer
591 DW_OP_plus_uconst <forward_fld_offset>
592 DW_OP_deref
593 DW_OP_plus_uconst <varName_fld_offset>
594
595 That is what this function does. */
596
Devang Patelc50078e2009-11-21 02:48:08 +0000597/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-08-31 21:19:37 +0000598/// provided, and generate the DWARF information necessary to find the
599/// actual Block variable (navigating the Block struct) based on the
600/// starting location. Add the DWARF information to the die. For
601/// more information, read large comment just above here.
602///
Devang Patelc50078e2009-11-21 02:48:08 +0000603void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000604 unsigned Attribute,
605 const MachineLocation &Location) {
Caroline Tice248d5572009-08-31 21:19:37 +0000606 const DIVariable &VD = DV->getVariable();
607 DIType Ty = VD.getType();
608 DIType TmpTy = Ty;
609 unsigned Tag = Ty.getTag();
610 bool isPointer = false;
611
Devang Patelaaf012e2009-09-29 18:40:58 +0000612 const char *varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000613
614 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000615 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000616 TmpTy = DTy.getTypeDerivedFrom();
617 isPointer = true;
618 }
619
620 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
621
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000622 // Find the __forwarding field and the variable field in the __Block_byref
623 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000624 DIArray Fields = blockStruct.getTypeArray();
625 DIDescriptor varField = DIDescriptor();
626 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000627
628
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000629 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
630 DIDescriptor Element = Fields.getElement(i);
631 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patelaaf012e2009-09-29 18:40:58 +0000632 const char *fieldName = DT.getName();
633 if (strcmp(fieldName, "__forwarding") == 0)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000634 forwardingField = Element;
Devang Patelaaf012e2009-09-29 18:40:58 +0000635 else if (strcmp(fieldName, varName) == 0)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000636 varField = Element;
637 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000638
Mike Stump2fd84e22009-09-24 23:21:26 +0000639 assert(!varField.isNull() && "Can't find byref variable in Block struct");
640 assert(!forwardingField.isNull()
641 && "Can't find forwarding field in Block struct");
Caroline Tice248d5572009-08-31 21:19:37 +0000642
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000643 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000644 unsigned int forwardingFieldOffset =
645 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
646 unsigned int varFieldOffset =
647 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000648
Mike Stump2fd84e22009-09-24 23:21:26 +0000649 // Decode the original location, and use that as the start of the byref
650 // variable's location.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000651 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
652 DIEBlock *Block = new DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000653
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000654 if (Location.isReg()) {
655 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000656 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000657 else {
658 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000659 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
660 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000661 }
662 } else {
663 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000664 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000665 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000666 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
667 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000668 }
Caroline Tice248d5572009-08-31 21:19:37 +0000669
Devang Patelc50078e2009-11-21 02:48:08 +0000670 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000671 }
Caroline Tice248d5572009-08-31 21:19:37 +0000672
Mike Stump2fd84e22009-09-24 23:21:26 +0000673 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000674 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000675 if (isPointer)
Devang Patelc50078e2009-11-21 02:48:08 +0000676 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000677
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000678 // Next add the offset for the '__forwarding' field:
679 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
680 // adding the offset if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000681 if (forwardingFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000682 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
683 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000684 }
Caroline Tice248d5572009-08-31 21:19:37 +0000685
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000686 // Now dereference the __forwarding field to get to the real __Block_byref
687 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000688 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000689
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000690 // Now that we've got the real __Block_byref... struct, add the offset
691 // for the variable's field to get to the location of the actual variable:
692 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000693 if (varFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000694 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
695 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000696 }
Caroline Tice248d5572009-08-31 21:19:37 +0000697
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000698 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000699 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000700}
701
Devang Patelc50078e2009-11-21 02:48:08 +0000702/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000703/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000704void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000705 const MachineLocation &Location) {
706 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
707 DIEBlock *Block = new DIEBlock();
708
709 if (Location.isReg()) {
710 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000711 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000712 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000713 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
714 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000715 }
716 } else {
717 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000718 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000719 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000720 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
721 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000722 }
723
Devang Patelc50078e2009-11-21 02:48:08 +0000724 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000725 }
726
Devang Patelc50078e2009-11-21 02:48:08 +0000727 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000728}
729
Devang Patelc50078e2009-11-21 02:48:08 +0000730/// addType - Add a new type attribute to the specified entity.
731void DwarfDebug::addType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000732 if (Ty.isNull())
733 return;
734
735 // Check for pre-existence.
Devang Patelc50078e2009-11-21 02:48:08 +0000736 DIEEntry *Entry = DW_Unit->getDIEEntry(Ty.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000737
738 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000739 if (Entry) {
740 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000741 return;
742 }
743
744 // Set up proxy.
Devang Patelc50078e2009-11-21 02:48:08 +0000745 Entry = createDIEEntry();
746 DW_Unit->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000747
748 // Construct type.
Devang Patel1233f912009-11-21 00:31:03 +0000749 DIE *Buffer = new DIE(dwarf::DW_TAG_base_type);
Devang Patel56843af2009-08-31 18:49:10 +0000750 if (Ty.isBasicType())
Devang Patelc50078e2009-11-21 02:48:08 +0000751 constructTypeDIE(DW_Unit, *Buffer, DIBasicType(Ty.getNode()));
Devang Patel56843af2009-08-31 18:49:10 +0000752 else if (Ty.isCompositeType())
Devang Patelc50078e2009-11-21 02:48:08 +0000753 constructTypeDIE(DW_Unit, *Buffer, DICompositeType(Ty.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000754 else {
Devang Patel56843af2009-08-31 18:49:10 +0000755 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patelc50078e2009-11-21 02:48:08 +0000756 constructTypeDIE(DW_Unit, *Buffer, DIDerivedType(Ty.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000757 }
758
759 // Add debug information entry to entity and appropriate context.
760 DIE *Die = NULL;
761 DIDescriptor Context = Ty.getContext();
762 if (!Context.isNull())
Devang Pateld90672c2009-11-20 21:37:22 +0000763 Die = DW_Unit->getDIE(Context.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000764
Jim Grosbach652b7432009-11-21 23:12:12 +0000765 if (Die)
Devang Patelc50078e2009-11-21 02:48:08 +0000766 Die->addChild(Buffer);
Jim Grosbach652b7432009-11-21 23:12:12 +0000767 else
Devang Patelc50078e2009-11-21 02:48:08 +0000768 DW_Unit->addDie(Buffer);
769 Entry->setEntry(Buffer);
770 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000771}
772
Devang Patelc50078e2009-11-21 02:48:08 +0000773/// constructTypeDIE - Construct basic type die from DIBasicType.
774void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000775 DIBasicType BTy) {
776 // Get core information.
Devang Patelaaf012e2009-09-29 18:40:58 +0000777 const char *Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000778 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000779 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000780 BTy.getEncoding());
781
782 // Add name if not anonymous or intermediate type.
Devang Patelaaf012e2009-09-29 18:40:58 +0000783 if (Name)
Devang Patelc50078e2009-11-21 02:48:08 +0000784 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000785 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000786 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000787}
788
Devang Patelc50078e2009-11-21 02:48:08 +0000789/// constructTypeDIE - Construct derived type die from DIDerivedType.
790void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000791 DIDerivedType DTy) {
792 // Get core information.
Devang Patelaaf012e2009-09-29 18:40:58 +0000793 const char *Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000794 uint64_t Size = DTy.getSizeInBits() >> 3;
795 unsigned Tag = DTy.getTag();
796
797 // FIXME - Workaround for templates.
798 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
799
800 Buffer.setTag(Tag);
801
802 // Map to main type, void will not have a type.
803 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelc50078e2009-11-21 02:48:08 +0000804 addType(DW_Unit, &Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000805
806 // Add name if not anonymous or intermediate type.
Devang Patel18a7d1c2009-10-16 21:27:43 +0000807 if (Name && Tag != dwarf::DW_TAG_pointer_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000808 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000809
810 // Add size if non-zero (derived types might be zero-sized.)
811 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000812 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000813
814 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000815 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000816 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000817}
818
Devang Patelc50078e2009-11-21 02:48:08 +0000819/// constructTypeDIE - Construct type DIE from DICompositeType.
820void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000821 DICompositeType CTy) {
822 // Get core information.
Devang Patelaaf012e2009-09-29 18:40:58 +0000823 const char *Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000824
825 uint64_t Size = CTy.getSizeInBits() >> 3;
826 unsigned Tag = CTy.getTag();
827 Buffer.setTag(Tag);
828
829 switch (Tag) {
830 case dwarf::DW_TAG_vector_type:
831 case dwarf::DW_TAG_array_type:
Devang Patelc50078e2009-11-21 02:48:08 +0000832 constructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000833 break;
834 case dwarf::DW_TAG_enumeration_type: {
835 DIArray Elements = CTy.getTypeArray();
836
837 // Add enumerators to enumeration type.
838 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
839 DIE *ElemDie = NULL;
Devang Patel15e723d2009-08-28 23:24:31 +0000840 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelfb812752009-10-09 17:51:49 +0000841 if (!Enum.isNull()) {
Devang Patelc50078e2009-11-21 02:48:08 +0000842 ElemDie = constructEnumTypeDIE(DW_Unit, &Enum);
843 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +0000844 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000845 }
846 }
847 break;
848 case dwarf::DW_TAG_subroutine_type: {
849 // Add return type.
850 DIArray Elements = CTy.getTypeArray();
851 DIDescriptor RTy = Elements.getElement(0);
Devang Patelc50078e2009-11-21 02:48:08 +0000852 addType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000853
854 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +0000855 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000856
857 // Add arguments.
858 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
859 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
860 DIDescriptor Ty = Elements.getElement(i);
Devang Patelc50078e2009-11-21 02:48:08 +0000861 addType(DW_Unit, Arg, DIType(Ty.getNode()));
862 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000863 }
864 }
865 break;
866 case dwarf::DW_TAG_structure_type:
867 case dwarf::DW_TAG_union_type:
868 case dwarf::DW_TAG_class_type: {
869 // Add elements to structure type.
870 DIArray Elements = CTy.getTypeArray();
871
872 // A forward struct declared type may not have elements available.
873 if (Elements.isNull())
874 break;
875
876 // Add elements to structure type.
877 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
878 DIDescriptor Element = Elements.getElement(i);
Devang Patel15e723d2009-08-28 23:24:31 +0000879 if (Element.isNull())
880 continue;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000881 DIE *ElemDie = NULL;
882 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patelc50078e2009-11-21 02:48:08 +0000883 ElemDie = createSubprogramDIE(DW_Unit,
Devang Patel15e723d2009-08-28 23:24:31 +0000884 DISubprogram(Element.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000885 else
Devang Patelc50078e2009-11-21 02:48:08 +0000886 ElemDie = createMemberDIE(DW_Unit,
Devang Patel15e723d2009-08-28 23:24:31 +0000887 DIDerivedType(Element.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000888 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000889 }
890
Devang Patel20b32102009-08-27 23:51:51 +0000891 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +0000892 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000893
894 unsigned RLang = CTy.getRunTimeLang();
895 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +0000896 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000897 dwarf::DW_FORM_data1, RLang);
898 break;
899 }
900 default:
901 break;
902 }
903
904 // Add name if not anonymous or intermediate type.
Devang Patelaaf012e2009-09-29 18:40:58 +0000905 if (Name)
Devang Patelc50078e2009-11-21 02:48:08 +0000906 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000907
908 if (Tag == dwarf::DW_TAG_enumeration_type ||
909 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
910 // Add size if non-zero (derived types might be zero-sized.)
911 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000912 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000913 else {
914 // Add zero size if it is not a forward declaration.
915 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000916 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000917 else
Devang Patelc50078e2009-11-21 02:48:08 +0000918 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000919 }
920
921 // Add source line info if available.
922 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000923 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000924 }
925}
926
Devang Patelc50078e2009-11-21 02:48:08 +0000927/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
928void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000929 int64_t L = SR.getLo();
930 int64_t H = SR.getHi();
931 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
932
Devang Patelc50078e2009-11-21 02:48:08 +0000933 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +0000934 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +0000935 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Patele7ff5092009-08-14 20:59:16 +0000936 if (H)
Devang Patelc50078e2009-11-21 02:48:08 +0000937 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000938
Devang Patelc50078e2009-11-21 02:48:08 +0000939 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000940}
941
Devang Patelc50078e2009-11-21 02:48:08 +0000942/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
943void DwarfDebug::constructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000944 DICompositeType *CTy) {
945 Buffer.setTag(dwarf::DW_TAG_array_type);
946 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000947 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000948
949 // Emit derived type.
Devang Patelc50078e2009-11-21 02:48:08 +0000950 addType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000951 DIArray Elements = CTy->getTypeArray();
952
Devang Patel1233f912009-11-21 00:31:03 +0000953 // Get an anonymous type for index type.
954 DIE *IdxTy = DW_Unit->getIndexTyDie();
955 if (!IdxTy) {
956 // Construct an anonymous type for index type.
957 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000958 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
959 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +0000960 dwarf::DW_ATE_signed);
Devang Patelc50078e2009-11-21 02:48:08 +0000961 DW_Unit->addDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +0000962 DW_Unit->setIndexTyDie(IdxTy);
963 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000964
965 // Add subranges to array type.
966 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
967 DIDescriptor Element = Elements.getElement(i);
968 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000969 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000970 }
971}
972
Devang Patelc50078e2009-11-21 02:48:08 +0000973/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
974DIE *DwarfDebug::constructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000975 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patelaaf012e2009-09-29 18:40:58 +0000976 const char *Name = ETy->getName();
Devang Patelc50078e2009-11-21 02:48:08 +0000977 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000978 int64_t Value = ETy->getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +0000979 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000980 return Enumerator;
981}
982
Devang Patelc50078e2009-11-21 02:48:08 +0000983/// createGlobalVariableDIE - Create new DIE using GV.
984DIE *DwarfDebug::createGlobalVariableDIE(CompileUnit *DW_Unit,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000985 const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +0000986 // If the global variable was optmized out then no need to create debug info
987 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +0000988 if (!GV.getGlobal()) return NULL;
989 if (!GV.getDisplayName()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +0000990
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000991 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +0000992 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +0000993 GV.getDisplayName());
994
995 const char *LinkageName = GV.getLinkageName();
996 if (LinkageName) {
Chris Lattner73266f92009-08-19 05:49:37 +0000997 // Skip special LLVM prefix that is used to inform the asm printer to not
998 // emit usual symbol prefix before the symbol name. This happens for
999 // Objective-C symbol names and symbol whose name is replaced using GCC's
1000 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00001001 if (LinkageName[0] == 1)
1002 LinkageName = &LinkageName[1];
Devang Patelc50078e2009-11-21 02:48:08 +00001003 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelbd760a52009-07-14 00:55:28 +00001004 LinkageName);
Devang Patel76031e82009-07-16 01:01:22 +00001005 }
Devang Patelc50078e2009-11-21 02:48:08 +00001006 addType(DW_Unit, GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001007 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001008 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1009 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001010
1011 // Add address.
1012 DIEBlock *Block = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001013 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1014 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patel6bd5cc82009-10-05 23:22:08 +00001015 Asm->Mang->getMangledName(GV.getGlobal()));
Devang Patelc50078e2009-11-21 02:48:08 +00001016 addBlock(GVDie, dwarf::DW_AT_location, 0, Block);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001017
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001018 return GVDie;
1019}
1020
Devang Patelc50078e2009-11-21 02:48:08 +00001021/// createMemberDIE - Create new member DIE.
1022DIE *DwarfDebug::createMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001023 DIE *MemberDie = new DIE(DT.getTag());
Devang Patelaaf012e2009-09-29 18:40:58 +00001024 if (const char *Name = DT.getName())
Devang Patelc50078e2009-11-21 02:48:08 +00001025 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001026
Devang Patelc50078e2009-11-21 02:48:08 +00001027 addType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001028
Devang Patelc50078e2009-11-21 02:48:08 +00001029 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001030
Devang Patel7d9fe582009-11-04 22:06:12 +00001031 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001032 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001033
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001034 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001035 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001036
1037 if (Size != FieldSize) {
1038 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001039 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1040 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001041
1042 uint64_t Offset = DT.getOffsetInBits();
1043 uint64_t FieldOffset = Offset;
1044 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1045 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1046 FieldOffset = (HiMark - FieldSize);
1047 Offset -= FieldOffset;
1048
1049 // Maybe we need to work from the other end.
1050 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001051 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001052
Devang Patel7d9fe582009-11-04 22:06:12 +00001053 // Here WD_AT_data_member_location points to the anonymous
1054 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001055 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001056
1057 } else
1058 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001059 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001060
Devang Patelc50078e2009-11-21 02:48:08 +00001061 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001062
1063 if (DT.isProtected())
Devang Patelc50078e2009-11-21 02:48:08 +00001064 addUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001065 dwarf::DW_ACCESS_protected);
1066 else if (DT.isPrivate())
Devang Patelc50078e2009-11-21 02:48:08 +00001067 addUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001068 dwarf::DW_ACCESS_private);
1069
1070 return MemberDie;
1071}
1072
Devang Patelc50078e2009-11-21 02:48:08 +00001073/// createSubprogramDIE - Create new DIE using SP.
1074DIE *DwarfDebug::createSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001075 const DISubprogram &SP,
Bill Wendling87307862009-05-18 22:02:36 +00001076 bool IsConstructor,
1077 bool IsInlined) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001078 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
1079
Devang Patelaaf012e2009-09-29 18:40:58 +00001080 const char * Name = SP.getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001081 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001082
Devang Patelaaf012e2009-09-29 18:40:58 +00001083 const char *LinkageName = SP.getLinkageName();
1084 if (LinkageName) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001085 // Skip special LLVM prefix that is used to inform the asm printer to not
1086 // emit usual symbol prefix before the symbol name. This happens for
1087 // Objective-C symbol names and symbol whose name is replaced using GCC's
1088 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00001089 if (LinkageName[0] == 1)
1090 LinkageName = &LinkageName[1];
Devang Patelc50078e2009-11-21 02:48:08 +00001091 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelbd760a52009-07-14 00:55:28 +00001092 LinkageName);
Devang Patel76031e82009-07-16 01:01:22 +00001093 }
Devang Patelc50078e2009-11-21 02:48:08 +00001094 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001095
1096 DICompositeType SPTy = SP.getType();
1097 DIArray Args = SPTy.getTypeArray();
1098
1099 // Add prototyped tag, if C or ObjC.
1100 unsigned Lang = SP.getCompileUnit().getLanguage();
1101 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1102 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001103 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001104
1105 // Add Return Type.
1106 unsigned SPTag = SPTy.getTag();
1107 if (!IsConstructor) {
1108 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001109 addType(DW_Unit, SPDie, SPTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001110 else
Devang Patelc50078e2009-11-21 02:48:08 +00001111 addType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001112 }
1113
1114 if (!SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001115 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001116
1117 // Add arguments. Do not add arguments for subprogram definition. They will
1118 // be handled through RecordVariable.
1119 if (SPTag == dwarf::DW_TAG_subroutine_type)
1120 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1121 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc50078e2009-11-21 02:48:08 +00001122 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
1123 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1124 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001125 }
1126 }
1127
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001128 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Pateld90672c2009-11-20 21:37:22 +00001129 DW_Unit->insertDIE(SP.getNode(), SPDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001130 return SPDie;
1131}
1132
Devang Patelc50078e2009-11-21 02:48:08 +00001133/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001134///
Devang Patelc50078e2009-11-21 02:48:08 +00001135CompileUnit &DwarfDebug::findCompileUnit(DICompileUnit Unit) const {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001136 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patel15e723d2009-08-28 23:24:31 +00001137 CompileUnitMap.find(Unit.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001138 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1139 return *I->second;
1140}
1141
Devang Patelc50078e2009-11-21 02:48:08 +00001142/// createDbgScopeVariable - Create a new scope variable.
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001143///
Devang Patelc50078e2009-11-21 02:48:08 +00001144DIE *DwarfDebug::createDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001145 // Get the descriptor.
1146 const DIVariable &VD = DV->getVariable();
Devang Patel11ac7e72009-11-03 18:30:27 +00001147 const char *Name = VD.getName();
1148 if (!Name)
1149 return NULL;
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001150
1151 // Translate tag to proper Dwarf tag. The result variable is dropped for
1152 // now.
1153 unsigned Tag;
1154 switch (VD.getTag()) {
1155 case dwarf::DW_TAG_return_variable:
1156 return NULL;
1157 case dwarf::DW_TAG_arg_variable:
1158 Tag = dwarf::DW_TAG_formal_parameter;
1159 break;
1160 case dwarf::DW_TAG_auto_variable: // fall thru
1161 default:
1162 Tag = dwarf::DW_TAG_variable;
1163 break;
1164 }
1165
1166 // Define variable debug information entry.
1167 DIE *VariableDie = new DIE(Tag);
Devang Patelc50078e2009-11-21 02:48:08 +00001168 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001169
1170 // Add source line info if available.
Devang Patelc50078e2009-11-21 02:48:08 +00001171 addSourceLine(VariableDie, &VD);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001172
1173 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001174 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001175 // addresses instead.
Caroline Tice248d5572009-08-31 21:19:37 +00001176 if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001177 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Caroline Tice248d5572009-08-31 21:19:37 +00001178 else
Devang Patelc50078e2009-11-21 02:48:08 +00001179 addType(Unit, VariableDie, VD.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001180
1181 // Add variable address.
Devang Patel90a0fe32009-11-10 23:06:00 +00001182 // Variables for abstract instances of inlined functions don't get a
1183 // location.
1184 MachineLocation Location;
Jim Grosbach587d4882009-11-22 20:14:00 +00001185 unsigned FrameReg;
1186 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1187 Location.set(FrameReg, Offset);
Jim Grosbach652b7432009-11-21 23:12:12 +00001188
1189
Devang Patel90a0fe32009-11-10 23:06:00 +00001190 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001191 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001192 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001193 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001194 else
Devang Patelc50078e2009-11-21 02:48:08 +00001195 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001196
1197 return VariableDie;
1198}
1199
Devang Patel90a0fe32009-11-10 23:06:00 +00001200/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1201/// Initialize scope and update scope hierarchy.
1202DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1203 MDNode *InlinedAt) {
1204 assert (N && "Invalid Scope encoding!");
1205 assert (MI && "Missing machine instruction!");
1206 bool GetConcreteScope = (MI && InlinedAt);
1207
1208 DbgScope *NScope = NULL;
1209
1210 if (InlinedAt)
1211 NScope = DbgScopeMap.lookup(InlinedAt);
1212 else
1213 NScope = DbgScopeMap.lookup(N);
1214 assert (NScope && "Unable to find working scope!");
1215
1216 if (NScope->getFirstInsn())
1217 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001218
1219 DbgScope *Parent = NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001220 if (GetConcreteScope) {
Devang Pateldd7bb432009-10-14 21:08:09 +00001221 DILocation IL(InlinedAt);
Jim Grosbach652b7432009-11-21 23:12:12 +00001222 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel90a0fe32009-11-10 23:06:00 +00001223 IL.getOrigLocation().getNode());
1224 assert (Parent && "Unable to find Parent scope!");
1225 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001226 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001227 } else if (DIDescriptor(N).isLexicalBlock()) {
1228 DILexicalBlock DB(N);
1229 if (!DB.getContext().isNull()) {
1230 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1231 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001232 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001233 }
Devang Pateldd7bb432009-10-14 21:08:09 +00001234 }
Devang Patel6a260102009-10-01 20:31:14 +00001235
Devang Patelf5278f22009-10-27 20:47:17 +00001236 NScope->setFirstInsn(MI);
Devang Patel6a260102009-10-01 20:31:14 +00001237
Devang Patel90a0fe32009-11-10 23:06:00 +00001238 if (!Parent && !InlinedAt) {
Devang Patelce8986f2009-11-11 00:31:36 +00001239 StringRef SPName = DISubprogram(N).getLinkageName();
1240 if (SPName == MF->getFunction()->getName())
1241 CurrentFnDbgScope = NScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00001242 }
Devang Patel6a260102009-10-01 20:31:14 +00001243
Devang Patel90a0fe32009-11-10 23:06:00 +00001244 if (GetConcreteScope) {
1245 ConcreteScopes[InlinedAt] = NScope;
1246 getOrCreateAbstractScope(N);
1247 }
1248
Devang Patelf5278f22009-10-27 20:47:17 +00001249 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001250}
1251
Devang Patel90a0fe32009-11-10 23:06:00 +00001252DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1253 assert (N && "Invalid Scope encoding!");
1254
1255 DbgScope *AScope = AbstractScopes.lookup(N);
1256 if (AScope)
1257 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001258
Devang Patel90a0fe32009-11-10 23:06:00 +00001259 DbgScope *Parent = NULL;
1260
1261 DIDescriptor Scope(N);
1262 if (Scope.isLexicalBlock()) {
1263 DILexicalBlock DB(N);
1264 DIDescriptor ParentDesc = DB.getContext();
1265 if (!ParentDesc.isNull())
1266 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1267 }
1268
1269 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1270
1271 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001272 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001273 AScope->setAbstractScope();
1274 AbstractScopes[N] = AScope;
1275 if (DIDescriptor(N).isSubprogram())
1276 AbstractScopesList.push_back(AScope);
1277 return AScope;
1278}
Devang Patel6a260102009-10-01 20:31:14 +00001279
Devang Patel90a0fe32009-11-10 23:06:00 +00001280static DISubprogram getDISubprogram(MDNode *N) {
1281
1282 DIDescriptor D(N);
1283 if (D.isNull())
1284 return DISubprogram();
1285
Jim Grosbach652b7432009-11-21 23:12:12 +00001286 if (D.isCompileUnit())
Devang Patel90a0fe32009-11-10 23:06:00 +00001287 return DISubprogram();
1288
1289 if (D.isSubprogram())
1290 return DISubprogram(N);
1291
1292 if (D.isLexicalBlock())
1293 return getDISubprogram(DILexicalBlock(N).getContext().getNode());
1294
1295 llvm_unreachable("Unexpected Descriptor!");
1296}
1297
Jim Grosbach652b7432009-11-21 23:12:12 +00001298/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-11-21 02:48:08 +00001299/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1300/// If there are global variables in this scope then create and insert
1301/// DIEs for these variables.
1302DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001303
Devang Pateld90672c2009-11-20 21:37:22 +00001304 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel90a0fe32009-11-10 23:06:00 +00001305 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001306 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001307 DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001308 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001309 DWLabel("func_end", SubprogramCount));
1310 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patelc50078e2009-11-21 02:48:08 +00001311 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach652b7432009-11-21 23:12:12 +00001312
Devang Patel90a0fe32009-11-10 23:06:00 +00001313 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001314 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001315
1316 // If there are global variables at this scope then add their dies.
Jim Grosbach652b7432009-11-21 23:12:12 +00001317 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
Devang Patel90a0fe32009-11-10 23:06:00 +00001318 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1319 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1320 if (!N) continue;
1321 DIGlobalVariable GV(N);
1322 if (GV.getContext().getNode() == SPNode) {
Devang Patelc50078e2009-11-21 02:48:08 +00001323 DIE *ScopedGVDie = createGlobalVariableDIE(ModuleCU, GV);
Devang Patelbad42262009-11-10 23:20:04 +00001324 if (ScopedGVDie)
Devang Patelc50078e2009-11-21 02:48:08 +00001325 SPDie->addChild(ScopedGVDie);
Devang Patel90a0fe32009-11-10 23:06:00 +00001326 }
1327 }
1328 return SPDie;
1329}
1330
Jim Grosbach652b7432009-11-21 23:12:12 +00001331/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001332/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1333DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001334 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1335 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1336
1337 // Ignore empty scopes.
1338 if (StartID == EndID && StartID != 0)
1339 return NULL;
1340
1341 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1342 if (Scope->isAbstractScope())
1343 return ScopeDIE;
1344
Devang Patelc50078e2009-11-21 02:48:08 +00001345 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach652b7432009-11-21 23:12:12 +00001346 StartID ?
1347 DWLabel("label", StartID)
Devang Patel90a0fe32009-11-10 23:06:00 +00001348 : DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001349 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach652b7432009-11-21 23:12:12 +00001350 EndID ?
1351 DWLabel("label", EndID)
Devang Patel90a0fe32009-11-10 23:06:00 +00001352 : DWLabel("func_end", SubprogramCount));
1353
1354
1355
1356 return ScopeDIE;
1357}
1358
Devang Patelc50078e2009-11-21 02:48:08 +00001359/// constructInlinedScopeDIE - This scope represents inlined body of
1360/// a function. Construct DIE to represent this concrete inlined copy
1361/// of the function.
1362DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001363 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1364 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1365 assert (StartID && "Invalid starting label for an inlined scope!");
1366 assert (EndID && "Invalid end label for an inlined scope!");
1367 // Ignore empty scopes.
1368 if (StartID == EndID && StartID != 0)
1369 return NULL;
1370
1371 DIScope DS(Scope->getScopeNode());
1372 if (DS.isNull())
1373 return NULL;
1374 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1375
1376 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001377 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001378 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001379 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001380 dwarf::DW_FORM_ref4, OriginDIE);
1381
Devang Patelc50078e2009-11-21 02:48:08 +00001382 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001383 DWLabel("label", StartID));
Devang Patelc50078e2009-11-21 02:48:08 +00001384 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001385 DWLabel("label", EndID));
1386
1387 InlinedSubprogramDIEs.insert(OriginDIE);
1388
1389 // Track the start label for this inlined function.
1390 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1391 I = InlineInfo.find(InlinedSP.getNode());
1392
1393 if (I == InlineInfo.end()) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001394 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1395 ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001396 InlinedSPNodes.push_back(InlinedSP.getNode());
1397 } else
1398 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1399
1400 StringPool.insert(InlinedSP.getName());
1401 StringPool.insert(InlinedSP.getLinkageName());
1402 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001403 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1404 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001405
1406 return ScopeDIE;
1407}
1408
Devang Patelc50078e2009-11-21 02:48:08 +00001409
1410/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Jim Grosbach652b7432009-11-21 23:12:12 +00001411DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV,
Devang Patel90a0fe32009-11-10 23:06:00 +00001412 DbgScope *Scope, CompileUnit *Unit) {
1413 // Get the descriptor.
1414 const DIVariable &VD = DV->getVariable();
Devang Patelab9a0682009-11-13 02:25:26 +00001415 const char *Name = VD.getName();
1416 if (!Name)
1417 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001418
1419 // Translate tag to proper Dwarf tag. The result variable is dropped for
1420 // now.
1421 unsigned Tag;
1422 switch (VD.getTag()) {
1423 case dwarf::DW_TAG_return_variable:
1424 return NULL;
1425 case dwarf::DW_TAG_arg_variable:
1426 Tag = dwarf::DW_TAG_formal_parameter;
1427 break;
1428 case dwarf::DW_TAG_auto_variable: // fall thru
1429 default:
1430 Tag = dwarf::DW_TAG_variable;
1431 break;
1432 }
1433
1434 // Define variable debug information entry.
1435 DIE *VariableDie = new DIE(Tag);
1436
1437
1438 DIE *AbsDIE = NULL;
1439 if (DbgVariable *AV = DV->getAbstractVariable())
1440 AbsDIE = AV->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001441
Devang Patel90a0fe32009-11-10 23:06:00 +00001442 if (AbsDIE) {
1443 DIScope DS(Scope->getScopeNode());
1444 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001445 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001446 (void) OriginSPDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001447 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1448 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1449 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001450 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001451 dwarf::DW_FORM_ref4, AbsDIE);
1452 }
1453 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001454 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1455 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001456
1457 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001458 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001459 // addresses instead.
1460 if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001461 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001462 else
Devang Patelc50078e2009-11-21 02:48:08 +00001463 addType(Unit, VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001464 }
1465
1466 // Add variable address.
1467 if (!Scope->isAbstractScope()) {
1468 MachineLocation Location;
Jim Grosbach587d4882009-11-22 20:14:00 +00001469 unsigned FrameReg;
1470 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1471 Location.set(FrameReg, Offset);
Jim Grosbach652b7432009-11-21 23:12:12 +00001472
Devang Patel90a0fe32009-11-10 23:06:00 +00001473 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001474 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001475 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001476 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001477 else
Devang Patelc50078e2009-11-21 02:48:08 +00001478 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001479 }
1480 DV->setDIE(VariableDie);
1481 return VariableDie;
1482
1483}
Devang Patelc50078e2009-11-21 02:48:08 +00001484
1485/// constructScopeDIE - Construct a DIE for this scope.
1486DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001487 if (!Scope)
1488 return NULL;
1489 DIScope DS(Scope->getScopeNode());
1490 if (DS.isNull())
1491 return NULL;
1492
1493 DIE *ScopeDIE = NULL;
1494 if (Scope->getInlinedAt())
Devang Patelc50078e2009-11-21 02:48:08 +00001495 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001496 else if (DS.isSubprogram()) {
1497 if (Scope->isAbstractScope())
Devang Pateld90672c2009-11-20 21:37:22 +00001498 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001499 else
Devang Patelc50078e2009-11-21 02:48:08 +00001500 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001501 }
1502 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001503 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001504 if (!ScopeDIE) return NULL;
1505 }
1506
1507 // Add variables to scope.
1508 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1509 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelc50078e2009-11-21 02:48:08 +00001510 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope, ModuleCU);
Jim Grosbach652b7432009-11-21 23:12:12 +00001511 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001512 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001513 }
1514
1515 // Add nested scopes.
1516 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1517 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1518 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001519 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001520 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001521 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001522 }
1523 return ScopeDIE;
1524}
1525
Bill Wendlingf5839192009-05-20 23:19:06 +00001526/// GetOrCreateSourceID - Look up the source id with the given directory and
1527/// source file names. If none currently exists, create a new id and insert it
1528/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1529/// maps as well.
Devang Patelaaf012e2009-09-29 18:40:58 +00001530unsigned DwarfDebug::GetOrCreateSourceID(const char *DirName,
1531 const char *FileName) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001532 unsigned DId;
1533 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1534 if (DI != DirectoryIdMap.end()) {
1535 DId = DI->getValue();
1536 } else {
1537 DId = DirectoryNames.size() + 1;
1538 DirectoryIdMap[DirName] = DId;
1539 DirectoryNames.push_back(DirName);
1540 }
1541
1542 unsigned FId;
1543 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1544 if (FI != SourceFileIdMap.end()) {
1545 FId = FI->getValue();
1546 } else {
1547 FId = SourceFileNames.size() + 1;
1548 SourceFileIdMap[FileName] = FId;
1549 SourceFileNames.push_back(FileName);
1550 }
1551
1552 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1553 SourceIdMap.find(std::make_pair(DId, FId));
1554 if (SI != SourceIdMap.end())
1555 return SI->second;
1556
1557 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1558 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1559 SourceIds.push_back(std::make_pair(DId, FId));
1560
1561 return SrcId;
1562}
1563
Devang Patelc50078e2009-11-21 02:48:08 +00001564void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001565 DICompileUnit DIUnit(N);
Devang Patelaaf012e2009-09-29 18:40:58 +00001566 const char *FN = DIUnit.getFilename();
1567 const char *Dir = DIUnit.getDirectory();
1568 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001569
1570 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelc50078e2009-11-21 02:48:08 +00001571 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf5839192009-05-20 23:19:06 +00001572 DWLabel("section_line", 0), DWLabel("section_line", 0),
1573 false);
Devang Patelc50078e2009-11-21 02:48:08 +00001574 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001575 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001576 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001577 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001578 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001579
Devang Patelaaf012e2009-09-29 18:40:58 +00001580 if (Dir)
Devang Patelc50078e2009-11-21 02:48:08 +00001581 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001582 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001583 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001584
Devang Patelaaf012e2009-09-29 18:40:58 +00001585 if (const char *Flags = DIUnit.getFlags())
Devang Patelc50078e2009-11-21 02:48:08 +00001586 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001587
1588 unsigned RVer = DIUnit.getRunTimeVersion();
1589 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001590 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001591 dwarf::DW_FORM_data1, RVer);
1592
1593 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel5a3d37f2009-06-29 20:45:18 +00001594 if (!ModuleCU && DIUnit.isMain()) {
Devang Patelf97a05a2009-06-29 20:38:13 +00001595 // Use first compile unit marked as isMain as the compile unit
1596 // for this module.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001597 ModuleCU = Unit;
Devang Patelf97a05a2009-06-29 20:38:13 +00001598 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001599
Devang Patel15e723d2009-08-28 23:24:31 +00001600 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf5839192009-05-20 23:19:06 +00001601 CompileUnits.push_back(Unit);
1602}
1603
Devang Patelc50078e2009-11-21 02:48:08 +00001604void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001605 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001606
Devang Patel0c03f062009-09-04 23:59:07 +00001607 // If debug information is malformed then ignore it.
1608 if (DI_GV.Verify() == false)
1609 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001610
1611 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001612 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel166f8432009-06-26 01:49:18 +00001613 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001614
Devang Patelc50078e2009-11-21 02:48:08 +00001615 DIE *VariableDie = createGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf5839192009-05-20 23:19:06 +00001616
Bill Wendlingf5839192009-05-20 23:19:06 +00001617 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001618 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001619
1620 // Add to context owner.
Devang Patelc50078e2009-11-21 02:48:08 +00001621 ModuleCU->getCUDie()->addChild(VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001622
1623 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001624 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel166f8432009-06-26 01:49:18 +00001625 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001626}
1627
Devang Patelc50078e2009-11-21 02:48:08 +00001628void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001629 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001630
1631 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001632 if (ModuleCU->getDIE(N))
Devang Patel166f8432009-06-26 01:49:18 +00001633 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001634
1635 if (!SP.isDefinition())
1636 // This is a method declaration which will be handled while constructing
1637 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001638 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001639
Devang Patelc50078e2009-11-21 02:48:08 +00001640 DIE *SubprogramDie = createSubprogramDIE(ModuleCU, SP);
Bill Wendlingf5839192009-05-20 23:19:06 +00001641
1642 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001643 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001644
1645 // Add to context owner.
Devang Patelc50078e2009-11-21 02:48:08 +00001646 ModuleCU->getCUDie()->addChild(SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001647
1648 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001649 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel166f8432009-06-26 01:49:18 +00001650 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001651}
1652
Devang Patelc50078e2009-11-21 02:48:08 +00001653/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001654/// content. Create global DIEs and emit initial debug info sections.
1655/// This is inovked by the target AsmPrinter.
Devang Patelc50078e2009-11-21 02:48:08 +00001656void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel59a1d422009-06-25 22:36:02 +00001657 this->M = M;
1658
Bill Wendlingf5839192009-05-20 23:19:06 +00001659 if (TimePassesIsEnabled)
1660 DebugTimer->startTimer();
1661
Devang Patel1b4d6832009-11-11 19:55:08 +00001662 if (!MAI->doesSupportDebugInformation())
1663 return;
1664
Devang Patelfda766d2009-07-30 18:56:46 +00001665 DebugInfoFinder DbgFinder;
1666 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001667
Bill Wendlingf5839192009-05-20 23:19:06 +00001668 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001669 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1670 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001671 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001672
1673 if (CompileUnits.empty()) {
1674 if (TimePassesIsEnabled)
1675 DebugTimer->stopTimer();
1676
1677 return;
1678 }
1679
Devang Patelf97a05a2009-06-29 20:38:13 +00001680 // If main compile unit for this module is not seen than randomly
1681 // select first compile unit.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001682 if (!ModuleCU)
1683 ModuleCU = CompileUnits[0];
Devang Patelf97a05a2009-06-29 20:38:13 +00001684
Devang Patel166f8432009-06-26 01:49:18 +00001685 // Create DIEs for each of the externally visible global variables.
Devang Patelfda766d2009-07-30 18:56:46 +00001686 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patel695c8b02009-10-05 23:40:42 +00001687 E = DbgFinder.global_variable_end(); I != E; ++I) {
1688 DIGlobalVariable GV(*I);
1689 if (GV.getContext().getNode() != GV.getCompileUnit().getNode())
1690 ScopedGVs.push_back(*I);
1691 else
Devang Patelc50078e2009-11-21 02:48:08 +00001692 constructGlobalVariableDIE(*I);
Devang Patel695c8b02009-10-05 23:40:42 +00001693 }
Devang Patel166f8432009-06-26 01:49:18 +00001694
Devang Patel90a0fe32009-11-10 23:06:00 +00001695 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001696 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1697 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001698 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001699
Bill Wendlingf5839192009-05-20 23:19:06 +00001700 MMI = mmi;
1701 shouldEmit = true;
1702 MMI->setDebugInfoAvailability(true);
1703
1704 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001705 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001706
1707 // Print out .file directives to specify files for .loc directives. These are
1708 // printed out early so that they precede any .loc directives.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001709 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001710 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1711 // Remember source id starts at 1.
1712 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1713 sys::Path FullPath(getSourceDirectoryName(Id.first));
1714 bool AppendOk =
1715 FullPath.appendComponent(getSourceFileName(Id.second));
1716 assert(AppendOk && "Could not append filename to directory!");
1717 AppendOk = false;
Chris Lattnerb1aa85b2009-08-23 22:45:37 +00001718 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001719 Asm->EOL();
1720 }
1721 }
1722
1723 // Emit initial sections
Devang Patelc50078e2009-11-21 02:48:08 +00001724 emitInitial();
Bill Wendlingf5839192009-05-20 23:19:06 +00001725
1726 if (TimePassesIsEnabled)
1727 DebugTimer->stopTimer();
1728}
1729
Devang Patelc50078e2009-11-21 02:48:08 +00001730/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001731///
Devang Patelc50078e2009-11-21 02:48:08 +00001732void DwarfDebug::endModule() {
Devang Patel95d477e2009-10-06 00:03:14 +00001733 if (!ModuleCU)
Bill Wendlingf5839192009-05-20 23:19:06 +00001734 return;
1735
1736 if (TimePassesIsEnabled)
1737 DebugTimer->startTimer();
1738
Devang Patel90a0fe32009-11-10 23:06:00 +00001739 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1740 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1741 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1742 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001743 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001744 }
1745
Bill Wendlingf5839192009-05-20 23:19:06 +00001746 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001747 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001748 EmitLabel("text_end", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00001749 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001750 EmitLabel("data_end", 0);
1751
1752 // End text sections.
1753 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001754 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf5839192009-05-20 23:19:06 +00001755 EmitLabel("section_end", i);
1756 }
1757
1758 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001759 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001760
1761 // Emit function debug frame information
1762 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1763 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001764 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001765
1766 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001767 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001768
1769 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001770 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001771
1772 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00001773 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00001774
1775 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00001776 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00001777
1778 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00001779 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00001780
1781 // Emit info into a debug str section.
Devang Patelc50078e2009-11-21 02:48:08 +00001782 emitDebugStr();
Bill Wendlingf5839192009-05-20 23:19:06 +00001783
1784 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00001785 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00001786
1787 // Emit info into a debug aranges section.
1788 EmitDebugARanges();
1789
1790 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00001791 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00001792
1793 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00001794 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001795
1796 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00001797 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001798
1799 if (TimePassesIsEnabled)
1800 DebugTimer->stopTimer();
1801}
1802
Devang Patel90a0fe32009-11-10 23:06:00 +00001803/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00001804DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1805 unsigned FrameIdx,
Devang Patel90a0fe32009-11-10 23:06:00 +00001806 DILocation &ScopeLoc) {
1807
1808 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1809 if (AbsDbgVariable)
1810 return AbsDbgVariable;
1811
1812 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1813 if (!Scope)
1814 return NULL;
1815
1816 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patelc50078e2009-11-21 02:48:08 +00001817 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00001818 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1819 return AbsDbgVariable;
1820}
1821
Devang Patelc50078e2009-11-21 02:48:08 +00001822/// collectVariableInfo - Populate DbgScope entries with variables' info.
1823void DwarfDebug::collectVariableInfo() {
Devang Patel40c80212009-10-09 22:42:28 +00001824 if (!MMI) return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001825
Devang Patel84139992009-10-06 01:26:37 +00001826 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1827 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1828 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel40c80212009-10-09 22:42:28 +00001829 MetadataBase *MB = VI->first;
1830 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel90a0fe32009-11-10 23:06:00 +00001831 if (!Var) continue;
Devang Patel6882dff2009-10-08 18:48:03 +00001832 DIVariable DV (Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00001833 std::pair< unsigned, MDNode *> VP = VI->second;
1834 DILocation ScopeLoc(VP.second);
1835
1836 DbgScope *Scope =
1837 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1838 if (!Scope)
Jim Grosbach652b7432009-11-21 23:12:12 +00001839 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelbad42262009-11-10 23:20:04 +00001840 // If variable scope is not found then skip this variable.
1841 if (!Scope)
1842 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00001843
1844 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patelc50078e2009-11-21 02:48:08 +00001845 Scope->addVariable(RegVar);
Jim Grosbachb23f2422009-11-22 19:20:36 +00001846 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1847 ScopeLoc))
Devang Patel90a0fe32009-11-10 23:06:00 +00001848 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patel84139992009-10-06 01:26:37 +00001849 }
1850}
1851
Devang Patelc50078e2009-11-21 02:48:08 +00001852/// beginScope - Process beginning of a scope starting at Label.
1853void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel393a46d2009-10-06 01:50:42 +00001854 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1855 if (I == DbgScopeBeginMap.end())
1856 return;
Dan Gohman8d34f972009-11-23 21:30:55 +00001857 ScopeVector &SD = I->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00001858 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001859 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00001860 (*SDI)->setStartLabelID(Label);
1861}
1862
Devang Patelc50078e2009-11-21 02:48:08 +00001863/// endScope - Process end of a scope.
1864void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel393a46d2009-10-06 01:50:42 +00001865 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patelf4348892009-10-06 03:15:38 +00001866 if (I == DbgScopeEndMap.end())
Devang Patel393a46d2009-10-06 01:50:42 +00001867 return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001868
1869 unsigned Label = MMI->NextLabelID();
1870 Asm->printLabel(Label);
1871
Devang Patel393a46d2009-10-06 01:50:42 +00001872 SmallVector<DbgScope *, 2> &SD = I->second;
1873 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001874 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00001875 (*SDI)->setEndLabelID(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00001876 return;
1877}
1878
1879/// createDbgScope - Create DbgScope for the scope.
1880void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1881
1882 if (!InlinedAt) {
1883 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1884 if (WScope)
1885 return;
1886 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1887 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach652b7432009-11-21 23:12:12 +00001888 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel53addbf2009-11-11 00:18:40 +00001889 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel90a0fe32009-11-10 23:06:00 +00001890 return;
1891 }
1892
1893 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1894 if (WScope)
1895 return;
1896
1897 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1898 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
1899 DILocation DL(InlinedAt);
1900 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel393a46d2009-10-06 01:50:42 +00001901}
1902
Devang Patelc50078e2009-11-21 02:48:08 +00001903/// extractScopeInformation - Scan machine instructions in this function
Devang Patel6a260102009-10-01 20:31:14 +00001904/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patelc50078e2009-11-21 02:48:08 +00001905bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patel6a260102009-10-01 20:31:14 +00001906 // If scope information was extracted using .dbg intrinsics then there is not
1907 // any need to extract these information by scanning each instruction.
1908 if (!DbgScopeMap.empty())
1909 return false;
1910
Devang Patel90a0fe32009-11-10 23:06:00 +00001911 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6a260102009-10-01 20:31:14 +00001912 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1913 I != E; ++I) {
1914 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1915 II != IE; ++II) {
1916 const MachineInstr *MInsn = II;
1917 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00001918 if (DL.isUnknown()) continue;
Devang Patel6a260102009-10-01 20:31:14 +00001919 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel90a0fe32009-11-10 23:06:00 +00001920 if (!DLT.Scope) continue;
Devang Patel6a260102009-10-01 20:31:14 +00001921 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00001922 // other scopes, create one DbgScope now. This will be translated
Devang Patel6a260102009-10-01 20:31:14 +00001923 // into a scope DIE at the end.
Devang Patel90a0fe32009-11-10 23:06:00 +00001924 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1925 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
1926 }
1927 }
1928
1929
1930 // Build scope hierarchy using working set of scopes.
1931 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1932 I != E; ++I) {
1933 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1934 II != IE; ++II) {
1935 const MachineInstr *MInsn = II;
1936 DebugLoc DL = MInsn->getDebugLoc();
1937 if (DL.isUnknown()) continue;
1938 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
1939 if (!DLT.Scope) continue;
1940 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00001941 // other scopes, create one DbgScope now. This will be translated
Devang Patel90a0fe32009-11-10 23:06:00 +00001942 // into a scope DIE at the end.
1943 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1944 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
1945 Scope->setLastInsn(MInsn);
Devang Patel6a260102009-10-01 20:31:14 +00001946 }
1947 }
1948
1949 // If a scope's last instruction is not set then use its child scope's
1950 // last instruction as this scope's last instrunction.
Devang Patelf5278f22009-10-27 20:47:17 +00001951 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00001952 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001953 if (DI->second->isAbstractScope())
1954 continue;
Devang Patel6a260102009-10-01 20:31:14 +00001955 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patelc50078e2009-11-21 02:48:08 +00001956 DI->second->fixInstructionMarkers();
Devang Patel6a260102009-10-01 20:31:14 +00001957 assert (DI->second->getLastInsn() && "Invalid last instruction!");
1958 }
1959
1960 // Each scope has first instruction and last instruction to mark beginning
1961 // and end of a scope respectively. Create an inverse map that list scopes
1962 // starts (and ends) with an instruction. One instruction may start (or end)
1963 // multiple scopes.
Devang Patelf5278f22009-10-27 20:47:17 +00001964 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00001965 DE = DbgScopeMap.end(); DI != DE; ++DI) {
1966 DbgScope *S = DI->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00001967 if (S->isAbstractScope())
1968 continue;
Devang Patel6a260102009-10-01 20:31:14 +00001969 const MachineInstr *MI = S->getFirstInsn();
1970 assert (MI && "DbgScope does not have first instruction!");
1971
1972 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
1973 if (IDI != DbgScopeBeginMap.end())
1974 IDI->second.push_back(S);
1975 else
Devang Patel90a0fe32009-11-10 23:06:00 +00001976 DbgScopeBeginMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00001977
1978 MI = S->getLastInsn();
1979 assert (MI && "DbgScope does not have last instruction!");
1980 IDI = DbgScopeEndMap.find(MI);
1981 if (IDI != DbgScopeEndMap.end())
1982 IDI->second.push_back(S);
1983 else
Devang Patel90a0fe32009-11-10 23:06:00 +00001984 DbgScopeEndMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00001985 }
1986
1987 return !DbgScopeMap.empty();
1988}
1989
Devang Patelc50078e2009-11-21 02:48:08 +00001990/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00001991/// emitted immediately after the function entry point.
Devang Patelc50078e2009-11-21 02:48:08 +00001992void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001993 this->MF = MF;
1994
1995 if (!ShouldEmitDwarfDebug()) return;
1996
1997 if (TimePassesIsEnabled)
1998 DebugTimer->startTimer();
1999
Devang Patelc50078e2009-11-21 02:48:08 +00002000 if (!extractScopeInformation(MF))
Devang Patel0feae422009-10-06 18:37:31 +00002001 return;
Devang Patelc50078e2009-11-21 02:48:08 +00002002
2003 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002004
Bill Wendlingf5839192009-05-20 23:19:06 +00002005 // Begin accumulating function debug information.
2006 MMI->BeginFunction(MF);
2007
2008 // Assumes in correct section after the entry point.
2009 EmitLabel("func_begin", ++SubprogramCount);
2010
2011 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2012 // function.
Devang Patel40c80212009-10-09 22:42:28 +00002013 DebugLoc FDL = MF->getDefaultDebugLoc();
2014 if (!FDL.isUnknown()) {
2015 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2016 unsigned LabelID = 0;
Devang Patelfc1df342009-10-13 23:28:53 +00002017 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002018 if (!SP.isNull())
Devang Patelc50078e2009-11-21 02:48:08 +00002019 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002020 else
Devang Patelc50078e2009-11-21 02:48:08 +00002021 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002022 Asm->printLabel(LabelID);
2023 O << '\n';
Bill Wendlingf5839192009-05-20 23:19:06 +00002024 }
Bill Wendlingf5839192009-05-20 23:19:06 +00002025 if (TimePassesIsEnabled)
2026 DebugTimer->stopTimer();
2027}
2028
Devang Patelc50078e2009-11-21 02:48:08 +00002029/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002030///
Devang Patelc50078e2009-11-21 02:48:08 +00002031void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002032 if (!ShouldEmitDwarfDebug()) return;
2033
2034 if (TimePassesIsEnabled)
2035 DebugTimer->startTimer();
2036
Devang Patel40c80212009-10-09 22:42:28 +00002037 if (DbgScopeMap.empty())
2038 return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002039
Bill Wendlingf5839192009-05-20 23:19:06 +00002040 // Define end label for subprogram.
2041 EmitLabel("func_end", SubprogramCount);
2042
2043 // Get function line info.
2044 if (!Lines.empty()) {
2045 // Get section line info.
Chris Lattnerebd055c2009-08-03 23:20:21 +00002046 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00002047 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2048 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2049 // Append the function info to section info.
2050 SectionLineInfos.insert(SectionLineInfos.end(),
2051 Lines.begin(), Lines.end());
2052 }
2053
Devang Patel90a0fe32009-11-10 23:06:00 +00002054 // Construct abstract scopes.
2055 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach652b7432009-11-21 23:12:12 +00002056 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patelc50078e2009-11-21 02:48:08 +00002057 constructScopeDIE(*AI);
Bill Wendlingf5839192009-05-20 23:19:06 +00002058
Devang Patelc50078e2009-11-21 02:48:08 +00002059 constructScopeDIE(CurrentFnDbgScope);
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002060
Bill Wendlingf5839192009-05-20 23:19:06 +00002061 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2062 MMI->getFrameMoves()));
2063
2064 // Clear debug info
Devang Patel90a0fe32009-11-10 23:06:00 +00002065 if (CurrentFnDbgScope) {
2066 CurrentFnDbgScope = NULL;
Bill Wendlingf5839192009-05-20 23:19:06 +00002067 DbgScopeMap.clear();
Devang Patel6a260102009-10-01 20:31:14 +00002068 DbgScopeBeginMap.clear();
2069 DbgScopeEndMap.clear();
Devang Patel90a0fe32009-11-10 23:06:00 +00002070 ConcreteScopes.clear();
2071 AbstractScopesList.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002072 }
2073
2074 Lines.clear();
2075
2076 if (TimePassesIsEnabled)
2077 DebugTimer->stopTimer();
2078}
2079
Devang Patelc50078e2009-11-21 02:48:08 +00002080/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf5839192009-05-20 23:19:06 +00002081/// label. Returns a unique label ID used to generate a label and provide
2082/// correspondence to the source line list.
Jim Grosbach652b7432009-11-21 23:12:12 +00002083unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel946d0ae2009-10-05 18:03:19 +00002084 MDNode *S) {
Devang Patel15e723d2009-08-28 23:24:31 +00002085 if (!MMI)
2086 return 0;
2087
Bill Wendlingf5839192009-05-20 23:19:06 +00002088 if (TimePassesIsEnabled)
2089 DebugTimer->startTimer();
2090
Devang Patel946d0ae2009-10-05 18:03:19 +00002091 const char *Dir = NULL;
2092 const char *Fn = NULL;
2093
2094 DIDescriptor Scope(S);
2095 if (Scope.isCompileUnit()) {
2096 DICompileUnit CU(S);
2097 Dir = CU.getDirectory();
2098 Fn = CU.getFilename();
2099 } else if (Scope.isSubprogram()) {
2100 DISubprogram SP(S);
2101 Dir = SP.getDirectory();
2102 Fn = SP.getFilename();
2103 } else if (Scope.isLexicalBlock()) {
2104 DILexicalBlock DB(S);
2105 Dir = DB.getDirectory();
2106 Fn = DB.getFilename();
2107 } else
2108 assert (0 && "Unexpected scope info");
2109
2110 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf5839192009-05-20 23:19:06 +00002111 unsigned ID = MMI->NextLabelID();
2112 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2113
2114 if (TimePassesIsEnabled)
2115 DebugTimer->stopTimer();
2116
2117 return ID;
2118}
2119
2120/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2121/// timed. Look up the source id with the given directory and source file
2122/// names. If none currently exists, create a new id and insert it in the
2123/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2124/// well.
2125unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2126 const std::string &FileName) {
2127 if (TimePassesIsEnabled)
2128 DebugTimer->startTimer();
2129
Devang Patelaaf012e2009-09-29 18:40:58 +00002130 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf5839192009-05-20 23:19:06 +00002131
2132 if (TimePassesIsEnabled)
2133 DebugTimer->stopTimer();
2134
2135 return SrcId;
2136}
2137
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002138//===----------------------------------------------------------------------===//
2139// Emit Methods
2140//===----------------------------------------------------------------------===//
2141
Devang Patelc50078e2009-11-21 02:48:08 +00002142/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002143///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002144unsigned
2145DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002146 // Get the children.
2147 const std::vector<DIE *> &Children = Die->getChildren();
2148
2149 // If not last sibling and has children then add sibling offset attribute.
Devang Patelc50078e2009-11-21 02:48:08 +00002150 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002151
2152 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002153 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002154
2155 // Get the abbreviation for this DIE.
2156 unsigned AbbrevNumber = Die->getAbbrevNumber();
2157 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2158
2159 // Set DIE offset
2160 Die->setOffset(Offset);
2161
2162 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002163 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002164
2165 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2166 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2167
2168 // Size the DIE attribute values.
2169 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2170 // Size attribute value.
2171 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2172
2173 // Size the DIE children if any.
2174 if (!Children.empty()) {
2175 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2176 "Children flag not set");
2177
2178 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002179 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002180
2181 // End of children marker.
2182 Offset += sizeof(int8_t);
2183 }
2184
2185 Die->setSize(Offset - Die->getOffset());
2186 return Offset;
2187}
2188
Devang Patelc50078e2009-11-21 02:48:08 +00002189/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002190///
Devang Patelc50078e2009-11-21 02:48:08 +00002191void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002192 // Compute size of compile unit header.
2193 static unsigned Offset =
2194 sizeof(int32_t) + // Length of Compilation Unit Info
2195 sizeof(int16_t) + // DWARF version number
2196 sizeof(int32_t) + // Offset Into Abbrev. Section
2197 sizeof(int8_t); // Pointer Size (in bytes)
2198
Devang Patelc50078e2009-11-21 02:48:08 +00002199 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel5a3d37f2009-06-29 20:45:18 +00002200 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling55fccda2009-05-20 23:21:38 +00002201}
2202
Devang Patelc50078e2009-11-21 02:48:08 +00002203/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling55fccda2009-05-20 23:21:38 +00002204/// tools to recognize the object file contains Dwarf information.
Devang Patelc50078e2009-11-21 02:48:08 +00002205void DwarfDebug::emitInitial() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002206 // Check to see if we already emitted intial headers.
2207 if (didInitial) return;
2208 didInitial = true;
2209
Chris Lattner73266f92009-08-19 05:49:37 +00002210 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002211
Bill Wendling55fccda2009-05-20 23:21:38 +00002212 // Dwarf sections base addresses.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002213 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner73266f92009-08-19 05:49:37 +00002214 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002215 EmitLabel("section_debug_frame", 0);
2216 }
2217
Chris Lattner73266f92009-08-19 05:49:37 +00002218 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002219 EmitLabel("section_info", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002220 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002221 EmitLabel("section_abbrev", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002222 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002223 EmitLabel("section_aranges", 0);
2224
Chris Lattner73266f92009-08-19 05:49:37 +00002225 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2226 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling55fccda2009-05-20 23:21:38 +00002227 EmitLabel("section_macinfo", 0);
2228 }
2229
Chris Lattner73266f92009-08-19 05:49:37 +00002230 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002231 EmitLabel("section_line", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002232 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002233 EmitLabel("section_loc", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002234 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002235 EmitLabel("section_pubnames", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002236 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002237 EmitLabel("section_str", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002238 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002239 EmitLabel("section_ranges", 0);
2240
Chris Lattner73266f92009-08-19 05:49:37 +00002241 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002242 EmitLabel("text_begin", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002243 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002244 EmitLabel("data_begin", 0);
2245}
2246
Devang Patelc50078e2009-11-21 02:48:08 +00002247/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002248///
Devang Patelc50078e2009-11-21 02:48:08 +00002249void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002250 // Get the abbreviation for this DIE.
2251 unsigned AbbrevNumber = Die->getAbbrevNumber();
2252 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2253
2254 Asm->EOL();
2255
2256 // Emit the code (index) for the abbreviation.
2257 Asm->EmitULEB128Bytes(AbbrevNumber);
2258
2259 if (Asm->isVerbose())
2260 Asm->EOL(std::string("Abbrev [" +
2261 utostr(AbbrevNumber) +
2262 "] 0x" + utohexstr(Die->getOffset()) +
2263 ":0x" + utohexstr(Die->getSize()) + " " +
2264 dwarf::TagString(Abbrev->getTag())));
2265 else
2266 Asm->EOL();
2267
2268 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2269 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2270
2271 // Emit the DIE attribute values.
2272 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2273 unsigned Attr = AbbrevData[i].getAttribute();
2274 unsigned Form = AbbrevData[i].getForm();
2275 assert(Form && "Too many attributes for DIE (check abbreviation)");
2276
2277 switch (Attr) {
2278 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002279 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002280 break;
2281 case dwarf::DW_AT_abstract_origin: {
2282 DIEEntry *E = cast<DIEEntry>(Values[i]);
2283 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002284 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002285 Asm->EmitInt32(Addr);
2286 break;
2287 }
2288 default:
2289 // Emit an attribute using the defined form.
2290 Values[i]->EmitValue(this, Form);
2291 break;
2292 }
2293
2294 Asm->EOL(dwarf::AttributeString(Attr));
2295 }
2296
2297 // Emit the DIE children if any.
2298 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2299 const std::vector<DIE *> &Children = Die->getChildren();
2300
2301 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002302 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002303
2304 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2305 }
2306}
2307
Devang Patelc50078e2009-11-21 02:48:08 +00002308/// emitDebugInfo / emitDebugInfoPerCU - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002309///
Devang Patelc50078e2009-11-21 02:48:08 +00002310void DwarfDebug::emitDebugInfoPerCU(CompileUnit *Unit) {
Devang Pateld90672c2009-11-20 21:37:22 +00002311 DIE *Die = Unit->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002312
2313 // Emit the compile units header.
2314 EmitLabel("info_begin", Unit->getID());
2315
2316 // Emit size of content not including length itself
2317 unsigned ContentSize = Die->getSize() +
2318 sizeof(int16_t) + // DWARF version number
2319 sizeof(int32_t) + // Offset Into Abbrev. Section
2320 sizeof(int8_t) + // Pointer Size (in bytes)
2321 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2322
2323 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2324 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2325 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2326 Asm->EOL("Offset Into Abbrev. Section");
2327 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2328
Devang Patelc50078e2009-11-21 02:48:08 +00002329 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002330 // FIXME - extra padding for gdb bug.
2331 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2332 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2333 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2334 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2335 EmitLabel("info_end", Unit->getID());
2336
2337 Asm->EOL();
2338}
2339
Devang Patelc50078e2009-11-21 02:48:08 +00002340void DwarfDebug::emitDebugInfo() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002341 // Start debug info section.
Chris Lattner73266f92009-08-19 05:49:37 +00002342 Asm->OutStreamer.SwitchSection(
2343 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002344
Devang Patelc50078e2009-11-21 02:48:08 +00002345 emitDebugInfoPerCU(ModuleCU);
Bill Wendling55fccda2009-05-20 23:21:38 +00002346}
2347
Devang Patelc50078e2009-11-21 02:48:08 +00002348/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002349///
Devang Patelc50078e2009-11-21 02:48:08 +00002350void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002351 // Check to see if it is worth the effort.
2352 if (!Abbreviations.empty()) {
2353 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002354 Asm->OutStreamer.SwitchSection(
2355 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002356
2357 EmitLabel("abbrev_begin", 0);
2358
2359 // For each abbrevation.
2360 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2361 // Get abbreviation data
2362 const DIEAbbrev *Abbrev = Abbreviations[i];
2363
2364 // Emit the abbrevations code (base 1 index.)
2365 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2366 Asm->EOL("Abbreviation Code");
2367
2368 // Emit the abbreviations data.
2369 Abbrev->Emit(Asm);
2370
2371 Asm->EOL();
2372 }
2373
2374 // Mark end of abbreviations.
2375 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2376
2377 EmitLabel("abbrev_end", 0);
2378 Asm->EOL();
2379 }
2380}
2381
Devang Patelc50078e2009-11-21 02:48:08 +00002382/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002383/// the line matrix.
2384///
Devang Patelc50078e2009-11-21 02:48:08 +00002385void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002386 // Define last address of section.
2387 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2388 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2389 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2390 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2391
2392 // Mark end of matrix.
2393 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2394 Asm->EmitULEB128Bytes(1); Asm->EOL();
2395 Asm->EmitInt8(1); Asm->EOL();
2396}
2397
Devang Patelc50078e2009-11-21 02:48:08 +00002398/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002399///
Devang Patelc50078e2009-11-21 02:48:08 +00002400void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002401 // If the target is using .loc/.file, the assembler will be emitting the
2402 // .debug_line table automatically.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002403 if (MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002404 return;
2405
2406 // Minimum line delta, thus ranging from -10..(255-10).
2407 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2408 // Maximum line delta, thus ranging from -10..(255-10).
2409 const int MaxLineDelta = 255 + MinLineDelta;
2410
2411 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002412 Asm->OutStreamer.SwitchSection(
2413 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002414
2415 // Construct the section header.
2416 EmitDifference("line_end", 0, "line_begin", 0, true);
2417 Asm->EOL("Length of Source Line Info");
2418 EmitLabel("line_begin", 0);
2419
2420 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2421
2422 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2423 Asm->EOL("Prolog Length");
2424 EmitLabel("line_prolog_begin", 0);
2425
2426 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2427
2428 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2429
2430 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2431
2432 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2433
2434 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2435
2436 // Line number standard opcode encodings argument count
2437 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2438 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2439 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2440 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2441 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2442 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2443 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2444 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2445 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2446
2447 // Emit directories.
2448 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2449 Asm->EmitString(getSourceDirectoryName(DI));
2450 Asm->EOL("Directory");
2451 }
2452
2453 Asm->EmitInt8(0); Asm->EOL("End of directories");
2454
2455 // Emit files.
2456 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2457 // Remember source id starts at 1.
2458 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2459 Asm->EmitString(getSourceFileName(Id.second));
2460 Asm->EOL("Source");
2461 Asm->EmitULEB128Bytes(Id.first);
2462 Asm->EOL("Directory #");
2463 Asm->EmitULEB128Bytes(0);
2464 Asm->EOL("Mod date");
2465 Asm->EmitULEB128Bytes(0);
2466 Asm->EOL("File size");
2467 }
2468
2469 Asm->EmitInt8(0); Asm->EOL("End of files");
2470
2471 EmitLabel("line_prolog_end", 0);
2472
2473 // A sequence for each text section.
2474 unsigned SecSrcLinesSize = SectionSourceLines.size();
2475
2476 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2477 // Isolate current sections line info.
2478 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2479
Chris Lattner26aabb92009-08-08 23:39:42 +00002480 /*if (Asm->isVerbose()) {
Chris Lattnere6ad12f2009-07-31 18:48:30 +00002481 const MCSection *S = SectionMap[j + 1];
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002482 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling55fccda2009-05-20 23:21:38 +00002483 << S->getName() << '\n';
Chris Lattner26aabb92009-08-08 23:39:42 +00002484 }*/
2485 Asm->EOL();
Bill Wendling55fccda2009-05-20 23:21:38 +00002486
2487 // Dwarf assumes we start with first line of first source file.
2488 unsigned Source = 1;
2489 unsigned Line = 1;
2490
2491 // Construct rows of the address, source, line, column matrix.
2492 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2493 const SrcLineInfo &LineInfo = LineInfos[i];
2494 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2495 if (!LabelID) continue;
2496
Caroline Tice9da96d82009-09-11 18:25:54 +00002497 if (LineInfo.getLine() == 0) continue;
2498
Bill Wendling55fccda2009-05-20 23:21:38 +00002499 if (!Asm->isVerbose())
2500 Asm->EOL();
2501 else {
2502 std::pair<unsigned, unsigned> SourceID =
2503 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002504 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling55fccda2009-05-20 23:21:38 +00002505 << getSourceDirectoryName(SourceID.first) << ' '
2506 << getSourceFileName(SourceID.second)
2507 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2508 }
2509
2510 // Define the line address.
2511 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2512 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2513 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2514 EmitReference("label", LabelID); Asm->EOL("Location label");
2515
2516 // If change of source, then switch to the new source.
2517 if (Source != LineInfo.getSourceID()) {
2518 Source = LineInfo.getSourceID();
2519 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2520 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2521 }
2522
2523 // If change of line.
2524 if (Line != LineInfo.getLine()) {
2525 // Determine offset.
2526 int Offset = LineInfo.getLine() - Line;
2527 int Delta = Offset - MinLineDelta;
2528
2529 // Update line.
2530 Line = LineInfo.getLine();
2531
2532 // If delta is small enough and in range...
2533 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2534 // ... then use fast opcode.
2535 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2536 } else {
2537 // ... otherwise use long hand.
2538 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2539 Asm->EOL("DW_LNS_advance_line");
2540 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2541 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2542 }
2543 } else {
2544 // Copy the previous row (different address or source)
2545 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2546 }
2547 }
2548
Devang Patelc50078e2009-11-21 02:48:08 +00002549 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002550 }
2551
2552 if (SecSrcLinesSize == 0)
2553 // Because we're emitting a debug_line section, we still need a line
2554 // table. The linker and friends expect it to exist. If there's nothing to
2555 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00002556 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002557
2558 EmitLabel("line_end", 0);
2559 Asm->EOL();
2560}
2561
Devang Patelc50078e2009-11-21 02:48:08 +00002562/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002563///
Devang Patelc50078e2009-11-21 02:48:08 +00002564void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002565 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002566 return;
2567
2568 int stackGrowth =
2569 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2570 TargetFrameInfo::StackGrowsUp ?
2571 TD->getPointerSize() : -TD->getPointerSize();
2572
2573 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002574 Asm->OutStreamer.SwitchSection(
2575 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002576
2577 EmitLabel("debug_frame_common", 0);
2578 EmitDifference("debug_frame_common_end", 0,
2579 "debug_frame_common_begin", 0, true);
2580 Asm->EOL("Length of Common Information Entry");
2581
2582 EmitLabel("debug_frame_common_begin", 0);
2583 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2584 Asm->EOL("CIE Identifier Tag");
2585 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2586 Asm->EOL("CIE Version");
2587 Asm->EmitString("");
2588 Asm->EOL("CIE Augmentation");
2589 Asm->EmitULEB128Bytes(1);
2590 Asm->EOL("CIE Code Alignment Factor");
2591 Asm->EmitSLEB128Bytes(stackGrowth);
2592 Asm->EOL("CIE Data Alignment Factor");
2593 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2594 Asm->EOL("CIE RA Column");
2595
2596 std::vector<MachineMove> Moves;
2597 RI->getInitialFrameState(Moves);
2598
2599 EmitFrameMoves(NULL, 0, Moves, false);
2600
2601 Asm->EmitAlignment(2, 0, 0, false);
2602 EmitLabel("debug_frame_common_end", 0);
2603
2604 Asm->EOL();
2605}
2606
Devang Patelc50078e2009-11-21 02:48:08 +00002607/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00002608/// section.
2609void
Devang Patelc50078e2009-11-21 02:48:08 +00002610DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002611 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002612 return;
2613
2614 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002615 Asm->OutStreamer.SwitchSection(
2616 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002617
2618 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2619 "debug_frame_begin", DebugFrameInfo.Number, true);
2620 Asm->EOL("Length of Frame Information Entry");
2621
2622 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2623
2624 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2625 0, 0, true, false);
2626 Asm->EOL("FDE CIE offset");
2627
2628 EmitReference("func_begin", DebugFrameInfo.Number);
2629 Asm->EOL("FDE initial location");
2630 EmitDifference("func_end", DebugFrameInfo.Number,
2631 "func_begin", DebugFrameInfo.Number);
2632 Asm->EOL("FDE address range");
2633
2634 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2635 false);
2636
2637 Asm->EmitAlignment(2, 0, 0, false);
2638 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2639
2640 Asm->EOL();
2641}
2642
Devang Patelc50078e2009-11-21 02:48:08 +00002643void DwarfDebug::emitDebugPubNamesPerCU(CompileUnit *Unit) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002644 EmitDifference("pubnames_end", Unit->getID(),
2645 "pubnames_begin", Unit->getID(), true);
2646 Asm->EOL("Length of Public Names Info");
2647
2648 EmitLabel("pubnames_begin", Unit->getID());
2649
2650 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2651
2652 EmitSectionOffset("info_begin", "section_info",
2653 Unit->getID(), 0, true, false);
2654 Asm->EOL("Offset of Compilation Unit Info");
2655
2656 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2657 true);
2658 Asm->EOL("Compilation Unit Length");
2659
2660 StringMap<DIE*> &Globals = Unit->getGlobals();
2661 for (StringMap<DIE*>::const_iterator
2662 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2663 const char *Name = GI->getKeyData();
2664 DIE * Entity = GI->second;
2665
2666 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2667 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2668 }
2669
2670 Asm->EmitInt32(0); Asm->EOL("End Mark");
2671 EmitLabel("pubnames_end", Unit->getID());
2672
2673 Asm->EOL();
2674}
2675
Devang Patelc50078e2009-11-21 02:48:08 +00002676/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002677///
Devang Patelc50078e2009-11-21 02:48:08 +00002678void DwarfDebug::emitDebugPubNames() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002679 // Start the dwarf pubnames section.
Chris Lattner73266f92009-08-19 05:49:37 +00002680 Asm->OutStreamer.SwitchSection(
2681 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002682
Devang Patelc50078e2009-11-21 02:48:08 +00002683 emitDebugPubNamesPerCU(ModuleCU);
Bill Wendling55fccda2009-05-20 23:21:38 +00002684}
2685
Devang Patelc50078e2009-11-21 02:48:08 +00002686/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002687///
Devang Patelc50078e2009-11-21 02:48:08 +00002688void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002689 // Check to see if it is worth the effort.
2690 if (!StringPool.empty()) {
2691 // Start the dwarf str section.
Chris Lattner73266f92009-08-19 05:49:37 +00002692 Asm->OutStreamer.SwitchSection(
2693 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002694
2695 // For each of strings in the string pool.
2696 for (unsigned StringID = 1, N = StringPool.size();
2697 StringID <= N; ++StringID) {
2698 // Emit a label for reference from debug information entries.
2699 EmitLabel("string", StringID);
2700
2701 // Emit the string itself.
2702 const std::string &String = StringPool[StringID];
2703 Asm->EmitString(String); Asm->EOL();
2704 }
2705
2706 Asm->EOL();
2707 }
2708}
2709
Devang Patelc50078e2009-11-21 02:48:08 +00002710/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002711///
Devang Patelc50078e2009-11-21 02:48:08 +00002712void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002713 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00002714 Asm->OutStreamer.SwitchSection(
2715 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002716 Asm->EOL();
2717}
2718
2719/// EmitDebugARanges - Emit visible names into a debug aranges section.
2720///
2721void DwarfDebug::EmitDebugARanges() {
2722 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002723 Asm->OutStreamer.SwitchSection(
2724 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002725
2726 // FIXME - Mock up
2727#if 0
2728 CompileUnit *Unit = GetBaseCompileUnit();
2729
2730 // Don't include size of length
2731 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2732
2733 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2734
2735 EmitReference("info_begin", Unit->getID());
2736 Asm->EOL("Offset of Compilation Unit Info");
2737
2738 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2739
2740 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2741
2742 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2743 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2744
2745 // Range 1
2746 EmitReference("text_begin", 0); Asm->EOL("Address");
2747 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2748
2749 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2750 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2751#endif
2752
2753 Asm->EOL();
2754}
2755
Devang Patelc50078e2009-11-21 02:48:08 +00002756/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002757///
Devang Patelc50078e2009-11-21 02:48:08 +00002758void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002759 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002760 Asm->OutStreamer.SwitchSection(
2761 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002762 Asm->EOL();
2763}
2764
Devang Patelc50078e2009-11-21 02:48:08 +00002765/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002766///
Devang Patelc50078e2009-11-21 02:48:08 +00002767void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00002768 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00002769 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002770 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00002771 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002772 Asm->EOL();
2773 }
2774}
2775
Devang Patelc50078e2009-11-21 02:48:08 +00002776/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00002777/// Section Header:
2778/// 1. length of section
2779/// 2. Dwarf version number
2780/// 3. address size.
2781///
2782/// Entries (one "entry" for each function that was inlined):
2783///
2784/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2785/// otherwise offset into __debug_str for regular function name.
2786/// 2. offset into __debug_str section for regular function name.
2787/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2788/// instances for the function.
2789///
2790/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2791/// inlined instance; the die_offset points to the inlined_subroutine die in the
2792/// __debug_info section, and the low_pc is the starting address for the
2793/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00002794void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002795 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002796 return;
2797
Devang Patel5a3d37f2009-06-29 20:45:18 +00002798 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00002799 return;
2800
Chris Lattner73266f92009-08-19 05:49:37 +00002801 Asm->OutStreamer.SwitchSection(
2802 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002803 Asm->EOL();
2804 EmitDifference("debug_inlined_end", 1,
2805 "debug_inlined_begin", 1, true);
2806 Asm->EOL("Length of Debug Inlined Information Entry");
2807
2808 EmitLabel("debug_inlined_begin", 1);
2809
2810 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2811 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2812
Devang Patel90a0fe32009-11-10 23:06:00 +00002813 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2814 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00002815
Devang Patel90a0fe32009-11-10 23:06:00 +00002816// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2817 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2818 MDNode *Node = *I;
Jim Grosbachb23f2422009-11-22 19:20:36 +00002819 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
2820 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00002821 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00002822 DISubprogram SP(Node);
Devang Patelaaf012e2009-09-29 18:40:58 +00002823 const char *LName = SP.getLinkageName();
2824 const char *Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00002825
Devang Patelaaf012e2009-09-29 18:40:58 +00002826 if (!LName)
Devang Patel76031e82009-07-16 01:01:22 +00002827 Asm->EmitString(Name);
2828 else {
Chris Lattner73266f92009-08-19 05:49:37 +00002829 // Skip special LLVM prefix that is used to inform the asm printer to not
2830 // emit usual symbol prefix before the symbol name. This happens for
2831 // Objective-C symbol names and symbol whose name is replaced using GCC's
2832 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00002833 if (LName[0] == 1)
2834 LName = &LName[1];
Devang Patel90a0fe32009-11-10 23:06:00 +00002835// Asm->EmitString(LName);
2836 EmitSectionOffset("string", "section_str",
2837 StringPool.idFor(LName), false, true);
2838
Devang Patel76031e82009-07-16 01:01:22 +00002839 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002840 Asm->EOL("MIPS linkage name");
Jim Grosbach652b7432009-11-21 23:12:12 +00002841// Asm->EmitString(Name);
Devang Patel90a0fe32009-11-10 23:06:00 +00002842 EmitSectionOffset("string", "section_str",
2843 StringPool.idFor(Name), false, true);
2844 Asm->EOL("Function name");
Bill Wendling55fccda2009-05-20 23:21:38 +00002845 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2846
Devang Patel90a0fe32009-11-10 23:06:00 +00002847 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00002848 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002849 DIE *SP = LI->second;
Bill Wendling55fccda2009-05-20 23:21:38 +00002850 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2851
2852 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002853 O << MAI->getData32bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00002854 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002855 O << MAI->getData64bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00002856
Devang Patel90a0fe32009-11-10 23:06:00 +00002857 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling55fccda2009-05-20 23:21:38 +00002858 }
2859 }
2860
2861 EmitLabel("debug_inlined_end", 1);
2862 Asm->EOL();
2863}