blob: b050d00543f7cac8d740a999205565bf1dfaa189 [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); }
96
97 /// 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
122 // setIndexTyDie - Set D as anonymous type for index which can be reused later.
123 void setIndexTyDie(DIE *D) {
124 IndexTyDie = D;
125 }
126
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000127};
128
129//===----------------------------------------------------------------------===//
130/// DbgVariable - This class is used to track local variable information.
131///
Devang Patel4cb32c32009-11-16 21:53:40 +0000132class DbgVariable {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000133 DIVariable Var; // Variable Descriptor.
134 unsigned FrameIndex; // Variable frame index.
Devang Patel90a0fe32009-11-10 23:06:00 +0000135 DbgVariable *AbstractVar; // Abstract variable for this variable.
136 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000137public:
Devang Patel90a0fe32009-11-10 23:06:00 +0000138 DbgVariable(DIVariable V, unsigned I)
139 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000140
141 // Accessors.
Devang Patel90a0fe32009-11-10 23:06:00 +0000142 DIVariable getVariable() const { return Var; }
143 unsigned getFrameIndex() const { return FrameIndex; }
144 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
145 DbgVariable *getAbstractVariable() const { return AbstractVar; }
146 void setDIE(DIE *D) { TheDIE = D; }
147 DIE *getDIE() const { return TheDIE; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000148};
149
150//===----------------------------------------------------------------------===//
151/// DbgScope - This class is used to track scope information.
152///
Devang Patel4cb32c32009-11-16 21:53:40 +0000153class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000154 DbgScope *Parent; // Parent to this scope.
Devang Patel90a0fe32009-11-10 23:06:00 +0000155 DIDescriptor Desc; // Debug info descriptor for scope.
156 WeakVH InlinedAtLocation; // Location at which scope is inlined.
157 bool AbstractScope; // Abstract Scope
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000158 unsigned StartLabelID; // Label ID of the beginning of scope.
159 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel90ecd192009-10-01 18:25:23 +0000160 const MachineInstr *LastInsn; // Last instruction of this scope.
161 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000162 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
163 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbar41716322009-09-19 20:40:05 +0000164
Owen Anderson696d4862009-06-24 22:53:20 +0000165 // Private state for dump()
166 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000167public:
Devang Pateldd7bb432009-10-14 21:08:09 +0000168 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000169 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
170 StartLabelID(0), EndLabelID(0),
Devang Pateldd7bb432009-10-14 21:08:09 +0000171 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000172 virtual ~DbgScope();
173
174 // Accessors.
175 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000176 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000177 DIDescriptor getDesc() const { return Desc; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000178 MDNode *getInlinedAt() const {
179 return dyn_cast_or_null<MDNode>(InlinedAtLocation);
Devang Pateldd7bb432009-10-14 21:08:09 +0000180 }
Devang Patel90a0fe32009-11-10 23:06:00 +0000181 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000182 unsigned getStartLabelID() const { return StartLabelID; }
183 unsigned getEndLabelID() const { return EndLabelID; }
184 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
185 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000186 void setStartLabelID(unsigned S) { StartLabelID = S; }
187 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Patel90ecd192009-10-01 18:25:23 +0000188 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
189 const MachineInstr *getLastInsn() { return LastInsn; }
190 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000191 void setAbstractScope() { AbstractScope = true; }
192 bool isAbstractScope() const { return AbstractScope; }
Devang Patel90ecd192009-10-01 18:25:23 +0000193 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000194
Devang Patelc50078e2009-11-21 02:48:08 +0000195 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000196 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000197 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000198
Devang Patelc50078e2009-11-21 02:48:08 +0000199 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000200 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000201 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000202
Devang Patelc50078e2009-11-21 02:48:08 +0000203 void fixInstructionMarkers() {
Devang Patel6a260102009-10-01 20:31:14 +0000204 assert (getFirstInsn() && "First instruction is missing!");
205 if (getLastInsn())
206 return;
207
208 // If a scope does not have an instruction to mark an end then use
209 // the end of last child scope.
210 SmallVector<DbgScope *, 4> &Scopes = getScopes();
211 assert (!Scopes.empty() && "Inner most scope does not have last insn!");
212 DbgScope *L = Scopes.back();
213 if (!L->getLastInsn())
Devang Patelc50078e2009-11-21 02:48:08 +0000214 L->fixInstructionMarkers();
Devang Patel6a260102009-10-01 20:31:14 +0000215 setLastInsn(L->getLastInsn());
216 }
217
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000218#ifndef NDEBUG
219 void dump() const;
220#endif
221};
222
223#ifndef NDEBUG
224void DbgScope::dump() const {
Chris Lattnerebb8c082009-08-23 00:51:00 +0000225 raw_ostream &err = errs();
226 err.indent(IndentLevel);
Devang Patel90a0fe32009-11-10 23:06:00 +0000227 MDNode *N = Desc.getNode();
228 N->dump();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000229 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel90a0fe32009-11-10 23:06:00 +0000230 if (AbstractScope)
231 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000232
233 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000234 if (!Scopes.empty())
235 err << "Children ...\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000236 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
237 if (Scopes[i] != this)
238 Scopes[i]->dump();
239
240 IndentLevel -= 2;
241}
242#endif
243
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000244DbgScope::~DbgScope() {
245 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
246 delete Scopes[i];
247 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
248 delete Variables[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000249}
250
251} // end llvm namespace
252
Chris Lattner621c44d2009-08-22 20:48:53 +0000253DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel5a3d37f2009-06-29 20:45:18 +0000254 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000255 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patelc50078e2009-11-21 02:48:08 +0000256 DIEValues(), StringPool(),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000257 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel90a0fe32009-11-10 23:06:00 +0000258 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000259 if (TimePassesIsEnabled)
260 DebugTimer = new Timer("Dwarf Debug Writer",
261 getDwarfTimerGroup());
262}
263DwarfDebug::~DwarfDebug() {
Devang Patelc50078e2009-11-21 02:48:08 +0000264 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
265 delete DIEValues[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000266
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000267 delete DebugTimer;
268}
269
Devang Patelc50078e2009-11-21 02:48:08 +0000270/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000271///
Devang Patelc50078e2009-11-21 02:48:08 +0000272void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000273 // Profile the node so that we can make it unique.
274 FoldingSetNodeID ID;
275 Abbrev.Profile(ID);
276
277 // Check the set for priors.
278 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
279
280 // If it's newly added.
281 if (InSet == &Abbrev) {
282 // Add to abbreviation list.
283 Abbreviations.push_back(&Abbrev);
284
285 // Assign the vector position + 1 as its number.
286 Abbrev.setNumber(Abbreviations.size());
287 } else {
288 // Assign existing abbreviation number.
289 Abbrev.setNumber(InSet->getNumber());
290 }
291}
292
Devang Patelc50078e2009-11-21 02:48:08 +0000293/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000294/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000295DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel1233f912009-11-21 00:31:03 +0000296 DIEEntry *Value = new DIEEntry(Entry);
Devang Patelc50078e2009-11-21 02:48:08 +0000297 DIEValues.push_back(Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000298 return Value;
299}
300
Devang Patelc50078e2009-11-21 02:48:08 +0000301/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000302///
Devang Patelc50078e2009-11-21 02:48:08 +0000303void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000304 unsigned Form, uint64_t Integer) {
305 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000306 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000307 DIEValues.push_back(Value);
308 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000309}
310
Devang Patelc50078e2009-11-21 02:48:08 +0000311/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000312///
Devang Patelc50078e2009-11-21 02:48:08 +0000313void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000314 unsigned Form, int64_t Integer) {
315 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000316 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000317 DIEValues.push_back(Value);
318 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000319}
320
Devang Patelc50078e2009-11-21 02:48:08 +0000321/// addString - Add a string attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000322///
Devang Patelc50078e2009-11-21 02:48:08 +0000323void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000324 const std::string &String) {
Devang Patel1233f912009-11-21 00:31:03 +0000325 DIEValue *Value = new DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000326 DIEValues.push_back(Value);
327 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000328}
329
Devang Patelc50078e2009-11-21 02:48:08 +0000330/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000331///
Devang Patelc50078e2009-11-21 02:48:08 +0000332void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000333 const DWLabel &Label) {
Devang Patel1233f912009-11-21 00:31:03 +0000334 DIEValue *Value = new DIEDwarfLabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000335 DIEValues.push_back(Value);
336 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000337}
338
Devang Patelc50078e2009-11-21 02:48:08 +0000339/// addObjectLabel - Add an non-Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000340///
Devang Patelc50078e2009-11-21 02:48:08 +0000341void DwarfDebug::addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000342 const std::string &Label) {
Devang Patel1233f912009-11-21 00:31:03 +0000343 DIEValue *Value = new DIEObjectLabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000344 DIEValues.push_back(Value);
345 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000346}
347
Devang Patelc50078e2009-11-21 02:48:08 +0000348/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000349///
Devang Patelc50078e2009-11-21 02:48:08 +0000350void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000351 const DWLabel &Label, const DWLabel &Section,
352 bool isEH, bool useSet) {
Devang Patel1233f912009-11-21 00:31:03 +0000353 DIEValue *Value = new DIESectionOffset(Label, Section, isEH, useSet);
Devang Patelc50078e2009-11-21 02:48:08 +0000354 DIEValues.push_back(Value);
355 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000356}
357
Devang Patelc50078e2009-11-21 02:48:08 +0000358/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000359///
Devang Patelc50078e2009-11-21 02:48:08 +0000360void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000361 const DWLabel &Hi, const DWLabel &Lo) {
Devang Patel1233f912009-11-21 00:31:03 +0000362 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000363 DIEValues.push_back(Value);
364 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000365}
366
Devang Patelc50078e2009-11-21 02:48:08 +0000367/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000368///
Devang Patelc50078e2009-11-21 02:48:08 +0000369void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000370 DIEBlock *Block) {
371 Block->ComputeSize(TD);
Devang Patelc50078e2009-11-21 02:48:08 +0000372 DIEValues.push_back(Block);
373 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000374}
375
Devang Patelc50078e2009-11-21 02:48:08 +0000376/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000377/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000378void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000379 // If there is no compile unit specified, don't add a line #.
380 if (V->getCompileUnit().isNull())
381 return;
382
383 unsigned Line = V->getLineNumber();
Devang Patelc50078e2009-11-21 02:48:08 +0000384 unsigned FileID = findCompileUnit(V->getCompileUnit()).getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000385 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000386 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
387 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000388}
389
Devang Patelc50078e2009-11-21 02:48:08 +0000390/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000391/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000392void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000393 // If there is no compile unit specified, don't add a line #.
394 if (G->getCompileUnit().isNull())
395 return;
396
397 unsigned Line = G->getLineNumber();
Devang Patelc50078e2009-11-21 02:48:08 +0000398 unsigned FileID = findCompileUnit(G->getCompileUnit()).getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000399 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000400 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
401 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000402}
Devang Patel318d70d2009-08-31 22:47:13 +0000403
Devang Patelc50078e2009-11-21 02:48:08 +0000404/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000405/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000406void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel318d70d2009-08-31 22:47:13 +0000407 // If there is no compile unit specified, don't add a line #.
408 if (SP->getCompileUnit().isNull())
409 return;
Caroline Tice9da96d82009-09-11 18:25:54 +0000410 // If the line number is 0, don't add it.
411 if (SP->getLineNumber() == 0)
412 return;
413
Devang Patel318d70d2009-08-31 22:47:13 +0000414
415 unsigned Line = SP->getLineNumber();
Devang Patelc50078e2009-11-21 02:48:08 +0000416 unsigned FileID = findCompileUnit(SP->getCompileUnit()).getID();
Devang Patel318d70d2009-08-31 22:47:13 +0000417 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000418 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
419 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel318d70d2009-08-31 22:47:13 +0000420}
421
Devang Patelc50078e2009-11-21 02:48:08 +0000422/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000423/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000424void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000425 // If there is no compile unit specified, don't add a line #.
426 DICompileUnit CU = Ty->getCompileUnit();
427 if (CU.isNull())
428 return;
429
430 unsigned Line = Ty->getLineNumber();
Devang Patelc50078e2009-11-21 02:48:08 +0000431 unsigned FileID = findCompileUnit(CU).getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000432 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000433 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
434 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000435}
436
Caroline Tice248d5572009-08-31 21:19:37 +0000437/* Byref variables, in Blocks, are declared by the programmer as
438 "SomeType VarName;", but the compiler creates a
439 __Block_byref_x_VarName struct, and gives the variable VarName
440 either the struct, or a pointer to the struct, as its type. This
441 is necessary for various behind-the-scenes things the compiler
442 needs to do with by-reference variables in blocks.
443
444 However, as far as the original *programmer* is concerned, the
445 variable should still have type 'SomeType', as originally declared.
446
447 The following function dives into the __Block_byref_x_VarName
448 struct to find the original type of the variable. This will be
449 passed back to the code generating the type for the Debug
450 Information Entry for the variable 'VarName'. 'VarName' will then
451 have the original type 'SomeType' in its debug information.
452
453 The original type 'SomeType' will be the type of the field named
454 'VarName' inside the __Block_byref_x_VarName struct.
455
456 NOTE: In order for this to not completely fail on the debugger
457 side, the Debug Information Entry for the variable VarName needs to
458 have a DW_AT_location that tells the debugger how to unwind through
459 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patelc50078e2009-11-21 02:48:08 +0000460 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-08-31 21:19:37 +0000461
462/// Find the type the programmer originally declared the variable to be
463/// and return that type.
464///
Devang Patelc50078e2009-11-21 02:48:08 +0000465DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-08-31 21:19:37 +0000466
467 DIType subType = Ty;
468 unsigned tag = Ty.getTag();
469
470 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000471 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000472 subType = DTy.getTypeDerivedFrom();
473 }
474
475 DICompositeType blockStruct = DICompositeType(subType.getNode());
476
477 DIArray Elements = blockStruct.getTypeArray();
478
479 if (Elements.isNull())
480 return Ty;
481
482 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
483 DIDescriptor Element = Elements.getElement(i);
484 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patelaaf012e2009-09-29 18:40:58 +0000485 if (strcmp(Name.c_str(), DT.getName()) == 0)
Caroline Tice248d5572009-08-31 21:19:37 +0000486 return (DT.getTypeDerivedFrom());
487 }
488
489 return Ty;
490}
491
Devang Patelc50078e2009-11-21 02:48:08 +0000492/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000493/// and generate the DWARF information necessary to find the actual variable
494/// given the extra address information encoded in the DIVariable, starting from
495/// the starting location. Add the DWARF information to the die.
496///
Devang Patelc50078e2009-11-21 02:48:08 +0000497void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000498 unsigned Attribute,
499 const MachineLocation &Location) {
500 const DIVariable &VD = DV->getVariable();
501 DIType Ty = VD.getType();
502
503 // Decode the original location, and use that as the start of the byref
504 // variable's location.
505 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
506 DIEBlock *Block = new DIEBlock();
507
508 if (Location.isReg()) {
509 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000510 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000511 } else {
512 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000513 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
514 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000515 }
516 } else {
517 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000518 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000519 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000520 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
521 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000522 }
523
Devang Patelc50078e2009-11-21 02:48:08 +0000524 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000525 }
526
527 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
528 uint64_t Element = VD.getAddrElement(i);
529
530 if (Element == DIFactory::OpPlus) {
Devang Patelc50078e2009-11-21 02:48:08 +0000531 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
532 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000533 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000534 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000535 } else llvm_unreachable("unknown DIFactory Opcode");
536 }
537
538 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000539 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000540}
541
Caroline Tice248d5572009-08-31 21:19:37 +0000542/* Byref variables, in Blocks, are declared by the programmer as "SomeType
543 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
544 gives the variable VarName either the struct, or a pointer to the struct, as
545 its type. This is necessary for various behind-the-scenes things the
546 compiler needs to do with by-reference variables in Blocks.
547
548 However, as far as the original *programmer* is concerned, the variable
549 should still have type 'SomeType', as originally declared.
550
Devang Patelc50078e2009-11-21 02:48:08 +0000551 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000552 struct to find the original type of the variable, which is then assigned to
553 the variable's Debug Information Entry as its real type. So far, so good.
554 However now the debugger will expect the variable VarName to have the type
555 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbar41716322009-09-19 20:40:05 +0000556 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-08-31 21:19:37 +0000557 pointers and struct to find the actual variable of type SomeType.
558
559 The following function does just that. We start by getting
560 the "normal" location for the variable. This will be the location
561 of either the struct __Block_byref_x_VarName or the pointer to the
562 struct __Block_byref_x_VarName.
563
564 The struct will look something like:
565
566 struct __Block_byref_x_VarName {
567 ... <various fields>
568 struct __Block_byref_x_VarName *forwarding;
569 ... <various other fields>
570 SomeType VarName;
571 ... <maybe more fields>
572 };
573
574 If we are given the struct directly (as our starting point) we
575 need to tell the debugger to:
576
577 1). Add the offset of the forwarding field.
578
579 2). Follow that pointer to get the the real __Block_byref_x_VarName
580 struct to use (the real one may have been copied onto the heap).
581
582 3). Add the offset for the field VarName, to find the actual variable.
583
584 If we started with a pointer to the struct, then we need to
585 dereference that pointer first, before the other steps.
586 Translating this into DWARF ops, we will need to append the following
587 to the current location description for the variable:
588
589 DW_OP_deref -- optional, if we start with a pointer
590 DW_OP_plus_uconst <forward_fld_offset>
591 DW_OP_deref
592 DW_OP_plus_uconst <varName_fld_offset>
593
594 That is what this function does. */
595
Devang Patelc50078e2009-11-21 02:48:08 +0000596/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-08-31 21:19:37 +0000597/// provided, and generate the DWARF information necessary to find the
598/// actual Block variable (navigating the Block struct) based on the
599/// starting location. Add the DWARF information to the die. For
600/// more information, read large comment just above here.
601///
Devang Patelc50078e2009-11-21 02:48:08 +0000602void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000603 unsigned Attribute,
604 const MachineLocation &Location) {
Caroline Tice248d5572009-08-31 21:19:37 +0000605 const DIVariable &VD = DV->getVariable();
606 DIType Ty = VD.getType();
607 DIType TmpTy = Ty;
608 unsigned Tag = Ty.getTag();
609 bool isPointer = false;
610
Devang Patelaaf012e2009-09-29 18:40:58 +0000611 const char *varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000612
613 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000614 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000615 TmpTy = DTy.getTypeDerivedFrom();
616 isPointer = true;
617 }
618
619 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
620
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000621 // Find the __forwarding field and the variable field in the __Block_byref
622 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000623 DIArray Fields = blockStruct.getTypeArray();
624 DIDescriptor varField = DIDescriptor();
625 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000626
627
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000628 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
629 DIDescriptor Element = Fields.getElement(i);
630 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patelaaf012e2009-09-29 18:40:58 +0000631 const char *fieldName = DT.getName();
632 if (strcmp(fieldName, "__forwarding") == 0)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000633 forwardingField = Element;
Devang Patelaaf012e2009-09-29 18:40:58 +0000634 else if (strcmp(fieldName, varName) == 0)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000635 varField = Element;
636 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000637
Mike Stump2fd84e22009-09-24 23:21:26 +0000638 assert(!varField.isNull() && "Can't find byref variable in Block struct");
639 assert(!forwardingField.isNull()
640 && "Can't find forwarding field in Block struct");
Caroline Tice248d5572009-08-31 21:19:37 +0000641
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000642 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000643 unsigned int forwardingFieldOffset =
644 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
645 unsigned int varFieldOffset =
646 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000647
Mike Stump2fd84e22009-09-24 23:21:26 +0000648 // Decode the original location, and use that as the start of the byref
649 // variable's location.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000650 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
651 DIEBlock *Block = new DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000652
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000653 if (Location.isReg()) {
654 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000655 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000656 else {
657 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000658 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
659 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000660 }
661 } else {
662 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000663 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000664 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000665 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
666 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000667 }
Caroline Tice248d5572009-08-31 21:19:37 +0000668
Devang Patelc50078e2009-11-21 02:48:08 +0000669 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000670 }
Caroline Tice248d5572009-08-31 21:19:37 +0000671
Mike Stump2fd84e22009-09-24 23:21:26 +0000672 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000673 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000674 if (isPointer)
Devang Patelc50078e2009-11-21 02:48:08 +0000675 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000676
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000677 // Next add the offset for the '__forwarding' field:
678 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
679 // adding the offset if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000680 if (forwardingFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000681 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
682 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000683 }
Caroline Tice248d5572009-08-31 21:19:37 +0000684
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000685 // Now dereference the __forwarding field to get to the real __Block_byref
686 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000687 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000688
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000689 // Now that we've got the real __Block_byref... struct, add the offset
690 // for the variable's field to get to the location of the actual variable:
691 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000692 if (varFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000693 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
694 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000695 }
Caroline Tice248d5572009-08-31 21:19:37 +0000696
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000697 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000698 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000699}
700
Devang Patelc50078e2009-11-21 02:48:08 +0000701/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000702/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000703void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000704 const MachineLocation &Location) {
705 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
706 DIEBlock *Block = new DIEBlock();
707
708 if (Location.isReg()) {
709 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000710 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000711 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000712 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
713 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000714 }
715 } else {
716 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000717 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000718 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000719 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
720 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000721 }
722
Devang Patelc50078e2009-11-21 02:48:08 +0000723 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000724 }
725
Devang Patelc50078e2009-11-21 02:48:08 +0000726 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000727}
728
Devang Patelc50078e2009-11-21 02:48:08 +0000729/// addType - Add a new type attribute to the specified entity.
730void DwarfDebug::addType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000731 if (Ty.isNull())
732 return;
733
734 // Check for pre-existence.
Devang Patelc50078e2009-11-21 02:48:08 +0000735 DIEEntry *Entry = DW_Unit->getDIEEntry(Ty.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000736
737 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000738 if (Entry) {
739 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000740 return;
741 }
742
743 // Set up proxy.
Devang Patelc50078e2009-11-21 02:48:08 +0000744 Entry = createDIEEntry();
745 DW_Unit->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000746
747 // Construct type.
Devang Patel1233f912009-11-21 00:31:03 +0000748 DIE *Buffer = new DIE(dwarf::DW_TAG_base_type);
Devang Patel56843af2009-08-31 18:49:10 +0000749 if (Ty.isBasicType())
Devang Patelc50078e2009-11-21 02:48:08 +0000750 constructTypeDIE(DW_Unit, *Buffer, DIBasicType(Ty.getNode()));
Devang Patel56843af2009-08-31 18:49:10 +0000751 else if (Ty.isCompositeType())
Devang Patelc50078e2009-11-21 02:48:08 +0000752 constructTypeDIE(DW_Unit, *Buffer, DICompositeType(Ty.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000753 else {
Devang Patel56843af2009-08-31 18:49:10 +0000754 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patelc50078e2009-11-21 02:48:08 +0000755 constructTypeDIE(DW_Unit, *Buffer, DIDerivedType(Ty.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000756 }
757
758 // Add debug information entry to entity and appropriate context.
759 DIE *Die = NULL;
760 DIDescriptor Context = Ty.getContext();
761 if (!Context.isNull())
Devang Pateld90672c2009-11-20 21:37:22 +0000762 Die = DW_Unit->getDIE(Context.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000763
Devang Patel1233f912009-11-21 00:31:03 +0000764 if (Die)
Devang Patelc50078e2009-11-21 02:48:08 +0000765 Die->addChild(Buffer);
Devang Patel1233f912009-11-21 00:31:03 +0000766 else
Devang Patelc50078e2009-11-21 02:48:08 +0000767 DW_Unit->addDie(Buffer);
768 Entry->setEntry(Buffer);
769 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000770}
771
Devang Patelc50078e2009-11-21 02:48:08 +0000772/// constructTypeDIE - Construct basic type die from DIBasicType.
773void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000774 DIBasicType BTy) {
775 // Get core information.
Devang Patelaaf012e2009-09-29 18:40:58 +0000776 const char *Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000777 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000778 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000779 BTy.getEncoding());
780
781 // Add name if not anonymous or intermediate type.
Devang Patelaaf012e2009-09-29 18:40:58 +0000782 if (Name)
Devang Patelc50078e2009-11-21 02:48:08 +0000783 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000784 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000785 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000786}
787
Devang Patelc50078e2009-11-21 02:48:08 +0000788/// constructTypeDIE - Construct derived type die from DIDerivedType.
789void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000790 DIDerivedType DTy) {
791 // Get core information.
Devang Patelaaf012e2009-09-29 18:40:58 +0000792 const char *Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000793 uint64_t Size = DTy.getSizeInBits() >> 3;
794 unsigned Tag = DTy.getTag();
795
796 // FIXME - Workaround for templates.
797 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
798
799 Buffer.setTag(Tag);
800
801 // Map to main type, void will not have a type.
802 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelc50078e2009-11-21 02:48:08 +0000803 addType(DW_Unit, &Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000804
805 // Add name if not anonymous or intermediate type.
Devang Patel18a7d1c2009-10-16 21:27:43 +0000806 if (Name && Tag != dwarf::DW_TAG_pointer_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000807 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000808
809 // Add size if non-zero (derived types might be zero-sized.)
810 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000811 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000812
813 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patel8268f722009-11-20 21:05:37 +0000814 if (!DTy.isForwardDecl() && Tag != dwarf::DW_TAG_pointer_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000815 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000816}
817
Devang Patelc50078e2009-11-21 02:48:08 +0000818/// constructTypeDIE - Construct type DIE from DICompositeType.
819void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000820 DICompositeType CTy) {
821 // Get core information.
Devang Patelaaf012e2009-09-29 18:40:58 +0000822 const char *Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000823
824 uint64_t Size = CTy.getSizeInBits() >> 3;
825 unsigned Tag = CTy.getTag();
826 Buffer.setTag(Tag);
827
828 switch (Tag) {
829 case dwarf::DW_TAG_vector_type:
830 case dwarf::DW_TAG_array_type:
Devang Patelc50078e2009-11-21 02:48:08 +0000831 constructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000832 break;
833 case dwarf::DW_TAG_enumeration_type: {
834 DIArray Elements = CTy.getTypeArray();
835
836 // Add enumerators to enumeration type.
837 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
838 DIE *ElemDie = NULL;
Devang Patel15e723d2009-08-28 23:24:31 +0000839 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelfb812752009-10-09 17:51:49 +0000840 if (!Enum.isNull()) {
Devang Patelc50078e2009-11-21 02:48:08 +0000841 ElemDie = constructEnumTypeDIE(DW_Unit, &Enum);
842 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +0000843 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000844 }
845 }
846 break;
847 case dwarf::DW_TAG_subroutine_type: {
848 // Add return type.
849 DIArray Elements = CTy.getTypeArray();
850 DIDescriptor RTy = Elements.getElement(0);
Devang Patelc50078e2009-11-21 02:48:08 +0000851 addType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000852
853 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +0000854 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000855
856 // Add arguments.
857 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
858 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
859 DIDescriptor Ty = Elements.getElement(i);
Devang Patelc50078e2009-11-21 02:48:08 +0000860 addType(DW_Unit, Arg, DIType(Ty.getNode()));
861 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000862 }
863 }
864 break;
865 case dwarf::DW_TAG_structure_type:
866 case dwarf::DW_TAG_union_type:
867 case dwarf::DW_TAG_class_type: {
868 // Add elements to structure type.
869 DIArray Elements = CTy.getTypeArray();
870
871 // A forward struct declared type may not have elements available.
872 if (Elements.isNull())
873 break;
874
875 // Add elements to structure type.
876 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
877 DIDescriptor Element = Elements.getElement(i);
Devang Patel15e723d2009-08-28 23:24:31 +0000878 if (Element.isNull())
879 continue;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000880 DIE *ElemDie = NULL;
881 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patelc50078e2009-11-21 02:48:08 +0000882 ElemDie = createSubprogramDIE(DW_Unit,
Devang Patel15e723d2009-08-28 23:24:31 +0000883 DISubprogram(Element.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000884 else
Devang Patelc50078e2009-11-21 02:48:08 +0000885 ElemDie = createMemberDIE(DW_Unit,
Devang Patel15e723d2009-08-28 23:24:31 +0000886 DIDerivedType(Element.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000887 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000888 }
889
Devang Patel20b32102009-08-27 23:51:51 +0000890 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +0000891 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000892
893 unsigned RLang = CTy.getRunTimeLang();
894 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +0000895 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000896 dwarf::DW_FORM_data1, RLang);
897 break;
898 }
899 default:
900 break;
901 }
902
903 // Add name if not anonymous or intermediate type.
Devang Patelaaf012e2009-09-29 18:40:58 +0000904 if (Name)
Devang Patelc50078e2009-11-21 02:48:08 +0000905 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000906
907 if (Tag == dwarf::DW_TAG_enumeration_type ||
908 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
909 // Add size if non-zero (derived types might be zero-sized.)
910 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000911 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000912 else {
913 // Add zero size if it is not a forward declaration.
914 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000915 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000916 else
Devang Patelc50078e2009-11-21 02:48:08 +0000917 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000918 }
919
920 // Add source line info if available.
921 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000922 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000923 }
924}
925
Devang Patelc50078e2009-11-21 02:48:08 +0000926/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
927void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000928 int64_t L = SR.getLo();
929 int64_t H = SR.getHi();
930 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
931
Devang Patelc50078e2009-11-21 02:48:08 +0000932 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +0000933 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +0000934 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Patele7ff5092009-08-14 20:59:16 +0000935 if (H)
Devang Patelc50078e2009-11-21 02:48:08 +0000936 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000937
Devang Patelc50078e2009-11-21 02:48:08 +0000938 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000939}
940
Devang Patelc50078e2009-11-21 02:48:08 +0000941/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
942void DwarfDebug::constructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000943 DICompositeType *CTy) {
944 Buffer.setTag(dwarf::DW_TAG_array_type);
945 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000946 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000947
948 // Emit derived type.
Devang Patelc50078e2009-11-21 02:48:08 +0000949 addType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000950 DIArray Elements = CTy->getTypeArray();
951
Devang Patel1233f912009-11-21 00:31:03 +0000952 // Get an anonymous type for index type.
953 DIE *IdxTy = DW_Unit->getIndexTyDie();
954 if (!IdxTy) {
955 // Construct an anonymous type for index type.
956 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000957 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
958 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +0000959 dwarf::DW_ATE_signed);
Devang Patelc50078e2009-11-21 02:48:08 +0000960 DW_Unit->addDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +0000961 DW_Unit->setIndexTyDie(IdxTy);
962 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000963
964 // Add subranges to array type.
965 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
966 DIDescriptor Element = Elements.getElement(i);
967 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000968 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000969 }
970}
971
Devang Patelc50078e2009-11-21 02:48:08 +0000972/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
973DIE *DwarfDebug::constructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000974 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patelaaf012e2009-09-29 18:40:58 +0000975 const char *Name = ETy->getName();
Devang Patelc50078e2009-11-21 02:48:08 +0000976 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000977 int64_t Value = ETy->getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +0000978 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000979 return Enumerator;
980}
981
Devang Patelc50078e2009-11-21 02:48:08 +0000982/// createGlobalVariableDIE - Create new DIE using GV.
983DIE *DwarfDebug::createGlobalVariableDIE(CompileUnit *DW_Unit,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000984 const DIGlobalVariable &GV) {
Devang Patelfabc47c2009-11-06 01:30:04 +0000985 // If the global variable was optmized out then no need to create debug info entry.
Devang Patel83e42c72009-11-06 17:58:12 +0000986 if (!GV.getGlobal()) return NULL;
987 if (!GV.getDisplayName()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +0000988
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000989 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Devang Patelc50078e2009-11-21 02:48:08 +0000990 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +0000991 GV.getDisplayName());
992
993 const char *LinkageName = GV.getLinkageName();
994 if (LinkageName) {
Chris Lattner73266f92009-08-19 05:49:37 +0000995 // Skip special LLVM prefix that is used to inform the asm printer to not
996 // emit usual symbol prefix before the symbol name. This happens for
997 // Objective-C symbol names and symbol whose name is replaced using GCC's
998 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +0000999 if (LinkageName[0] == 1)
1000 LinkageName = &LinkageName[1];
Devang Patelc50078e2009-11-21 02:48:08 +00001001 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelbd760a52009-07-14 00:55:28 +00001002 LinkageName);
Devang Patel76031e82009-07-16 01:01:22 +00001003 }
Devang Patelc50078e2009-11-21 02:48:08 +00001004 addType(DW_Unit, GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001005 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001006 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1007 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001008
1009 // Add address.
1010 DIEBlock *Block = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001011 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1012 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patel6bd5cc82009-10-05 23:22:08 +00001013 Asm->Mang->getMangledName(GV.getGlobal()));
Devang Patelc50078e2009-11-21 02:48:08 +00001014 addBlock(GVDie, dwarf::DW_AT_location, 0, Block);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001015
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001016 return GVDie;
1017}
1018
Devang Patelc50078e2009-11-21 02:48:08 +00001019/// createMemberDIE - Create new member DIE.
1020DIE *DwarfDebug::createMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001021 DIE *MemberDie = new DIE(DT.getTag());
Devang Patelaaf012e2009-09-29 18:40:58 +00001022 if (const char *Name = DT.getName())
Devang Patelc50078e2009-11-21 02:48:08 +00001023 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001024
Devang Patelc50078e2009-11-21 02:48:08 +00001025 addType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001026
Devang Patelc50078e2009-11-21 02:48:08 +00001027 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001028
Devang Patel7d9fe582009-11-04 22:06:12 +00001029 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001030 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001031
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001032 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001033 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001034
1035 if (Size != FieldSize) {
1036 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001037 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1038 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001039
1040 uint64_t Offset = DT.getOffsetInBits();
1041 uint64_t FieldOffset = Offset;
1042 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1043 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1044 FieldOffset = (HiMark - FieldSize);
1045 Offset -= FieldOffset;
1046
1047 // Maybe we need to work from the other end.
1048 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001049 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001050
Devang Patel7d9fe582009-11-04 22:06:12 +00001051 // Here WD_AT_data_member_location points to the anonymous
1052 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001053 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001054
1055 } else
1056 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001057 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001058
Devang Patelc50078e2009-11-21 02:48:08 +00001059 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001060
1061 if (DT.isProtected())
Devang Patelc50078e2009-11-21 02:48:08 +00001062 addUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001063 dwarf::DW_ACCESS_protected);
1064 else if (DT.isPrivate())
Devang Patelc50078e2009-11-21 02:48:08 +00001065 addUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001066 dwarf::DW_ACCESS_private);
1067
1068 return MemberDie;
1069}
1070
Devang Patelc50078e2009-11-21 02:48:08 +00001071/// createSubprogramDIE - Create new DIE using SP.
1072DIE *DwarfDebug::createSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001073 const DISubprogram &SP,
Bill Wendling87307862009-05-18 22:02:36 +00001074 bool IsConstructor,
1075 bool IsInlined) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001076 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
1077
Devang Patelaaf012e2009-09-29 18:40:58 +00001078 const char * Name = SP.getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001079 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001080
Devang Patelaaf012e2009-09-29 18:40:58 +00001081 const char *LinkageName = SP.getLinkageName();
1082 if (LinkageName) {
Devang Patel76031e82009-07-16 01:01:22 +00001083 // Skip special LLVM prefix that is used to inform the asm printer to not emit
1084 // usual symbol prefix before the symbol name. This happens for Objective-C
1085 // symbol names and symbol whose name is replaced using GCC's __asm__ attribute.
1086 if (LinkageName[0] == 1)
1087 LinkageName = &LinkageName[1];
Devang Patelc50078e2009-11-21 02:48:08 +00001088 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelbd760a52009-07-14 00:55:28 +00001089 LinkageName);
Devang Patel76031e82009-07-16 01:01:22 +00001090 }
Devang Patelc50078e2009-11-21 02:48:08 +00001091 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001092
1093 DICompositeType SPTy = SP.getType();
1094 DIArray Args = SPTy.getTypeArray();
1095
1096 // Add prototyped tag, if C or ObjC.
1097 unsigned Lang = SP.getCompileUnit().getLanguage();
1098 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1099 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001100 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001101
1102 // Add Return Type.
1103 unsigned SPTag = SPTy.getTag();
1104 if (!IsConstructor) {
1105 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001106 addType(DW_Unit, SPDie, SPTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001107 else
Devang Patelc50078e2009-11-21 02:48:08 +00001108 addType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001109 }
1110
1111 if (!SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001112 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001113
1114 // Add arguments. Do not add arguments for subprogram definition. They will
1115 // be handled through RecordVariable.
1116 if (SPTag == dwarf::DW_TAG_subroutine_type)
1117 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1118 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc50078e2009-11-21 02:48:08 +00001119 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
1120 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1121 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001122 }
1123 }
1124
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001125 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Pateld90672c2009-11-20 21:37:22 +00001126 DW_Unit->insertDIE(SP.getNode(), SPDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001127 return SPDie;
1128}
1129
Devang Patelc50078e2009-11-21 02:48:08 +00001130/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001131///
Devang Patelc50078e2009-11-21 02:48:08 +00001132CompileUnit &DwarfDebug::findCompileUnit(DICompileUnit Unit) const {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001133 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patel15e723d2009-08-28 23:24:31 +00001134 CompileUnitMap.find(Unit.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001135 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1136 return *I->second;
1137}
1138
Devang Patelc50078e2009-11-21 02:48:08 +00001139/// createDbgScopeVariable - Create a new scope variable.
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001140///
Devang Patelc50078e2009-11-21 02:48:08 +00001141DIE *DwarfDebug::createDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001142 // Get the descriptor.
1143 const DIVariable &VD = DV->getVariable();
Devang Patel11ac7e72009-11-03 18:30:27 +00001144 const char *Name = VD.getName();
1145 if (!Name)
1146 return NULL;
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001147
1148 // Translate tag to proper Dwarf tag. The result variable is dropped for
1149 // now.
1150 unsigned Tag;
1151 switch (VD.getTag()) {
1152 case dwarf::DW_TAG_return_variable:
1153 return NULL;
1154 case dwarf::DW_TAG_arg_variable:
1155 Tag = dwarf::DW_TAG_formal_parameter;
1156 break;
1157 case dwarf::DW_TAG_auto_variable: // fall thru
1158 default:
1159 Tag = dwarf::DW_TAG_variable;
1160 break;
1161 }
1162
1163 // Define variable debug information entry.
1164 DIE *VariableDie = new DIE(Tag);
Devang Patelc50078e2009-11-21 02:48:08 +00001165 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001166
1167 // Add source line info if available.
Devang Patelc50078e2009-11-21 02:48:08 +00001168 addSourceLine(VariableDie, &VD);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001169
1170 // Add variable type.
Devang Patel90a0fe32009-11-10 23:06:00 +00001171 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
1172 // addresses instead.
Caroline Tice248d5572009-08-31 21:19:37 +00001173 if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001174 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Caroline Tice248d5572009-08-31 21:19:37 +00001175 else
Devang Patelc50078e2009-11-21 02:48:08 +00001176 addType(Unit, VariableDie, VD.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001177
1178 // Add variable address.
Devang Patel90a0fe32009-11-10 23:06:00 +00001179 // Variables for abstract instances of inlined functions don't get a
1180 // location.
1181 MachineLocation Location;
1182 Location.set(RI->getFrameRegister(*MF),
1183 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1184
1185
1186 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001187 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001188 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001189 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001190 else
Devang Patelc50078e2009-11-21 02:48:08 +00001191 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001192
1193 return VariableDie;
1194}
1195
Devang Patel90a0fe32009-11-10 23:06:00 +00001196/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1197/// Initialize scope and update scope hierarchy.
1198DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1199 MDNode *InlinedAt) {
1200 assert (N && "Invalid Scope encoding!");
1201 assert (MI && "Missing machine instruction!");
1202 bool GetConcreteScope = (MI && InlinedAt);
1203
1204 DbgScope *NScope = NULL;
1205
1206 if (InlinedAt)
1207 NScope = DbgScopeMap.lookup(InlinedAt);
1208 else
1209 NScope = DbgScopeMap.lookup(N);
1210 assert (NScope && "Unable to find working scope!");
1211
1212 if (NScope->getFirstInsn())
1213 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001214
1215 DbgScope *Parent = NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001216 if (GetConcreteScope) {
Devang Pateldd7bb432009-10-14 21:08:09 +00001217 DILocation IL(InlinedAt);
Devang Patel90a0fe32009-11-10 23:06:00 +00001218 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
1219 IL.getOrigLocation().getNode());
1220 assert (Parent && "Unable to find Parent scope!");
1221 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001222 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001223 } else if (DIDescriptor(N).isLexicalBlock()) {
1224 DILexicalBlock DB(N);
1225 if (!DB.getContext().isNull()) {
1226 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1227 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001228 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001229 }
Devang Pateldd7bb432009-10-14 21:08:09 +00001230 }
Devang Patel6a260102009-10-01 20:31:14 +00001231
Devang Patelf5278f22009-10-27 20:47:17 +00001232 NScope->setFirstInsn(MI);
Devang Patel6a260102009-10-01 20:31:14 +00001233
Devang Patel90a0fe32009-11-10 23:06:00 +00001234 if (!Parent && !InlinedAt) {
Devang Patelce8986f2009-11-11 00:31:36 +00001235 StringRef SPName = DISubprogram(N).getLinkageName();
1236 if (SPName == MF->getFunction()->getName())
1237 CurrentFnDbgScope = NScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00001238 }
Devang Patel6a260102009-10-01 20:31:14 +00001239
Devang Patel90a0fe32009-11-10 23:06:00 +00001240 if (GetConcreteScope) {
1241 ConcreteScopes[InlinedAt] = NScope;
1242 getOrCreateAbstractScope(N);
1243 }
1244
Devang Patelf5278f22009-10-27 20:47:17 +00001245 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001246}
1247
Devang Patel90a0fe32009-11-10 23:06:00 +00001248DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1249 assert (N && "Invalid Scope encoding!");
1250
1251 DbgScope *AScope = AbstractScopes.lookup(N);
1252 if (AScope)
1253 return AScope;
1254
1255 DbgScope *Parent = NULL;
1256
1257 DIDescriptor Scope(N);
1258 if (Scope.isLexicalBlock()) {
1259 DILexicalBlock DB(N);
1260 DIDescriptor ParentDesc = DB.getContext();
1261 if (!ParentDesc.isNull())
1262 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1263 }
1264
1265 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1266
1267 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001268 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001269 AScope->setAbstractScope();
1270 AbstractScopes[N] = AScope;
1271 if (DIDescriptor(N).isSubprogram())
1272 AbstractScopesList.push_back(AScope);
1273 return AScope;
1274}
Devang Patel6a260102009-10-01 20:31:14 +00001275
Devang Patel90a0fe32009-11-10 23:06:00 +00001276static DISubprogram getDISubprogram(MDNode *N) {
1277
1278 DIDescriptor D(N);
1279 if (D.isNull())
1280 return DISubprogram();
1281
1282 if (D.isCompileUnit())
1283 return DISubprogram();
1284
1285 if (D.isSubprogram())
1286 return DISubprogram(N);
1287
1288 if (D.isLexicalBlock())
1289 return getDISubprogram(DILexicalBlock(N).getContext().getNode());
1290
1291 llvm_unreachable("Unexpected Descriptor!");
1292}
1293
Devang Patelc50078e2009-11-21 02:48:08 +00001294/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
1295/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1296/// If there are global variables in this scope then create and insert
1297/// DIEs for these variables.
1298DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001299
Devang Pateld90672c2009-11-20 21:37:22 +00001300 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel90a0fe32009-11-10 23:06:00 +00001301 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001302 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001303 DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001304 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001305 DWLabel("func_end", SubprogramCount));
1306 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patelc50078e2009-11-21 02:48:08 +00001307 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001308
1309 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001310 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001311
1312 // If there are global variables at this scope then add their dies.
1313 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
1314 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1315 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1316 if (!N) continue;
1317 DIGlobalVariable GV(N);
1318 if (GV.getContext().getNode() == SPNode) {
Devang Patelc50078e2009-11-21 02:48:08 +00001319 DIE *ScopedGVDie = createGlobalVariableDIE(ModuleCU, GV);
Devang Patelbad42262009-11-10 23:20:04 +00001320 if (ScopedGVDie)
Devang Patelc50078e2009-11-21 02:48:08 +00001321 SPDie->addChild(ScopedGVDie);
Devang Patel90a0fe32009-11-10 23:06:00 +00001322 }
1323 }
1324 return SPDie;
1325}
1326
Devang Patelc50078e2009-11-21 02:48:08 +00001327/// constructLexicalScope - Construct new DW_TAG_lexical_block
1328/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1329DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001330 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1331 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1332
1333 // Ignore empty scopes.
1334 if (StartID == EndID && StartID != 0)
1335 return NULL;
1336
1337 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1338 if (Scope->isAbstractScope())
1339 return ScopeDIE;
1340
Devang Patelc50078e2009-11-21 02:48:08 +00001341 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001342 StartID ?
1343 DWLabel("label", StartID)
1344 : DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001345 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001346 EndID ?
1347 DWLabel("label", EndID)
1348 : DWLabel("func_end", SubprogramCount));
1349
1350
1351
1352 return ScopeDIE;
1353}
1354
Devang Patelc50078e2009-11-21 02:48:08 +00001355/// constructInlinedScopeDIE - This scope represents inlined body of
1356/// a function. Construct DIE to represent this concrete inlined copy
1357/// of the function.
1358DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001359 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1360 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1361 assert (StartID && "Invalid starting label for an inlined scope!");
1362 assert (EndID && "Invalid end label for an inlined scope!");
1363 // Ignore empty scopes.
1364 if (StartID == EndID && StartID != 0)
1365 return NULL;
1366
1367 DIScope DS(Scope->getScopeNode());
1368 if (DS.isNull())
1369 return NULL;
1370 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1371
1372 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001373 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001374 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001375 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001376 dwarf::DW_FORM_ref4, OriginDIE);
1377
Devang Patelc50078e2009-11-21 02:48:08 +00001378 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001379 DWLabel("label", StartID));
Devang Patelc50078e2009-11-21 02:48:08 +00001380 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001381 DWLabel("label", EndID));
1382
1383 InlinedSubprogramDIEs.insert(OriginDIE);
1384
1385 // Track the start label for this inlined function.
1386 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1387 I = InlineInfo.find(InlinedSP.getNode());
1388
1389 if (I == InlineInfo.end()) {
1390 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID, ScopeDIE));
1391 InlinedSPNodes.push_back(InlinedSP.getNode());
1392 } else
1393 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1394
1395 StringPool.insert(InlinedSP.getName());
1396 StringPool.insert(InlinedSP.getLinkageName());
1397 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001398 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1399 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001400
1401 return ScopeDIE;
1402}
1403
Devang Patelc50078e2009-11-21 02:48:08 +00001404
1405/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1406DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV,
Devang Patel90a0fe32009-11-10 23:06:00 +00001407 DbgScope *Scope, CompileUnit *Unit) {
1408 // Get the descriptor.
1409 const DIVariable &VD = DV->getVariable();
Devang Patelab9a0682009-11-13 02:25:26 +00001410 const char *Name = VD.getName();
1411 if (!Name)
1412 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001413
1414 // Translate tag to proper Dwarf tag. The result variable is dropped for
1415 // now.
1416 unsigned Tag;
1417 switch (VD.getTag()) {
1418 case dwarf::DW_TAG_return_variable:
1419 return NULL;
1420 case dwarf::DW_TAG_arg_variable:
1421 Tag = dwarf::DW_TAG_formal_parameter;
1422 break;
1423 case dwarf::DW_TAG_auto_variable: // fall thru
1424 default:
1425 Tag = dwarf::DW_TAG_variable;
1426 break;
1427 }
1428
1429 // Define variable debug information entry.
1430 DIE *VariableDie = new DIE(Tag);
1431
1432
1433 DIE *AbsDIE = NULL;
1434 if (DbgVariable *AV = DV->getAbstractVariable())
1435 AbsDIE = AV->getDIE();
1436
1437 if (AbsDIE) {
1438 DIScope DS(Scope->getScopeNode());
1439 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001440 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001441 (void) OriginSPDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001442 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1443 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1444 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001445 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001446 dwarf::DW_FORM_ref4, AbsDIE);
1447 }
1448 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001449 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1450 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001451
1452 // Add variable type.
1453 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
1454 // addresses instead.
1455 if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001456 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001457 else
Devang Patelc50078e2009-11-21 02:48:08 +00001458 addType(Unit, VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001459 }
1460
1461 // Add variable address.
1462 if (!Scope->isAbstractScope()) {
1463 MachineLocation Location;
1464 Location.set(RI->getFrameRegister(*MF),
1465 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1466
1467
1468 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001469 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001470 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001471 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001472 else
Devang Patelc50078e2009-11-21 02:48:08 +00001473 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001474 }
1475 DV->setDIE(VariableDie);
1476 return VariableDie;
1477
1478}
Devang Patelc50078e2009-11-21 02:48:08 +00001479
1480/// constructScopeDIE - Construct a DIE for this scope.
1481DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001482 if (!Scope)
1483 return NULL;
1484 DIScope DS(Scope->getScopeNode());
1485 if (DS.isNull())
1486 return NULL;
1487
1488 DIE *ScopeDIE = NULL;
1489 if (Scope->getInlinedAt())
Devang Patelc50078e2009-11-21 02:48:08 +00001490 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001491 else if (DS.isSubprogram()) {
1492 if (Scope->isAbstractScope())
Devang Pateld90672c2009-11-20 21:37:22 +00001493 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001494 else
Devang Patelc50078e2009-11-21 02:48:08 +00001495 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001496 }
1497 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001498 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001499 if (!ScopeDIE) return NULL;
1500 }
1501
1502 // Add variables to scope.
1503 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1504 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelc50078e2009-11-21 02:48:08 +00001505 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope, ModuleCU);
Devang Patel90a0fe32009-11-10 23:06:00 +00001506 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001507 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001508 }
1509
1510 // Add nested scopes.
1511 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1512 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1513 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001514 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Devang Patel90a0fe32009-11-10 23:06:00 +00001515 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001516 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001517 }
1518 return ScopeDIE;
1519}
1520
Bill Wendlingf5839192009-05-20 23:19:06 +00001521/// GetOrCreateSourceID - Look up the source id with the given directory and
1522/// source file names. If none currently exists, create a new id and insert it
1523/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1524/// maps as well.
Devang Patelaaf012e2009-09-29 18:40:58 +00001525unsigned DwarfDebug::GetOrCreateSourceID(const char *DirName,
1526 const char *FileName) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001527 unsigned DId;
1528 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1529 if (DI != DirectoryIdMap.end()) {
1530 DId = DI->getValue();
1531 } else {
1532 DId = DirectoryNames.size() + 1;
1533 DirectoryIdMap[DirName] = DId;
1534 DirectoryNames.push_back(DirName);
1535 }
1536
1537 unsigned FId;
1538 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1539 if (FI != SourceFileIdMap.end()) {
1540 FId = FI->getValue();
1541 } else {
1542 FId = SourceFileNames.size() + 1;
1543 SourceFileIdMap[FileName] = FId;
1544 SourceFileNames.push_back(FileName);
1545 }
1546
1547 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1548 SourceIdMap.find(std::make_pair(DId, FId));
1549 if (SI != SourceIdMap.end())
1550 return SI->second;
1551
1552 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1553 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1554 SourceIds.push_back(std::make_pair(DId, FId));
1555
1556 return SrcId;
1557}
1558
Devang Patelc50078e2009-11-21 02:48:08 +00001559void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001560 DICompileUnit DIUnit(N);
Devang Patelaaf012e2009-09-29 18:40:58 +00001561 const char *FN = DIUnit.getFilename();
1562 const char *Dir = DIUnit.getDirectory();
1563 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001564
1565 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelc50078e2009-11-21 02:48:08 +00001566 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf5839192009-05-20 23:19:06 +00001567 DWLabel("section_line", 0), DWLabel("section_line", 0),
1568 false);
Devang Patelc50078e2009-11-21 02:48:08 +00001569 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001570 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001571 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001572 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001573 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001574
Devang Patelaaf012e2009-09-29 18:40:58 +00001575 if (Dir)
Devang Patelc50078e2009-11-21 02:48:08 +00001576 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001577 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001578 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001579
Devang Patelaaf012e2009-09-29 18:40:58 +00001580 if (const char *Flags = DIUnit.getFlags())
Devang Patelc50078e2009-11-21 02:48:08 +00001581 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001582
1583 unsigned RVer = DIUnit.getRunTimeVersion();
1584 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001585 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001586 dwarf::DW_FORM_data1, RVer);
1587
1588 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel5a3d37f2009-06-29 20:45:18 +00001589 if (!ModuleCU && DIUnit.isMain()) {
Devang Patelf97a05a2009-06-29 20:38:13 +00001590 // Use first compile unit marked as isMain as the compile unit
1591 // for this module.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001592 ModuleCU = Unit;
Devang Patelf97a05a2009-06-29 20:38:13 +00001593 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001594
Devang Patel15e723d2009-08-28 23:24:31 +00001595 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf5839192009-05-20 23:19:06 +00001596 CompileUnits.push_back(Unit);
1597}
1598
Devang Patelc50078e2009-11-21 02:48:08 +00001599void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001600 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001601
Devang Patel0c03f062009-09-04 23:59:07 +00001602 // If debug information is malformed then ignore it.
1603 if (DI_GV.Verify() == false)
1604 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001605
1606 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001607 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel166f8432009-06-26 01:49:18 +00001608 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001609
Devang Patelc50078e2009-11-21 02:48:08 +00001610 DIE *VariableDie = createGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf5839192009-05-20 23:19:06 +00001611
Bill Wendlingf5839192009-05-20 23:19:06 +00001612 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001613 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001614
1615 // Add to context owner.
Devang Patelc50078e2009-11-21 02:48:08 +00001616 ModuleCU->getCUDie()->addChild(VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001617
1618 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001619 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel166f8432009-06-26 01:49:18 +00001620 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001621}
1622
Devang Patelc50078e2009-11-21 02:48:08 +00001623void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001624 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001625
1626 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001627 if (ModuleCU->getDIE(N))
Devang Patel166f8432009-06-26 01:49:18 +00001628 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001629
1630 if (!SP.isDefinition())
1631 // This is a method declaration which will be handled while constructing
1632 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001633 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001634
Devang Patelc50078e2009-11-21 02:48:08 +00001635 DIE *SubprogramDie = createSubprogramDIE(ModuleCU, SP);
Bill Wendlingf5839192009-05-20 23:19:06 +00001636
1637 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001638 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001639
1640 // Add to context owner.
Devang Patelc50078e2009-11-21 02:48:08 +00001641 ModuleCU->getCUDie()->addChild(SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001642
1643 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001644 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel166f8432009-06-26 01:49:18 +00001645 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001646}
1647
Devang Patelc50078e2009-11-21 02:48:08 +00001648/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001649/// content. Create global DIEs and emit initial debug info sections.
1650/// This is inovked by the target AsmPrinter.
Devang Patelc50078e2009-11-21 02:48:08 +00001651void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel59a1d422009-06-25 22:36:02 +00001652 this->M = M;
1653
Bill Wendlingf5839192009-05-20 23:19:06 +00001654 if (TimePassesIsEnabled)
1655 DebugTimer->startTimer();
1656
Devang Patel1b4d6832009-11-11 19:55:08 +00001657 if (!MAI->doesSupportDebugInformation())
1658 return;
1659
Devang Patelfda766d2009-07-30 18:56:46 +00001660 DebugInfoFinder DbgFinder;
1661 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001662
Bill Wendlingf5839192009-05-20 23:19:06 +00001663 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001664 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1665 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001666 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001667
1668 if (CompileUnits.empty()) {
1669 if (TimePassesIsEnabled)
1670 DebugTimer->stopTimer();
1671
1672 return;
1673 }
1674
Devang Patelf97a05a2009-06-29 20:38:13 +00001675 // If main compile unit for this module is not seen than randomly
1676 // select first compile unit.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001677 if (!ModuleCU)
1678 ModuleCU = CompileUnits[0];
Devang Patelf97a05a2009-06-29 20:38:13 +00001679
Devang Patel166f8432009-06-26 01:49:18 +00001680 // Create DIEs for each of the externally visible global variables.
Devang Patelfda766d2009-07-30 18:56:46 +00001681 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patel695c8b02009-10-05 23:40:42 +00001682 E = DbgFinder.global_variable_end(); I != E; ++I) {
1683 DIGlobalVariable GV(*I);
1684 if (GV.getContext().getNode() != GV.getCompileUnit().getNode())
1685 ScopedGVs.push_back(*I);
1686 else
Devang Patelc50078e2009-11-21 02:48:08 +00001687 constructGlobalVariableDIE(*I);
Devang Patel695c8b02009-10-05 23:40:42 +00001688 }
Devang Patel166f8432009-06-26 01:49:18 +00001689
Devang Patel90a0fe32009-11-10 23:06:00 +00001690 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001691 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1692 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001693 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001694
Bill Wendlingf5839192009-05-20 23:19:06 +00001695 MMI = mmi;
1696 shouldEmit = true;
1697 MMI->setDebugInfoAvailability(true);
1698
1699 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001700 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001701
1702 // Print out .file directives to specify files for .loc directives. These are
1703 // printed out early so that they precede any .loc directives.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001704 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001705 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1706 // Remember source id starts at 1.
1707 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1708 sys::Path FullPath(getSourceDirectoryName(Id.first));
1709 bool AppendOk =
1710 FullPath.appendComponent(getSourceFileName(Id.second));
1711 assert(AppendOk && "Could not append filename to directory!");
1712 AppendOk = false;
Chris Lattnerb1aa85b2009-08-23 22:45:37 +00001713 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001714 Asm->EOL();
1715 }
1716 }
1717
1718 // Emit initial sections
Devang Patelc50078e2009-11-21 02:48:08 +00001719 emitInitial();
Bill Wendlingf5839192009-05-20 23:19:06 +00001720
1721 if (TimePassesIsEnabled)
1722 DebugTimer->stopTimer();
1723}
1724
Devang Patelc50078e2009-11-21 02:48:08 +00001725/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001726///
Devang Patelc50078e2009-11-21 02:48:08 +00001727void DwarfDebug::endModule() {
Devang Patel95d477e2009-10-06 00:03:14 +00001728 if (!ModuleCU)
Bill Wendlingf5839192009-05-20 23:19:06 +00001729 return;
1730
1731 if (TimePassesIsEnabled)
1732 DebugTimer->startTimer();
1733
Devang Patel90a0fe32009-11-10 23:06:00 +00001734 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1735 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1736 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1737 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001738 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001739 }
1740
Bill Wendlingf5839192009-05-20 23:19:06 +00001741 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001742 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001743 EmitLabel("text_end", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00001744 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001745 EmitLabel("data_end", 0);
1746
1747 // End text sections.
1748 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001749 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf5839192009-05-20 23:19:06 +00001750 EmitLabel("section_end", i);
1751 }
1752
1753 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001754 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001755
1756 // Emit function debug frame information
1757 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1758 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001759 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001760
1761 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001762 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001763
1764 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001765 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001766
1767 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00001768 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00001769
1770 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00001771 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00001772
1773 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00001774 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00001775
1776 // Emit info into a debug str section.
Devang Patelc50078e2009-11-21 02:48:08 +00001777 emitDebugStr();
Bill Wendlingf5839192009-05-20 23:19:06 +00001778
1779 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00001780 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00001781
1782 // Emit info into a debug aranges section.
1783 EmitDebugARanges();
1784
1785 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00001786 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00001787
1788 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00001789 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001790
1791 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00001792 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001793
1794 if (TimePassesIsEnabled)
1795 DebugTimer->stopTimer();
1796}
1797
Devang Patel90a0fe32009-11-10 23:06:00 +00001798/// findAbstractVariable - Find abstract variable, if any, associated with Var.
1799DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
1800 DILocation &ScopeLoc) {
1801
1802 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1803 if (AbsDbgVariable)
1804 return AbsDbgVariable;
1805
1806 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1807 if (!Scope)
1808 return NULL;
1809
1810 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patelc50078e2009-11-21 02:48:08 +00001811 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00001812 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1813 return AbsDbgVariable;
1814}
1815
Devang Patelc50078e2009-11-21 02:48:08 +00001816/// collectVariableInfo - Populate DbgScope entries with variables' info.
1817void DwarfDebug::collectVariableInfo() {
Devang Patel40c80212009-10-09 22:42:28 +00001818 if (!MMI) return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001819
Devang Patel84139992009-10-06 01:26:37 +00001820 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1821 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1822 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel40c80212009-10-09 22:42:28 +00001823 MetadataBase *MB = VI->first;
1824 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel90a0fe32009-11-10 23:06:00 +00001825 if (!Var) continue;
Devang Patel6882dff2009-10-08 18:48:03 +00001826 DIVariable DV (Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00001827 std::pair< unsigned, MDNode *> VP = VI->second;
1828 DILocation ScopeLoc(VP.second);
1829
1830 DbgScope *Scope =
1831 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1832 if (!Scope)
1833 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelbad42262009-11-10 23:20:04 +00001834 // If variable scope is not found then skip this variable.
1835 if (!Scope)
1836 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00001837
1838 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patelc50078e2009-11-21 02:48:08 +00001839 Scope->addVariable(RegVar);
Devang Patel90a0fe32009-11-10 23:06:00 +00001840 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, ScopeLoc))
1841 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patel84139992009-10-06 01:26:37 +00001842 }
1843}
1844
Devang Patelc50078e2009-11-21 02:48:08 +00001845/// beginScope - Process beginning of a scope starting at Label.
1846void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel393a46d2009-10-06 01:50:42 +00001847 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1848 if (I == DbgScopeBeginMap.end())
1849 return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001850 ScopeVector &SD = DbgScopeBeginMap[MI];
1851 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Devang Patel393a46d2009-10-06 01:50:42 +00001852 SDI != SDE; ++SDI)
1853 (*SDI)->setStartLabelID(Label);
1854}
1855
Devang Patelc50078e2009-11-21 02:48:08 +00001856/// endScope - Process end of a scope.
1857void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel393a46d2009-10-06 01:50:42 +00001858 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patelf4348892009-10-06 03:15:38 +00001859 if (I == DbgScopeEndMap.end())
Devang Patel393a46d2009-10-06 01:50:42 +00001860 return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001861
1862 unsigned Label = MMI->NextLabelID();
1863 Asm->printLabel(Label);
1864
Devang Patel393a46d2009-10-06 01:50:42 +00001865 SmallVector<DbgScope *, 2> &SD = I->second;
1866 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
1867 SDI != SDE; ++SDI)
1868 (*SDI)->setEndLabelID(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00001869 return;
1870}
1871
1872/// createDbgScope - Create DbgScope for the scope.
1873void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1874
1875 if (!InlinedAt) {
1876 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1877 if (WScope)
1878 return;
1879 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1880 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Devang Patel53addbf2009-11-11 00:18:40 +00001881 if (DIDescriptor(Scope).isLexicalBlock())
1882 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel90a0fe32009-11-10 23:06:00 +00001883 return;
1884 }
1885
1886 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1887 if (WScope)
1888 return;
1889
1890 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1891 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
1892 DILocation DL(InlinedAt);
1893 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel393a46d2009-10-06 01:50:42 +00001894}
1895
Devang Patelc50078e2009-11-21 02:48:08 +00001896/// extractScopeInformation - Scan machine instructions in this function
Devang Patel6a260102009-10-01 20:31:14 +00001897/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patelc50078e2009-11-21 02:48:08 +00001898bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patel6a260102009-10-01 20:31:14 +00001899 // If scope information was extracted using .dbg intrinsics then there is not
1900 // any need to extract these information by scanning each instruction.
1901 if (!DbgScopeMap.empty())
1902 return false;
1903
Devang Patel90a0fe32009-11-10 23:06:00 +00001904 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6a260102009-10-01 20:31:14 +00001905 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1906 I != E; ++I) {
1907 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1908 II != IE; ++II) {
1909 const MachineInstr *MInsn = II;
1910 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00001911 if (DL.isUnknown()) continue;
Devang Patel6a260102009-10-01 20:31:14 +00001912 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel90a0fe32009-11-10 23:06:00 +00001913 if (!DLT.Scope) continue;
Devang Patel6a260102009-10-01 20:31:14 +00001914 // There is no need to create another DIE for compile unit. For all
1915 // other scopes, create one DbgScope now. This will be translated
1916 // into a scope DIE at the end.
Devang Patel90a0fe32009-11-10 23:06:00 +00001917 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1918 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
1919 }
1920 }
1921
1922
1923 // Build scope hierarchy using working set of scopes.
1924 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1925 I != E; ++I) {
1926 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1927 II != IE; ++II) {
1928 const MachineInstr *MInsn = II;
1929 DebugLoc DL = MInsn->getDebugLoc();
1930 if (DL.isUnknown()) continue;
1931 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
1932 if (!DLT.Scope) continue;
1933 // There is no need to create another DIE for compile unit. For all
1934 // other scopes, create one DbgScope now. This will be translated
1935 // into a scope DIE at the end.
1936 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1937 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
1938 Scope->setLastInsn(MInsn);
Devang Patel6a260102009-10-01 20:31:14 +00001939 }
1940 }
1941
1942 // If a scope's last instruction is not set then use its child scope's
1943 // last instruction as this scope's last instrunction.
Devang Patelf5278f22009-10-27 20:47:17 +00001944 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00001945 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001946 if (DI->second->isAbstractScope())
1947 continue;
Devang Patel6a260102009-10-01 20:31:14 +00001948 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patelc50078e2009-11-21 02:48:08 +00001949 DI->second->fixInstructionMarkers();
Devang Patel6a260102009-10-01 20:31:14 +00001950 assert (DI->second->getLastInsn() && "Invalid last instruction!");
1951 }
1952
1953 // Each scope has first instruction and last instruction to mark beginning
1954 // and end of a scope respectively. Create an inverse map that list scopes
1955 // starts (and ends) with an instruction. One instruction may start (or end)
1956 // multiple scopes.
Devang Patelf5278f22009-10-27 20:47:17 +00001957 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00001958 DE = DbgScopeMap.end(); DI != DE; ++DI) {
1959 DbgScope *S = DI->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00001960 if (S->isAbstractScope())
1961 continue;
Devang Patel6a260102009-10-01 20:31:14 +00001962 const MachineInstr *MI = S->getFirstInsn();
1963 assert (MI && "DbgScope does not have first instruction!");
1964
1965 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
1966 if (IDI != DbgScopeBeginMap.end())
1967 IDI->second.push_back(S);
1968 else
Devang Patel90a0fe32009-11-10 23:06:00 +00001969 DbgScopeBeginMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00001970
1971 MI = S->getLastInsn();
1972 assert (MI && "DbgScope does not have last instruction!");
1973 IDI = DbgScopeEndMap.find(MI);
1974 if (IDI != DbgScopeEndMap.end())
1975 IDI->second.push_back(S);
1976 else
Devang Patel90a0fe32009-11-10 23:06:00 +00001977 DbgScopeEndMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00001978 }
1979
1980 return !DbgScopeMap.empty();
1981}
1982
Devang Patelc50078e2009-11-21 02:48:08 +00001983/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00001984/// emitted immediately after the function entry point.
Devang Patelc50078e2009-11-21 02:48:08 +00001985void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001986 this->MF = MF;
1987
1988 if (!ShouldEmitDwarfDebug()) return;
1989
1990 if (TimePassesIsEnabled)
1991 DebugTimer->startTimer();
1992
Devang Patelc50078e2009-11-21 02:48:08 +00001993 if (!extractScopeInformation(MF))
Devang Patel0feae422009-10-06 18:37:31 +00001994 return;
Devang Patelc50078e2009-11-21 02:48:08 +00001995
1996 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00001997
Bill Wendlingf5839192009-05-20 23:19:06 +00001998 // Begin accumulating function debug information.
1999 MMI->BeginFunction(MF);
2000
2001 // Assumes in correct section after the entry point.
2002 EmitLabel("func_begin", ++SubprogramCount);
2003
2004 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2005 // function.
Devang Patel40c80212009-10-09 22:42:28 +00002006 DebugLoc FDL = MF->getDefaultDebugLoc();
2007 if (!FDL.isUnknown()) {
2008 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2009 unsigned LabelID = 0;
Devang Patelfc1df342009-10-13 23:28:53 +00002010 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002011 if (!SP.isNull())
Devang Patelc50078e2009-11-21 02:48:08 +00002012 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002013 else
Devang Patelc50078e2009-11-21 02:48:08 +00002014 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002015 Asm->printLabel(LabelID);
2016 O << '\n';
Bill Wendlingf5839192009-05-20 23:19:06 +00002017 }
Bill Wendlingf5839192009-05-20 23:19:06 +00002018 if (TimePassesIsEnabled)
2019 DebugTimer->stopTimer();
2020}
2021
Devang Patelc50078e2009-11-21 02:48:08 +00002022/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002023///
Devang Patelc50078e2009-11-21 02:48:08 +00002024void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002025 if (!ShouldEmitDwarfDebug()) return;
2026
2027 if (TimePassesIsEnabled)
2028 DebugTimer->startTimer();
2029
Devang Patel40c80212009-10-09 22:42:28 +00002030 if (DbgScopeMap.empty())
2031 return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002032
Bill Wendlingf5839192009-05-20 23:19:06 +00002033 // Define end label for subprogram.
2034 EmitLabel("func_end", SubprogramCount);
2035
2036 // Get function line info.
2037 if (!Lines.empty()) {
2038 // Get section line info.
Chris Lattnerebd055c2009-08-03 23:20:21 +00002039 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00002040 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2041 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2042 // Append the function info to section info.
2043 SectionLineInfos.insert(SectionLineInfos.end(),
2044 Lines.begin(), Lines.end());
2045 }
2046
Devang Patel90a0fe32009-11-10 23:06:00 +00002047 // Construct abstract scopes.
2048 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2049 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patelc50078e2009-11-21 02:48:08 +00002050 constructScopeDIE(*AI);
Bill Wendlingf5839192009-05-20 23:19:06 +00002051
Devang Patelc50078e2009-11-21 02:48:08 +00002052 constructScopeDIE(CurrentFnDbgScope);
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002053
Bill Wendlingf5839192009-05-20 23:19:06 +00002054 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2055 MMI->getFrameMoves()));
2056
2057 // Clear debug info
Devang Patel90a0fe32009-11-10 23:06:00 +00002058 if (CurrentFnDbgScope) {
2059 CurrentFnDbgScope = NULL;
Bill Wendlingf5839192009-05-20 23:19:06 +00002060 DbgScopeMap.clear();
Devang Patel6a260102009-10-01 20:31:14 +00002061 DbgScopeBeginMap.clear();
2062 DbgScopeEndMap.clear();
Devang Patel90a0fe32009-11-10 23:06:00 +00002063 ConcreteScopes.clear();
2064 AbstractScopesList.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002065 }
2066
2067 Lines.clear();
2068
2069 if (TimePassesIsEnabled)
2070 DebugTimer->stopTimer();
2071}
2072
Devang Patelc50078e2009-11-21 02:48:08 +00002073/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf5839192009-05-20 23:19:06 +00002074/// label. Returns a unique label ID used to generate a label and provide
2075/// correspondence to the source line list.
Devang Patelc50078e2009-11-21 02:48:08 +00002076unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel946d0ae2009-10-05 18:03:19 +00002077 MDNode *S) {
Devang Patel15e723d2009-08-28 23:24:31 +00002078 if (!MMI)
2079 return 0;
2080
Bill Wendlingf5839192009-05-20 23:19:06 +00002081 if (TimePassesIsEnabled)
2082 DebugTimer->startTimer();
2083
Devang Patel946d0ae2009-10-05 18:03:19 +00002084 const char *Dir = NULL;
2085 const char *Fn = NULL;
2086
2087 DIDescriptor Scope(S);
2088 if (Scope.isCompileUnit()) {
2089 DICompileUnit CU(S);
2090 Dir = CU.getDirectory();
2091 Fn = CU.getFilename();
2092 } else if (Scope.isSubprogram()) {
2093 DISubprogram SP(S);
2094 Dir = SP.getDirectory();
2095 Fn = SP.getFilename();
2096 } else if (Scope.isLexicalBlock()) {
2097 DILexicalBlock DB(S);
2098 Dir = DB.getDirectory();
2099 Fn = DB.getFilename();
2100 } else
2101 assert (0 && "Unexpected scope info");
2102
2103 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf5839192009-05-20 23:19:06 +00002104 unsigned ID = MMI->NextLabelID();
2105 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2106
2107 if (TimePassesIsEnabled)
2108 DebugTimer->stopTimer();
2109
2110 return ID;
2111}
2112
2113/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2114/// timed. Look up the source id with the given directory and source file
2115/// names. If none currently exists, create a new id and insert it in the
2116/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2117/// well.
2118unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2119 const std::string &FileName) {
2120 if (TimePassesIsEnabled)
2121 DebugTimer->startTimer();
2122
Devang Patelaaf012e2009-09-29 18:40:58 +00002123 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf5839192009-05-20 23:19:06 +00002124
2125 if (TimePassesIsEnabled)
2126 DebugTimer->stopTimer();
2127
2128 return SrcId;
2129}
2130
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002131//===----------------------------------------------------------------------===//
2132// Emit Methods
2133//===----------------------------------------------------------------------===//
2134
Devang Patelc50078e2009-11-21 02:48:08 +00002135/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002136///
Devang Patelc50078e2009-11-21 02:48:08 +00002137unsigned DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002138 // Get the children.
2139 const std::vector<DIE *> &Children = Die->getChildren();
2140
2141 // If not last sibling and has children then add sibling offset attribute.
Devang Patelc50078e2009-11-21 02:48:08 +00002142 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002143
2144 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002145 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002146
2147 // Get the abbreviation for this DIE.
2148 unsigned AbbrevNumber = Die->getAbbrevNumber();
2149 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2150
2151 // Set DIE offset
2152 Die->setOffset(Offset);
2153
2154 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002155 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002156
2157 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2158 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2159
2160 // Size the DIE attribute values.
2161 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2162 // Size attribute value.
2163 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2164
2165 // Size the DIE children if any.
2166 if (!Children.empty()) {
2167 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2168 "Children flag not set");
2169
2170 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002171 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002172
2173 // End of children marker.
2174 Offset += sizeof(int8_t);
2175 }
2176
2177 Die->setSize(Offset - Die->getOffset());
2178 return Offset;
2179}
2180
Devang Patelc50078e2009-11-21 02:48:08 +00002181/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002182///
Devang Patelc50078e2009-11-21 02:48:08 +00002183void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002184 // Compute size of compile unit header.
2185 static unsigned Offset =
2186 sizeof(int32_t) + // Length of Compilation Unit Info
2187 sizeof(int16_t) + // DWARF version number
2188 sizeof(int32_t) + // Offset Into Abbrev. Section
2189 sizeof(int8_t); // Pointer Size (in bytes)
2190
Devang Patelc50078e2009-11-21 02:48:08 +00002191 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel5a3d37f2009-06-29 20:45:18 +00002192 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling55fccda2009-05-20 23:21:38 +00002193}
2194
Devang Patelc50078e2009-11-21 02:48:08 +00002195/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling55fccda2009-05-20 23:21:38 +00002196/// tools to recognize the object file contains Dwarf information.
Devang Patelc50078e2009-11-21 02:48:08 +00002197void DwarfDebug::emitInitial() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002198 // Check to see if we already emitted intial headers.
2199 if (didInitial) return;
2200 didInitial = true;
2201
Chris Lattner73266f92009-08-19 05:49:37 +00002202 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002203
Bill Wendling55fccda2009-05-20 23:21:38 +00002204 // Dwarf sections base addresses.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002205 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner73266f92009-08-19 05:49:37 +00002206 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002207 EmitLabel("section_debug_frame", 0);
2208 }
2209
Chris Lattner73266f92009-08-19 05:49:37 +00002210 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002211 EmitLabel("section_info", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002212 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002213 EmitLabel("section_abbrev", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002214 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002215 EmitLabel("section_aranges", 0);
2216
Chris Lattner73266f92009-08-19 05:49:37 +00002217 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2218 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling55fccda2009-05-20 23:21:38 +00002219 EmitLabel("section_macinfo", 0);
2220 }
2221
Chris Lattner73266f92009-08-19 05:49:37 +00002222 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002223 EmitLabel("section_line", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002224 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002225 EmitLabel("section_loc", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002226 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002227 EmitLabel("section_pubnames", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002228 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002229 EmitLabel("section_str", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002230 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002231 EmitLabel("section_ranges", 0);
2232
Chris Lattner73266f92009-08-19 05:49:37 +00002233 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002234 EmitLabel("text_begin", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002235 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002236 EmitLabel("data_begin", 0);
2237}
2238
Devang Patelc50078e2009-11-21 02:48:08 +00002239/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002240///
Devang Patelc50078e2009-11-21 02:48:08 +00002241void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002242 // Get the abbreviation for this DIE.
2243 unsigned AbbrevNumber = Die->getAbbrevNumber();
2244 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2245
2246 Asm->EOL();
2247
2248 // Emit the code (index) for the abbreviation.
2249 Asm->EmitULEB128Bytes(AbbrevNumber);
2250
2251 if (Asm->isVerbose())
2252 Asm->EOL(std::string("Abbrev [" +
2253 utostr(AbbrevNumber) +
2254 "] 0x" + utohexstr(Die->getOffset()) +
2255 ":0x" + utohexstr(Die->getSize()) + " " +
2256 dwarf::TagString(Abbrev->getTag())));
2257 else
2258 Asm->EOL();
2259
2260 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2261 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2262
2263 // Emit the DIE attribute values.
2264 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2265 unsigned Attr = AbbrevData[i].getAttribute();
2266 unsigned Form = AbbrevData[i].getForm();
2267 assert(Form && "Too many attributes for DIE (check abbreviation)");
2268
2269 switch (Attr) {
2270 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002271 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002272 break;
2273 case dwarf::DW_AT_abstract_origin: {
2274 DIEEntry *E = cast<DIEEntry>(Values[i]);
2275 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002276 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002277 Asm->EmitInt32(Addr);
2278 break;
2279 }
2280 default:
2281 // Emit an attribute using the defined form.
2282 Values[i]->EmitValue(this, Form);
2283 break;
2284 }
2285
2286 Asm->EOL(dwarf::AttributeString(Attr));
2287 }
2288
2289 // Emit the DIE children if any.
2290 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2291 const std::vector<DIE *> &Children = Die->getChildren();
2292
2293 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002294 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002295
2296 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2297 }
2298}
2299
Devang Patelc50078e2009-11-21 02:48:08 +00002300/// emitDebugInfo / emitDebugInfoPerCU - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002301///
Devang Patelc50078e2009-11-21 02:48:08 +00002302void DwarfDebug::emitDebugInfoPerCU(CompileUnit *Unit) {
Devang Pateld90672c2009-11-20 21:37:22 +00002303 DIE *Die = Unit->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002304
2305 // Emit the compile units header.
2306 EmitLabel("info_begin", Unit->getID());
2307
2308 // Emit size of content not including length itself
2309 unsigned ContentSize = Die->getSize() +
2310 sizeof(int16_t) + // DWARF version number
2311 sizeof(int32_t) + // Offset Into Abbrev. Section
2312 sizeof(int8_t) + // Pointer Size (in bytes)
2313 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2314
2315 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2316 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2317 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2318 Asm->EOL("Offset Into Abbrev. Section");
2319 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2320
Devang Patelc50078e2009-11-21 02:48:08 +00002321 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002322 // FIXME - extra padding for gdb bug.
2323 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2324 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2325 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2326 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2327 EmitLabel("info_end", Unit->getID());
2328
2329 Asm->EOL();
2330}
2331
Devang Patelc50078e2009-11-21 02:48:08 +00002332void DwarfDebug::emitDebugInfo() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002333 // Start debug info section.
Chris Lattner73266f92009-08-19 05:49:37 +00002334 Asm->OutStreamer.SwitchSection(
2335 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002336
Devang Patelc50078e2009-11-21 02:48:08 +00002337 emitDebugInfoPerCU(ModuleCU);
Bill Wendling55fccda2009-05-20 23:21:38 +00002338}
2339
Devang Patelc50078e2009-11-21 02:48:08 +00002340/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002341///
Devang Patelc50078e2009-11-21 02:48:08 +00002342void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002343 // Check to see if it is worth the effort.
2344 if (!Abbreviations.empty()) {
2345 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002346 Asm->OutStreamer.SwitchSection(
2347 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002348
2349 EmitLabel("abbrev_begin", 0);
2350
2351 // For each abbrevation.
2352 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2353 // Get abbreviation data
2354 const DIEAbbrev *Abbrev = Abbreviations[i];
2355
2356 // Emit the abbrevations code (base 1 index.)
2357 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2358 Asm->EOL("Abbreviation Code");
2359
2360 // Emit the abbreviations data.
2361 Abbrev->Emit(Asm);
2362
2363 Asm->EOL();
2364 }
2365
2366 // Mark end of abbreviations.
2367 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2368
2369 EmitLabel("abbrev_end", 0);
2370 Asm->EOL();
2371 }
2372}
2373
Devang Patelc50078e2009-11-21 02:48:08 +00002374/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002375/// the line matrix.
2376///
Devang Patelc50078e2009-11-21 02:48:08 +00002377void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002378 // Define last address of section.
2379 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2380 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2381 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2382 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2383
2384 // Mark end of matrix.
2385 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2386 Asm->EmitULEB128Bytes(1); Asm->EOL();
2387 Asm->EmitInt8(1); Asm->EOL();
2388}
2389
Devang Patelc50078e2009-11-21 02:48:08 +00002390/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002391///
Devang Patelc50078e2009-11-21 02:48:08 +00002392void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002393 // If the target is using .loc/.file, the assembler will be emitting the
2394 // .debug_line table automatically.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002395 if (MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002396 return;
2397
2398 // Minimum line delta, thus ranging from -10..(255-10).
2399 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2400 // Maximum line delta, thus ranging from -10..(255-10).
2401 const int MaxLineDelta = 255 + MinLineDelta;
2402
2403 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002404 Asm->OutStreamer.SwitchSection(
2405 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002406
2407 // Construct the section header.
2408 EmitDifference("line_end", 0, "line_begin", 0, true);
2409 Asm->EOL("Length of Source Line Info");
2410 EmitLabel("line_begin", 0);
2411
2412 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2413
2414 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2415 Asm->EOL("Prolog Length");
2416 EmitLabel("line_prolog_begin", 0);
2417
2418 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2419
2420 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2421
2422 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2423
2424 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2425
2426 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2427
2428 // Line number standard opcode encodings argument count
2429 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2430 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2431 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2432 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2433 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2434 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2435 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2436 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2437 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2438
2439 // Emit directories.
2440 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2441 Asm->EmitString(getSourceDirectoryName(DI));
2442 Asm->EOL("Directory");
2443 }
2444
2445 Asm->EmitInt8(0); Asm->EOL("End of directories");
2446
2447 // Emit files.
2448 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2449 // Remember source id starts at 1.
2450 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2451 Asm->EmitString(getSourceFileName(Id.second));
2452 Asm->EOL("Source");
2453 Asm->EmitULEB128Bytes(Id.first);
2454 Asm->EOL("Directory #");
2455 Asm->EmitULEB128Bytes(0);
2456 Asm->EOL("Mod date");
2457 Asm->EmitULEB128Bytes(0);
2458 Asm->EOL("File size");
2459 }
2460
2461 Asm->EmitInt8(0); Asm->EOL("End of files");
2462
2463 EmitLabel("line_prolog_end", 0);
2464
2465 // A sequence for each text section.
2466 unsigned SecSrcLinesSize = SectionSourceLines.size();
2467
2468 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2469 // Isolate current sections line info.
2470 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2471
Chris Lattner26aabb92009-08-08 23:39:42 +00002472 /*if (Asm->isVerbose()) {
Chris Lattnere6ad12f2009-07-31 18:48:30 +00002473 const MCSection *S = SectionMap[j + 1];
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002474 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling55fccda2009-05-20 23:21:38 +00002475 << S->getName() << '\n';
Chris Lattner26aabb92009-08-08 23:39:42 +00002476 }*/
2477 Asm->EOL();
Bill Wendling55fccda2009-05-20 23:21:38 +00002478
2479 // Dwarf assumes we start with first line of first source file.
2480 unsigned Source = 1;
2481 unsigned Line = 1;
2482
2483 // Construct rows of the address, source, line, column matrix.
2484 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2485 const SrcLineInfo &LineInfo = LineInfos[i];
2486 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2487 if (!LabelID) continue;
2488
Caroline Tice9da96d82009-09-11 18:25:54 +00002489 if (LineInfo.getLine() == 0) continue;
2490
Bill Wendling55fccda2009-05-20 23:21:38 +00002491 if (!Asm->isVerbose())
2492 Asm->EOL();
2493 else {
2494 std::pair<unsigned, unsigned> SourceID =
2495 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002496 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling55fccda2009-05-20 23:21:38 +00002497 << getSourceDirectoryName(SourceID.first) << ' '
2498 << getSourceFileName(SourceID.second)
2499 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2500 }
2501
2502 // Define the line address.
2503 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2504 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2505 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2506 EmitReference("label", LabelID); Asm->EOL("Location label");
2507
2508 // If change of source, then switch to the new source.
2509 if (Source != LineInfo.getSourceID()) {
2510 Source = LineInfo.getSourceID();
2511 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2512 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2513 }
2514
2515 // If change of line.
2516 if (Line != LineInfo.getLine()) {
2517 // Determine offset.
2518 int Offset = LineInfo.getLine() - Line;
2519 int Delta = Offset - MinLineDelta;
2520
2521 // Update line.
2522 Line = LineInfo.getLine();
2523
2524 // If delta is small enough and in range...
2525 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2526 // ... then use fast opcode.
2527 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2528 } else {
2529 // ... otherwise use long hand.
2530 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2531 Asm->EOL("DW_LNS_advance_line");
2532 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2533 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2534 }
2535 } else {
2536 // Copy the previous row (different address or source)
2537 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2538 }
2539 }
2540
Devang Patelc50078e2009-11-21 02:48:08 +00002541 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002542 }
2543
2544 if (SecSrcLinesSize == 0)
2545 // Because we're emitting a debug_line section, we still need a line
2546 // table. The linker and friends expect it to exist. If there's nothing to
2547 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00002548 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002549
2550 EmitLabel("line_end", 0);
2551 Asm->EOL();
2552}
2553
Devang Patelc50078e2009-11-21 02:48:08 +00002554/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002555///
Devang Patelc50078e2009-11-21 02:48:08 +00002556void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002557 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002558 return;
2559
2560 int stackGrowth =
2561 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2562 TargetFrameInfo::StackGrowsUp ?
2563 TD->getPointerSize() : -TD->getPointerSize();
2564
2565 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002566 Asm->OutStreamer.SwitchSection(
2567 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002568
2569 EmitLabel("debug_frame_common", 0);
2570 EmitDifference("debug_frame_common_end", 0,
2571 "debug_frame_common_begin", 0, true);
2572 Asm->EOL("Length of Common Information Entry");
2573
2574 EmitLabel("debug_frame_common_begin", 0);
2575 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2576 Asm->EOL("CIE Identifier Tag");
2577 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2578 Asm->EOL("CIE Version");
2579 Asm->EmitString("");
2580 Asm->EOL("CIE Augmentation");
2581 Asm->EmitULEB128Bytes(1);
2582 Asm->EOL("CIE Code Alignment Factor");
2583 Asm->EmitSLEB128Bytes(stackGrowth);
2584 Asm->EOL("CIE Data Alignment Factor");
2585 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2586 Asm->EOL("CIE RA Column");
2587
2588 std::vector<MachineMove> Moves;
2589 RI->getInitialFrameState(Moves);
2590
2591 EmitFrameMoves(NULL, 0, Moves, false);
2592
2593 Asm->EmitAlignment(2, 0, 0, false);
2594 EmitLabel("debug_frame_common_end", 0);
2595
2596 Asm->EOL();
2597}
2598
Devang Patelc50078e2009-11-21 02:48:08 +00002599/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00002600/// section.
2601void
Devang Patelc50078e2009-11-21 02:48:08 +00002602DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002603 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002604 return;
2605
2606 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002607 Asm->OutStreamer.SwitchSection(
2608 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002609
2610 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2611 "debug_frame_begin", DebugFrameInfo.Number, true);
2612 Asm->EOL("Length of Frame Information Entry");
2613
2614 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2615
2616 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2617 0, 0, true, false);
2618 Asm->EOL("FDE CIE offset");
2619
2620 EmitReference("func_begin", DebugFrameInfo.Number);
2621 Asm->EOL("FDE initial location");
2622 EmitDifference("func_end", DebugFrameInfo.Number,
2623 "func_begin", DebugFrameInfo.Number);
2624 Asm->EOL("FDE address range");
2625
2626 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2627 false);
2628
2629 Asm->EmitAlignment(2, 0, 0, false);
2630 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2631
2632 Asm->EOL();
2633}
2634
Devang Patelc50078e2009-11-21 02:48:08 +00002635void DwarfDebug::emitDebugPubNamesPerCU(CompileUnit *Unit) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002636 EmitDifference("pubnames_end", Unit->getID(),
2637 "pubnames_begin", Unit->getID(), true);
2638 Asm->EOL("Length of Public Names Info");
2639
2640 EmitLabel("pubnames_begin", Unit->getID());
2641
2642 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2643
2644 EmitSectionOffset("info_begin", "section_info",
2645 Unit->getID(), 0, true, false);
2646 Asm->EOL("Offset of Compilation Unit Info");
2647
2648 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2649 true);
2650 Asm->EOL("Compilation Unit Length");
2651
2652 StringMap<DIE*> &Globals = Unit->getGlobals();
2653 for (StringMap<DIE*>::const_iterator
2654 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2655 const char *Name = GI->getKeyData();
2656 DIE * Entity = GI->second;
2657
2658 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2659 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2660 }
2661
2662 Asm->EmitInt32(0); Asm->EOL("End Mark");
2663 EmitLabel("pubnames_end", Unit->getID());
2664
2665 Asm->EOL();
2666}
2667
Devang Patelc50078e2009-11-21 02:48:08 +00002668/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002669///
Devang Patelc50078e2009-11-21 02:48:08 +00002670void DwarfDebug::emitDebugPubNames() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002671 // Start the dwarf pubnames section.
Chris Lattner73266f92009-08-19 05:49:37 +00002672 Asm->OutStreamer.SwitchSection(
2673 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002674
Devang Patelc50078e2009-11-21 02:48:08 +00002675 emitDebugPubNamesPerCU(ModuleCU);
Bill Wendling55fccda2009-05-20 23:21:38 +00002676}
2677
Devang Patelc50078e2009-11-21 02:48:08 +00002678/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002679///
Devang Patelc50078e2009-11-21 02:48:08 +00002680void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002681 // Check to see if it is worth the effort.
2682 if (!StringPool.empty()) {
2683 // Start the dwarf str section.
Chris Lattner73266f92009-08-19 05:49:37 +00002684 Asm->OutStreamer.SwitchSection(
2685 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002686
2687 // For each of strings in the string pool.
2688 for (unsigned StringID = 1, N = StringPool.size();
2689 StringID <= N; ++StringID) {
2690 // Emit a label for reference from debug information entries.
2691 EmitLabel("string", StringID);
2692
2693 // Emit the string itself.
2694 const std::string &String = StringPool[StringID];
2695 Asm->EmitString(String); Asm->EOL();
2696 }
2697
2698 Asm->EOL();
2699 }
2700}
2701
Devang Patelc50078e2009-11-21 02:48:08 +00002702/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002703///
Devang Patelc50078e2009-11-21 02:48:08 +00002704void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002705 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00002706 Asm->OutStreamer.SwitchSection(
2707 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002708 Asm->EOL();
2709}
2710
2711/// EmitDebugARanges - Emit visible names into a debug aranges section.
2712///
2713void DwarfDebug::EmitDebugARanges() {
2714 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002715 Asm->OutStreamer.SwitchSection(
2716 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002717
2718 // FIXME - Mock up
2719#if 0
2720 CompileUnit *Unit = GetBaseCompileUnit();
2721
2722 // Don't include size of length
2723 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2724
2725 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2726
2727 EmitReference("info_begin", Unit->getID());
2728 Asm->EOL("Offset of Compilation Unit Info");
2729
2730 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2731
2732 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2733
2734 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2735 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2736
2737 // Range 1
2738 EmitReference("text_begin", 0); Asm->EOL("Address");
2739 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2740
2741 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2742 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2743#endif
2744
2745 Asm->EOL();
2746}
2747
Devang Patelc50078e2009-11-21 02:48:08 +00002748/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002749///
Devang Patelc50078e2009-11-21 02:48:08 +00002750void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002751 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002752 Asm->OutStreamer.SwitchSection(
2753 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002754 Asm->EOL();
2755}
2756
Devang Patelc50078e2009-11-21 02:48:08 +00002757/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002758///
Devang Patelc50078e2009-11-21 02:48:08 +00002759void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00002760 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00002761 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002762 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00002763 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002764 Asm->EOL();
2765 }
2766}
2767
Devang Patelc50078e2009-11-21 02:48:08 +00002768/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00002769/// Section Header:
2770/// 1. length of section
2771/// 2. Dwarf version number
2772/// 3. address size.
2773///
2774/// Entries (one "entry" for each function that was inlined):
2775///
2776/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2777/// otherwise offset into __debug_str for regular function name.
2778/// 2. offset into __debug_str section for regular function name.
2779/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2780/// instances for the function.
2781///
2782/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2783/// inlined instance; the die_offset points to the inlined_subroutine die in the
2784/// __debug_info section, and the low_pc is the starting address for the
2785/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00002786void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002787 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002788 return;
2789
Devang Patel5a3d37f2009-06-29 20:45:18 +00002790 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00002791 return;
2792
Chris Lattner73266f92009-08-19 05:49:37 +00002793 Asm->OutStreamer.SwitchSection(
2794 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002795 Asm->EOL();
2796 EmitDifference("debug_inlined_end", 1,
2797 "debug_inlined_begin", 1, true);
2798 Asm->EOL("Length of Debug Inlined Information Entry");
2799
2800 EmitLabel("debug_inlined_begin", 1);
2801
2802 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2803 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2804
Devang Patel90a0fe32009-11-10 23:06:00 +00002805 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2806 E = InlinedSPNodes.end(); I != E; ++I) {
2807
2808// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2809 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2810 MDNode *Node = *I;
2811 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II = InlineInfo.find(Node);
2812 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00002813 DISubprogram SP(Node);
Devang Patelaaf012e2009-09-29 18:40:58 +00002814 const char *LName = SP.getLinkageName();
2815 const char *Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00002816
Devang Patelaaf012e2009-09-29 18:40:58 +00002817 if (!LName)
Devang Patel76031e82009-07-16 01:01:22 +00002818 Asm->EmitString(Name);
2819 else {
Chris Lattner73266f92009-08-19 05:49:37 +00002820 // Skip special LLVM prefix that is used to inform the asm printer to not
2821 // emit usual symbol prefix before the symbol name. This happens for
2822 // Objective-C symbol names and symbol whose name is replaced using GCC's
2823 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00002824 if (LName[0] == 1)
2825 LName = &LName[1];
Devang Patel90a0fe32009-11-10 23:06:00 +00002826// Asm->EmitString(LName);
2827 EmitSectionOffset("string", "section_str",
2828 StringPool.idFor(LName), false, true);
2829
Devang Patel76031e82009-07-16 01:01:22 +00002830 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002831 Asm->EOL("MIPS linkage name");
Devang Patel90a0fe32009-11-10 23:06:00 +00002832// Asm->EmitString(Name);
2833 EmitSectionOffset("string", "section_str",
2834 StringPool.idFor(Name), false, true);
2835 Asm->EOL("Function name");
Bill Wendling55fccda2009-05-20 23:21:38 +00002836 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2837
Devang Patel90a0fe32009-11-10 23:06:00 +00002838 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00002839 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002840 DIE *SP = LI->second;
Bill Wendling55fccda2009-05-20 23:21:38 +00002841 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2842
2843 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002844 O << MAI->getData32bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00002845 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002846 O << MAI->getData64bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00002847
Devang Patel90a0fe32009-11-10 23:06:00 +00002848 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling55fccda2009-05-20 23:21:38 +00002849 }
2850 }
2851
2852 EmitLabel("debug_inlined_end", 1);
2853 Asm->EOL();
2854}