blob: c200a46100c02fe5cae99f2023087588d7affb9a [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
Devang Patelec13b4f2009-11-24 01:14:22 +000075 /// GlobalTypes - A map of globally visible types for this unit.
76 ///
77 StringMap<DIE*> GlobalTypes;
78
Bill Wendlingb12b3d72009-05-15 09:23:25 +000079public:
80 CompileUnit(unsigned I, DIE *D)
Devang Patelc50078e2009-11-21 02:48:08 +000081 : ID(I), CUDie(D), IndexTyDie(0) {}
82 ~CompileUnit() { delete CUDie; delete IndexTyDie; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000083
84 // Accessors.
Devang Patelec13b4f2009-11-24 01:14:22 +000085 unsigned getID() const { return ID; }
86 DIE* getCUDie() const { return CUDie; }
87 const StringMap<DIE*> &getGlobals() const { return Globals; }
88 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000089
90 /// hasContent - Return true if this compile unit has something to write out.
91 ///
Devang Patelc50078e2009-11-21 02:48:08 +000092 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000093
Devang Patelc50078e2009-11-21 02:48:08 +000094 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +000095 ///
Devang Patelc50078e2009-11-21 02:48:08 +000096 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +000097
Devang Patelec13b4f2009-11-24 01:14:22 +000098 /// addGlobalType - Add a new global type to the compile unit.
99 ///
100 void addGlobalType(const std::string &Name, DIE *Die) {
101 GlobalTypes[Name] = Die;
102 }
103
Devang Pateld90672c2009-11-20 21:37:22 +0000104 /// getDIE - Returns the debug information entry map slot for the
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000105 /// specified debug variable.
Devang Pateld90672c2009-11-20 21:37:22 +0000106 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach652b7432009-11-21 23:12:12 +0000107
Devang Pateld90672c2009-11-20 21:37:22 +0000108 /// insertDIE - Insert DIE into the map.
109 void insertDIE(MDNode *N, DIE *D) {
110 GVToDieMap.insert(std::make_pair(N, D));
111 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000112
Devang Pateld90672c2009-11-20 21:37:22 +0000113 /// getDIEEntry - Returns the debug information entry for the speciefied
114 /// debug variable.
115 DIEEntry *getDIEEntry(MDNode *N) { return GVToDIEEntryMap.lookup(N); }
116
117 /// insertDIEEntry - Insert debug information entry into the map.
118 void insertDIEEntry(MDNode *N, DIEEntry *E) {
119 GVToDIEEntryMap.insert(std::make_pair(N, E));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000120 }
121
Devang Patelc50078e2009-11-21 02:48:08 +0000122 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000123 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000124 void addDie(DIE *Buffer) {
125 this->CUDie->addChild(Buffer);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000126 }
Devang Patel1233f912009-11-21 00:31:03 +0000127
128 // getIndexTyDie - Get an anonymous type for index type.
129 DIE *getIndexTyDie() {
130 return IndexTyDie;
131 }
132
Jim Grosbachb23f2422009-11-22 19:20:36 +0000133 // setIndexTyDie - Set D as anonymous type for index which can be reused
134 // later.
Devang Patel1233f912009-11-21 00:31:03 +0000135 void setIndexTyDie(DIE *D) {
136 IndexTyDie = D;
137 }
138
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000139};
140
141//===----------------------------------------------------------------------===//
142/// DbgVariable - This class is used to track local variable information.
143///
Devang Patel4cb32c32009-11-16 21:53:40 +0000144class DbgVariable {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000145 DIVariable Var; // Variable Descriptor.
146 unsigned FrameIndex; // Variable frame index.
Devang Patel90a0fe32009-11-10 23:06:00 +0000147 DbgVariable *AbstractVar; // Abstract variable for this variable.
148 DIE *TheDIE;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000149public:
Devang Patel90a0fe32009-11-10 23:06:00 +0000150 DbgVariable(DIVariable V, unsigned I)
151 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000152
153 // Accessors.
Devang Patel90a0fe32009-11-10 23:06:00 +0000154 DIVariable getVariable() const { return Var; }
155 unsigned getFrameIndex() const { return FrameIndex; }
156 void setAbstractVariable(DbgVariable *V) { AbstractVar = V; }
157 DbgVariable *getAbstractVariable() const { return AbstractVar; }
158 void setDIE(DIE *D) { TheDIE = D; }
159 DIE *getDIE() const { return TheDIE; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000160};
161
162//===----------------------------------------------------------------------===//
163/// DbgScope - This class is used to track scope information.
164///
Devang Patel4cb32c32009-11-16 21:53:40 +0000165class DbgScope {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000166 DbgScope *Parent; // Parent to this scope.
Jim Grosbach652b7432009-11-21 23:12:12 +0000167 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patel90a0fe32009-11-10 23:06:00 +0000168 WeakVH InlinedAtLocation; // Location at which scope is inlined.
169 bool AbstractScope; // Abstract Scope
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000170 unsigned StartLabelID; // Label ID of the beginning of scope.
171 unsigned EndLabelID; // Label ID of the end of scope.
Devang Patel90ecd192009-10-01 18:25:23 +0000172 const MachineInstr *LastInsn; // Last instruction of this scope.
173 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000174 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
175 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbar41716322009-09-19 20:40:05 +0000176
Owen Anderson696d4862009-06-24 22:53:20 +0000177 // Private state for dump()
178 mutable unsigned IndentLevel;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000179public:
Devang Pateldd7bb432009-10-14 21:08:09 +0000180 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patel90a0fe32009-11-10 23:06:00 +0000181 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Jim Grosbach652b7432009-11-21 23:12:12 +0000182 StartLabelID(0), EndLabelID(0),
Devang Pateldd7bb432009-10-14 21:08:09 +0000183 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000184 virtual ~DbgScope();
185
186 // Accessors.
187 DbgScope *getParent() const { return Parent; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000188 void setParent(DbgScope *P) { Parent = P; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000189 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach652b7432009-11-21 23:12:12 +0000190 MDNode *getInlinedAt() const {
Devang Patel90a0fe32009-11-10 23:06:00 +0000191 return dyn_cast_or_null<MDNode>(InlinedAtLocation);
Devang Pateldd7bb432009-10-14 21:08:09 +0000192 }
Devang Patel90a0fe32009-11-10 23:06:00 +0000193 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000194 unsigned getStartLabelID() const { return StartLabelID; }
195 unsigned getEndLabelID() const { return EndLabelID; }
196 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
197 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000198 void setStartLabelID(unsigned S) { StartLabelID = S; }
199 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Patel90ecd192009-10-01 18:25:23 +0000200 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
201 const MachineInstr *getLastInsn() { return LastInsn; }
202 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000203 void setAbstractScope() { AbstractScope = true; }
204 bool isAbstractScope() const { return AbstractScope; }
Devang Patel90ecd192009-10-01 18:25:23 +0000205 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patel90a0fe32009-11-10 23:06:00 +0000206
Devang Patelc50078e2009-11-21 02:48:08 +0000207 /// addScope - Add a scope to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000208 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000209 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000210
Devang Patelc50078e2009-11-21 02:48:08 +0000211 /// addVariable - Add a variable to the scope.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000212 ///
Devang Patelc50078e2009-11-21 02:48:08 +0000213 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000214
Devang Patelc50078e2009-11-21 02:48:08 +0000215 void fixInstructionMarkers() {
Devang Patel6a260102009-10-01 20:31:14 +0000216 assert (getFirstInsn() && "First instruction is missing!");
217 if (getLastInsn())
218 return;
Jim Grosbach652b7432009-11-21 23:12:12 +0000219
Devang Patel6a260102009-10-01 20:31:14 +0000220 // If a scope does not have an instruction to mark an end then use
221 // the end of last child scope.
222 SmallVector<DbgScope *, 4> &Scopes = getScopes();
223 assert (!Scopes.empty() && "Inner most scope does not have last insn!");
224 DbgScope *L = Scopes.back();
225 if (!L->getLastInsn())
Devang Patelc50078e2009-11-21 02:48:08 +0000226 L->fixInstructionMarkers();
Devang Patel6a260102009-10-01 20:31:14 +0000227 setLastInsn(L->getLastInsn());
228 }
229
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000230#ifndef NDEBUG
231 void dump() const;
232#endif
233};
234
235#ifndef NDEBUG
236void DbgScope::dump() const {
Chris Lattnerebb8c082009-08-23 00:51:00 +0000237 raw_ostream &err = errs();
238 err.indent(IndentLevel);
Devang Patel90a0fe32009-11-10 23:06:00 +0000239 MDNode *N = Desc.getNode();
240 N->dump();
Chris Lattnerebb8c082009-08-23 00:51:00 +0000241 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patel90a0fe32009-11-10 23:06:00 +0000242 if (AbstractScope)
243 err << "Abstract Scope\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000244
245 IndentLevel += 2;
Devang Patel90a0fe32009-11-10 23:06:00 +0000246 if (!Scopes.empty())
247 err << "Children ...\n";
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000248 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
249 if (Scopes[i] != this)
250 Scopes[i]->dump();
251
252 IndentLevel -= 2;
253}
254#endif
255
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000256DbgScope::~DbgScope() {
257 for (unsigned i = 0, N = Scopes.size(); i < N; ++i)
258 delete Scopes[i];
259 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
260 delete Variables[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000261}
262
263} // end llvm namespace
264
Chris Lattner621c44d2009-08-22 20:48:53 +0000265DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel5a3d37f2009-06-29 20:45:18 +0000266 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000267 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patelc50078e2009-11-21 02:48:08 +0000268 DIEValues(), StringPool(),
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000269 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patel90a0fe32009-11-10 23:06:00 +0000270 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000271 if (TimePassesIsEnabled)
272 DebugTimer = new Timer("Dwarf Debug Writer",
273 getDwarfTimerGroup());
274}
275DwarfDebug::~DwarfDebug() {
Devang Patelc50078e2009-11-21 02:48:08 +0000276 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
277 delete DIEValues[j];
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000278
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000279 delete DebugTimer;
280}
281
Devang Patelc50078e2009-11-21 02:48:08 +0000282/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000283///
Devang Patelc50078e2009-11-21 02:48:08 +0000284void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000285 // Profile the node so that we can make it unique.
286 FoldingSetNodeID ID;
287 Abbrev.Profile(ID);
288
289 // Check the set for priors.
290 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
291
292 // If it's newly added.
293 if (InSet == &Abbrev) {
294 // Add to abbreviation list.
295 Abbreviations.push_back(&Abbrev);
296
297 // Assign the vector position + 1 as its number.
298 Abbrev.setNumber(Abbreviations.size());
299 } else {
300 // Assign existing abbreviation number.
301 Abbrev.setNumber(InSet->getNumber());
302 }
303}
304
Devang Patelc50078e2009-11-21 02:48:08 +0000305/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendling0d3db8b2009-05-20 23:24:48 +0000306/// information entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000307DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel1233f912009-11-21 00:31:03 +0000308 DIEEntry *Value = new DIEEntry(Entry);
Devang Patelc50078e2009-11-21 02:48:08 +0000309 DIEValues.push_back(Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000310 return Value;
311}
312
Devang Patelc50078e2009-11-21 02:48:08 +0000313/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000314///
Devang Patelc50078e2009-11-21 02:48:08 +0000315void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000316 unsigned Form, uint64_t Integer) {
317 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000318 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000319 DIEValues.push_back(Value);
320 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000321}
322
Devang Patelc50078e2009-11-21 02:48:08 +0000323/// addSInt - Add an signed integer attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000324///
Devang Patelc50078e2009-11-21 02:48:08 +0000325void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000326 unsigned Form, int64_t Integer) {
327 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel1233f912009-11-21 00:31:03 +0000328 DIEValue *Value = new DIEInteger(Integer);
Devang Patelc50078e2009-11-21 02:48:08 +0000329 DIEValues.push_back(Value);
330 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000331}
332
Devang Pateldf0f2152009-12-02 15:25:16 +0000333/// addString - Add a string attribute data and value. DIEString only
334/// keeps string reference.
Devang Patelc50078e2009-11-21 02:48:08 +0000335void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patelc10337d2009-11-24 19:42:17 +0000336 const StringRef String) {
Devang Patel1233f912009-11-21 00:31:03 +0000337 DIEValue *Value = new DIEString(String);
Devang Patelc50078e2009-11-21 02:48:08 +0000338 DIEValues.push_back(Value);
339 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000340}
341
Devang Patelc50078e2009-11-21 02:48:08 +0000342/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000343///
Devang Patelc50078e2009-11-21 02:48:08 +0000344void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000345 const DWLabel &Label) {
Devang Patel1233f912009-11-21 00:31:03 +0000346 DIEValue *Value = new DIEDwarfLabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000347 DIEValues.push_back(Value);
348 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000349}
350
Devang Patelc50078e2009-11-21 02:48:08 +0000351/// addObjectLabel - Add an non-Dwarf label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000352///
Devang Patelc50078e2009-11-21 02:48:08 +0000353void DwarfDebug::addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000354 const std::string &Label) {
Devang Patel1233f912009-11-21 00:31:03 +0000355 DIEValue *Value = new DIEObjectLabel(Label);
Devang Patelc50078e2009-11-21 02:48:08 +0000356 DIEValues.push_back(Value);
357 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000358}
359
Devang Patelc50078e2009-11-21 02:48:08 +0000360/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000361///
Devang Patelc50078e2009-11-21 02:48:08 +0000362void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000363 const DWLabel &Label, const DWLabel &Section,
364 bool isEH, bool useSet) {
Devang Patel1233f912009-11-21 00:31:03 +0000365 DIEValue *Value = new DIESectionOffset(Label, Section, isEH, useSet);
Devang Patelc50078e2009-11-21 02:48:08 +0000366 DIEValues.push_back(Value);
367 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000368}
369
Devang Patelc50078e2009-11-21 02:48:08 +0000370/// addDelta - Add a label delta attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000371///
Devang Patelc50078e2009-11-21 02:48:08 +0000372void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000373 const DWLabel &Hi, const DWLabel &Lo) {
Devang Patel1233f912009-11-21 00:31:03 +0000374 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patelc50078e2009-11-21 02:48:08 +0000375 DIEValues.push_back(Value);
376 Die->addValue(Attribute, Form, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000377}
378
Devang Patelc50078e2009-11-21 02:48:08 +0000379/// addBlock - Add block data.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000380///
Devang Patelc50078e2009-11-21 02:48:08 +0000381void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000382 DIEBlock *Block) {
383 Block->ComputeSize(TD);
Devang Patelc50078e2009-11-21 02:48:08 +0000384 DIEValues.push_back(Block);
385 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000386}
387
Devang Patelc50078e2009-11-21 02:48:08 +0000388/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000389/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000390void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000391 // If there is no compile unit specified, don't add a line #.
392 if (V->getCompileUnit().isNull())
393 return;
394
395 unsigned Line = V->getLineNumber();
Devang Patelb9f2c6b2009-12-11 21:37:07 +0000396 unsigned FileID = findCompileUnit(V->getCompileUnit())->getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000397 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000398 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
399 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000400}
401
Devang Patelc50078e2009-11-21 02:48:08 +0000402/// addSourceLine - Add location information to specified debug information
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000403/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000404void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000405 // If there is no compile unit specified, don't add a line #.
406 if (G->getCompileUnit().isNull())
407 return;
408
409 unsigned Line = G->getLineNumber();
Devang Patelb9f2c6b2009-12-11 21:37:07 +0000410 unsigned FileID = findCompileUnit(G->getCompileUnit())->getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000411 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000412 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
413 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000414}
Devang Patel318d70d2009-08-31 22:47:13 +0000415
Devang Patelc50078e2009-11-21 02:48:08 +0000416/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000417/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000418void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patel318d70d2009-08-31 22:47:13 +0000419 // If there is no compile unit specified, don't add a line #.
420 if (SP->getCompileUnit().isNull())
421 return;
Caroline Tice9da96d82009-09-11 18:25:54 +0000422 // If the line number is 0, don't add it.
423 if (SP->getLineNumber() == 0)
424 return;
425
Devang Patel318d70d2009-08-31 22:47:13 +0000426
427 unsigned Line = SP->getLineNumber();
Devang Patelb9f2c6b2009-12-11 21:37:07 +0000428 unsigned FileID = findCompileUnit(SP->getCompileUnit())->getID();
Devang Patel318d70d2009-08-31 22:47:13 +0000429 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000430 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
431 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Devang Patel318d70d2009-08-31 22:47:13 +0000432}
433
Devang Patelc50078e2009-11-21 02:48:08 +0000434/// addSourceLine - Add location information to specified debug information
Devang Patel318d70d2009-08-31 22:47:13 +0000435/// entry.
Devang Patelc50078e2009-11-21 02:48:08 +0000436void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000437 // If there is no compile unit specified, don't add a line #.
438 DICompileUnit CU = Ty->getCompileUnit();
439 if (CU.isNull())
440 return;
441
442 unsigned Line = Ty->getLineNumber();
Devang Patelb9f2c6b2009-12-11 21:37:07 +0000443 unsigned FileID = findCompileUnit(CU)->getID();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000444 assert(FileID && "Invalid file id");
Devang Patelc50078e2009-11-21 02:48:08 +0000445 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
446 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000447}
448
Caroline Tice248d5572009-08-31 21:19:37 +0000449/* Byref variables, in Blocks, are declared by the programmer as
450 "SomeType VarName;", but the compiler creates a
451 __Block_byref_x_VarName struct, and gives the variable VarName
452 either the struct, or a pointer to the struct, as its type. This
453 is necessary for various behind-the-scenes things the compiler
454 needs to do with by-reference variables in blocks.
455
456 However, as far as the original *programmer* is concerned, the
457 variable should still have type 'SomeType', as originally declared.
458
459 The following function dives into the __Block_byref_x_VarName
460 struct to find the original type of the variable. This will be
461 passed back to the code generating the type for the Debug
462 Information Entry for the variable 'VarName'. 'VarName' will then
463 have the original type 'SomeType' in its debug information.
464
465 The original type 'SomeType' will be the type of the field named
466 'VarName' inside the __Block_byref_x_VarName struct.
467
468 NOTE: In order for this to not completely fail on the debugger
469 side, the Debug Information Entry for the variable VarName needs to
470 have a DW_AT_location that tells the debugger how to unwind through
471 the pointers and __Block_byref_x_VarName struct to find the actual
Devang Patelc50078e2009-11-21 02:48:08 +0000472 value of the variable. The function addBlockByrefType does this. */
Caroline Tice248d5572009-08-31 21:19:37 +0000473
474/// Find the type the programmer originally declared the variable to be
475/// and return that type.
476///
Devang Patelc50078e2009-11-21 02:48:08 +0000477DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Tice248d5572009-08-31 21:19:37 +0000478
479 DIType subType = Ty;
480 unsigned tag = Ty.getTag();
481
482 if (tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000483 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000484 subType = DTy.getTypeDerivedFrom();
485 }
486
487 DICompositeType blockStruct = DICompositeType(subType.getNode());
488
489 DIArray Elements = blockStruct.getTypeArray();
490
491 if (Elements.isNull())
492 return Ty;
493
494 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
495 DIDescriptor Element = Elements.getElement(i);
496 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000497 if (Name == DT.getName())
Caroline Tice248d5572009-08-31 21:19:37 +0000498 return (DT.getTypeDerivedFrom());
499 }
500
501 return Ty;
502}
503
Devang Patelc50078e2009-11-21 02:48:08 +0000504/// addComplexAddress - Start with the address based on the location provided,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000505/// and generate the DWARF information necessary to find the actual variable
506/// given the extra address information encoded in the DIVariable, starting from
507/// the starting location. Add the DWARF information to the die.
508///
Devang Patelc50078e2009-11-21 02:48:08 +0000509void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000510 unsigned Attribute,
511 const MachineLocation &Location) {
512 const DIVariable &VD = DV->getVariable();
513 DIType Ty = VD.getType();
514
515 // Decode the original location, and use that as the start of the byref
516 // variable's location.
517 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
518 DIEBlock *Block = new DIEBlock();
519
520 if (Location.isReg()) {
521 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000522 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000523 } else {
524 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000525 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
526 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000527 }
528 } else {
529 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000530 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000531 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000532 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
533 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000534 }
535
Devang Patelc50078e2009-11-21 02:48:08 +0000536 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000537 }
538
539 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
540 uint64_t Element = VD.getAddrElement(i);
541
542 if (Element == DIFactory::OpPlus) {
Devang Patelc50078e2009-11-21 02:48:08 +0000543 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
544 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000545 } else if (Element == DIFactory::OpDeref) {
Devang Patelc50078e2009-11-21 02:48:08 +0000546 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000547 } else llvm_unreachable("unknown DIFactory Opcode");
548 }
549
550 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000551 addBlock(Die, Attribute, 0, Block);
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000552}
553
Caroline Tice248d5572009-08-31 21:19:37 +0000554/* Byref variables, in Blocks, are declared by the programmer as "SomeType
555 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
556 gives the variable VarName either the struct, or a pointer to the struct, as
557 its type. This is necessary for various behind-the-scenes things the
558 compiler needs to do with by-reference variables in Blocks.
559
560 However, as far as the original *programmer* is concerned, the variable
561 should still have type 'SomeType', as originally declared.
562
Devang Patelc50078e2009-11-21 02:48:08 +0000563 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Tice248d5572009-08-31 21:19:37 +0000564 struct to find the original type of the variable, which is then assigned to
565 the variable's Debug Information Entry as its real type. So far, so good.
566 However now the debugger will expect the variable VarName to have the type
567 SomeType. So we need the location attribute for the variable to be an
Daniel Dunbar41716322009-09-19 20:40:05 +0000568 expression that explains to the debugger how to navigate through the
Caroline Tice248d5572009-08-31 21:19:37 +0000569 pointers and struct to find the actual variable of type SomeType.
570
571 The following function does just that. We start by getting
572 the "normal" location for the variable. This will be the location
573 of either the struct __Block_byref_x_VarName or the pointer to the
574 struct __Block_byref_x_VarName.
575
576 The struct will look something like:
577
578 struct __Block_byref_x_VarName {
579 ... <various fields>
580 struct __Block_byref_x_VarName *forwarding;
581 ... <various other fields>
582 SomeType VarName;
583 ... <maybe more fields>
584 };
585
586 If we are given the struct directly (as our starting point) we
587 need to tell the debugger to:
588
589 1). Add the offset of the forwarding field.
590
591 2). Follow that pointer to get the the real __Block_byref_x_VarName
592 struct to use (the real one may have been copied onto the heap).
593
594 3). Add the offset for the field VarName, to find the actual variable.
595
596 If we started with a pointer to the struct, then we need to
597 dereference that pointer first, before the other steps.
598 Translating this into DWARF ops, we will need to append the following
599 to the current location description for the variable:
600
601 DW_OP_deref -- optional, if we start with a pointer
602 DW_OP_plus_uconst <forward_fld_offset>
603 DW_OP_deref
604 DW_OP_plus_uconst <varName_fld_offset>
605
606 That is what this function does. */
607
Devang Patelc50078e2009-11-21 02:48:08 +0000608/// addBlockByrefAddress - Start with the address based on the location
Caroline Tice248d5572009-08-31 21:19:37 +0000609/// provided, and generate the DWARF information necessary to find the
610/// actual Block variable (navigating the Block struct) based on the
611/// starting location. Add the DWARF information to the die. For
612/// more information, read large comment just above here.
613///
Devang Patelc50078e2009-11-21 02:48:08 +0000614void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000615 unsigned Attribute,
616 const MachineLocation &Location) {
Caroline Tice248d5572009-08-31 21:19:37 +0000617 const DIVariable &VD = DV->getVariable();
618 DIType Ty = VD.getType();
619 DIType TmpTy = Ty;
620 unsigned Tag = Ty.getTag();
621 bool isPointer = false;
622
Devang Patel7f75bbe2009-11-25 17:36:49 +0000623 StringRef varName = VD.getName();
Caroline Tice248d5572009-08-31 21:19:37 +0000624
625 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stumpb22cd0f2009-09-30 00:08:22 +0000626 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Tice248d5572009-08-31 21:19:37 +0000627 TmpTy = DTy.getTypeDerivedFrom();
628 isPointer = true;
629 }
630
631 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
632
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000633 // Find the __forwarding field and the variable field in the __Block_byref
634 // struct.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000635 DIArray Fields = blockStruct.getTypeArray();
636 DIDescriptor varField = DIDescriptor();
637 DIDescriptor forwardingField = DIDescriptor();
Caroline Tice248d5572009-08-31 21:19:37 +0000638
639
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000640 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
641 DIDescriptor Element = Fields.getElement(i);
642 DIDerivedType DT = DIDerivedType(Element.getNode());
Devang Patel7f75bbe2009-11-25 17:36:49 +0000643 StringRef fieldName = DT.getName();
644 if (fieldName == "__forwarding")
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000645 forwardingField = Element;
Devang Patel7f75bbe2009-11-25 17:36:49 +0000646 else if (fieldName == varName)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000647 varField = Element;
648 }
Daniel Dunbar41716322009-09-19 20:40:05 +0000649
Mike Stump2fd84e22009-09-24 23:21:26 +0000650 assert(!varField.isNull() && "Can't find byref variable in Block struct");
651 assert(!forwardingField.isNull()
652 && "Can't find forwarding field in Block struct");
Caroline Tice248d5572009-08-31 21:19:37 +0000653
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000654 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000655 unsigned int forwardingFieldOffset =
656 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
657 unsigned int varFieldOffset =
658 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
Caroline Tice248d5572009-08-31 21:19:37 +0000659
Mike Stump2fd84e22009-09-24 23:21:26 +0000660 // Decode the original location, and use that as the start of the byref
661 // variable's location.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000662 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
663 DIEBlock *Block = new DIEBlock();
Caroline Tice248d5572009-08-31 21:19:37 +0000664
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000665 if (Location.isReg()) {
666 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000667 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000668 else {
669 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patelc50078e2009-11-21 02:48:08 +0000670 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
671 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000672 }
673 } else {
674 if (Reg < 32)
Devang Patelc50078e2009-11-21 02:48:08 +0000675 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000676 else {
Devang Patelc50078e2009-11-21 02:48:08 +0000677 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
678 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000679 }
Caroline Tice248d5572009-08-31 21:19:37 +0000680
Devang Patelc50078e2009-11-21 02:48:08 +0000681 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000682 }
Caroline Tice248d5572009-08-31 21:19:37 +0000683
Mike Stump2fd84e22009-09-24 23:21:26 +0000684 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000685 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000686 if (isPointer)
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 // Next add the offset for the '__forwarding' field:
690 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
691 // adding the offset if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000692 if (forwardingFieldOffset > 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, forwardingFieldOffset);
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 dereference the __forwarding field to get to the real __Block_byref
698 // struct: DW_OP_deref.
Devang Patelc50078e2009-11-21 02:48:08 +0000699 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Tice248d5572009-08-31 21:19:37 +0000700
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000701 // Now that we've got the real __Block_byref... struct, add the offset
702 // for the variable's field to get to the location of the actual variable:
703 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000704 if (varFieldOffset > 0) {
Devang Patelc50078e2009-11-21 02:48:08 +0000705 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
706 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000707 }
Caroline Tice248d5572009-08-31 21:19:37 +0000708
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000709 // Now attach the location information to the DIE.
Devang Patelc50078e2009-11-21 02:48:08 +0000710 addBlock(Die, Attribute, 0, Block);
Caroline Tice248d5572009-08-31 21:19:37 +0000711}
712
Devang Patelc50078e2009-11-21 02:48:08 +0000713/// addAddress - Add an address attribute to a die based on the location
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000714/// provided.
Devang Patelc50078e2009-11-21 02:48:08 +0000715void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000716 const MachineLocation &Location) {
717 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
718 DIEBlock *Block = new DIEBlock();
719
720 if (Location.isReg()) {
721 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000722 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000723 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000724 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
725 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000726 }
727 } else {
728 if (Reg < 32) {
Devang Patelc50078e2009-11-21 02:48:08 +0000729 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000730 } else {
Devang Patelc50078e2009-11-21 02:48:08 +0000731 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
732 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000733 }
734
Devang Patelc50078e2009-11-21 02:48:08 +0000735 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000736 }
737
Devang Patelc50078e2009-11-21 02:48:08 +0000738 addBlock(Die, Attribute, 0, Block);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000739}
740
Devang Patel1a8f9a82009-12-10 19:14:49 +0000741/// addToContextOwner - Add Die into the list of its context owner's children.
742void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
743 if (Context.isNull())
744 ModuleCU->addDie(Die);
745 else if (Context.isType()) {
746 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
747 ContextDIE->addChild(Die);
748 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
749 ContextDIE->addChild(Die);
750 else
751 ModuleCU->addDie(Die);
752}
753
Devang Patel7f139c12009-12-10 18:05:33 +0000754/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
755/// given DIType.
756DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
757 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
758 if (TyDIE)
759 return TyDIE;
760
761 // Create new type.
762 TyDIE = new DIE(dwarf::DW_TAG_base_type);
763 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
764 if (Ty.isBasicType())
765 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
766 else if (Ty.isCompositeType())
767 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
768 else {
769 assert(Ty.isDerivedType() && "Unknown kind of DIType");
770 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
771 }
772
Devang Patel1a8f9a82009-12-10 19:14:49 +0000773 addToContextOwner(TyDIE, Ty.getContext());
Devang Patel7f139c12009-12-10 18:05:33 +0000774 return TyDIE;
775}
776
Devang Patelc50078e2009-11-21 02:48:08 +0000777/// addType - Add a new type attribute to the specified entity.
Devang Patelfe0be132009-12-09 18:24:21 +0000778void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000779 if (Ty.isNull())
780 return;
781
782 // Check for pre-existence.
Devang Patelfe0be132009-12-09 18:24:21 +0000783 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000784
785 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000786 if (Entry) {
787 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000788 return;
789 }
790
791 // Set up proxy.
Devang Patelc50078e2009-11-21 02:48:08 +0000792 Entry = createDIEEntry();
Devang Patelfe0be132009-12-09 18:24:21 +0000793 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000794
795 // Construct type.
Devang Patel7f139c12009-12-10 18:05:33 +0000796 DIE *Buffer = getOrCreateTypeDIE(Ty);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000797
Devang Patelc50078e2009-11-21 02:48:08 +0000798 Entry->setEntry(Buffer);
799 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000800}
801
Devang Patelc50078e2009-11-21 02:48:08 +0000802/// constructTypeDIE - Construct basic type die from DIBasicType.
Devang Patelfe0be132009-12-09 18:24:21 +0000803void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000804 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000805 StringRef Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000806 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000807 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000808 BTy.getEncoding());
809
810 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000811 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000812 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000813 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000814 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000815}
816
Devang Patelc50078e2009-11-21 02:48:08 +0000817/// constructTypeDIE - Construct derived type die from DIDerivedType.
Devang Patelfe0be132009-12-09 18:24:21 +0000818void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000819 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000820 StringRef Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000821 uint64_t Size = DTy.getSizeInBits() >> 3;
822 unsigned Tag = DTy.getTag();
823
824 // FIXME - Workaround for templates.
825 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
826
827 Buffer.setTag(Tag);
828
829 // Map to main type, void will not have a type.
830 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelfe0be132009-12-09 18:24:21 +0000831 addType(&Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000832
833 // Add name if not anonymous or intermediate type.
Devang Patel76b80672009-11-30 23:56:56 +0000834 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000835 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000836
837 // Add size if non-zero (derived types might be zero-sized.)
838 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000839 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000840
841 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000842 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000843 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000844}
845
Devang Patelc50078e2009-11-21 02:48:08 +0000846/// constructTypeDIE - Construct type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +0000847void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000848 // Get core information.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000849 StringRef Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000850
851 uint64_t Size = CTy.getSizeInBits() >> 3;
852 unsigned Tag = CTy.getTag();
853 Buffer.setTag(Tag);
854
855 switch (Tag) {
856 case dwarf::DW_TAG_vector_type:
857 case dwarf::DW_TAG_array_type:
Devang Patelfe0be132009-12-09 18:24:21 +0000858 constructArrayTypeDIE(Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000859 break;
860 case dwarf::DW_TAG_enumeration_type: {
861 DIArray Elements = CTy.getTypeArray();
862
863 // Add enumerators to enumeration type.
864 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
865 DIE *ElemDie = NULL;
Devang Patel15e723d2009-08-28 23:24:31 +0000866 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelfb812752009-10-09 17:51:49 +0000867 if (!Enum.isNull()) {
Devang Patelfe0be132009-12-09 18:24:21 +0000868 ElemDie = constructEnumTypeDIE(&Enum);
Devang Patelc50078e2009-11-21 02:48:08 +0000869 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +0000870 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000871 }
872 }
873 break;
874 case dwarf::DW_TAG_subroutine_type: {
875 // Add return type.
876 DIArray Elements = CTy.getTypeArray();
877 DIDescriptor RTy = Elements.getElement(0);
Devang Patelfe0be132009-12-09 18:24:21 +0000878 addType(&Buffer, DIType(RTy.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000879
880 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +0000881 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000882
883 // Add arguments.
884 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
885 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
886 DIDescriptor Ty = Elements.getElement(i);
Devang Patelfe0be132009-12-09 18:24:21 +0000887 addType(Arg, DIType(Ty.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000888 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000889 }
890 }
891 break;
892 case dwarf::DW_TAG_structure_type:
893 case dwarf::DW_TAG_union_type:
894 case dwarf::DW_TAG_class_type: {
895 // Add elements to structure type.
896 DIArray Elements = CTy.getTypeArray();
897
898 // A forward struct declared type may not have elements available.
899 if (Elements.isNull())
900 break;
901
902 // Add elements to structure type.
903 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
904 DIDescriptor Element = Elements.getElement(i);
Devang Patel15e723d2009-08-28 23:24:31 +0000905 if (Element.isNull())
906 continue;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000907 DIE *ElemDie = NULL;
908 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel814a12c2009-12-14 16:18:45 +0000909 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000910 else
Devang Patelfe0be132009-12-09 18:24:21 +0000911 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000912 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000913 }
914
Devang Patel20b32102009-08-27 23:51:51 +0000915 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +0000916 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000917
918 unsigned RLang = CTy.getRunTimeLang();
919 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +0000920 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000921 dwarf::DW_FORM_data1, RLang);
922 break;
923 }
924 default:
925 break;
926 }
927
928 // Add name if not anonymous or intermediate type.
Devang Patel7f75bbe2009-11-25 17:36:49 +0000929 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +0000930 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000931
932 if (Tag == dwarf::DW_TAG_enumeration_type ||
933 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
934 // Add size if non-zero (derived types might be zero-sized.)
935 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000936 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000937 else {
938 // Add zero size if it is not a forward declaration.
939 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000940 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000941 else
Devang Patelc50078e2009-11-21 02:48:08 +0000942 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000943 }
944
945 // Add source line info if available.
946 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000947 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000948 }
949}
950
Devang Patelc50078e2009-11-21 02:48:08 +0000951/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
952void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000953 int64_t L = SR.getLo();
954 int64_t H = SR.getHi();
955 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
956
Devang Patelc50078e2009-11-21 02:48:08 +0000957 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +0000958 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +0000959 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Pateld3df6972009-12-04 23:10:24 +0000960 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000961
Devang Patelc50078e2009-11-21 02:48:08 +0000962 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000963}
964
Devang Patelc50078e2009-11-21 02:48:08 +0000965/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Devang Patelfe0be132009-12-09 18:24:21 +0000966void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000967 DICompositeType *CTy) {
968 Buffer.setTag(dwarf::DW_TAG_array_type);
969 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000970 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000971
972 // Emit derived type.
Devang Patelfe0be132009-12-09 18:24:21 +0000973 addType(&Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000974 DIArray Elements = CTy->getTypeArray();
975
Devang Patel1233f912009-11-21 00:31:03 +0000976 // Get an anonymous type for index type.
Devang Patelfe0be132009-12-09 18:24:21 +0000977 DIE *IdxTy = ModuleCU->getIndexTyDie();
Devang Patel1233f912009-11-21 00:31:03 +0000978 if (!IdxTy) {
979 // Construct an anonymous type for index type.
980 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000981 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
982 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +0000983 dwarf::DW_ATE_signed);
Devang Patelfe0be132009-12-09 18:24:21 +0000984 ModuleCU->addDie(IdxTy);
985 ModuleCU->setIndexTyDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +0000986 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000987
988 // Add subranges to array type.
989 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
990 DIDescriptor Element = Elements.getElement(i);
991 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000992 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000993 }
994}
995
Devang Patelc50078e2009-11-21 02:48:08 +0000996/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Devang Patelfe0be132009-12-09 18:24:21 +0000997DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator *ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000998 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel7f75bbe2009-11-25 17:36:49 +0000999 StringRef Name = ETy->getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001000 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001001 int64_t Value = ETy->getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +00001002 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001003 return Enumerator;
1004}
1005
Devang Patelc50078e2009-11-21 02:48:08 +00001006/// createGlobalVariableDIE - Create new DIE using GV.
Devang Patelfe0be132009-12-09 18:24:21 +00001007DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001008 // If the global variable was optmized out then no need to create debug info
1009 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +00001010 if (!GV.getGlobal()) return NULL;
Devang Patel7f75bbe2009-11-25 17:36:49 +00001011 if (GV.getDisplayName().empty()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +00001012
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001013 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +00001014 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001015 GV.getDisplayName());
1016
Devang Patel7f75bbe2009-11-25 17:36:49 +00001017 StringRef LinkageName = GV.getLinkageName();
1018 if (!LinkageName.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001019 // Skip special LLVM prefix that is used to inform the asm printer to not
1020 // emit usual symbol prefix before the symbol name. This happens for
1021 // Objective-C symbol names and symbol whose name is replaced using GCC's
1022 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00001023 if (LinkageName[0] == 1)
Benjamin Kramer62b81882009-11-25 18:26:09 +00001024 LinkageName = LinkageName.substr(1);
Devang Patelc50078e2009-11-21 02:48:08 +00001025 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelbd760a52009-07-14 00:55:28 +00001026 LinkageName);
Devang Patel76031e82009-07-16 01:01:22 +00001027 }
Devang Patelfe0be132009-12-09 18:24:21 +00001028 addType(GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001029 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001030 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1031 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001032
1033 // Add address.
1034 DIEBlock *Block = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001035 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1036 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patel6bd5cc82009-10-05 23:22:08 +00001037 Asm->Mang->getMangledName(GV.getGlobal()));
Devang Patelc50078e2009-11-21 02:48:08 +00001038 addBlock(GVDie, dwarf::DW_AT_location, 0, Block);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001039
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001040 return GVDie;
1041}
1042
Devang Patelc50078e2009-11-21 02:48:08 +00001043/// createMemberDIE - Create new member DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001044DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001045 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel7f75bbe2009-11-25 17:36:49 +00001046 StringRef Name = DT.getName();
1047 if (!Name.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001048 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001049
Devang Patelfe0be132009-12-09 18:24:21 +00001050 addType(MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001051
Devang Patelc50078e2009-11-21 02:48:08 +00001052 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001053
Devang Patel7d9fe582009-11-04 22:06:12 +00001054 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001055 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001056
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001057 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001058 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001059
1060 if (Size != FieldSize) {
1061 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001062 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1063 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001064
1065 uint64_t Offset = DT.getOffsetInBits();
1066 uint64_t FieldOffset = Offset;
1067 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1068 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1069 FieldOffset = (HiMark - FieldSize);
1070 Offset -= FieldOffset;
1071
1072 // Maybe we need to work from the other end.
1073 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001074 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001075
Devang Patel7d9fe582009-11-04 22:06:12 +00001076 // Here WD_AT_data_member_location points to the anonymous
1077 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001078 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001079
1080 } else
1081 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001082 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001083
Devang Patelc50078e2009-11-21 02:48:08 +00001084 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001085
1086 if (DT.isProtected())
Devang Patel188c85d2009-12-03 19:11:07 +00001087 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001088 dwarf::DW_ACCESS_protected);
1089 else if (DT.isPrivate())
Devang Patel188c85d2009-12-03 19:11:07 +00001090 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001091 dwarf::DW_ACCESS_private);
Devang Patel188c85d2009-12-03 19:11:07 +00001092 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1093 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1094 dwarf::DW_ACCESS_public);
1095 if (DT.isVirtual())
1096 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1097 dwarf::DW_VIRTUALITY_virtual);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001098 return MemberDie;
1099}
1100
Devang Patel814a12c2009-12-14 16:18:45 +00001101/// createSubprogramDIE - Create new DIE using SP.
1102DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1103 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1104 if (SPDie)
1105 return SPDie;
1106
1107 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001108 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001109
Devang Patel7f75bbe2009-11-25 17:36:49 +00001110 StringRef LinkageName = SP.getLinkageName();
1111 if (!LinkageName.empty()) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001112 // Skip special LLVM prefix that is used to inform the asm printer to not
1113 // emit usual symbol prefix before the symbol name. This happens for
1114 // Objective-C symbol names and symbol whose name is replaced using GCC's
1115 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00001116 if (LinkageName[0] == 1)
Benjamin Kramer62b81882009-11-25 18:26:09 +00001117 LinkageName = LinkageName.substr(1);
Devang Patelc50078e2009-11-21 02:48:08 +00001118 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelbd760a52009-07-14 00:55:28 +00001119 LinkageName);
Devang Patel76031e82009-07-16 01:01:22 +00001120 }
Devang Patelc50078e2009-11-21 02:48:08 +00001121 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001122
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001123 // Add prototyped tag, if C or ObjC.
1124 unsigned Lang = SP.getCompileUnit().getLanguage();
1125 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1126 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001127 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001128
1129 // Add Return Type.
Devang Patelc1df8792009-12-03 01:25:38 +00001130 DICompositeType SPTy = SP.getType();
1131 DIArray Args = SPTy.getTypeArray();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001132 unsigned SPTag = SPTy.getTag();
Devang Patel188c85d2009-12-03 19:11:07 +00001133
Devang Patelc1df8792009-12-03 01:25:38 +00001134 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelfe0be132009-12-09 18:24:21 +00001135 addType(SPDie, SPTy);
Devang Patelc1df8792009-12-03 01:25:38 +00001136 else
Devang Patelfe0be132009-12-09 18:24:21 +00001137 addType(SPDie, DIType(Args.getElement(0).getNode()));
Devang Patelc1df8792009-12-03 01:25:38 +00001138
Devang Patel188c85d2009-12-03 19:11:07 +00001139 unsigned VK = SP.getVirtuality();
1140 if (VK) {
1141 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1142 DIEBlock *Block = new DIEBlock();
1143 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1144 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1145 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1146 ContainingTypeMap.insert(std::make_pair(SPDie, WeakVH(SP.getContainingType().getNode())));
1147 }
1148
Devang Patel814a12c2009-12-14 16:18:45 +00001149 if (MakeDecl || !SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001150 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001151
1152 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patelc1df8792009-12-03 01:25:38 +00001153 // be handled while processing variables.
1154 DICompositeType SPTy = SP.getType();
1155 DIArray Args = SPTy.getTypeArray();
1156 unsigned SPTag = SPTy.getTag();
1157
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001158 if (SPTag == dwarf::DW_TAG_subroutine_type)
1159 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1160 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelfe0be132009-12-09 18:24:21 +00001161 addType(Arg, DIType(Args.getElement(i).getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +00001162 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1163 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001164 }
1165 }
1166
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001167 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patelfe0be132009-12-09 18:24:21 +00001168 ModuleCU->insertDIE(SP.getNode(), SPDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001169 return SPDie;
1170}
1171
Devang Patelc50078e2009-11-21 02:48:08 +00001172/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001173///
Devang Patelb9f2c6b2009-12-11 21:37:07 +00001174CompileUnit *DwarfDebug::findCompileUnit(DICompileUnit Unit) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001175 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patel15e723d2009-08-28 23:24:31 +00001176 CompileUnitMap.find(Unit.getNode());
Devang Patelb9f2c6b2009-12-11 21:37:07 +00001177 if (I == CompileUnitMap.end())
1178 return constructCompileUnit(Unit.getNode());
1179 return I->second;
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001180}
1181
Devang Patel90a0fe32009-11-10 23:06:00 +00001182/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1183/// Initialize scope and update scope hierarchy.
1184DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1185 MDNode *InlinedAt) {
1186 assert (N && "Invalid Scope encoding!");
1187 assert (MI && "Missing machine instruction!");
1188 bool GetConcreteScope = (MI && InlinedAt);
1189
1190 DbgScope *NScope = NULL;
1191
1192 if (InlinedAt)
1193 NScope = DbgScopeMap.lookup(InlinedAt);
1194 else
1195 NScope = DbgScopeMap.lookup(N);
1196 assert (NScope && "Unable to find working scope!");
1197
1198 if (NScope->getFirstInsn())
1199 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001200
1201 DbgScope *Parent = NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001202 if (GetConcreteScope) {
Devang Pateldd7bb432009-10-14 21:08:09 +00001203 DILocation IL(InlinedAt);
Jim Grosbach652b7432009-11-21 23:12:12 +00001204 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel90a0fe32009-11-10 23:06:00 +00001205 IL.getOrigLocation().getNode());
1206 assert (Parent && "Unable to find Parent scope!");
1207 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001208 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001209 } else if (DIDescriptor(N).isLexicalBlock()) {
1210 DILexicalBlock DB(N);
1211 if (!DB.getContext().isNull()) {
1212 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1213 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001214 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001215 }
Devang Pateldd7bb432009-10-14 21:08:09 +00001216 }
Devang Patel6a260102009-10-01 20:31:14 +00001217
Devang Patelf5278f22009-10-27 20:47:17 +00001218 NScope->setFirstInsn(MI);
Devang Patel6a260102009-10-01 20:31:14 +00001219
Devang Patel90a0fe32009-11-10 23:06:00 +00001220 if (!Parent && !InlinedAt) {
Devang Patelce8986f2009-11-11 00:31:36 +00001221 StringRef SPName = DISubprogram(N).getLinkageName();
1222 if (SPName == MF->getFunction()->getName())
1223 CurrentFnDbgScope = NScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00001224 }
Devang Patel6a260102009-10-01 20:31:14 +00001225
Devang Patel90a0fe32009-11-10 23:06:00 +00001226 if (GetConcreteScope) {
1227 ConcreteScopes[InlinedAt] = NScope;
1228 getOrCreateAbstractScope(N);
1229 }
1230
Devang Patelf5278f22009-10-27 20:47:17 +00001231 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001232}
1233
Devang Patel90a0fe32009-11-10 23:06:00 +00001234DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1235 assert (N && "Invalid Scope encoding!");
1236
1237 DbgScope *AScope = AbstractScopes.lookup(N);
1238 if (AScope)
1239 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001240
Devang Patel90a0fe32009-11-10 23:06:00 +00001241 DbgScope *Parent = NULL;
1242
1243 DIDescriptor Scope(N);
1244 if (Scope.isLexicalBlock()) {
1245 DILexicalBlock DB(N);
1246 DIDescriptor ParentDesc = DB.getContext();
1247 if (!ParentDesc.isNull())
1248 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1249 }
1250
1251 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1252
1253 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001254 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001255 AScope->setAbstractScope();
1256 AbstractScopes[N] = AScope;
1257 if (DIDescriptor(N).isSubprogram())
1258 AbstractScopesList.push_back(AScope);
1259 return AScope;
1260}
Devang Patel6a260102009-10-01 20:31:14 +00001261
Jim Grosbach652b7432009-11-21 23:12:12 +00001262/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-11-21 02:48:08 +00001263/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1264/// If there are global variables in this scope then create and insert
1265/// DIEs for these variables.
1266DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001267
Devang Pateld90672c2009-11-20 21:37:22 +00001268 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel90a0fe32009-11-10 23:06:00 +00001269 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patel814a12c2009-12-14 16:18:45 +00001270 DISubprogram SP(SPNode);
1271 if (SP.isDefinition() && !SP.getContext().isCompileUnit()) {
1272 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1273 // Add arguments.
1274 DICompositeType SPTy = SP.getType();
1275 DIArray Args = SPTy.getTypeArray();
1276 unsigned SPTag = SPTy.getTag();
1277 if (SPTag == dwarf::DW_TAG_subroutine_type)
1278 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1279 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1280 addType(Arg, DIType(Args.getElement(i).getNode()));
1281 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1282 SPDie->addChild(Arg);
1283 }
1284 DIE *SPDeclDie = SPDie;
1285 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1286 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1287 SPDeclDie);
1288
1289 ModuleCU->addDie(SPDie);
1290 }
1291
Devang Patelc50078e2009-11-21 02:48:08 +00001292 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001293 DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001294 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001295 DWLabel("func_end", SubprogramCount));
1296 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patelc50078e2009-11-21 02:48:08 +00001297 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach652b7432009-11-21 23:12:12 +00001298
Devang Patel90a0fe32009-11-10 23:06:00 +00001299 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001300 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001301
Devang Patel90a0fe32009-11-10 23:06:00 +00001302 return SPDie;
1303}
1304
Jim Grosbach652b7432009-11-21 23:12:12 +00001305/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001306/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1307DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001308 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1309 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1310
1311 // Ignore empty scopes.
1312 if (StartID == EndID && StartID != 0)
1313 return NULL;
1314
1315 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1316 if (Scope->isAbstractScope())
1317 return ScopeDIE;
1318
Devang Patelc50078e2009-11-21 02:48:08 +00001319 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach652b7432009-11-21 23:12:12 +00001320 StartID ?
1321 DWLabel("label", StartID)
Devang Patel90a0fe32009-11-10 23:06:00 +00001322 : DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001323 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach652b7432009-11-21 23:12:12 +00001324 EndID ?
1325 DWLabel("label", EndID)
Devang Patel90a0fe32009-11-10 23:06:00 +00001326 : DWLabel("func_end", SubprogramCount));
1327
1328
1329
1330 return ScopeDIE;
1331}
1332
Devang Patelc50078e2009-11-21 02:48:08 +00001333/// constructInlinedScopeDIE - This scope represents inlined body of
1334/// a function. Construct DIE to represent this concrete inlined copy
1335/// of the function.
1336DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001337 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1338 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1339 assert (StartID && "Invalid starting label for an inlined scope!");
1340 assert (EndID && "Invalid end label for an inlined scope!");
1341 // Ignore empty scopes.
1342 if (StartID == EndID && StartID != 0)
1343 return NULL;
1344
1345 DIScope DS(Scope->getScopeNode());
1346 if (DS.isNull())
1347 return NULL;
1348 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1349
1350 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001351 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001352 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001353 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001354 dwarf::DW_FORM_ref4, OriginDIE);
1355
Devang Patelc50078e2009-11-21 02:48:08 +00001356 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001357 DWLabel("label", StartID));
Devang Patelc50078e2009-11-21 02:48:08 +00001358 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001359 DWLabel("label", EndID));
1360
1361 InlinedSubprogramDIEs.insert(OriginDIE);
1362
1363 // Track the start label for this inlined function.
1364 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1365 I = InlineInfo.find(InlinedSP.getNode());
1366
1367 if (I == InlineInfo.end()) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001368 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1369 ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001370 InlinedSPNodes.push_back(InlinedSP.getNode());
1371 } else
1372 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1373
1374 StringPool.insert(InlinedSP.getName());
1375 StringPool.insert(InlinedSP.getLinkageName());
1376 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001377 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1378 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001379
1380 return ScopeDIE;
1381}
1382
Devang Patelc50078e2009-11-21 02:48:08 +00001383
1384/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Devang Patelfe0be132009-12-09 18:24:21 +00001385DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001386 // Get the descriptor.
1387 const DIVariable &VD = DV->getVariable();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001388 StringRef Name = VD.getName();
1389 if (Name.empty())
Devang Patelab9a0682009-11-13 02:25:26 +00001390 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001391
1392 // Translate tag to proper Dwarf tag. The result variable is dropped for
1393 // now.
1394 unsigned Tag;
1395 switch (VD.getTag()) {
1396 case dwarf::DW_TAG_return_variable:
1397 return NULL;
1398 case dwarf::DW_TAG_arg_variable:
1399 Tag = dwarf::DW_TAG_formal_parameter;
1400 break;
1401 case dwarf::DW_TAG_auto_variable: // fall thru
1402 default:
1403 Tag = dwarf::DW_TAG_variable;
1404 break;
1405 }
1406
1407 // Define variable debug information entry.
1408 DIE *VariableDie = new DIE(Tag);
1409
1410
1411 DIE *AbsDIE = NULL;
1412 if (DbgVariable *AV = DV->getAbstractVariable())
1413 AbsDIE = AV->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001414
Devang Patel90a0fe32009-11-10 23:06:00 +00001415 if (AbsDIE) {
1416 DIScope DS(Scope->getScopeNode());
1417 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001418 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001419 (void) OriginSPDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001420 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1421 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1422 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001423 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001424 dwarf::DW_FORM_ref4, AbsDIE);
1425 }
1426 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001427 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1428 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001429
1430 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001431 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001432 // addresses instead.
1433 if (VD.isBlockByrefVariable())
Devang Patelfe0be132009-12-09 18:24:21 +00001434 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001435 else
Devang Patelfe0be132009-12-09 18:24:21 +00001436 addType(VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001437 }
1438
1439 // Add variable address.
1440 if (!Scope->isAbstractScope()) {
1441 MachineLocation Location;
Jim Grosbach587d4882009-11-22 20:14:00 +00001442 unsigned FrameReg;
1443 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1444 Location.set(FrameReg, Offset);
Jim Grosbach652b7432009-11-21 23:12:12 +00001445
Devang Patel90a0fe32009-11-10 23:06:00 +00001446 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001447 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001448 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001449 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001450 else
Devang Patelc50078e2009-11-21 02:48:08 +00001451 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001452 }
1453 DV->setDIE(VariableDie);
1454 return VariableDie;
1455
1456}
Devang Patelc50078e2009-11-21 02:48:08 +00001457
Devang Patelec13b4f2009-11-24 01:14:22 +00001458void DwarfDebug::addPubTypes(DISubprogram SP) {
1459 DICompositeType SPTy = SP.getType();
1460 unsigned SPTag = SPTy.getTag();
1461 if (SPTag != dwarf::DW_TAG_subroutine_type)
1462 return;
1463
1464 DIArray Args = SPTy.getTypeArray();
1465 if (Args.isNull())
1466 return;
1467
1468 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1469 DIType ATy(Args.getElement(i).getNode());
1470 if (ATy.isNull())
1471 continue;
1472 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001473 if (!CATy.isNull() && !CATy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001474 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1475 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1476 }
1477 }
1478}
1479
Devang Patelc50078e2009-11-21 02:48:08 +00001480/// 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 Patelfe0be132009-12-09 18:24:21 +00001505 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
Jim Grosbach652b7432009-11-21 23:12:12 +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]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001515 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001516 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001517 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001518
1519 if (DS.isSubprogram())
1520 addPubTypes(DISubprogram(DS.getNode()));
1521
1522 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001523}
1524
Bill Wendlingf5839192009-05-20 23:19:06 +00001525/// GetOrCreateSourceID - Look up the source id with the given directory and
1526/// source file names. If none currently exists, create a new id and insert it
1527/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1528/// maps as well.
Devang Patel7f75bbe2009-11-25 17:36:49 +00001529unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001530 unsigned DId;
1531 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1532 if (DI != DirectoryIdMap.end()) {
1533 DId = DI->getValue();
1534 } else {
1535 DId = DirectoryNames.size() + 1;
1536 DirectoryIdMap[DirName] = DId;
1537 DirectoryNames.push_back(DirName);
1538 }
1539
1540 unsigned FId;
1541 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1542 if (FI != SourceFileIdMap.end()) {
1543 FId = FI->getValue();
1544 } else {
1545 FId = SourceFileNames.size() + 1;
1546 SourceFileIdMap[FileName] = FId;
1547 SourceFileNames.push_back(FileName);
1548 }
1549
1550 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1551 SourceIdMap.find(std::make_pair(DId, FId));
1552 if (SI != SourceIdMap.end())
1553 return SI->second;
1554
1555 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1556 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1557 SourceIds.push_back(std::make_pair(DId, FId));
1558
1559 return SrcId;
1560}
1561
Devang Patelb9f2c6b2009-12-11 21:37:07 +00001562CompileUnit *DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001563 DICompileUnit DIUnit(N);
Devang Patel7f75bbe2009-11-25 17:36:49 +00001564 StringRef FN = DIUnit.getFilename();
1565 StringRef Dir = DIUnit.getDirectory();
Devang Patelaaf012e2009-09-29 18:40:58 +00001566 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001567
1568 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelc50078e2009-11-21 02:48:08 +00001569 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf5839192009-05-20 23:19:06 +00001570 DWLabel("section_line", 0), DWLabel("section_line", 0),
1571 false);
Devang Patelc50078e2009-11-21 02:48:08 +00001572 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001573 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001574 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001575 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001576 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001577
Devang Patel7f75bbe2009-11-25 17:36:49 +00001578 if (!Dir.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001579 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001580 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001581 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001582
Devang Patel7f75bbe2009-11-25 17:36:49 +00001583 StringRef Flags = DIUnit.getFlags();
1584 if (!Flags.empty())
Devang Patelc50078e2009-11-21 02:48:08 +00001585 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001586
1587 unsigned RVer = DIUnit.getRunTimeVersion();
1588 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001589 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001590 dwarf::DW_FORM_data1, RVer);
1591
1592 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel5a3d37f2009-06-29 20:45:18 +00001593 if (!ModuleCU && DIUnit.isMain()) {
Devang Patelf97a05a2009-06-29 20:38:13 +00001594 // Use first compile unit marked as isMain as the compile unit
1595 // for this module.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001596 ModuleCU = Unit;
Devang Patelf97a05a2009-06-29 20:38:13 +00001597 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001598
Devang Patel15e723d2009-08-28 23:24:31 +00001599 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf5839192009-05-20 23:19:06 +00001600 CompileUnits.push_back(Unit);
Devang Patelb9f2c6b2009-12-11 21:37:07 +00001601 return Unit;
Bill Wendlingf5839192009-05-20 23:19:06 +00001602}
1603
Devang Patelc50078e2009-11-21 02:48:08 +00001604void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001605 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001606
Devang Patel0c03f062009-09-04 23:59:07 +00001607 // If debug information is malformed then ignore it.
1608 if (DI_GV.Verify() == false)
1609 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001610
1611 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001612 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel166f8432009-06-26 01:49:18 +00001613 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001614
Devang Patelfe0be132009-12-09 18:24:21 +00001615 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
Devang Patel6d479632009-12-10 23:25:41 +00001616 if (!VariableDie)
1617 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001618
Bill Wendlingf5839192009-05-20 23:19:06 +00001619 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001620 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001621
1622 // Add to context owner.
Devang Patel1a8f9a82009-12-10 19:14:49 +00001623 addToContextOwner(VariableDie, DI_GV.getContext());
1624
Bill Wendlingf5839192009-05-20 23:19:06 +00001625 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001626 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001627
1628 DIType GTy = DI_GV.getType();
Devang Patel7f75bbe2009-11-25 17:36:49 +00001629 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patelec13b4f2009-11-24 01:14:22 +00001630 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1631 assert (Entry && "Missing global type!");
1632 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1633 }
Devang Patel166f8432009-06-26 01:49:18 +00001634 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001635}
1636
Devang Patelc50078e2009-11-21 02:48:08 +00001637void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001638 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001639
1640 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001641 if (ModuleCU->getDIE(N))
Devang Patel166f8432009-06-26 01:49:18 +00001642 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001643
1644 if (!SP.isDefinition())
1645 // This is a method declaration which will be handled while constructing
1646 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001647 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001648
Devang Patelfe0be132009-12-09 18:24:21 +00001649 DIE *SubprogramDie = createSubprogramDIE(SP);
Bill Wendlingf5839192009-05-20 23:19:06 +00001650
1651 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001652 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001653
1654 // Add to context owner.
Devang Patelde2d3682009-12-08 23:21:45 +00001655 if (SP.getContext().getNode() == SP.getCompileUnit().getNode())
Devang Patelc1df8792009-12-03 01:25:38 +00001656 if (TopLevelDIEs.insert(SubprogramDie))
1657 TopLevelDIEsVector.push_back(SubprogramDie);
Devang Patelde2d3682009-12-08 23:21:45 +00001658
Bill Wendlingf5839192009-05-20 23:19:06 +00001659 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001660 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001661
Devang Patel166f8432009-06-26 01:49:18 +00001662 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001663}
1664
Devang Patelc50078e2009-11-21 02:48:08 +00001665/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001666/// content. Create global DIEs and emit initial debug info sections.
1667/// This is inovked by the target AsmPrinter.
Devang Patelc50078e2009-11-21 02:48:08 +00001668void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel59a1d422009-06-25 22:36:02 +00001669 this->M = M;
1670
Bill Wendlingf5839192009-05-20 23:19:06 +00001671 if (TimePassesIsEnabled)
1672 DebugTimer->startTimer();
1673
Devang Patel1b4d6832009-11-11 19:55:08 +00001674 if (!MAI->doesSupportDebugInformation())
1675 return;
1676
Devang Patelfda766d2009-07-30 18:56:46 +00001677 DebugInfoFinder DbgFinder;
1678 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001679
Bill Wendlingf5839192009-05-20 23:19:06 +00001680 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001681 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1682 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001683 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001684
1685 if (CompileUnits.empty()) {
1686 if (TimePassesIsEnabled)
1687 DebugTimer->stopTimer();
1688
1689 return;
1690 }
1691
Devang Patelf97a05a2009-06-29 20:38:13 +00001692 // If main compile unit for this module is not seen than randomly
1693 // select first compile unit.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001694 if (!ModuleCU)
1695 ModuleCU = CompileUnits[0];
Devang Patelf97a05a2009-06-29 20:38:13 +00001696
Devang Patel90a0fe32009-11-10 23:06:00 +00001697 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001698 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1699 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001700 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001701
Devang Patel1a8f9a82009-12-10 19:14:49 +00001702 // Create DIEs for each global variable.
1703 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1704 E = DbgFinder.global_variable_end(); I != E; ++I)
1705 constructGlobalVariableDIE(*I);
1706
Bill Wendlingf5839192009-05-20 23:19:06 +00001707 MMI = mmi;
1708 shouldEmit = true;
1709 MMI->setDebugInfoAvailability(true);
1710
1711 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001712 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001713
1714 // Print out .file directives to specify files for .loc directives. These are
1715 // printed out early so that they precede any .loc directives.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001716 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001717 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1718 // Remember source id starts at 1.
1719 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1720 sys::Path FullPath(getSourceDirectoryName(Id.first));
1721 bool AppendOk =
1722 FullPath.appendComponent(getSourceFileName(Id.second));
1723 assert(AppendOk && "Could not append filename to directory!");
1724 AppendOk = false;
Chris Lattnerb1aa85b2009-08-23 22:45:37 +00001725 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001726 Asm->EOL();
1727 }
1728 }
1729
1730 // Emit initial sections
Devang Patelc50078e2009-11-21 02:48:08 +00001731 emitInitial();
Bill Wendlingf5839192009-05-20 23:19:06 +00001732
1733 if (TimePassesIsEnabled)
1734 DebugTimer->stopTimer();
1735}
1736
Devang Patelc50078e2009-11-21 02:48:08 +00001737/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001738///
Devang Patelc50078e2009-11-21 02:48:08 +00001739void DwarfDebug::endModule() {
Devang Patel95d477e2009-10-06 00:03:14 +00001740 if (!ModuleCU)
Bill Wendlingf5839192009-05-20 23:19:06 +00001741 return;
1742
1743 if (TimePassesIsEnabled)
1744 DebugTimer->startTimer();
1745
Devang Patel90a0fe32009-11-10 23:06:00 +00001746 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1747 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1748 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1749 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001750 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001751 }
1752
Devang Patelc1df8792009-12-03 01:25:38 +00001753 // Insert top level DIEs.
1754 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1755 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1756 ModuleCU->getCUDie()->addChild(*TI);
1757
Devang Patel188c85d2009-12-03 19:11:07 +00001758 for (DenseMap<DIE *, WeakVH>::iterator CI = ContainingTypeMap.begin(),
1759 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1760 DIE *SPDie = CI->first;
1761 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1762 if (!N) continue;
1763 DIE *NDie = ModuleCU->getDIE(N);
1764 if (!NDie) continue;
1765 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1766 addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1767 }
1768
Bill Wendlingf5839192009-05-20 23:19:06 +00001769 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001770 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001771 EmitLabel("text_end", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00001772 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001773 EmitLabel("data_end", 0);
1774
1775 // End text sections.
1776 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001777 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf5839192009-05-20 23:19:06 +00001778 EmitLabel("section_end", i);
1779 }
1780
1781 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001782 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001783
1784 // Emit function debug frame information
1785 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1786 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001787 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001788
1789 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001790 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001791
1792 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001793 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001794
1795 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00001796 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00001797
1798 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00001799 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00001800
1801 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00001802 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00001803
Devang Patelec13b4f2009-11-24 01:14:22 +00001804 // Emit info into a debug pubtypes section.
1805 emitDebugPubTypes();
1806
Bill Wendlingf5839192009-05-20 23:19:06 +00001807 // Emit info into a debug str section.
Devang Patelc50078e2009-11-21 02:48:08 +00001808 emitDebugStr();
Bill Wendlingf5839192009-05-20 23:19:06 +00001809
1810 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00001811 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00001812
1813 // Emit info into a debug aranges section.
1814 EmitDebugARanges();
1815
1816 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00001817 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00001818
1819 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00001820 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001821
1822 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00001823 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001824
1825 if (TimePassesIsEnabled)
1826 DebugTimer->stopTimer();
1827}
1828
Devang Patel90a0fe32009-11-10 23:06:00 +00001829/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00001830DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1831 unsigned FrameIdx,
Devang Patel90a0fe32009-11-10 23:06:00 +00001832 DILocation &ScopeLoc) {
1833
1834 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1835 if (AbsDbgVariable)
1836 return AbsDbgVariable;
1837
1838 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1839 if (!Scope)
1840 return NULL;
1841
1842 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patelc50078e2009-11-21 02:48:08 +00001843 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00001844 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1845 return AbsDbgVariable;
1846}
1847
Devang Patelc50078e2009-11-21 02:48:08 +00001848/// collectVariableInfo - Populate DbgScope entries with variables' info.
1849void DwarfDebug::collectVariableInfo() {
Devang Patel40c80212009-10-09 22:42:28 +00001850 if (!MMI) return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001851
Devang Patel84139992009-10-06 01:26:37 +00001852 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1853 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1854 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel40c80212009-10-09 22:42:28 +00001855 MetadataBase *MB = VI->first;
1856 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel90a0fe32009-11-10 23:06:00 +00001857 if (!Var) continue;
Devang Patel6882dff2009-10-08 18:48:03 +00001858 DIVariable DV (Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00001859 std::pair< unsigned, MDNode *> VP = VI->second;
1860 DILocation ScopeLoc(VP.second);
1861
1862 DbgScope *Scope =
1863 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1864 if (!Scope)
Jim Grosbach652b7432009-11-21 23:12:12 +00001865 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelbad42262009-11-10 23:20:04 +00001866 // If variable scope is not found then skip this variable.
1867 if (!Scope)
1868 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00001869
1870 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patelc50078e2009-11-21 02:48:08 +00001871 Scope->addVariable(RegVar);
Jim Grosbachb23f2422009-11-22 19:20:36 +00001872 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1873 ScopeLoc))
Devang Patel90a0fe32009-11-10 23:06:00 +00001874 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patel84139992009-10-06 01:26:37 +00001875 }
1876}
1877
Devang Patelc50078e2009-11-21 02:48:08 +00001878/// beginScope - Process beginning of a scope starting at Label.
1879void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel393a46d2009-10-06 01:50:42 +00001880 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1881 if (I == DbgScopeBeginMap.end())
1882 return;
Dan Gohman8d34f972009-11-23 21:30:55 +00001883 ScopeVector &SD = I->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00001884 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001885 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00001886 (*SDI)->setStartLabelID(Label);
1887}
1888
Devang Patelc50078e2009-11-21 02:48:08 +00001889/// endScope - Process end of a scope.
1890void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel393a46d2009-10-06 01:50:42 +00001891 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patelf4348892009-10-06 03:15:38 +00001892 if (I == DbgScopeEndMap.end())
Devang Patel393a46d2009-10-06 01:50:42 +00001893 return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001894
1895 unsigned Label = MMI->NextLabelID();
1896 Asm->printLabel(Label);
Dan Gohmancfca6e32009-12-05 01:42:34 +00001897 O << '\n';
Devang Patel90a0fe32009-11-10 23:06:00 +00001898
Devang Patel393a46d2009-10-06 01:50:42 +00001899 SmallVector<DbgScope *, 2> &SD = I->second;
1900 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001901 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00001902 (*SDI)->setEndLabelID(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00001903 return;
1904}
1905
1906/// createDbgScope - Create DbgScope for the scope.
1907void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1908
1909 if (!InlinedAt) {
1910 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1911 if (WScope)
1912 return;
1913 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1914 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach652b7432009-11-21 23:12:12 +00001915 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel53addbf2009-11-11 00:18:40 +00001916 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel90a0fe32009-11-10 23:06:00 +00001917 return;
1918 }
1919
1920 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1921 if (WScope)
1922 return;
1923
1924 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1925 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
1926 DILocation DL(InlinedAt);
1927 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel393a46d2009-10-06 01:50:42 +00001928}
1929
Devang Patelc50078e2009-11-21 02:48:08 +00001930/// extractScopeInformation - Scan machine instructions in this function
Devang Patel6a260102009-10-01 20:31:14 +00001931/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patelc50078e2009-11-21 02:48:08 +00001932bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patel6a260102009-10-01 20:31:14 +00001933 // If scope information was extracted using .dbg intrinsics then there is not
1934 // any need to extract these information by scanning each instruction.
1935 if (!DbgScopeMap.empty())
1936 return false;
1937
Devang Patel90a0fe32009-11-10 23:06:00 +00001938 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6a260102009-10-01 20:31:14 +00001939 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1940 I != E; ++I) {
1941 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1942 II != IE; ++II) {
1943 const MachineInstr *MInsn = II;
1944 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00001945 if (DL.isUnknown()) continue;
Devang Patel6a260102009-10-01 20:31:14 +00001946 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel90a0fe32009-11-10 23:06:00 +00001947 if (!DLT.Scope) continue;
Devang Patel6a260102009-10-01 20:31:14 +00001948 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00001949 // other scopes, create one DbgScope now. This will be translated
Devang Patel6a260102009-10-01 20:31:14 +00001950 // into a scope DIE at the end.
Devang Patel90a0fe32009-11-10 23:06:00 +00001951 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1952 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
1953 }
1954 }
1955
1956
1957 // Build scope hierarchy using working set of scopes.
1958 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1959 I != E; ++I) {
1960 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1961 II != IE; ++II) {
1962 const MachineInstr *MInsn = II;
1963 DebugLoc DL = MInsn->getDebugLoc();
1964 if (DL.isUnknown()) continue;
1965 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
1966 if (!DLT.Scope) continue;
1967 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00001968 // other scopes, create one DbgScope now. This will be translated
Devang Patel90a0fe32009-11-10 23:06:00 +00001969 // into a scope DIE at the end.
1970 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1971 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
1972 Scope->setLastInsn(MInsn);
Devang Patel6a260102009-10-01 20:31:14 +00001973 }
1974 }
1975
1976 // If a scope's last instruction is not set then use its child scope's
1977 // last instruction as this scope's last instrunction.
Devang Patelf5278f22009-10-27 20:47:17 +00001978 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00001979 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001980 if (DI->second->isAbstractScope())
1981 continue;
Devang Patel6a260102009-10-01 20:31:14 +00001982 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patelc50078e2009-11-21 02:48:08 +00001983 DI->second->fixInstructionMarkers();
Devang Patel6a260102009-10-01 20:31:14 +00001984 assert (DI->second->getLastInsn() && "Invalid last instruction!");
1985 }
1986
1987 // Each scope has first instruction and last instruction to mark beginning
1988 // and end of a scope respectively. Create an inverse map that list scopes
1989 // starts (and ends) with an instruction. One instruction may start (or end)
1990 // multiple scopes.
Devang Patelf5278f22009-10-27 20:47:17 +00001991 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00001992 DE = DbgScopeMap.end(); DI != DE; ++DI) {
1993 DbgScope *S = DI->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00001994 if (S->isAbstractScope())
1995 continue;
Devang Patel6a260102009-10-01 20:31:14 +00001996 const MachineInstr *MI = S->getFirstInsn();
1997 assert (MI && "DbgScope does not have first instruction!");
1998
1999 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2000 if (IDI != DbgScopeBeginMap.end())
2001 IDI->second.push_back(S);
2002 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002003 DbgScopeBeginMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002004
2005 MI = S->getLastInsn();
2006 assert (MI && "DbgScope does not have last instruction!");
2007 IDI = DbgScopeEndMap.find(MI);
2008 if (IDI != DbgScopeEndMap.end())
2009 IDI->second.push_back(S);
2010 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002011 DbgScopeEndMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002012 }
2013
2014 return !DbgScopeMap.empty();
2015}
2016
Devang Patelc50078e2009-11-21 02:48:08 +00002017/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002018/// emitted immediately after the function entry point.
Devang Patelc50078e2009-11-21 02:48:08 +00002019void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002020 this->MF = MF;
2021
2022 if (!ShouldEmitDwarfDebug()) return;
2023
2024 if (TimePassesIsEnabled)
2025 DebugTimer->startTimer();
2026
Devang Patelc50078e2009-11-21 02:48:08 +00002027 if (!extractScopeInformation(MF))
Devang Patel0feae422009-10-06 18:37:31 +00002028 return;
Devang Patelc50078e2009-11-21 02:48:08 +00002029
2030 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002031
Bill Wendlingf5839192009-05-20 23:19:06 +00002032 // Begin accumulating function debug information.
2033 MMI->BeginFunction(MF);
2034
2035 // Assumes in correct section after the entry point.
2036 EmitLabel("func_begin", ++SubprogramCount);
2037
2038 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2039 // function.
Devang Patel40c80212009-10-09 22:42:28 +00002040 DebugLoc FDL = MF->getDefaultDebugLoc();
2041 if (!FDL.isUnknown()) {
2042 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2043 unsigned LabelID = 0;
Devang Patelfc1df342009-10-13 23:28:53 +00002044 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002045 if (!SP.isNull())
Devang Patelc50078e2009-11-21 02:48:08 +00002046 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002047 else
Devang Patelc50078e2009-11-21 02:48:08 +00002048 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002049 Asm->printLabel(LabelID);
2050 O << '\n';
Bill Wendlingf5839192009-05-20 23:19:06 +00002051 }
Bill Wendlingf5839192009-05-20 23:19:06 +00002052 if (TimePassesIsEnabled)
2053 DebugTimer->stopTimer();
2054}
2055
Devang Patelc50078e2009-11-21 02:48:08 +00002056/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002057///
Devang Patelc50078e2009-11-21 02:48:08 +00002058void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002059 if (!ShouldEmitDwarfDebug()) return;
2060
2061 if (TimePassesIsEnabled)
2062 DebugTimer->startTimer();
2063
Devang Patel40c80212009-10-09 22:42:28 +00002064 if (DbgScopeMap.empty())
2065 return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002066
Bill Wendlingf5839192009-05-20 23:19:06 +00002067 // Define end label for subprogram.
2068 EmitLabel("func_end", SubprogramCount);
2069
2070 // Get function line info.
2071 if (!Lines.empty()) {
2072 // Get section line info.
Chris Lattnerebd055c2009-08-03 23:20:21 +00002073 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00002074 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2075 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2076 // Append the function info to section info.
2077 SectionLineInfos.insert(SectionLineInfos.end(),
2078 Lines.begin(), Lines.end());
2079 }
2080
Devang Patel90a0fe32009-11-10 23:06:00 +00002081 // Construct abstract scopes.
2082 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach652b7432009-11-21 23:12:12 +00002083 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patelc50078e2009-11-21 02:48:08 +00002084 constructScopeDIE(*AI);
Bill Wendlingf5839192009-05-20 23:19:06 +00002085
Devang Patelc50078e2009-11-21 02:48:08 +00002086 constructScopeDIE(CurrentFnDbgScope);
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002087
Bill Wendlingf5839192009-05-20 23:19:06 +00002088 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2089 MMI->getFrameMoves()));
2090
2091 // Clear debug info
Devang Patel67533ab2009-12-01 18:13:48 +00002092 CurrentFnDbgScope = NULL;
2093 DbgScopeMap.clear();
2094 DbgScopeBeginMap.clear();
2095 DbgScopeEndMap.clear();
2096 ConcreteScopes.clear();
2097 AbstractScopesList.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002098
2099 Lines.clear();
Devang Patel67533ab2009-12-01 18:13:48 +00002100
Bill Wendlingf5839192009-05-20 23:19:06 +00002101 if (TimePassesIsEnabled)
2102 DebugTimer->stopTimer();
2103}
2104
Devang Patelc50078e2009-11-21 02:48:08 +00002105/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf5839192009-05-20 23:19:06 +00002106/// label. Returns a unique label ID used to generate a label and provide
2107/// correspondence to the source line list.
Jim Grosbach652b7432009-11-21 23:12:12 +00002108unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel946d0ae2009-10-05 18:03:19 +00002109 MDNode *S) {
Devang Patel15e723d2009-08-28 23:24:31 +00002110 if (!MMI)
2111 return 0;
2112
Bill Wendlingf5839192009-05-20 23:19:06 +00002113 if (TimePassesIsEnabled)
2114 DebugTimer->startTimer();
2115
Devang Patel7f75bbe2009-11-25 17:36:49 +00002116 StringRef Dir;
2117 StringRef Fn;
Devang Patel946d0ae2009-10-05 18:03:19 +00002118
2119 DIDescriptor Scope(S);
2120 if (Scope.isCompileUnit()) {
2121 DICompileUnit CU(S);
2122 Dir = CU.getDirectory();
2123 Fn = CU.getFilename();
2124 } else if (Scope.isSubprogram()) {
2125 DISubprogram SP(S);
2126 Dir = SP.getDirectory();
2127 Fn = SP.getFilename();
2128 } else if (Scope.isLexicalBlock()) {
2129 DILexicalBlock DB(S);
2130 Dir = DB.getDirectory();
2131 Fn = DB.getFilename();
2132 } else
2133 assert (0 && "Unexpected scope info");
2134
2135 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf5839192009-05-20 23:19:06 +00002136 unsigned ID = MMI->NextLabelID();
2137 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2138
2139 if (TimePassesIsEnabled)
2140 DebugTimer->stopTimer();
2141
2142 return ID;
2143}
2144
2145/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2146/// timed. Look up the source id with the given directory and source file
2147/// names. If none currently exists, create a new id and insert it in the
2148/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2149/// well.
2150unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2151 const std::string &FileName) {
2152 if (TimePassesIsEnabled)
2153 DebugTimer->startTimer();
2154
Devang Patelaaf012e2009-09-29 18:40:58 +00002155 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf5839192009-05-20 23:19:06 +00002156
2157 if (TimePassesIsEnabled)
2158 DebugTimer->stopTimer();
2159
2160 return SrcId;
2161}
2162
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002163//===----------------------------------------------------------------------===//
2164// Emit Methods
2165//===----------------------------------------------------------------------===//
2166
Devang Patelc50078e2009-11-21 02:48:08 +00002167/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002168///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002169unsigned
2170DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002171 // Get the children.
2172 const std::vector<DIE *> &Children = Die->getChildren();
2173
2174 // If not last sibling and has children then add sibling offset attribute.
Devang Patelc50078e2009-11-21 02:48:08 +00002175 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002176
2177 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002178 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002179
2180 // Get the abbreviation for this DIE.
2181 unsigned AbbrevNumber = Die->getAbbrevNumber();
2182 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2183
2184 // Set DIE offset
2185 Die->setOffset(Offset);
2186
2187 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002188 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002189
2190 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2191 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2192
2193 // Size the DIE attribute values.
2194 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2195 // Size attribute value.
2196 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2197
2198 // Size the DIE children if any.
2199 if (!Children.empty()) {
2200 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2201 "Children flag not set");
2202
2203 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002204 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002205
2206 // End of children marker.
2207 Offset += sizeof(int8_t);
2208 }
2209
2210 Die->setSize(Offset - Die->getOffset());
2211 return Offset;
2212}
2213
Devang Patelc50078e2009-11-21 02:48:08 +00002214/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002215///
Devang Patelc50078e2009-11-21 02:48:08 +00002216void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002217 // Compute size of compile unit header.
2218 static unsigned Offset =
2219 sizeof(int32_t) + // Length of Compilation Unit Info
2220 sizeof(int16_t) + // DWARF version number
2221 sizeof(int32_t) + // Offset Into Abbrev. Section
2222 sizeof(int8_t); // Pointer Size (in bytes)
2223
Devang Patelc50078e2009-11-21 02:48:08 +00002224 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel5a3d37f2009-06-29 20:45:18 +00002225 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling55fccda2009-05-20 23:21:38 +00002226}
2227
Devang Patelc50078e2009-11-21 02:48:08 +00002228/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling55fccda2009-05-20 23:21:38 +00002229/// tools to recognize the object file contains Dwarf information.
Devang Patelc50078e2009-11-21 02:48:08 +00002230void DwarfDebug::emitInitial() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002231 // Check to see if we already emitted intial headers.
2232 if (didInitial) return;
2233 didInitial = true;
2234
Chris Lattner73266f92009-08-19 05:49:37 +00002235 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002236
Bill Wendling55fccda2009-05-20 23:21:38 +00002237 // Dwarf sections base addresses.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002238 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner73266f92009-08-19 05:49:37 +00002239 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002240 EmitLabel("section_debug_frame", 0);
2241 }
2242
Chris Lattner73266f92009-08-19 05:49:37 +00002243 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002244 EmitLabel("section_info", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002245 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002246 EmitLabel("section_abbrev", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002247 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002248 EmitLabel("section_aranges", 0);
2249
Chris Lattner73266f92009-08-19 05:49:37 +00002250 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2251 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling55fccda2009-05-20 23:21:38 +00002252 EmitLabel("section_macinfo", 0);
2253 }
2254
Chris Lattner73266f92009-08-19 05:49:37 +00002255 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002256 EmitLabel("section_line", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002257 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002258 EmitLabel("section_loc", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002259 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002260 EmitLabel("section_pubnames", 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00002261 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2262 EmitLabel("section_pubtypes", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002263 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002264 EmitLabel("section_str", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002265 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002266 EmitLabel("section_ranges", 0);
2267
Chris Lattner73266f92009-08-19 05:49:37 +00002268 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002269 EmitLabel("text_begin", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002270 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002271 EmitLabel("data_begin", 0);
2272}
2273
Devang Patelc50078e2009-11-21 02:48:08 +00002274/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002275///
Devang Patelc50078e2009-11-21 02:48:08 +00002276void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002277 // Get the abbreviation for this DIE.
2278 unsigned AbbrevNumber = Die->getAbbrevNumber();
2279 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2280
2281 Asm->EOL();
2282
2283 // Emit the code (index) for the abbreviation.
2284 Asm->EmitULEB128Bytes(AbbrevNumber);
2285
2286 if (Asm->isVerbose())
2287 Asm->EOL(std::string("Abbrev [" +
2288 utostr(AbbrevNumber) +
2289 "] 0x" + utohexstr(Die->getOffset()) +
2290 ":0x" + utohexstr(Die->getSize()) + " " +
2291 dwarf::TagString(Abbrev->getTag())));
2292 else
2293 Asm->EOL();
2294
2295 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2296 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2297
2298 // Emit the DIE attribute values.
2299 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2300 unsigned Attr = AbbrevData[i].getAttribute();
2301 unsigned Form = AbbrevData[i].getForm();
2302 assert(Form && "Too many attributes for DIE (check abbreviation)");
2303
2304 switch (Attr) {
2305 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002306 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002307 break;
2308 case dwarf::DW_AT_abstract_origin: {
2309 DIEEntry *E = cast<DIEEntry>(Values[i]);
2310 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002311 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002312 Asm->EmitInt32(Addr);
2313 break;
2314 }
2315 default:
2316 // Emit an attribute using the defined form.
2317 Values[i]->EmitValue(this, Form);
2318 break;
2319 }
2320
2321 Asm->EOL(dwarf::AttributeString(Attr));
2322 }
2323
2324 // Emit the DIE children if any.
2325 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2326 const std::vector<DIE *> &Children = Die->getChildren();
2327
2328 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002329 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002330
2331 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2332 }
2333}
2334
Devang Patelfe0be132009-12-09 18:24:21 +00002335/// emitDebugInfo - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002336///
Devang Patelfe0be132009-12-09 18:24:21 +00002337void DwarfDebug::emitDebugInfo() {
2338 // Start debug info section.
2339 Asm->OutStreamer.SwitchSection(
2340 Asm->getObjFileLowering().getDwarfInfoSection());
2341 DIE *Die = ModuleCU->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002342
2343 // Emit the compile units header.
Devang Patelfe0be132009-12-09 18:24:21 +00002344 EmitLabel("info_begin", ModuleCU->getID());
Bill Wendling55fccda2009-05-20 23:21:38 +00002345
2346 // Emit size of content not including length itself
2347 unsigned ContentSize = Die->getSize() +
2348 sizeof(int16_t) + // DWARF version number
2349 sizeof(int32_t) + // Offset Into Abbrev. Section
2350 sizeof(int8_t) + // Pointer Size (in bytes)
2351 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2352
2353 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2354 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2355 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2356 Asm->EOL("Offset Into Abbrev. Section");
2357 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2358
Devang Patelc50078e2009-11-21 02:48:08 +00002359 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002360 // FIXME - extra padding for gdb bug.
2361 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2362 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2363 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2364 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
Devang Patelfe0be132009-12-09 18:24:21 +00002365 EmitLabel("info_end", ModuleCU->getID());
Bill Wendling55fccda2009-05-20 23:21:38 +00002366
2367 Asm->EOL();
Bill Wendling55fccda2009-05-20 23:21:38 +00002368
Bill Wendling55fccda2009-05-20 23:21:38 +00002369}
2370
Devang Patelc50078e2009-11-21 02:48:08 +00002371/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002372///
Devang Patelc50078e2009-11-21 02:48:08 +00002373void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002374 // Check to see if it is worth the effort.
2375 if (!Abbreviations.empty()) {
2376 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002377 Asm->OutStreamer.SwitchSection(
2378 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002379
2380 EmitLabel("abbrev_begin", 0);
2381
2382 // For each abbrevation.
2383 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2384 // Get abbreviation data
2385 const DIEAbbrev *Abbrev = Abbreviations[i];
2386
2387 // Emit the abbrevations code (base 1 index.)
2388 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2389 Asm->EOL("Abbreviation Code");
2390
2391 // Emit the abbreviations data.
2392 Abbrev->Emit(Asm);
2393
2394 Asm->EOL();
2395 }
2396
2397 // Mark end of abbreviations.
2398 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2399
2400 EmitLabel("abbrev_end", 0);
2401 Asm->EOL();
2402 }
2403}
2404
Devang Patelc50078e2009-11-21 02:48:08 +00002405/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002406/// the line matrix.
2407///
Devang Patelc50078e2009-11-21 02:48:08 +00002408void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002409 // Define last address of section.
2410 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2411 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2412 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2413 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2414
2415 // Mark end of matrix.
2416 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2417 Asm->EmitULEB128Bytes(1); Asm->EOL();
2418 Asm->EmitInt8(1); Asm->EOL();
2419}
2420
Devang Patelc50078e2009-11-21 02:48:08 +00002421/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002422///
Devang Patelc50078e2009-11-21 02:48:08 +00002423void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002424 // If the target is using .loc/.file, the assembler will be emitting the
2425 // .debug_line table automatically.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002426 if (MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002427 return;
2428
2429 // Minimum line delta, thus ranging from -10..(255-10).
2430 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2431 // Maximum line delta, thus ranging from -10..(255-10).
2432 const int MaxLineDelta = 255 + MinLineDelta;
2433
2434 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002435 Asm->OutStreamer.SwitchSection(
2436 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002437
2438 // Construct the section header.
2439 EmitDifference("line_end", 0, "line_begin", 0, true);
2440 Asm->EOL("Length of Source Line Info");
2441 EmitLabel("line_begin", 0);
2442
2443 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2444
2445 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2446 Asm->EOL("Prolog Length");
2447 EmitLabel("line_prolog_begin", 0);
2448
2449 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2450
2451 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2452
2453 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2454
2455 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2456
2457 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2458
2459 // Line number standard opcode encodings argument count
2460 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2461 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2462 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2463 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2464 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2465 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2466 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2467 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2468 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2469
2470 // Emit directories.
2471 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2472 Asm->EmitString(getSourceDirectoryName(DI));
2473 Asm->EOL("Directory");
2474 }
2475
2476 Asm->EmitInt8(0); Asm->EOL("End of directories");
2477
2478 // Emit files.
2479 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2480 // Remember source id starts at 1.
2481 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2482 Asm->EmitString(getSourceFileName(Id.second));
2483 Asm->EOL("Source");
2484 Asm->EmitULEB128Bytes(Id.first);
2485 Asm->EOL("Directory #");
2486 Asm->EmitULEB128Bytes(0);
2487 Asm->EOL("Mod date");
2488 Asm->EmitULEB128Bytes(0);
2489 Asm->EOL("File size");
2490 }
2491
2492 Asm->EmitInt8(0); Asm->EOL("End of files");
2493
2494 EmitLabel("line_prolog_end", 0);
2495
2496 // A sequence for each text section.
2497 unsigned SecSrcLinesSize = SectionSourceLines.size();
2498
2499 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2500 // Isolate current sections line info.
2501 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2502
Chris Lattner26aabb92009-08-08 23:39:42 +00002503 /*if (Asm->isVerbose()) {
Chris Lattnere6ad12f2009-07-31 18:48:30 +00002504 const MCSection *S = SectionMap[j + 1];
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002505 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling55fccda2009-05-20 23:21:38 +00002506 << S->getName() << '\n';
Chris Lattner26aabb92009-08-08 23:39:42 +00002507 }*/
2508 Asm->EOL();
Bill Wendling55fccda2009-05-20 23:21:38 +00002509
2510 // Dwarf assumes we start with first line of first source file.
2511 unsigned Source = 1;
2512 unsigned Line = 1;
2513
2514 // Construct rows of the address, source, line, column matrix.
2515 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2516 const SrcLineInfo &LineInfo = LineInfos[i];
2517 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2518 if (!LabelID) continue;
2519
Caroline Tice9da96d82009-09-11 18:25:54 +00002520 if (LineInfo.getLine() == 0) continue;
2521
Bill Wendling55fccda2009-05-20 23:21:38 +00002522 if (!Asm->isVerbose())
2523 Asm->EOL();
2524 else {
2525 std::pair<unsigned, unsigned> SourceID =
2526 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002527 O << '\t' << MAI->getCommentString() << ' '
Dan Gohman1792bc62009-12-05 02:00:34 +00002528 << getSourceDirectoryName(SourceID.first) << '/'
Bill Wendling55fccda2009-05-20 23:21:38 +00002529 << getSourceFileName(SourceID.second)
Dan Gohman1792bc62009-12-05 02:00:34 +00002530 << ':' << utostr_32(LineInfo.getLine()) << '\n';
Bill Wendling55fccda2009-05-20 23:21:38 +00002531 }
2532
2533 // Define the line address.
2534 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2535 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2536 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2537 EmitReference("label", LabelID); Asm->EOL("Location label");
2538
2539 // If change of source, then switch to the new source.
2540 if (Source != LineInfo.getSourceID()) {
2541 Source = LineInfo.getSourceID();
2542 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2543 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2544 }
2545
2546 // If change of line.
2547 if (Line != LineInfo.getLine()) {
2548 // Determine offset.
2549 int Offset = LineInfo.getLine() - Line;
2550 int Delta = Offset - MinLineDelta;
2551
2552 // Update line.
2553 Line = LineInfo.getLine();
2554
2555 // If delta is small enough and in range...
2556 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2557 // ... then use fast opcode.
2558 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2559 } else {
2560 // ... otherwise use long hand.
2561 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2562 Asm->EOL("DW_LNS_advance_line");
2563 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2564 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2565 }
2566 } else {
2567 // Copy the previous row (different address or source)
2568 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2569 }
2570 }
2571
Devang Patelc50078e2009-11-21 02:48:08 +00002572 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002573 }
2574
2575 if (SecSrcLinesSize == 0)
2576 // Because we're emitting a debug_line section, we still need a line
2577 // table. The linker and friends expect it to exist. If there's nothing to
2578 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00002579 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002580
2581 EmitLabel("line_end", 0);
2582 Asm->EOL();
2583}
2584
Devang Patelc50078e2009-11-21 02:48:08 +00002585/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002586///
Devang Patelc50078e2009-11-21 02:48:08 +00002587void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002588 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002589 return;
2590
2591 int stackGrowth =
2592 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2593 TargetFrameInfo::StackGrowsUp ?
2594 TD->getPointerSize() : -TD->getPointerSize();
2595
2596 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002597 Asm->OutStreamer.SwitchSection(
2598 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002599
2600 EmitLabel("debug_frame_common", 0);
2601 EmitDifference("debug_frame_common_end", 0,
2602 "debug_frame_common_begin", 0, true);
2603 Asm->EOL("Length of Common Information Entry");
2604
2605 EmitLabel("debug_frame_common_begin", 0);
2606 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2607 Asm->EOL("CIE Identifier Tag");
2608 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2609 Asm->EOL("CIE Version");
2610 Asm->EmitString("");
2611 Asm->EOL("CIE Augmentation");
2612 Asm->EmitULEB128Bytes(1);
2613 Asm->EOL("CIE Code Alignment Factor");
2614 Asm->EmitSLEB128Bytes(stackGrowth);
2615 Asm->EOL("CIE Data Alignment Factor");
2616 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2617 Asm->EOL("CIE RA Column");
2618
2619 std::vector<MachineMove> Moves;
2620 RI->getInitialFrameState(Moves);
2621
2622 EmitFrameMoves(NULL, 0, Moves, false);
2623
2624 Asm->EmitAlignment(2, 0, 0, false);
2625 EmitLabel("debug_frame_common_end", 0);
2626
2627 Asm->EOL();
2628}
2629
Devang Patelc50078e2009-11-21 02:48:08 +00002630/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00002631/// section.
2632void
Devang Patelc50078e2009-11-21 02:48:08 +00002633DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002634 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002635 return;
2636
2637 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002638 Asm->OutStreamer.SwitchSection(
2639 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002640
2641 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2642 "debug_frame_begin", DebugFrameInfo.Number, true);
2643 Asm->EOL("Length of Frame Information Entry");
2644
2645 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2646
2647 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2648 0, 0, true, false);
2649 Asm->EOL("FDE CIE offset");
2650
2651 EmitReference("func_begin", DebugFrameInfo.Number);
2652 Asm->EOL("FDE initial location");
2653 EmitDifference("func_end", DebugFrameInfo.Number,
2654 "func_begin", DebugFrameInfo.Number);
2655 Asm->EOL("FDE address range");
2656
2657 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2658 false);
2659
2660 Asm->EmitAlignment(2, 0, 0, false);
2661 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2662
2663 Asm->EOL();
2664}
2665
Devang Patelfe0be132009-12-09 18:24:21 +00002666/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2667///
2668void DwarfDebug::emitDebugPubNames() {
2669 // Start the dwarf pubnames section.
2670 Asm->OutStreamer.SwitchSection(
2671 Asm->getObjFileLowering().getDwarfPubNamesSection());
2672
2673 EmitDifference("pubnames_end", ModuleCU->getID(),
2674 "pubnames_begin", ModuleCU->getID(), true);
Bill Wendling55fccda2009-05-20 23:21:38 +00002675 Asm->EOL("Length of Public Names Info");
2676
Devang Patelfe0be132009-12-09 18:24:21 +00002677 EmitLabel("pubnames_begin", ModuleCU->getID());
Bill Wendling55fccda2009-05-20 23:21:38 +00002678
2679 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2680
2681 EmitSectionOffset("info_begin", "section_info",
Devang Patelfe0be132009-12-09 18:24:21 +00002682 ModuleCU->getID(), 0, true, false);
Bill Wendling55fccda2009-05-20 23:21:38 +00002683 Asm->EOL("Offset of Compilation Unit Info");
2684
Devang Patelfe0be132009-12-09 18:24:21 +00002685 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
Bill Wendling55fccda2009-05-20 23:21:38 +00002686 true);
2687 Asm->EOL("Compilation Unit Length");
2688
Devang Patelfe0be132009-12-09 18:24:21 +00002689 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
Bill Wendling55fccda2009-05-20 23:21:38 +00002690 for (StringMap<DIE*>::const_iterator
2691 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2692 const char *Name = GI->getKeyData();
2693 DIE * Entity = GI->second;
2694
2695 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2696 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2697 }
2698
2699 Asm->EmitInt32(0); Asm->EOL("End Mark");
Devang Patelfe0be132009-12-09 18:24:21 +00002700 EmitLabel("pubnames_end", ModuleCU->getID());
Bill Wendling55fccda2009-05-20 23:21:38 +00002701
2702 Asm->EOL();
2703}
2704
Devang Patelec13b4f2009-11-24 01:14:22 +00002705void DwarfDebug::emitDebugPubTypes() {
Devang Patel6f2bdd52009-11-24 19:18:41 +00002706 // Start the dwarf pubnames section.
2707 Asm->OutStreamer.SwitchSection(
2708 Asm->getObjFileLowering().getDwarfPubTypesSection());
Devang Patelec13b4f2009-11-24 01:14:22 +00002709 EmitDifference("pubtypes_end", ModuleCU->getID(),
2710 "pubtypes_begin", ModuleCU->getID(), true);
2711 Asm->EOL("Length of Public Types Info");
2712
2713 EmitLabel("pubtypes_begin", ModuleCU->getID());
2714
2715 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2716
2717 EmitSectionOffset("info_begin", "section_info",
2718 ModuleCU->getID(), 0, true, false);
2719 Asm->EOL("Offset of Compilation ModuleCU Info");
2720
2721 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2722 true);
2723 Asm->EOL("Compilation ModuleCU Length");
2724
2725 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2726 for (StringMap<DIE*>::const_iterator
2727 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2728 const char *Name = GI->getKeyData();
2729 DIE * Entity = GI->second;
2730
2731 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2732 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2733 }
2734
2735 Asm->EmitInt32(0); Asm->EOL("End Mark");
2736 EmitLabel("pubtypes_end", ModuleCU->getID());
2737
2738 Asm->EOL();
2739}
2740
Devang Patelc50078e2009-11-21 02:48:08 +00002741/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002742///
Devang Patelc50078e2009-11-21 02:48:08 +00002743void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002744 // Check to see if it is worth the effort.
2745 if (!StringPool.empty()) {
2746 // Start the dwarf str section.
Chris Lattner73266f92009-08-19 05:49:37 +00002747 Asm->OutStreamer.SwitchSection(
2748 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002749
2750 // For each of strings in the string pool.
2751 for (unsigned StringID = 1, N = StringPool.size();
2752 StringID <= N; ++StringID) {
2753 // Emit a label for reference from debug information entries.
2754 EmitLabel("string", StringID);
2755
2756 // Emit the string itself.
2757 const std::string &String = StringPool[StringID];
2758 Asm->EmitString(String); Asm->EOL();
2759 }
2760
2761 Asm->EOL();
2762 }
2763}
2764
Devang Patelc50078e2009-11-21 02:48:08 +00002765/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002766///
Devang Patelc50078e2009-11-21 02:48:08 +00002767void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002768 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00002769 Asm->OutStreamer.SwitchSection(
2770 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002771 Asm->EOL();
2772}
2773
2774/// EmitDebugARanges - Emit visible names into a debug aranges section.
2775///
2776void DwarfDebug::EmitDebugARanges() {
2777 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002778 Asm->OutStreamer.SwitchSection(
2779 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002780
2781 // FIXME - Mock up
2782#if 0
2783 CompileUnit *Unit = GetBaseCompileUnit();
2784
2785 // Don't include size of length
2786 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2787
2788 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2789
2790 EmitReference("info_begin", Unit->getID());
2791 Asm->EOL("Offset of Compilation Unit Info");
2792
2793 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2794
2795 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2796
2797 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2798 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2799
2800 // Range 1
2801 EmitReference("text_begin", 0); Asm->EOL("Address");
2802 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2803
2804 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2805 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2806#endif
2807
2808 Asm->EOL();
2809}
2810
Devang Patelc50078e2009-11-21 02:48:08 +00002811/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002812///
Devang Patelc50078e2009-11-21 02:48:08 +00002813void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002814 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002815 Asm->OutStreamer.SwitchSection(
2816 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002817 Asm->EOL();
2818}
2819
Devang Patelc50078e2009-11-21 02:48:08 +00002820/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002821///
Devang Patelc50078e2009-11-21 02:48:08 +00002822void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00002823 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00002824 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002825 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00002826 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002827 Asm->EOL();
2828 }
2829}
2830
Devang Patelc50078e2009-11-21 02:48:08 +00002831/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00002832/// Section Header:
2833/// 1. length of section
2834/// 2. Dwarf version number
2835/// 3. address size.
2836///
2837/// Entries (one "entry" for each function that was inlined):
2838///
2839/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2840/// otherwise offset into __debug_str for regular function name.
2841/// 2. offset into __debug_str section for regular function name.
2842/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2843/// instances for the function.
2844///
2845/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2846/// inlined instance; the die_offset points to the inlined_subroutine die in the
2847/// __debug_info section, and the low_pc is the starting address for the
2848/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00002849void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002850 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002851 return;
2852
Devang Patel5a3d37f2009-06-29 20:45:18 +00002853 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00002854 return;
2855
Chris Lattner73266f92009-08-19 05:49:37 +00002856 Asm->OutStreamer.SwitchSection(
2857 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002858 Asm->EOL();
2859 EmitDifference("debug_inlined_end", 1,
2860 "debug_inlined_begin", 1, true);
2861 Asm->EOL("Length of Debug Inlined Information Entry");
2862
2863 EmitLabel("debug_inlined_begin", 1);
2864
2865 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2866 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2867
Devang Patel90a0fe32009-11-10 23:06:00 +00002868 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2869 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00002870
Devang Patel90a0fe32009-11-10 23:06:00 +00002871// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2872 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2873 MDNode *Node = *I;
Jim Grosbachb23f2422009-11-22 19:20:36 +00002874 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
2875 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00002876 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00002877 DISubprogram SP(Node);
Devang Patel7f75bbe2009-11-25 17:36:49 +00002878 StringRef LName = SP.getLinkageName();
2879 StringRef Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00002880
Devang Patel7f75bbe2009-11-25 17:36:49 +00002881 if (LName.empty())
Devang Patel76031e82009-07-16 01:01:22 +00002882 Asm->EmitString(Name);
2883 else {
Chris Lattner73266f92009-08-19 05:49:37 +00002884 // Skip special LLVM prefix that is used to inform the asm printer to not
2885 // emit usual symbol prefix before the symbol name. This happens for
2886 // Objective-C symbol names and symbol whose name is replaced using GCC's
2887 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00002888 if (LName[0] == 1)
Benjamin Kramer62b81882009-11-25 18:26:09 +00002889 LName = LName.substr(1);
Devang Patel90a0fe32009-11-10 23:06:00 +00002890// Asm->EmitString(LName);
2891 EmitSectionOffset("string", "section_str",
2892 StringPool.idFor(LName), false, true);
2893
Devang Patel76031e82009-07-16 01:01:22 +00002894 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002895 Asm->EOL("MIPS linkage name");
Jim Grosbach652b7432009-11-21 23:12:12 +00002896// Asm->EmitString(Name);
Devang Patel90a0fe32009-11-10 23:06:00 +00002897 EmitSectionOffset("string", "section_str",
2898 StringPool.idFor(Name), false, true);
2899 Asm->EOL("Function name");
Bill Wendling55fccda2009-05-20 23:21:38 +00002900 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2901
Devang Patel90a0fe32009-11-10 23:06:00 +00002902 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00002903 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002904 DIE *SP = LI->second;
Bill Wendling55fccda2009-05-20 23:21:38 +00002905 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2906
2907 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002908 O << MAI->getData32bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00002909 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002910 O << MAI->getData64bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00002911
Devang Patel90a0fe32009-11-10 23:06:00 +00002912 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling55fccda2009-05-20 23:21:38 +00002913 }
2914 }
2915
2916 EmitLabel("debug_inlined_end", 1);
2917 Asm->EOL();
2918}