blob: 4b678a23c4d3a96355b2b9a0e39fb9bbff16c396 [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 Patelc50078e2009-11-21 02:48:08 +0000333/// addString - Add a string attribute data and value.
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000334///
Devang Patelc50078e2009-11-21 02:48:08 +0000335void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000336 const std::string &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 Patelc50078e2009-11-21 02:48:08 +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 Patelc50078e2009-11-21 02:48:08 +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 Patelc50078e2009-11-21 02:48:08 +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 Patelc50078e2009-11-21 02:48:08 +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 Patelaaf012e2009-09-29 18:40:58 +0000497 if (strcmp(Name.c_str(), DT.getName()) == 0)
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 Patelaaf012e2009-09-29 18:40:58 +0000623 const char *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 Patelaaf012e2009-09-29 18:40:58 +0000643 const char *fieldName = DT.getName();
644 if (strcmp(fieldName, "__forwarding") == 0)
Daniel Dunbar19f1d442009-09-19 20:40:14 +0000645 forwardingField = Element;
Devang Patelaaf012e2009-09-29 18:40:58 +0000646 else if (strcmp(fieldName, varName) == 0)
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 Patelc50078e2009-11-21 02:48:08 +0000741/// addType - Add a new type attribute to the specified entity.
742void DwarfDebug::addType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000743 if (Ty.isNull())
744 return;
745
746 // Check for pre-existence.
Devang Patelc50078e2009-11-21 02:48:08 +0000747 DIEEntry *Entry = DW_Unit->getDIEEntry(Ty.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000748
749 // If it exists then use the existing value.
Devang Patelc50078e2009-11-21 02:48:08 +0000750 if (Entry) {
751 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000752 return;
753 }
754
755 // Set up proxy.
Devang Patelc50078e2009-11-21 02:48:08 +0000756 Entry = createDIEEntry();
757 DW_Unit->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000758
759 // Construct type.
Devang Patel1233f912009-11-21 00:31:03 +0000760 DIE *Buffer = new DIE(dwarf::DW_TAG_base_type);
Devang Patel56843af2009-08-31 18:49:10 +0000761 if (Ty.isBasicType())
Devang Patelc50078e2009-11-21 02:48:08 +0000762 constructTypeDIE(DW_Unit, *Buffer, DIBasicType(Ty.getNode()));
Devang Patel56843af2009-08-31 18:49:10 +0000763 else if (Ty.isCompositeType())
Devang Patelc50078e2009-11-21 02:48:08 +0000764 constructTypeDIE(DW_Unit, *Buffer, DICompositeType(Ty.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000765 else {
Devang Patel56843af2009-08-31 18:49:10 +0000766 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patelc50078e2009-11-21 02:48:08 +0000767 constructTypeDIE(DW_Unit, *Buffer, DIDerivedType(Ty.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000768 }
769
770 // Add debug information entry to entity and appropriate context.
771 DIE *Die = NULL;
772 DIDescriptor Context = Ty.getContext();
773 if (!Context.isNull())
Devang Pateld90672c2009-11-20 21:37:22 +0000774 Die = DW_Unit->getDIE(Context.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000775
Jim Grosbach652b7432009-11-21 23:12:12 +0000776 if (Die)
Devang Patelc50078e2009-11-21 02:48:08 +0000777 Die->addChild(Buffer);
Jim Grosbach652b7432009-11-21 23:12:12 +0000778 else
Devang Patelc50078e2009-11-21 02:48:08 +0000779 DW_Unit->addDie(Buffer);
780 Entry->setEntry(Buffer);
781 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000782}
783
Devang Patelc50078e2009-11-21 02:48:08 +0000784/// constructTypeDIE - Construct basic type die from DIBasicType.
785void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000786 DIBasicType BTy) {
787 // Get core information.
Devang Patelaaf012e2009-09-29 18:40:58 +0000788 const char *Name = BTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000789 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000790 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000791 BTy.getEncoding());
792
793 // Add name if not anonymous or intermediate type.
Devang Patelaaf012e2009-09-29 18:40:58 +0000794 if (Name)
Devang Patelc50078e2009-11-21 02:48:08 +0000795 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000796 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patelc50078e2009-11-21 02:48:08 +0000797 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000798}
799
Devang Patelc50078e2009-11-21 02:48:08 +0000800/// constructTypeDIE - Construct derived type die from DIDerivedType.
801void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000802 DIDerivedType DTy) {
803 // Get core information.
Devang Patelaaf012e2009-09-29 18:40:58 +0000804 const char *Name = DTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000805 uint64_t Size = DTy.getSizeInBits() >> 3;
806 unsigned Tag = DTy.getTag();
807
808 // FIXME - Workaround for templates.
809 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
810
811 Buffer.setTag(Tag);
812
813 // Map to main type, void will not have a type.
814 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patelc50078e2009-11-21 02:48:08 +0000815 addType(DW_Unit, &Buffer, FromTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000816
817 // Add name if not anonymous or intermediate type.
Devang Patel18a7d1c2009-10-16 21:27:43 +0000818 if (Name && Tag != dwarf::DW_TAG_pointer_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000819 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000820
821 // Add size if non-zero (derived types might be zero-sized.)
822 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000823 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000824
825 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb125c6e2009-11-23 18:43:37 +0000826 if (!DTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000827 addSourceLine(&Buffer, &DTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000828}
829
Devang Patelc50078e2009-11-21 02:48:08 +0000830/// constructTypeDIE - Construct type DIE from DICompositeType.
831void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000832 DICompositeType CTy) {
833 // Get core information.
Devang Patelaaf012e2009-09-29 18:40:58 +0000834 const char *Name = CTy.getName();
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000835
836 uint64_t Size = CTy.getSizeInBits() >> 3;
837 unsigned Tag = CTy.getTag();
838 Buffer.setTag(Tag);
839
840 switch (Tag) {
841 case dwarf::DW_TAG_vector_type:
842 case dwarf::DW_TAG_array_type:
Devang Patelc50078e2009-11-21 02:48:08 +0000843 constructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000844 break;
845 case dwarf::DW_TAG_enumeration_type: {
846 DIArray Elements = CTy.getTypeArray();
847
848 // Add enumerators to enumeration type.
849 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
850 DIE *ElemDie = NULL;
Devang Patel15e723d2009-08-28 23:24:31 +0000851 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelfb812752009-10-09 17:51:49 +0000852 if (!Enum.isNull()) {
Devang Patelc50078e2009-11-21 02:48:08 +0000853 ElemDie = constructEnumTypeDIE(DW_Unit, &Enum);
854 Buffer.addChild(ElemDie);
Devang Patelfb812752009-10-09 17:51:49 +0000855 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000856 }
857 }
858 break;
859 case dwarf::DW_TAG_subroutine_type: {
860 // Add return type.
861 DIArray Elements = CTy.getTypeArray();
862 DIDescriptor RTy = Elements.getElement(0);
Devang Patelc50078e2009-11-21 02:48:08 +0000863 addType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000864
865 // Add prototype flag.
Devang Patelc50078e2009-11-21 02:48:08 +0000866 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000867
868 // Add arguments.
869 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
870 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
871 DIDescriptor Ty = Elements.getElement(i);
Devang Patelc50078e2009-11-21 02:48:08 +0000872 addType(DW_Unit, Arg, DIType(Ty.getNode()));
873 Buffer.addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000874 }
875 }
876 break;
877 case dwarf::DW_TAG_structure_type:
878 case dwarf::DW_TAG_union_type:
879 case dwarf::DW_TAG_class_type: {
880 // Add elements to structure type.
881 DIArray Elements = CTy.getTypeArray();
882
883 // A forward struct declared type may not have elements available.
884 if (Elements.isNull())
885 break;
886
887 // Add elements to structure type.
888 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
889 DIDescriptor Element = Elements.getElement(i);
Devang Patel15e723d2009-08-28 23:24:31 +0000890 if (Element.isNull())
891 continue;
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000892 DIE *ElemDie = NULL;
893 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patelc50078e2009-11-21 02:48:08 +0000894 ElemDie = createSubprogramDIE(DW_Unit,
Devang Patel15e723d2009-08-28 23:24:31 +0000895 DISubprogram(Element.getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000896 else
Devang Patelc50078e2009-11-21 02:48:08 +0000897 ElemDie = createMemberDIE(DW_Unit,
Devang Patel15e723d2009-08-28 23:24:31 +0000898 DIDerivedType(Element.getNode()));
Devang Patelc50078e2009-11-21 02:48:08 +0000899 Buffer.addChild(ElemDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000900 }
901
Devang Patel20b32102009-08-27 23:51:51 +0000902 if (CTy.isAppleBlockExtension())
Devang Patelc50078e2009-11-21 02:48:08 +0000903 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000904
905 unsigned RLang = CTy.getRunTimeLang();
906 if (RLang)
Devang Patelc50078e2009-11-21 02:48:08 +0000907 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000908 dwarf::DW_FORM_data1, RLang);
909 break;
910 }
911 default:
912 break;
913 }
914
915 // Add name if not anonymous or intermediate type.
Devang Patelaaf012e2009-09-29 18:40:58 +0000916 if (Name)
Devang Patelc50078e2009-11-21 02:48:08 +0000917 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000918
919 if (Tag == dwarf::DW_TAG_enumeration_type ||
920 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
921 // Add size if non-zero (derived types might be zero-sized.)
922 if (Size)
Devang Patelc50078e2009-11-21 02:48:08 +0000923 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000924 else {
925 // Add zero size if it is not a forward declaration.
926 if (CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000927 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000928 else
Devang Patelc50078e2009-11-21 02:48:08 +0000929 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000930 }
931
932 // Add source line info if available.
933 if (!CTy.isForwardDecl())
Devang Patelc50078e2009-11-21 02:48:08 +0000934 addSourceLine(&Buffer, &CTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000935 }
936}
937
Devang Patelc50078e2009-11-21 02:48:08 +0000938/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
939void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000940 int64_t L = SR.getLo();
941 int64_t H = SR.getHi();
942 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
943
Devang Patelc50078e2009-11-21 02:48:08 +0000944 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patele7ff5092009-08-14 20:59:16 +0000945 if (L)
Devang Patelc50078e2009-11-21 02:48:08 +0000946 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Patele7ff5092009-08-14 20:59:16 +0000947 if (H)
Devang Patelc50078e2009-11-21 02:48:08 +0000948 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000949
Devang Patelc50078e2009-11-21 02:48:08 +0000950 Buffer.addChild(DW_Subrange);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000951}
952
Devang Patelc50078e2009-11-21 02:48:08 +0000953/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
954void DwarfDebug::constructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000955 DICompositeType *CTy) {
956 Buffer.setTag(dwarf::DW_TAG_array_type);
957 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000958 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000959
960 // Emit derived type.
Devang Patelc50078e2009-11-21 02:48:08 +0000961 addType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000962 DIArray Elements = CTy->getTypeArray();
963
Devang Patel1233f912009-11-21 00:31:03 +0000964 // Get an anonymous type for index type.
965 DIE *IdxTy = DW_Unit->getIndexTyDie();
966 if (!IdxTy) {
967 // Construct an anonymous type for index type.
968 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Devang Patelc50078e2009-11-21 02:48:08 +0000969 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
970 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel1233f912009-11-21 00:31:03 +0000971 dwarf::DW_ATE_signed);
Devang Patelc50078e2009-11-21 02:48:08 +0000972 DW_Unit->addDie(IdxTy);
Devang Patel1233f912009-11-21 00:31:03 +0000973 DW_Unit->setIndexTyDie(IdxTy);
974 }
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000975
976 // Add subranges to array type.
977 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
978 DIDescriptor Element = Elements.getElement(i);
979 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
Devang Patelc50078e2009-11-21 02:48:08 +0000980 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000981 }
982}
983
Devang Patelc50078e2009-11-21 02:48:08 +0000984/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
985DIE *DwarfDebug::constructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000986 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patelaaf012e2009-09-29 18:40:58 +0000987 const char *Name = ETy->getName();
Devang Patelc50078e2009-11-21 02:48:08 +0000988 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000989 int64_t Value = ETy->getEnumValue();
Devang Patelc50078e2009-11-21 02:48:08 +0000990 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000991 return Enumerator;
992}
993
Devang Patelc50078e2009-11-21 02:48:08 +0000994/// createGlobalVariableDIE - Create new DIE using GV.
995DIE *DwarfDebug::createGlobalVariableDIE(CompileUnit *DW_Unit,
Bill Wendlingb12b3d72009-05-15 09:23:25 +0000996 const DIGlobalVariable &GV) {
Jim Grosbachb23f2422009-11-22 19:20:36 +0000997 // If the global variable was optmized out then no need to create debug info
998 // entry.
Devang Patel83e42c72009-11-06 17:58:12 +0000999 if (!GV.getGlobal()) return NULL;
1000 if (!GV.getDisplayName()) return NULL;
Devang Patelfabc47c2009-11-06 01:30:04 +00001001
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001002 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach652b7432009-11-21 23:12:12 +00001003 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001004 GV.getDisplayName());
1005
1006 const char *LinkageName = GV.getLinkageName();
1007 if (LinkageName) {
Chris Lattner73266f92009-08-19 05:49:37 +00001008 // Skip special LLVM prefix that is used to inform the asm printer to not
1009 // emit usual symbol prefix before the symbol name. This happens for
1010 // Objective-C symbol names and symbol whose name is replaced using GCC's
1011 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00001012 if (LinkageName[0] == 1)
1013 LinkageName = &LinkageName[1];
Devang Patelc50078e2009-11-21 02:48:08 +00001014 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelbd760a52009-07-14 00:55:28 +00001015 LinkageName);
Devang Patel76031e82009-07-16 01:01:22 +00001016 }
Devang Patelc50078e2009-11-21 02:48:08 +00001017 addType(DW_Unit, GVDie, GV.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001018 if (!GV.isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001019 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1020 addSourceLine(GVDie, &GV);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001021
1022 // Add address.
1023 DIEBlock *Block = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001024 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1025 addObjectLabel(Block, 0, dwarf::DW_FORM_udata,
Devang Patel6bd5cc82009-10-05 23:22:08 +00001026 Asm->Mang->getMangledName(GV.getGlobal()));
Devang Patelc50078e2009-11-21 02:48:08 +00001027 addBlock(GVDie, dwarf::DW_AT_location, 0, Block);
Devang Patel6bd5cc82009-10-05 23:22:08 +00001028
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001029 return GVDie;
1030}
1031
Devang Patelc50078e2009-11-21 02:48:08 +00001032/// createMemberDIE - Create new member DIE.
1033DIE *DwarfDebug::createMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001034 DIE *MemberDie = new DIE(DT.getTag());
Devang Patelaaf012e2009-09-29 18:40:58 +00001035 if (const char *Name = DT.getName())
Devang Patelc50078e2009-11-21 02:48:08 +00001036 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001037
Devang Patelc50078e2009-11-21 02:48:08 +00001038 addType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001039
Devang Patelc50078e2009-11-21 02:48:08 +00001040 addSourceLine(MemberDie, &DT);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001041
Devang Patel7d9fe582009-11-04 22:06:12 +00001042 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patelc50078e2009-11-21 02:48:08 +00001043 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel7d9fe582009-11-04 22:06:12 +00001044
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001045 uint64_t Size = DT.getSizeInBits();
Devang Patel71842a92009-11-04 23:48:00 +00001046 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001047
1048 if (Size != FieldSize) {
1049 // Handle bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001050 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1051 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001052
1053 uint64_t Offset = DT.getOffsetInBits();
1054 uint64_t FieldOffset = Offset;
1055 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1056 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1057 FieldOffset = (HiMark - FieldSize);
1058 Offset -= FieldOffset;
1059
1060 // Maybe we need to work from the other end.
1061 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patelc50078e2009-11-21 02:48:08 +00001062 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001063
Devang Patel7d9fe582009-11-04 22:06:12 +00001064 // Here WD_AT_data_member_location points to the anonymous
1065 // field that includes this bit field.
Devang Patelc50078e2009-11-21 02:48:08 +00001066 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001067
1068 } else
1069 // This is not a bitfield.
Devang Patelc50078e2009-11-21 02:48:08 +00001070 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel7d9fe582009-11-04 22:06:12 +00001071
Devang Patelc50078e2009-11-21 02:48:08 +00001072 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001073
1074 if (DT.isProtected())
Devang Patelc50078e2009-11-21 02:48:08 +00001075 addUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001076 dwarf::DW_ACCESS_protected);
1077 else if (DT.isPrivate())
Devang Patelc50078e2009-11-21 02:48:08 +00001078 addUInt(MemberDie, dwarf::DW_AT_accessibility, 0,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001079 dwarf::DW_ACCESS_private);
1080
1081 return MemberDie;
1082}
1083
Devang Patelc50078e2009-11-21 02:48:08 +00001084/// createSubprogramDIE - Create new DIE using SP.
1085DIE *DwarfDebug::createSubprogramDIE(CompileUnit *DW_Unit,
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001086 const DISubprogram &SP,
Bill Wendling87307862009-05-18 22:02:36 +00001087 bool IsConstructor,
1088 bool IsInlined) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001089 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
1090
Devang Patelaaf012e2009-09-29 18:40:58 +00001091 const char * Name = SP.getName();
Devang Patelc50078e2009-11-21 02:48:08 +00001092 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001093
Devang Patelaaf012e2009-09-29 18:40:58 +00001094 const char *LinkageName = SP.getLinkageName();
1095 if (LinkageName) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001096 // Skip special LLVM prefix that is used to inform the asm printer to not
1097 // emit usual symbol prefix before the symbol name. This happens for
1098 // Objective-C symbol names and symbol whose name is replaced using GCC's
1099 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00001100 if (LinkageName[0] == 1)
1101 LinkageName = &LinkageName[1];
Devang Patelc50078e2009-11-21 02:48:08 +00001102 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelbd760a52009-07-14 00:55:28 +00001103 LinkageName);
Devang Patel76031e82009-07-16 01:01:22 +00001104 }
Devang Patelc50078e2009-11-21 02:48:08 +00001105 addSourceLine(SPDie, &SP);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001106
1107 DICompositeType SPTy = SP.getType();
1108 DIArray Args = SPTy.getTypeArray();
1109
1110 // Add prototyped tag, if C or ObjC.
1111 unsigned Lang = SP.getCompileUnit().getLanguage();
1112 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1113 Lang == dwarf::DW_LANG_ObjC)
Devang Patelc50078e2009-11-21 02:48:08 +00001114 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001115
1116 // Add Return Type.
1117 unsigned SPTag = SPTy.getTag();
1118 if (!IsConstructor) {
1119 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
Devang Patelc50078e2009-11-21 02:48:08 +00001120 addType(DW_Unit, SPDie, SPTy);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001121 else
Devang Patelc50078e2009-11-21 02:48:08 +00001122 addType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001123 }
1124
1125 if (!SP.isDefinition()) {
Devang Patelc50078e2009-11-21 02:48:08 +00001126 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001127
1128 // Add arguments. Do not add arguments for subprogram definition. They will
1129 // be handled through RecordVariable.
1130 if (SPTag == dwarf::DW_TAG_subroutine_type)
1131 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1132 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patelc50078e2009-11-21 02:48:08 +00001133 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
1134 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1135 SPDie->addChild(Arg);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001136 }
1137 }
1138
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001139 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Pateld90672c2009-11-20 21:37:22 +00001140 DW_Unit->insertDIE(SP.getNode(), SPDie);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001141 return SPDie;
1142}
1143
Devang Patelc50078e2009-11-21 02:48:08 +00001144/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001145///
Devang Patelc50078e2009-11-21 02:48:08 +00001146CompileUnit &DwarfDebug::findCompileUnit(DICompileUnit Unit) const {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001147 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patel15e723d2009-08-28 23:24:31 +00001148 CompileUnitMap.find(Unit.getNode());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001149 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1150 return *I->second;
1151}
1152
Devang Patelc50078e2009-11-21 02:48:08 +00001153/// createDbgScopeVariable - Create a new scope variable.
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001154///
Devang Patelc50078e2009-11-21 02:48:08 +00001155DIE *DwarfDebug::createDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001156 // Get the descriptor.
1157 const DIVariable &VD = DV->getVariable();
Devang Patel11ac7e72009-11-03 18:30:27 +00001158 const char *Name = VD.getName();
1159 if (!Name)
1160 return NULL;
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001161
1162 // Translate tag to proper Dwarf tag. The result variable is dropped for
1163 // now.
1164 unsigned Tag;
1165 switch (VD.getTag()) {
1166 case dwarf::DW_TAG_return_variable:
1167 return NULL;
1168 case dwarf::DW_TAG_arg_variable:
1169 Tag = dwarf::DW_TAG_formal_parameter;
1170 break;
1171 case dwarf::DW_TAG_auto_variable: // fall thru
1172 default:
1173 Tag = dwarf::DW_TAG_variable;
1174 break;
1175 }
1176
1177 // Define variable debug information entry.
1178 DIE *VariableDie = new DIE(Tag);
Devang Patelc50078e2009-11-21 02:48:08 +00001179 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001180
1181 // Add source line info if available.
Devang Patelc50078e2009-11-21 02:48:08 +00001182 addSourceLine(VariableDie, &VD);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001183
1184 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001185 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001186 // addresses instead.
Caroline Tice248d5572009-08-31 21:19:37 +00001187 if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001188 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Caroline Tice248d5572009-08-31 21:19:37 +00001189 else
Devang Patelc50078e2009-11-21 02:48:08 +00001190 addType(Unit, VariableDie, VD.getType());
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001191
1192 // Add variable address.
Devang Patel90a0fe32009-11-10 23:06:00 +00001193 // Variables for abstract instances of inlined functions don't get a
1194 // location.
1195 MachineLocation Location;
Jim Grosbach587d4882009-11-22 20:14:00 +00001196 unsigned FrameReg;
1197 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1198 Location.set(FrameReg, Offset);
Jim Grosbach652b7432009-11-21 23:12:12 +00001199
1200
Devang Patel90a0fe32009-11-10 23:06:00 +00001201 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001202 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001203 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001204 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001205 else
Devang Patelc50078e2009-11-21 02:48:08 +00001206 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendlingb12b3d72009-05-15 09:23:25 +00001207
1208 return VariableDie;
1209}
1210
Devang Patel90a0fe32009-11-10 23:06:00 +00001211/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1212/// Initialize scope and update scope hierarchy.
1213DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1214 MDNode *InlinedAt) {
1215 assert (N && "Invalid Scope encoding!");
1216 assert (MI && "Missing machine instruction!");
1217 bool GetConcreteScope = (MI && InlinedAt);
1218
1219 DbgScope *NScope = NULL;
1220
1221 if (InlinedAt)
1222 NScope = DbgScopeMap.lookup(InlinedAt);
1223 else
1224 NScope = DbgScopeMap.lookup(N);
1225 assert (NScope && "Unable to find working scope!");
1226
1227 if (NScope->getFirstInsn())
1228 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001229
1230 DbgScope *Parent = NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001231 if (GetConcreteScope) {
Devang Pateldd7bb432009-10-14 21:08:09 +00001232 DILocation IL(InlinedAt);
Jim Grosbach652b7432009-11-21 23:12:12 +00001233 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patel90a0fe32009-11-10 23:06:00 +00001234 IL.getOrigLocation().getNode());
1235 assert (Parent && "Unable to find Parent scope!");
1236 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001237 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001238 } else if (DIDescriptor(N).isLexicalBlock()) {
1239 DILexicalBlock DB(N);
1240 if (!DB.getContext().isNull()) {
1241 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1242 NScope->setParent(Parent);
Devang Patelc50078e2009-11-21 02:48:08 +00001243 Parent->addScope(NScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001244 }
Devang Pateldd7bb432009-10-14 21:08:09 +00001245 }
Devang Patel6a260102009-10-01 20:31:14 +00001246
Devang Patelf5278f22009-10-27 20:47:17 +00001247 NScope->setFirstInsn(MI);
Devang Patel6a260102009-10-01 20:31:14 +00001248
Devang Patel90a0fe32009-11-10 23:06:00 +00001249 if (!Parent && !InlinedAt) {
Devang Patelce8986f2009-11-11 00:31:36 +00001250 StringRef SPName = DISubprogram(N).getLinkageName();
1251 if (SPName == MF->getFunction()->getName())
1252 CurrentFnDbgScope = NScope;
Devang Patel90a0fe32009-11-10 23:06:00 +00001253 }
Devang Patel6a260102009-10-01 20:31:14 +00001254
Devang Patel90a0fe32009-11-10 23:06:00 +00001255 if (GetConcreteScope) {
1256 ConcreteScopes[InlinedAt] = NScope;
1257 getOrCreateAbstractScope(N);
1258 }
1259
Devang Patelf5278f22009-10-27 20:47:17 +00001260 return NScope;
Devang Patel6a260102009-10-01 20:31:14 +00001261}
1262
Devang Patel90a0fe32009-11-10 23:06:00 +00001263DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1264 assert (N && "Invalid Scope encoding!");
1265
1266 DbgScope *AScope = AbstractScopes.lookup(N);
1267 if (AScope)
1268 return AScope;
Jim Grosbach652b7432009-11-21 23:12:12 +00001269
Devang Patel90a0fe32009-11-10 23:06:00 +00001270 DbgScope *Parent = NULL;
1271
1272 DIDescriptor Scope(N);
1273 if (Scope.isLexicalBlock()) {
1274 DILexicalBlock DB(N);
1275 DIDescriptor ParentDesc = DB.getContext();
1276 if (!ParentDesc.isNull())
1277 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1278 }
1279
1280 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1281
1282 if (Parent)
Devang Patelc50078e2009-11-21 02:48:08 +00001283 Parent->addScope(AScope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001284 AScope->setAbstractScope();
1285 AbstractScopes[N] = AScope;
1286 if (DIDescriptor(N).isSubprogram())
1287 AbstractScopesList.push_back(AScope);
1288 return AScope;
1289}
Devang Patel6a260102009-10-01 20:31:14 +00001290
Jim Grosbach652b7432009-11-21 23:12:12 +00001291/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patelc50078e2009-11-21 02:48:08 +00001292/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1293/// If there are global variables in this scope then create and insert
1294/// DIEs for these variables.
1295DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001296
Devang Pateld90672c2009-11-20 21:37:22 +00001297 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patel90a0fe32009-11-10 23:06:00 +00001298 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001299 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001300 DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001301 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001302 DWLabel("func_end", SubprogramCount));
1303 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patelc50078e2009-11-21 02:48:08 +00001304 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach652b7432009-11-21 23:12:12 +00001305
Devang Patel90a0fe32009-11-10 23:06:00 +00001306 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patelc50078e2009-11-21 02:48:08 +00001307 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patel90a0fe32009-11-10 23:06:00 +00001308
1309 // If there are global variables at this scope then add their dies.
Jim Grosbach652b7432009-11-21 23:12:12 +00001310 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
Devang Patel90a0fe32009-11-10 23:06:00 +00001311 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1312 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1313 if (!N) continue;
1314 DIGlobalVariable GV(N);
1315 if (GV.getContext().getNode() == SPNode) {
Devang Patelc50078e2009-11-21 02:48:08 +00001316 DIE *ScopedGVDie = createGlobalVariableDIE(ModuleCU, GV);
Devang Patelbad42262009-11-10 23:20:04 +00001317 if (ScopedGVDie)
Devang Patelc50078e2009-11-21 02:48:08 +00001318 SPDie->addChild(ScopedGVDie);
Devang Patel90a0fe32009-11-10 23:06:00 +00001319 }
1320 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001321
Devang Patel90a0fe32009-11-10 23:06:00 +00001322 return SPDie;
1323}
1324
Jim Grosbach652b7432009-11-21 23:12:12 +00001325/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patelc50078e2009-11-21 02:48:08 +00001326/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1327DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001328 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1329 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1330
1331 // Ignore empty scopes.
1332 if (StartID == EndID && StartID != 0)
1333 return NULL;
1334
1335 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1336 if (Scope->isAbstractScope())
1337 return ScopeDIE;
1338
Devang Patelc50078e2009-11-21 02:48:08 +00001339 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach652b7432009-11-21 23:12:12 +00001340 StartID ?
1341 DWLabel("label", StartID)
Devang Patel90a0fe32009-11-10 23:06:00 +00001342 : DWLabel("func_begin", SubprogramCount));
Devang Patelc50078e2009-11-21 02:48:08 +00001343 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach652b7432009-11-21 23:12:12 +00001344 EndID ?
1345 DWLabel("label", EndID)
Devang Patel90a0fe32009-11-10 23:06:00 +00001346 : DWLabel("func_end", SubprogramCount));
1347
1348
1349
1350 return ScopeDIE;
1351}
1352
Devang Patelc50078e2009-11-21 02:48:08 +00001353/// constructInlinedScopeDIE - This scope represents inlined body of
1354/// a function. Construct DIE to represent this concrete inlined copy
1355/// of the function.
1356DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001357 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1358 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1359 assert (StartID && "Invalid starting label for an inlined scope!");
1360 assert (EndID && "Invalid end label for an inlined scope!");
1361 // Ignore empty scopes.
1362 if (StartID == EndID && StartID != 0)
1363 return NULL;
1364
1365 DIScope DS(Scope->getScopeNode());
1366 if (DS.isNull())
1367 return NULL;
1368 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1369
1370 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001371 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001372 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patelc50078e2009-11-21 02:48:08 +00001373 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001374 dwarf::DW_FORM_ref4, OriginDIE);
1375
Devang Patelc50078e2009-11-21 02:48:08 +00001376 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001377 DWLabel("label", StartID));
Devang Patelc50078e2009-11-21 02:48:08 +00001378 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patel90a0fe32009-11-10 23:06:00 +00001379 DWLabel("label", EndID));
1380
1381 InlinedSubprogramDIEs.insert(OriginDIE);
1382
1383 // Track the start label for this inlined function.
1384 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1385 I = InlineInfo.find(InlinedSP.getNode());
1386
1387 if (I == InlineInfo.end()) {
Jim Grosbachb23f2422009-11-22 19:20:36 +00001388 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1389 ScopeDIE));
Devang Patel90a0fe32009-11-10 23:06:00 +00001390 InlinedSPNodes.push_back(InlinedSP.getNode());
1391 } else
1392 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1393
1394 StringPool.insert(InlinedSP.getName());
1395 StringPool.insert(InlinedSP.getLinkageName());
1396 DILocation DL(Scope->getInlinedAt());
Devang Patelc50078e2009-11-21 02:48:08 +00001397 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1398 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel90a0fe32009-11-10 23:06:00 +00001399
1400 return ScopeDIE;
1401}
1402
Devang Patelc50078e2009-11-21 02:48:08 +00001403
1404/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Jim Grosbach652b7432009-11-21 23:12:12 +00001405DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV,
Devang Patel90a0fe32009-11-10 23:06:00 +00001406 DbgScope *Scope, CompileUnit *Unit) {
1407 // Get the descriptor.
1408 const DIVariable &VD = DV->getVariable();
Devang Patelab9a0682009-11-13 02:25:26 +00001409 const char *Name = VD.getName();
1410 if (!Name)
1411 return NULL;
Devang Patel90a0fe32009-11-10 23:06:00 +00001412
1413 // Translate tag to proper Dwarf tag. The result variable is dropped for
1414 // now.
1415 unsigned Tag;
1416 switch (VD.getTag()) {
1417 case dwarf::DW_TAG_return_variable:
1418 return NULL;
1419 case dwarf::DW_TAG_arg_variable:
1420 Tag = dwarf::DW_TAG_formal_parameter;
1421 break;
1422 case dwarf::DW_TAG_auto_variable: // fall thru
1423 default:
1424 Tag = dwarf::DW_TAG_variable;
1425 break;
1426 }
1427
1428 // Define variable debug information entry.
1429 DIE *VariableDie = new DIE(Tag);
1430
1431
1432 DIE *AbsDIE = NULL;
1433 if (DbgVariable *AV = DV->getAbstractVariable())
1434 AbsDIE = AV->getDIE();
Jim Grosbach652b7432009-11-21 23:12:12 +00001435
Devang Patel90a0fe32009-11-10 23:06:00 +00001436 if (AbsDIE) {
1437 DIScope DS(Scope->getScopeNode());
1438 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Pateld90672c2009-11-20 21:37:22 +00001439 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbarc9f2d242009-11-11 03:09:50 +00001440 (void) OriginSPDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001441 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1442 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1443 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patelc50078e2009-11-21 02:48:08 +00001444 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patel90a0fe32009-11-10 23:06:00 +00001445 dwarf::DW_FORM_ref4, AbsDIE);
1446 }
1447 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001448 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1449 addSourceLine(VariableDie, &VD);
Devang Patel90a0fe32009-11-10 23:06:00 +00001450
1451 // Add variable type.
Jim Grosbach652b7432009-11-21 23:12:12 +00001452 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patel90a0fe32009-11-10 23:06:00 +00001453 // addresses instead.
1454 if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001455 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patel90a0fe32009-11-10 23:06:00 +00001456 else
Devang Patelc50078e2009-11-21 02:48:08 +00001457 addType(Unit, VariableDie, VD.getType());
Devang Patel90a0fe32009-11-10 23:06:00 +00001458 }
1459
1460 // Add variable address.
1461 if (!Scope->isAbstractScope()) {
1462 MachineLocation Location;
Jim Grosbach587d4882009-11-22 20:14:00 +00001463 unsigned FrameReg;
1464 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1465 Location.set(FrameReg, Offset);
Jim Grosbach652b7432009-11-21 23:12:12 +00001466
Devang Patel90a0fe32009-11-10 23:06:00 +00001467 if (VD.hasComplexAddress())
Devang Patelc50078e2009-11-21 02:48:08 +00001468 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001469 else if (VD.isBlockByrefVariable())
Devang Patelc50078e2009-11-21 02:48:08 +00001470 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001471 else
Devang Patelc50078e2009-11-21 02:48:08 +00001472 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patel90a0fe32009-11-10 23:06:00 +00001473 }
1474 DV->setDIE(VariableDie);
1475 return VariableDie;
1476
1477}
Devang Patelc50078e2009-11-21 02:48:08 +00001478
Devang Patelec13b4f2009-11-24 01:14:22 +00001479void DwarfDebug::addPubTypes(DISubprogram SP) {
1480 DICompositeType SPTy = SP.getType();
1481 unsigned SPTag = SPTy.getTag();
1482 if (SPTag != dwarf::DW_TAG_subroutine_type)
1483 return;
1484
1485 DIArray Args = SPTy.getTypeArray();
1486 if (Args.isNull())
1487 return;
1488
1489 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1490 DIType ATy(Args.getElement(i).getNode());
1491 if (ATy.isNull())
1492 continue;
1493 DICompositeType CATy = getDICompositeType(ATy);
1494 if (!CATy.isNull() && CATy.getName()) {
1495 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1496 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1497 }
1498 }
1499}
1500
Devang Patelc50078e2009-11-21 02:48:08 +00001501/// constructScopeDIE - Construct a DIE for this scope.
1502DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001503 if (!Scope)
1504 return NULL;
1505 DIScope DS(Scope->getScopeNode());
1506 if (DS.isNull())
1507 return NULL;
1508
1509 DIE *ScopeDIE = NULL;
1510 if (Scope->getInlinedAt())
Devang Patelc50078e2009-11-21 02:48:08 +00001511 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001512 else if (DS.isSubprogram()) {
1513 if (Scope->isAbstractScope())
Devang Pateld90672c2009-11-20 21:37:22 +00001514 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001515 else
Devang Patelc50078e2009-11-21 02:48:08 +00001516 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001517 }
1518 else {
Devang Patelc50078e2009-11-21 02:48:08 +00001519 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patel90a0fe32009-11-10 23:06:00 +00001520 if (!ScopeDIE) return NULL;
1521 }
1522
1523 // Add variables to scope.
1524 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1525 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patelc50078e2009-11-21 02:48:08 +00001526 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope, ModuleCU);
Jim Grosbach652b7432009-11-21 23:12:12 +00001527 if (VariableDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001528 ScopeDIE->addChild(VariableDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001529 }
1530
1531 // Add nested scopes.
1532 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1533 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1534 // Define the Scope debug information entry.
Devang Patelc50078e2009-11-21 02:48:08 +00001535 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach652b7432009-11-21 23:12:12 +00001536 if (NestedDIE)
Devang Patelc50078e2009-11-21 02:48:08 +00001537 ScopeDIE->addChild(NestedDIE);
Devang Patel90a0fe32009-11-10 23:06:00 +00001538 }
Devang Patelec13b4f2009-11-24 01:14:22 +00001539
1540 if (DS.isSubprogram())
1541 addPubTypes(DISubprogram(DS.getNode()));
1542
1543 return ScopeDIE;
Devang Patel90a0fe32009-11-10 23:06:00 +00001544}
1545
Bill Wendlingf5839192009-05-20 23:19:06 +00001546/// GetOrCreateSourceID - Look up the source id with the given directory and
1547/// source file names. If none currently exists, create a new id and insert it
1548/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1549/// maps as well.
Devang Patelaaf012e2009-09-29 18:40:58 +00001550unsigned DwarfDebug::GetOrCreateSourceID(const char *DirName,
1551 const char *FileName) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001552 unsigned DId;
1553 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1554 if (DI != DirectoryIdMap.end()) {
1555 DId = DI->getValue();
1556 } else {
1557 DId = DirectoryNames.size() + 1;
1558 DirectoryIdMap[DirName] = DId;
1559 DirectoryNames.push_back(DirName);
1560 }
1561
1562 unsigned FId;
1563 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1564 if (FI != SourceFileIdMap.end()) {
1565 FId = FI->getValue();
1566 } else {
1567 FId = SourceFileNames.size() + 1;
1568 SourceFileIdMap[FileName] = FId;
1569 SourceFileNames.push_back(FileName);
1570 }
1571
1572 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1573 SourceIdMap.find(std::make_pair(DId, FId));
1574 if (SI != SourceIdMap.end())
1575 return SI->second;
1576
1577 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1578 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1579 SourceIds.push_back(std::make_pair(DId, FId));
1580
1581 return SrcId;
1582}
1583
Devang Patelc50078e2009-11-21 02:48:08 +00001584void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001585 DICompileUnit DIUnit(N);
Devang Patelaaf012e2009-09-29 18:40:58 +00001586 const char *FN = DIUnit.getFilename();
1587 const char *Dir = DIUnit.getDirectory();
1588 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001589
1590 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patelc50078e2009-11-21 02:48:08 +00001591 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendlingf5839192009-05-20 23:19:06 +00001592 DWLabel("section_line", 0), DWLabel("section_line", 0),
1593 false);
Devang Patelc50078e2009-11-21 02:48:08 +00001594 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelaaf012e2009-09-29 18:40:58 +00001595 DIUnit.getProducer());
Devang Patelc50078e2009-11-21 02:48:08 +00001596 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendlingf5839192009-05-20 23:19:06 +00001597 DIUnit.getLanguage());
Devang Patelc50078e2009-11-21 02:48:08 +00001598 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendlingf5839192009-05-20 23:19:06 +00001599
Devang Patelaaf012e2009-09-29 18:40:58 +00001600 if (Dir)
Devang Patelc50078e2009-11-21 02:48:08 +00001601 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendlingf5839192009-05-20 23:19:06 +00001602 if (DIUnit.isOptimized())
Devang Patelc50078e2009-11-21 02:48:08 +00001603 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendlingf5839192009-05-20 23:19:06 +00001604
Devang Patelaaf012e2009-09-29 18:40:58 +00001605 if (const char *Flags = DIUnit.getFlags())
Devang Patelc50078e2009-11-21 02:48:08 +00001606 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendlingf5839192009-05-20 23:19:06 +00001607
1608 unsigned RVer = DIUnit.getRunTimeVersion();
1609 if (RVer)
Devang Patelc50078e2009-11-21 02:48:08 +00001610 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf5839192009-05-20 23:19:06 +00001611 dwarf::DW_FORM_data1, RVer);
1612
1613 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel5a3d37f2009-06-29 20:45:18 +00001614 if (!ModuleCU && DIUnit.isMain()) {
Devang Patelf97a05a2009-06-29 20:38:13 +00001615 // Use first compile unit marked as isMain as the compile unit
1616 // for this module.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001617 ModuleCU = Unit;
Devang Patelf97a05a2009-06-29 20:38:13 +00001618 }
Bill Wendlingf5839192009-05-20 23:19:06 +00001619
Devang Patel15e723d2009-08-28 23:24:31 +00001620 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendlingf5839192009-05-20 23:19:06 +00001621 CompileUnits.push_back(Unit);
1622}
1623
Devang Patelc50078e2009-11-21 02:48:08 +00001624void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001625 DIGlobalVariable DI_GV(N);
Daniel Dunbar41716322009-09-19 20:40:05 +00001626
Devang Patel0c03f062009-09-04 23:59:07 +00001627 // If debug information is malformed then ignore it.
1628 if (DI_GV.Verify() == false)
1629 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001630
1631 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001632 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel166f8432009-06-26 01:49:18 +00001633 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001634
Devang Patelc50078e2009-11-21 02:48:08 +00001635 DIE *VariableDie = createGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendlingf5839192009-05-20 23:19:06 +00001636
Bill Wendlingf5839192009-05-20 23:19:06 +00001637 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001638 ModuleCU->insertDIE(N, VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001639
1640 // Add to context owner.
Devang Patelc50078e2009-11-21 02:48:08 +00001641 ModuleCU->getCUDie()->addChild(VariableDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001642
1643 // Expose as global. FIXME - need to check external flag.
Devang Patelc50078e2009-11-21 02:48:08 +00001644 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001645
1646 DIType GTy = DI_GV.getType();
1647 if (GTy.isCompositeType() && GTy.getName()) {
1648 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1649 assert (Entry && "Missing global type!");
1650 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1651 }
Devang Patel166f8432009-06-26 01:49:18 +00001652 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001653}
1654
Devang Patelc50078e2009-11-21 02:48:08 +00001655void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel15e723d2009-08-28 23:24:31 +00001656 DISubprogram SP(N);
Bill Wendlingf5839192009-05-20 23:19:06 +00001657
1658 // Check for pre-existence.
Devang Pateld90672c2009-11-20 21:37:22 +00001659 if (ModuleCU->getDIE(N))
Devang Patel166f8432009-06-26 01:49:18 +00001660 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001661
1662 if (!SP.isDefinition())
1663 // This is a method declaration which will be handled while constructing
1664 // class type.
Devang Patel166f8432009-06-26 01:49:18 +00001665 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001666
Devang Patelc50078e2009-11-21 02:48:08 +00001667 DIE *SubprogramDie = createSubprogramDIE(ModuleCU, SP);
Bill Wendlingf5839192009-05-20 23:19:06 +00001668
1669 // Add to map.
Devang Pateld90672c2009-11-20 21:37:22 +00001670 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001671
1672 // Add to context owner.
Devang Patelc50078e2009-11-21 02:48:08 +00001673 ModuleCU->getCUDie()->addChild(SubprogramDie);
Bill Wendlingf5839192009-05-20 23:19:06 +00001674
1675 // Expose as global.
Devang Patelc50078e2009-11-21 02:48:08 +00001676 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patelec13b4f2009-11-24 01:14:22 +00001677
Devang Patel166f8432009-06-26 01:49:18 +00001678 return;
Bill Wendlingf5839192009-05-20 23:19:06 +00001679}
1680
Devang Patelc50078e2009-11-21 02:48:08 +00001681/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbar19f1d442009-09-19 20:40:14 +00001682/// content. Create global DIEs and emit initial debug info sections.
1683/// This is inovked by the target AsmPrinter.
Devang Patelc50078e2009-11-21 02:48:08 +00001684void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel59a1d422009-06-25 22:36:02 +00001685 this->M = M;
1686
Bill Wendlingf5839192009-05-20 23:19:06 +00001687 if (TimePassesIsEnabled)
1688 DebugTimer->startTimer();
1689
Devang Patel1b4d6832009-11-11 19:55:08 +00001690 if (!MAI->doesSupportDebugInformation())
1691 return;
1692
Devang Patelfda766d2009-07-30 18:56:46 +00001693 DebugInfoFinder DbgFinder;
1694 DbgFinder.processModule(*M);
Devang Patel166f8432009-06-26 01:49:18 +00001695
Bill Wendlingf5839192009-05-20 23:19:06 +00001696 // Create all the compile unit DIEs.
Devang Patelfda766d2009-07-30 18:56:46 +00001697 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1698 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001699 constructCompileUnit(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001700
1701 if (CompileUnits.empty()) {
1702 if (TimePassesIsEnabled)
1703 DebugTimer->stopTimer();
1704
1705 return;
1706 }
1707
Devang Patelf97a05a2009-06-29 20:38:13 +00001708 // If main compile unit for this module is not seen than randomly
1709 // select first compile unit.
Devang Patel5a3d37f2009-06-29 20:45:18 +00001710 if (!ModuleCU)
1711 ModuleCU = CompileUnits[0];
Devang Patelf97a05a2009-06-29 20:38:13 +00001712
Devang Patel166f8432009-06-26 01:49:18 +00001713 // Create DIEs for each of the externally visible global variables.
Devang Patelfda766d2009-07-30 18:56:46 +00001714 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patel695c8b02009-10-05 23:40:42 +00001715 E = DbgFinder.global_variable_end(); I != E; ++I) {
1716 DIGlobalVariable GV(*I);
1717 if (GV.getContext().getNode() != GV.getCompileUnit().getNode())
1718 ScopedGVs.push_back(*I);
1719 else
Devang Patelc50078e2009-11-21 02:48:08 +00001720 constructGlobalVariableDIE(*I);
Devang Patel695c8b02009-10-05 23:40:42 +00001721 }
Devang Patel166f8432009-06-26 01:49:18 +00001722
Devang Patel90a0fe32009-11-10 23:06:00 +00001723 // Create DIEs for each subprogram.
Devang Patelfda766d2009-07-30 18:56:46 +00001724 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1725 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001726 constructSubprogramDIE(*I);
Devang Patel166f8432009-06-26 01:49:18 +00001727
Bill Wendlingf5839192009-05-20 23:19:06 +00001728 MMI = mmi;
1729 shouldEmit = true;
1730 MMI->setDebugInfoAvailability(true);
1731
1732 // Prime section data.
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001733 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001734
1735 // Print out .file directives to specify files for .loc directives. These are
1736 // printed out early so that they precede any .loc directives.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001737 if (MAI->hasDotLocAndDotFile()) {
Bill Wendlingf5839192009-05-20 23:19:06 +00001738 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1739 // Remember source id starts at 1.
1740 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1741 sys::Path FullPath(getSourceDirectoryName(Id.first));
1742 bool AppendOk =
1743 FullPath.appendComponent(getSourceFileName(Id.second));
1744 assert(AppendOk && "Could not append filename to directory!");
1745 AppendOk = false;
Chris Lattnerb1aa85b2009-08-23 22:45:37 +00001746 Asm->EmitFile(i, FullPath.str());
Bill Wendlingf5839192009-05-20 23:19:06 +00001747 Asm->EOL();
1748 }
1749 }
1750
1751 // Emit initial sections
Devang Patelc50078e2009-11-21 02:48:08 +00001752 emitInitial();
Bill Wendlingf5839192009-05-20 23:19:06 +00001753
1754 if (TimePassesIsEnabled)
1755 DebugTimer->stopTimer();
1756}
1757
Devang Patelc50078e2009-11-21 02:48:08 +00001758/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendlingf5839192009-05-20 23:19:06 +00001759///
Devang Patelc50078e2009-11-21 02:48:08 +00001760void DwarfDebug::endModule() {
Devang Patel95d477e2009-10-06 00:03:14 +00001761 if (!ModuleCU)
Bill Wendlingf5839192009-05-20 23:19:06 +00001762 return;
1763
1764 if (TimePassesIsEnabled)
1765 DebugTimer->startTimer();
1766
Devang Patel90a0fe32009-11-10 23:06:00 +00001767 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1768 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1769 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1770 DIE *ISP = *AI;
Devang Patelc50078e2009-11-21 02:48:08 +00001771 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel90a0fe32009-11-10 23:06:00 +00001772 }
1773
Bill Wendlingf5839192009-05-20 23:19:06 +00001774 // Standard sections final addresses.
Chris Lattner73266f92009-08-19 05:49:37 +00001775 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001776 EmitLabel("text_end", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00001777 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00001778 EmitLabel("data_end", 0);
1779
1780 // End text sections.
1781 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +00001782 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendlingf5839192009-05-20 23:19:06 +00001783 EmitLabel("section_end", i);
1784 }
1785
1786 // Emit common frame information.
Devang Patelc50078e2009-11-21 02:48:08 +00001787 emitCommonDebugFrame();
Bill Wendlingf5839192009-05-20 23:19:06 +00001788
1789 // Emit function debug frame information
1790 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1791 E = DebugFrames.end(); I != E; ++I)
Devang Patelc50078e2009-11-21 02:48:08 +00001792 emitFunctionDebugFrame(*I);
Bill Wendlingf5839192009-05-20 23:19:06 +00001793
1794 // Compute DIE offsets and sizes.
Devang Patelc50078e2009-11-21 02:48:08 +00001795 computeSizeAndOffsets();
Bill Wendlingf5839192009-05-20 23:19:06 +00001796
1797 // Emit all the DIEs into a debug info section
Devang Patelc50078e2009-11-21 02:48:08 +00001798 emitDebugInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001799
1800 // Corresponding abbreviations into a abbrev section.
Devang Patelc50078e2009-11-21 02:48:08 +00001801 emitAbbreviations();
Bill Wendlingf5839192009-05-20 23:19:06 +00001802
1803 // Emit source line correspondence into a debug line section.
Devang Patelc50078e2009-11-21 02:48:08 +00001804 emitDebugLines();
Bill Wendlingf5839192009-05-20 23:19:06 +00001805
1806 // Emit info into a debug pubnames section.
Devang Patelc50078e2009-11-21 02:48:08 +00001807 emitDebugPubNames();
Bill Wendlingf5839192009-05-20 23:19:06 +00001808
Devang Patelec13b4f2009-11-24 01:14:22 +00001809 // Emit info into a debug pubtypes section.
1810 emitDebugPubTypes();
1811
Bill Wendlingf5839192009-05-20 23:19:06 +00001812 // Emit info into a debug str section.
Devang Patelc50078e2009-11-21 02:48:08 +00001813 emitDebugStr();
Bill Wendlingf5839192009-05-20 23:19:06 +00001814
1815 // Emit info into a debug loc section.
Devang Patelc50078e2009-11-21 02:48:08 +00001816 emitDebugLoc();
Bill Wendlingf5839192009-05-20 23:19:06 +00001817
1818 // Emit info into a debug aranges section.
1819 EmitDebugARanges();
1820
1821 // Emit info into a debug ranges section.
Devang Patelc50078e2009-11-21 02:48:08 +00001822 emitDebugRanges();
Bill Wendlingf5839192009-05-20 23:19:06 +00001823
1824 // Emit info into a debug macinfo section.
Devang Patelc50078e2009-11-21 02:48:08 +00001825 emitDebugMacInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001826
1827 // Emit inline info.
Devang Patelc50078e2009-11-21 02:48:08 +00001828 emitDebugInlineInfo();
Bill Wendlingf5839192009-05-20 23:19:06 +00001829
1830 if (TimePassesIsEnabled)
1831 DebugTimer->stopTimer();
1832}
1833
Devang Patel90a0fe32009-11-10 23:06:00 +00001834/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbachb23f2422009-11-22 19:20:36 +00001835DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1836 unsigned FrameIdx,
Devang Patel90a0fe32009-11-10 23:06:00 +00001837 DILocation &ScopeLoc) {
1838
1839 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1840 if (AbsDbgVariable)
1841 return AbsDbgVariable;
1842
1843 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1844 if (!Scope)
1845 return NULL;
1846
1847 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patelc50078e2009-11-21 02:48:08 +00001848 Scope->addVariable(AbsDbgVariable);
Devang Patel90a0fe32009-11-10 23:06:00 +00001849 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1850 return AbsDbgVariable;
1851}
1852
Devang Patelc50078e2009-11-21 02:48:08 +00001853/// collectVariableInfo - Populate DbgScope entries with variables' info.
1854void DwarfDebug::collectVariableInfo() {
Devang Patel40c80212009-10-09 22:42:28 +00001855 if (!MMI) return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001856
Devang Patel84139992009-10-06 01:26:37 +00001857 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1858 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1859 VE = VMap.end(); VI != VE; ++VI) {
Devang Patel40c80212009-10-09 22:42:28 +00001860 MetadataBase *MB = VI->first;
1861 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patel90a0fe32009-11-10 23:06:00 +00001862 if (!Var) continue;
Devang Patel6882dff2009-10-08 18:48:03 +00001863 DIVariable DV (Var);
Devang Patel90a0fe32009-11-10 23:06:00 +00001864 std::pair< unsigned, MDNode *> VP = VI->second;
1865 DILocation ScopeLoc(VP.second);
1866
1867 DbgScope *Scope =
1868 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1869 if (!Scope)
Jim Grosbach652b7432009-11-21 23:12:12 +00001870 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelbad42262009-11-10 23:20:04 +00001871 // If variable scope is not found then skip this variable.
1872 if (!Scope)
1873 continue;
Devang Patel90a0fe32009-11-10 23:06:00 +00001874
1875 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patelc50078e2009-11-21 02:48:08 +00001876 Scope->addVariable(RegVar);
Jim Grosbachb23f2422009-11-22 19:20:36 +00001877 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1878 ScopeLoc))
Devang Patel90a0fe32009-11-10 23:06:00 +00001879 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patel84139992009-10-06 01:26:37 +00001880 }
1881}
1882
Devang Patelc50078e2009-11-21 02:48:08 +00001883/// beginScope - Process beginning of a scope starting at Label.
1884void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel393a46d2009-10-06 01:50:42 +00001885 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1886 if (I == DbgScopeBeginMap.end())
1887 return;
Dan Gohman8d34f972009-11-23 21:30:55 +00001888 ScopeVector &SD = I->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00001889 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001890 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00001891 (*SDI)->setStartLabelID(Label);
1892}
1893
Devang Patelc50078e2009-11-21 02:48:08 +00001894/// endScope - Process end of a scope.
1895void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel393a46d2009-10-06 01:50:42 +00001896 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patelf4348892009-10-06 03:15:38 +00001897 if (I == DbgScopeEndMap.end())
Devang Patel393a46d2009-10-06 01:50:42 +00001898 return;
Devang Patel90a0fe32009-11-10 23:06:00 +00001899
1900 unsigned Label = MMI->NextLabelID();
1901 Asm->printLabel(Label);
1902
Devang Patel393a46d2009-10-06 01:50:42 +00001903 SmallVector<DbgScope *, 2> &SD = I->second;
1904 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach652b7432009-11-21 23:12:12 +00001905 SDI != SDE; ++SDI)
Devang Patel393a46d2009-10-06 01:50:42 +00001906 (*SDI)->setEndLabelID(Label);
Devang Patel90a0fe32009-11-10 23:06:00 +00001907 return;
1908}
1909
1910/// createDbgScope - Create DbgScope for the scope.
1911void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1912
1913 if (!InlinedAt) {
1914 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1915 if (WScope)
1916 return;
1917 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1918 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach652b7432009-11-21 23:12:12 +00001919 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel53addbf2009-11-11 00:18:40 +00001920 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patel90a0fe32009-11-10 23:06:00 +00001921 return;
1922 }
1923
1924 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
1925 if (WScope)
1926 return;
1927
1928 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
1929 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
1930 DILocation DL(InlinedAt);
1931 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel393a46d2009-10-06 01:50:42 +00001932}
1933
Devang Patelc50078e2009-11-21 02:48:08 +00001934/// extractScopeInformation - Scan machine instructions in this function
Devang Patel6a260102009-10-01 20:31:14 +00001935/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patelc50078e2009-11-21 02:48:08 +00001936bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patel6a260102009-10-01 20:31:14 +00001937 // If scope information was extracted using .dbg intrinsics then there is not
1938 // any need to extract these information by scanning each instruction.
1939 if (!DbgScopeMap.empty())
1940 return false;
1941
Devang Patel90a0fe32009-11-10 23:06:00 +00001942 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel6a260102009-10-01 20:31:14 +00001943 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1944 I != E; ++I) {
1945 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1946 II != IE; ++II) {
1947 const MachineInstr *MInsn = II;
1948 DebugLoc DL = MInsn->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00001949 if (DL.isUnknown()) continue;
Devang Patel6a260102009-10-01 20:31:14 +00001950 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patel90a0fe32009-11-10 23:06:00 +00001951 if (!DLT.Scope) continue;
Devang Patel6a260102009-10-01 20:31:14 +00001952 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00001953 // other scopes, create one DbgScope now. This will be translated
Devang Patel6a260102009-10-01 20:31:14 +00001954 // into a scope DIE at the end.
Devang Patel90a0fe32009-11-10 23:06:00 +00001955 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1956 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
1957 }
1958 }
1959
1960
1961 // Build scope hierarchy using working set of scopes.
1962 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1963 I != E; ++I) {
1964 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1965 II != IE; ++II) {
1966 const MachineInstr *MInsn = II;
1967 DebugLoc DL = MInsn->getDebugLoc();
1968 if (DL.isUnknown()) continue;
1969 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
1970 if (!DLT.Scope) continue;
1971 // There is no need to create another DIE for compile unit. For all
Jim Grosbach652b7432009-11-21 23:12:12 +00001972 // other scopes, create one DbgScope now. This will be translated
Devang Patel90a0fe32009-11-10 23:06:00 +00001973 // into a scope DIE at the end.
1974 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
1975 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
1976 Scope->setLastInsn(MInsn);
Devang Patel6a260102009-10-01 20:31:14 +00001977 }
1978 }
1979
1980 // If a scope's last instruction is not set then use its child scope's
1981 // last instruction as this scope's last instrunction.
Devang Patelf5278f22009-10-27 20:47:17 +00001982 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00001983 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001984 if (DI->second->isAbstractScope())
1985 continue;
Devang Patel6a260102009-10-01 20:31:14 +00001986 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patelc50078e2009-11-21 02:48:08 +00001987 DI->second->fixInstructionMarkers();
Devang Patel6a260102009-10-01 20:31:14 +00001988 assert (DI->second->getLastInsn() && "Invalid last instruction!");
1989 }
1990
1991 // Each scope has first instruction and last instruction to mark beginning
1992 // and end of a scope respectively. Create an inverse map that list scopes
1993 // starts (and ends) with an instruction. One instruction may start (or end)
1994 // multiple scopes.
Devang Patelf5278f22009-10-27 20:47:17 +00001995 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel6a260102009-10-01 20:31:14 +00001996 DE = DbgScopeMap.end(); DI != DE; ++DI) {
1997 DbgScope *S = DI->second;
Devang Patel90a0fe32009-11-10 23:06:00 +00001998 if (S->isAbstractScope())
1999 continue;
Devang Patel6a260102009-10-01 20:31:14 +00002000 const MachineInstr *MI = S->getFirstInsn();
2001 assert (MI && "DbgScope does not have first instruction!");
2002
2003 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2004 if (IDI != DbgScopeBeginMap.end())
2005 IDI->second.push_back(S);
2006 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002007 DbgScopeBeginMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002008
2009 MI = S->getLastInsn();
2010 assert (MI && "DbgScope does not have last instruction!");
2011 IDI = DbgScopeEndMap.find(MI);
2012 if (IDI != DbgScopeEndMap.end())
2013 IDI->second.push_back(S);
2014 else
Devang Patel90a0fe32009-11-10 23:06:00 +00002015 DbgScopeEndMap[MI].push_back(S);
Devang Patel6a260102009-10-01 20:31:14 +00002016 }
2017
2018 return !DbgScopeMap.empty();
2019}
2020
Devang Patelc50078e2009-11-21 02:48:08 +00002021/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendlingf5839192009-05-20 23:19:06 +00002022/// emitted immediately after the function entry point.
Devang Patelc50078e2009-11-21 02:48:08 +00002023void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002024 this->MF = MF;
2025
2026 if (!ShouldEmitDwarfDebug()) return;
2027
2028 if (TimePassesIsEnabled)
2029 DebugTimer->startTimer();
2030
Devang Patelc50078e2009-11-21 02:48:08 +00002031 if (!extractScopeInformation(MF))
Devang Patel0feae422009-10-06 18:37:31 +00002032 return;
Devang Patelc50078e2009-11-21 02:48:08 +00002033
2034 collectVariableInfo();
Devang Patel0feae422009-10-06 18:37:31 +00002035
Bill Wendlingf5839192009-05-20 23:19:06 +00002036 // Begin accumulating function debug information.
2037 MMI->BeginFunction(MF);
2038
2039 // Assumes in correct section after the entry point.
2040 EmitLabel("func_begin", ++SubprogramCount);
2041
2042 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2043 // function.
Devang Patel40c80212009-10-09 22:42:28 +00002044 DebugLoc FDL = MF->getDefaultDebugLoc();
2045 if (!FDL.isUnknown()) {
2046 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2047 unsigned LabelID = 0;
Devang Patelfc1df342009-10-13 23:28:53 +00002048 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002049 if (!SP.isNull())
Devang Patelc50078e2009-11-21 02:48:08 +00002050 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002051 else
Devang Patelc50078e2009-11-21 02:48:08 +00002052 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Patel40c80212009-10-09 22:42:28 +00002053 Asm->printLabel(LabelID);
2054 O << '\n';
Bill Wendlingf5839192009-05-20 23:19:06 +00002055 }
Bill Wendlingf5839192009-05-20 23:19:06 +00002056 if (TimePassesIsEnabled)
2057 DebugTimer->stopTimer();
2058}
2059
Devang Patelc50078e2009-11-21 02:48:08 +00002060/// endFunction - Gather and emit post-function debug information.
Bill Wendlingf5839192009-05-20 23:19:06 +00002061///
Devang Patelc50078e2009-11-21 02:48:08 +00002062void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendlingf5839192009-05-20 23:19:06 +00002063 if (!ShouldEmitDwarfDebug()) return;
2064
2065 if (TimePassesIsEnabled)
2066 DebugTimer->startTimer();
2067
Devang Patel40c80212009-10-09 22:42:28 +00002068 if (DbgScopeMap.empty())
2069 return;
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002070
Bill Wendlingf5839192009-05-20 23:19:06 +00002071 // Define end label for subprogram.
2072 EmitLabel("func_end", SubprogramCount);
2073
2074 // Get function line info.
2075 if (!Lines.empty()) {
2076 // Get section line info.
Chris Lattnerebd055c2009-08-03 23:20:21 +00002077 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendlingf5839192009-05-20 23:19:06 +00002078 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2079 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2080 // Append the function info to section info.
2081 SectionLineInfos.insert(SectionLineInfos.end(),
2082 Lines.begin(), Lines.end());
2083 }
2084
Devang Patel90a0fe32009-11-10 23:06:00 +00002085 // Construct abstract scopes.
2086 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach652b7432009-11-21 23:12:12 +00002087 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patelc50078e2009-11-21 02:48:08 +00002088 constructScopeDIE(*AI);
Bill Wendlingf5839192009-05-20 23:19:06 +00002089
Devang Patelc50078e2009-11-21 02:48:08 +00002090 constructScopeDIE(CurrentFnDbgScope);
Devang Patel4a7ef8d2009-11-12 19:02:56 +00002091
Bill Wendlingf5839192009-05-20 23:19:06 +00002092 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2093 MMI->getFrameMoves()));
2094
2095 // Clear debug info
Devang Patel90a0fe32009-11-10 23:06:00 +00002096 if (CurrentFnDbgScope) {
2097 CurrentFnDbgScope = NULL;
Bill Wendlingf5839192009-05-20 23:19:06 +00002098 DbgScopeMap.clear();
Devang Patel6a260102009-10-01 20:31:14 +00002099 DbgScopeBeginMap.clear();
2100 DbgScopeEndMap.clear();
Devang Patel90a0fe32009-11-10 23:06:00 +00002101 ConcreteScopes.clear();
2102 AbstractScopesList.clear();
Bill Wendlingf5839192009-05-20 23:19:06 +00002103 }
2104
2105 Lines.clear();
2106
2107 if (TimePassesIsEnabled)
2108 DebugTimer->stopTimer();
2109}
2110
Devang Patelc50078e2009-11-21 02:48:08 +00002111/// recordSourceLine - Records location information and associates it with a
Bill Wendlingf5839192009-05-20 23:19:06 +00002112/// label. Returns a unique label ID used to generate a label and provide
2113/// correspondence to the source line list.
Jim Grosbach652b7432009-11-21 23:12:12 +00002114unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel946d0ae2009-10-05 18:03:19 +00002115 MDNode *S) {
Devang Patel15e723d2009-08-28 23:24:31 +00002116 if (!MMI)
2117 return 0;
2118
Bill Wendlingf5839192009-05-20 23:19:06 +00002119 if (TimePassesIsEnabled)
2120 DebugTimer->startTimer();
2121
Devang Patel946d0ae2009-10-05 18:03:19 +00002122 const char *Dir = NULL;
2123 const char *Fn = NULL;
2124
2125 DIDescriptor Scope(S);
2126 if (Scope.isCompileUnit()) {
2127 DICompileUnit CU(S);
2128 Dir = CU.getDirectory();
2129 Fn = CU.getFilename();
2130 } else if (Scope.isSubprogram()) {
2131 DISubprogram SP(S);
2132 Dir = SP.getDirectory();
2133 Fn = SP.getFilename();
2134 } else if (Scope.isLexicalBlock()) {
2135 DILexicalBlock DB(S);
2136 Dir = DB.getDirectory();
2137 Fn = DB.getFilename();
2138 } else
2139 assert (0 && "Unexpected scope info");
2140
2141 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendlingf5839192009-05-20 23:19:06 +00002142 unsigned ID = MMI->NextLabelID();
2143 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2144
2145 if (TimePassesIsEnabled)
2146 DebugTimer->stopTimer();
2147
2148 return ID;
2149}
2150
2151/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2152/// timed. Look up the source id with the given directory and source file
2153/// names. If none currently exists, create a new id and insert it in the
2154/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2155/// well.
2156unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2157 const std::string &FileName) {
2158 if (TimePassesIsEnabled)
2159 DebugTimer->startTimer();
2160
Devang Patelaaf012e2009-09-29 18:40:58 +00002161 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendlingf5839192009-05-20 23:19:06 +00002162
2163 if (TimePassesIsEnabled)
2164 DebugTimer->stopTimer();
2165
2166 return SrcId;
2167}
2168
Bill Wendlinge1a5bbb2009-05-20 23:22:40 +00002169//===----------------------------------------------------------------------===//
2170// Emit Methods
2171//===----------------------------------------------------------------------===//
2172
Devang Patelc50078e2009-11-21 02:48:08 +00002173/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling55fccda2009-05-20 23:21:38 +00002174///
Jim Grosbachb23f2422009-11-22 19:20:36 +00002175unsigned
2176DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002177 // Get the children.
2178 const std::vector<DIE *> &Children = Die->getChildren();
2179
2180 // If not last sibling and has children then add sibling offset attribute.
Devang Patelc50078e2009-11-21 02:48:08 +00002181 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002182
2183 // Record the abbreviation.
Devang Patelc50078e2009-11-21 02:48:08 +00002184 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling55fccda2009-05-20 23:21:38 +00002185
2186 // Get the abbreviation for this DIE.
2187 unsigned AbbrevNumber = Die->getAbbrevNumber();
2188 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2189
2190 // Set DIE offset
2191 Die->setOffset(Offset);
2192
2193 // Start the size with the size of abbreviation code.
Chris Lattner621c44d2009-08-22 20:48:53 +00002194 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling55fccda2009-05-20 23:21:38 +00002195
2196 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2197 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2198
2199 // Size the DIE attribute values.
2200 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2201 // Size attribute value.
2202 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2203
2204 // Size the DIE children if any.
2205 if (!Children.empty()) {
2206 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2207 "Children flag not set");
2208
2209 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002210 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling55fccda2009-05-20 23:21:38 +00002211
2212 // End of children marker.
2213 Offset += sizeof(int8_t);
2214 }
2215
2216 Die->setSize(Offset - Die->getOffset());
2217 return Offset;
2218}
2219
Devang Patelc50078e2009-11-21 02:48:08 +00002220/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling55fccda2009-05-20 23:21:38 +00002221///
Devang Patelc50078e2009-11-21 02:48:08 +00002222void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002223 // Compute size of compile unit header.
2224 static unsigned Offset =
2225 sizeof(int32_t) + // Length of Compilation Unit Info
2226 sizeof(int16_t) + // DWARF version number
2227 sizeof(int32_t) + // Offset Into Abbrev. Section
2228 sizeof(int8_t); // Pointer Size (in bytes)
2229
Devang Patelc50078e2009-11-21 02:48:08 +00002230 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel5a3d37f2009-06-29 20:45:18 +00002231 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling55fccda2009-05-20 23:21:38 +00002232}
2233
Devang Patelc50078e2009-11-21 02:48:08 +00002234/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling55fccda2009-05-20 23:21:38 +00002235/// tools to recognize the object file contains Dwarf information.
Devang Patelc50078e2009-11-21 02:48:08 +00002236void DwarfDebug::emitInitial() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002237 // Check to see if we already emitted intial headers.
2238 if (didInitial) return;
2239 didInitial = true;
2240
Chris Lattner73266f92009-08-19 05:49:37 +00002241 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbar41716322009-09-19 20:40:05 +00002242
Bill Wendling55fccda2009-05-20 23:21:38 +00002243 // Dwarf sections base addresses.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002244 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner73266f92009-08-19 05:49:37 +00002245 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002246 EmitLabel("section_debug_frame", 0);
2247 }
2248
Chris Lattner73266f92009-08-19 05:49:37 +00002249 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002250 EmitLabel("section_info", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002251 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002252 EmitLabel("section_abbrev", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002253 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002254 EmitLabel("section_aranges", 0);
2255
Chris Lattner73266f92009-08-19 05:49:37 +00002256 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2257 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling55fccda2009-05-20 23:21:38 +00002258 EmitLabel("section_macinfo", 0);
2259 }
2260
Chris Lattner73266f92009-08-19 05:49:37 +00002261 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002262 EmitLabel("section_line", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002263 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002264 EmitLabel("section_loc", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002265 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002266 EmitLabel("section_pubnames", 0);
Devang Patelec13b4f2009-11-24 01:14:22 +00002267 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2268 EmitLabel("section_pubtypes", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002269 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002270 EmitLabel("section_str", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002271 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002272 EmitLabel("section_ranges", 0);
2273
Chris Lattner73266f92009-08-19 05:49:37 +00002274 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002275 EmitLabel("text_begin", 0);
Chris Lattner73266f92009-08-19 05:49:37 +00002276 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002277 EmitLabel("data_begin", 0);
2278}
2279
Devang Patelc50078e2009-11-21 02:48:08 +00002280/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling55fccda2009-05-20 23:21:38 +00002281///
Devang Patelc50078e2009-11-21 02:48:08 +00002282void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002283 // Get the abbreviation for this DIE.
2284 unsigned AbbrevNumber = Die->getAbbrevNumber();
2285 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2286
2287 Asm->EOL();
2288
2289 // Emit the code (index) for the abbreviation.
2290 Asm->EmitULEB128Bytes(AbbrevNumber);
2291
2292 if (Asm->isVerbose())
2293 Asm->EOL(std::string("Abbrev [" +
2294 utostr(AbbrevNumber) +
2295 "] 0x" + utohexstr(Die->getOffset()) +
2296 ":0x" + utohexstr(Die->getSize()) + " " +
2297 dwarf::TagString(Abbrev->getTag())));
2298 else
2299 Asm->EOL();
2300
2301 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2302 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2303
2304 // Emit the DIE attribute values.
2305 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2306 unsigned Attr = AbbrevData[i].getAttribute();
2307 unsigned Form = AbbrevData[i].getForm();
2308 assert(Form && "Too many attributes for DIE (check abbreviation)");
2309
2310 switch (Attr) {
2311 case dwarf::DW_AT_sibling:
Devang Patelc50078e2009-11-21 02:48:08 +00002312 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling55fccda2009-05-20 23:21:38 +00002313 break;
2314 case dwarf::DW_AT_abstract_origin: {
2315 DIEEntry *E = cast<DIEEntry>(Values[i]);
2316 DIE *Origin = E->getEntry();
Devang Patel90a0fe32009-11-10 23:06:00 +00002317 unsigned Addr = Origin->getOffset();
Bill Wendling55fccda2009-05-20 23:21:38 +00002318 Asm->EmitInt32(Addr);
2319 break;
2320 }
2321 default:
2322 // Emit an attribute using the defined form.
2323 Values[i]->EmitValue(this, Form);
2324 break;
2325 }
2326
2327 Asm->EOL(dwarf::AttributeString(Attr));
2328 }
2329
2330 // Emit the DIE children if any.
2331 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2332 const std::vector<DIE *> &Children = Die->getChildren();
2333
2334 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patelc50078e2009-11-21 02:48:08 +00002335 emitDIE(Children[j]);
Bill Wendling55fccda2009-05-20 23:21:38 +00002336
2337 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2338 }
2339}
2340
Devang Patelc50078e2009-11-21 02:48:08 +00002341/// emitDebugInfo / emitDebugInfoPerCU - Emit the debug info section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002342///
Devang Patelc50078e2009-11-21 02:48:08 +00002343void DwarfDebug::emitDebugInfoPerCU(CompileUnit *Unit) {
Devang Pateld90672c2009-11-20 21:37:22 +00002344 DIE *Die = Unit->getCUDie();
Bill Wendling55fccda2009-05-20 23:21:38 +00002345
2346 // Emit the compile units header.
2347 EmitLabel("info_begin", Unit->getID());
2348
2349 // Emit size of content not including length itself
2350 unsigned ContentSize = Die->getSize() +
2351 sizeof(int16_t) + // DWARF version number
2352 sizeof(int32_t) + // Offset Into Abbrev. Section
2353 sizeof(int8_t) + // Pointer Size (in bytes)
2354 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2355
2356 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2357 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2358 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2359 Asm->EOL("Offset Into Abbrev. Section");
2360 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2361
Devang Patelc50078e2009-11-21 02:48:08 +00002362 emitDIE(Die);
Bill Wendling55fccda2009-05-20 23:21:38 +00002363 // FIXME - extra padding for gdb bug.
2364 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2365 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2366 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2367 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2368 EmitLabel("info_end", Unit->getID());
2369
2370 Asm->EOL();
2371}
2372
Devang Patelc50078e2009-11-21 02:48:08 +00002373void DwarfDebug::emitDebugInfo() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002374 // Start debug info section.
Chris Lattner73266f92009-08-19 05:49:37 +00002375 Asm->OutStreamer.SwitchSection(
2376 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002377
Devang Patelc50078e2009-11-21 02:48:08 +00002378 emitDebugInfoPerCU(ModuleCU);
Bill Wendling55fccda2009-05-20 23:21:38 +00002379}
2380
Devang Patelc50078e2009-11-21 02:48:08 +00002381/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002382///
Devang Patelc50078e2009-11-21 02:48:08 +00002383void DwarfDebug::emitAbbreviations() const {
Bill Wendling55fccda2009-05-20 23:21:38 +00002384 // Check to see if it is worth the effort.
2385 if (!Abbreviations.empty()) {
2386 // Start the debug abbrev section.
Chris Lattner73266f92009-08-19 05:49:37 +00002387 Asm->OutStreamer.SwitchSection(
2388 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002389
2390 EmitLabel("abbrev_begin", 0);
2391
2392 // For each abbrevation.
2393 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2394 // Get abbreviation data
2395 const DIEAbbrev *Abbrev = Abbreviations[i];
2396
2397 // Emit the abbrevations code (base 1 index.)
2398 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2399 Asm->EOL("Abbreviation Code");
2400
2401 // Emit the abbreviations data.
2402 Abbrev->Emit(Asm);
2403
2404 Asm->EOL();
2405 }
2406
2407 // Mark end of abbreviations.
2408 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2409
2410 EmitLabel("abbrev_end", 0);
2411 Asm->EOL();
2412 }
2413}
2414
Devang Patelc50078e2009-11-21 02:48:08 +00002415/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling55fccda2009-05-20 23:21:38 +00002416/// the line matrix.
2417///
Devang Patelc50078e2009-11-21 02:48:08 +00002418void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002419 // Define last address of section.
2420 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2421 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2422 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2423 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2424
2425 // Mark end of matrix.
2426 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2427 Asm->EmitULEB128Bytes(1); Asm->EOL();
2428 Asm->EmitInt8(1); Asm->EOL();
2429}
2430
Devang Patelc50078e2009-11-21 02:48:08 +00002431/// emitDebugLines - Emit source line information.
Bill Wendling55fccda2009-05-20 23:21:38 +00002432///
Devang Patelc50078e2009-11-21 02:48:08 +00002433void DwarfDebug::emitDebugLines() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002434 // If the target is using .loc/.file, the assembler will be emitting the
2435 // .debug_line table automatically.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002436 if (MAI->hasDotLocAndDotFile())
Bill Wendling55fccda2009-05-20 23:21:38 +00002437 return;
2438
2439 // Minimum line delta, thus ranging from -10..(255-10).
2440 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2441 // Maximum line delta, thus ranging from -10..(255-10).
2442 const int MaxLineDelta = 255 + MinLineDelta;
2443
2444 // Start the dwarf line section.
Chris Lattner73266f92009-08-19 05:49:37 +00002445 Asm->OutStreamer.SwitchSection(
2446 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002447
2448 // Construct the section header.
2449 EmitDifference("line_end", 0, "line_begin", 0, true);
2450 Asm->EOL("Length of Source Line Info");
2451 EmitLabel("line_begin", 0);
2452
2453 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2454
2455 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2456 Asm->EOL("Prolog Length");
2457 EmitLabel("line_prolog_begin", 0);
2458
2459 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2460
2461 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2462
2463 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2464
2465 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2466
2467 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2468
2469 // Line number standard opcode encodings argument count
2470 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2471 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2472 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2473 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2474 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2475 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2476 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2477 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2478 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2479
2480 // Emit directories.
2481 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2482 Asm->EmitString(getSourceDirectoryName(DI));
2483 Asm->EOL("Directory");
2484 }
2485
2486 Asm->EmitInt8(0); Asm->EOL("End of directories");
2487
2488 // Emit files.
2489 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2490 // Remember source id starts at 1.
2491 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2492 Asm->EmitString(getSourceFileName(Id.second));
2493 Asm->EOL("Source");
2494 Asm->EmitULEB128Bytes(Id.first);
2495 Asm->EOL("Directory #");
2496 Asm->EmitULEB128Bytes(0);
2497 Asm->EOL("Mod date");
2498 Asm->EmitULEB128Bytes(0);
2499 Asm->EOL("File size");
2500 }
2501
2502 Asm->EmitInt8(0); Asm->EOL("End of files");
2503
2504 EmitLabel("line_prolog_end", 0);
2505
2506 // A sequence for each text section.
2507 unsigned SecSrcLinesSize = SectionSourceLines.size();
2508
2509 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2510 // Isolate current sections line info.
2511 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2512
Chris Lattner26aabb92009-08-08 23:39:42 +00002513 /*if (Asm->isVerbose()) {
Chris Lattnere6ad12f2009-07-31 18:48:30 +00002514 const MCSection *S = SectionMap[j + 1];
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002515 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling55fccda2009-05-20 23:21:38 +00002516 << S->getName() << '\n';
Chris Lattner26aabb92009-08-08 23:39:42 +00002517 }*/
2518 Asm->EOL();
Bill Wendling55fccda2009-05-20 23:21:38 +00002519
2520 // Dwarf assumes we start with first line of first source file.
2521 unsigned Source = 1;
2522 unsigned Line = 1;
2523
2524 // Construct rows of the address, source, line, column matrix.
2525 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2526 const SrcLineInfo &LineInfo = LineInfos[i];
2527 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2528 if (!LabelID) continue;
2529
Caroline Tice9da96d82009-09-11 18:25:54 +00002530 if (LineInfo.getLine() == 0) continue;
2531
Bill Wendling55fccda2009-05-20 23:21:38 +00002532 if (!Asm->isVerbose())
2533 Asm->EOL();
2534 else {
2535 std::pair<unsigned, unsigned> SourceID =
2536 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002537 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling55fccda2009-05-20 23:21:38 +00002538 << getSourceDirectoryName(SourceID.first) << ' '
2539 << getSourceFileName(SourceID.second)
2540 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2541 }
2542
2543 // Define the line address.
2544 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2545 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2546 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2547 EmitReference("label", LabelID); Asm->EOL("Location label");
2548
2549 // If change of source, then switch to the new source.
2550 if (Source != LineInfo.getSourceID()) {
2551 Source = LineInfo.getSourceID();
2552 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2553 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2554 }
2555
2556 // If change of line.
2557 if (Line != LineInfo.getLine()) {
2558 // Determine offset.
2559 int Offset = LineInfo.getLine() - Line;
2560 int Delta = Offset - MinLineDelta;
2561
2562 // Update line.
2563 Line = LineInfo.getLine();
2564
2565 // If delta is small enough and in range...
2566 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2567 // ... then use fast opcode.
2568 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2569 } else {
2570 // ... otherwise use long hand.
2571 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2572 Asm->EOL("DW_LNS_advance_line");
2573 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2574 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2575 }
2576 } else {
2577 // Copy the previous row (different address or source)
2578 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2579 }
2580 }
2581
Devang Patelc50078e2009-11-21 02:48:08 +00002582 emitEndOfLineMatrix(j + 1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002583 }
2584
2585 if (SecSrcLinesSize == 0)
2586 // Because we're emitting a debug_line section, we still need a line
2587 // table. The linker and friends expect it to exist. If there's nothing to
2588 // put into it, emit an empty table.
Devang Patelc50078e2009-11-21 02:48:08 +00002589 emitEndOfLineMatrix(1);
Bill Wendling55fccda2009-05-20 23:21:38 +00002590
2591 EmitLabel("line_end", 0);
2592 Asm->EOL();
2593}
2594
Devang Patelc50078e2009-11-21 02:48:08 +00002595/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002596///
Devang Patelc50078e2009-11-21 02:48:08 +00002597void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002598 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002599 return;
2600
2601 int stackGrowth =
2602 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2603 TargetFrameInfo::StackGrowsUp ?
2604 TD->getPointerSize() : -TD->getPointerSize();
2605
2606 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002607 Asm->OutStreamer.SwitchSection(
2608 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002609
2610 EmitLabel("debug_frame_common", 0);
2611 EmitDifference("debug_frame_common_end", 0,
2612 "debug_frame_common_begin", 0, true);
2613 Asm->EOL("Length of Common Information Entry");
2614
2615 EmitLabel("debug_frame_common_begin", 0);
2616 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2617 Asm->EOL("CIE Identifier Tag");
2618 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2619 Asm->EOL("CIE Version");
2620 Asm->EmitString("");
2621 Asm->EOL("CIE Augmentation");
2622 Asm->EmitULEB128Bytes(1);
2623 Asm->EOL("CIE Code Alignment Factor");
2624 Asm->EmitSLEB128Bytes(stackGrowth);
2625 Asm->EOL("CIE Data Alignment Factor");
2626 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2627 Asm->EOL("CIE RA Column");
2628
2629 std::vector<MachineMove> Moves;
2630 RI->getInitialFrameState(Moves);
2631
2632 EmitFrameMoves(NULL, 0, Moves, false);
2633
2634 Asm->EmitAlignment(2, 0, 0, false);
2635 EmitLabel("debug_frame_common_end", 0);
2636
2637 Asm->EOL();
2638}
2639
Devang Patelc50078e2009-11-21 02:48:08 +00002640/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling55fccda2009-05-20 23:21:38 +00002641/// section.
2642void
Devang Patelc50078e2009-11-21 02:48:08 +00002643DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002644 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002645 return;
2646
2647 // Start the dwarf frame section.
Chris Lattner73266f92009-08-19 05:49:37 +00002648 Asm->OutStreamer.SwitchSection(
2649 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002650
2651 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2652 "debug_frame_begin", DebugFrameInfo.Number, true);
2653 Asm->EOL("Length of Frame Information Entry");
2654
2655 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2656
2657 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2658 0, 0, true, false);
2659 Asm->EOL("FDE CIE offset");
2660
2661 EmitReference("func_begin", DebugFrameInfo.Number);
2662 Asm->EOL("FDE initial location");
2663 EmitDifference("func_end", DebugFrameInfo.Number,
2664 "func_begin", DebugFrameInfo.Number);
2665 Asm->EOL("FDE address range");
2666
2667 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2668 false);
2669
2670 Asm->EmitAlignment(2, 0, 0, false);
2671 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2672
2673 Asm->EOL();
2674}
2675
Devang Patelc50078e2009-11-21 02:48:08 +00002676void DwarfDebug::emitDebugPubNamesPerCU(CompileUnit *Unit) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002677 EmitDifference("pubnames_end", Unit->getID(),
2678 "pubnames_begin", Unit->getID(), true);
2679 Asm->EOL("Length of Public Names Info");
2680
2681 EmitLabel("pubnames_begin", Unit->getID());
2682
2683 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2684
2685 EmitSectionOffset("info_begin", "section_info",
2686 Unit->getID(), 0, true, false);
2687 Asm->EOL("Offset of Compilation Unit Info");
2688
2689 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2690 true);
2691 Asm->EOL("Compilation Unit Length");
2692
Devang Patelec13b4f2009-11-24 01:14:22 +00002693 const StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendling55fccda2009-05-20 23:21:38 +00002694 for (StringMap<DIE*>::const_iterator
2695 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2696 const char *Name = GI->getKeyData();
2697 DIE * Entity = GI->second;
2698
2699 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2700 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2701 }
2702
2703 Asm->EmitInt32(0); Asm->EOL("End Mark");
2704 EmitLabel("pubnames_end", Unit->getID());
2705
2706 Asm->EOL();
2707}
2708
Devang Patelc50078e2009-11-21 02:48:08 +00002709/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002710///
Devang Patelc50078e2009-11-21 02:48:08 +00002711void DwarfDebug::emitDebugPubNames() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002712 // Start the dwarf pubnames section.
Chris Lattner73266f92009-08-19 05:49:37 +00002713 Asm->OutStreamer.SwitchSection(
2714 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002715
Devang Patelc50078e2009-11-21 02:48:08 +00002716 emitDebugPubNamesPerCU(ModuleCU);
Bill Wendling55fccda2009-05-20 23:21:38 +00002717}
2718
Devang Patelec13b4f2009-11-24 01:14:22 +00002719void DwarfDebug::emitDebugPubTypes() {
2720 EmitDifference("pubtypes_end", ModuleCU->getID(),
2721 "pubtypes_begin", ModuleCU->getID(), true);
2722 Asm->EOL("Length of Public Types Info");
2723
2724 EmitLabel("pubtypes_begin", ModuleCU->getID());
2725
2726 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2727
2728 EmitSectionOffset("info_begin", "section_info",
2729 ModuleCU->getID(), 0, true, false);
2730 Asm->EOL("Offset of Compilation ModuleCU Info");
2731
2732 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2733 true);
2734 Asm->EOL("Compilation ModuleCU Length");
2735
2736 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2737 for (StringMap<DIE*>::const_iterator
2738 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2739 const char *Name = GI->getKeyData();
2740 DIE * Entity = GI->second;
2741
2742 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2743 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2744 }
2745
2746 Asm->EmitInt32(0); Asm->EOL("End Mark");
2747 EmitLabel("pubtypes_end", ModuleCU->getID());
2748
2749 Asm->EOL();
2750}
2751
Devang Patelc50078e2009-11-21 02:48:08 +00002752/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002753///
Devang Patelc50078e2009-11-21 02:48:08 +00002754void DwarfDebug::emitDebugStr() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002755 // Check to see if it is worth the effort.
2756 if (!StringPool.empty()) {
2757 // Start the dwarf str section.
Chris Lattner73266f92009-08-19 05:49:37 +00002758 Asm->OutStreamer.SwitchSection(
2759 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002760
2761 // For each of strings in the string pool.
2762 for (unsigned StringID = 1, N = StringPool.size();
2763 StringID <= N; ++StringID) {
2764 // Emit a label for reference from debug information entries.
2765 EmitLabel("string", StringID);
2766
2767 // Emit the string itself.
2768 const std::string &String = StringPool[StringID];
2769 Asm->EmitString(String); Asm->EOL();
2770 }
2771
2772 Asm->EOL();
2773 }
2774}
2775
Devang Patelc50078e2009-11-21 02:48:08 +00002776/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002777///
Devang Patelc50078e2009-11-21 02:48:08 +00002778void DwarfDebug::emitDebugLoc() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002779 // Start the dwarf loc section.
Chris Lattner73266f92009-08-19 05:49:37 +00002780 Asm->OutStreamer.SwitchSection(
2781 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002782 Asm->EOL();
2783}
2784
2785/// EmitDebugARanges - Emit visible names into a debug aranges section.
2786///
2787void DwarfDebug::EmitDebugARanges() {
2788 // Start the dwarf aranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002789 Asm->OutStreamer.SwitchSection(
2790 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002791
2792 // FIXME - Mock up
2793#if 0
2794 CompileUnit *Unit = GetBaseCompileUnit();
2795
2796 // Don't include size of length
2797 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2798
2799 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2800
2801 EmitReference("info_begin", Unit->getID());
2802 Asm->EOL("Offset of Compilation Unit Info");
2803
2804 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2805
2806 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2807
2808 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2809 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2810
2811 // Range 1
2812 EmitReference("text_begin", 0); Asm->EOL("Address");
2813 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2814
2815 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2816 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2817#endif
2818
2819 Asm->EOL();
2820}
2821
Devang Patelc50078e2009-11-21 02:48:08 +00002822/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002823///
Devang Patelc50078e2009-11-21 02:48:08 +00002824void DwarfDebug::emitDebugRanges() {
Bill Wendling55fccda2009-05-20 23:21:38 +00002825 // Start the dwarf ranges section.
Chris Lattner73266f92009-08-19 05:49:37 +00002826 Asm->OutStreamer.SwitchSection(
2827 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002828 Asm->EOL();
2829}
2830
Devang Patelc50078e2009-11-21 02:48:08 +00002831/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling55fccda2009-05-20 23:21:38 +00002832///
Devang Patelc50078e2009-11-21 02:48:08 +00002833void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbar41716322009-09-19 20:40:05 +00002834 if (const MCSection *LineInfo =
Chris Lattner72d228d2009-08-02 07:24:22 +00002835 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling55fccda2009-05-20 23:21:38 +00002836 // Start the dwarf macinfo section.
Chris Lattner73266f92009-08-19 05:49:37 +00002837 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling55fccda2009-05-20 23:21:38 +00002838 Asm->EOL();
2839 }
2840}
2841
Devang Patelc50078e2009-11-21 02:48:08 +00002842/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling55fccda2009-05-20 23:21:38 +00002843/// Section Header:
2844/// 1. length of section
2845/// 2. Dwarf version number
2846/// 3. address size.
2847///
2848/// Entries (one "entry" for each function that was inlined):
2849///
2850/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2851/// otherwise offset into __debug_str for regular function name.
2852/// 2. offset into __debug_str section for regular function name.
2853/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2854/// instances for the function.
2855///
2856/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2857/// inlined instance; the die_offset points to the inlined_subroutine die in the
2858/// __debug_info section, and the low_pc is the starting address for the
2859/// inlining instance.
Devang Patelc50078e2009-11-21 02:48:08 +00002860void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002861 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling55fccda2009-05-20 23:21:38 +00002862 return;
2863
Devang Patel5a3d37f2009-06-29 20:45:18 +00002864 if (!ModuleCU)
Bill Wendling55fccda2009-05-20 23:21:38 +00002865 return;
2866
Chris Lattner73266f92009-08-19 05:49:37 +00002867 Asm->OutStreamer.SwitchSection(
2868 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling55fccda2009-05-20 23:21:38 +00002869 Asm->EOL();
2870 EmitDifference("debug_inlined_end", 1,
2871 "debug_inlined_begin", 1, true);
2872 Asm->EOL("Length of Debug Inlined Information Entry");
2873
2874 EmitLabel("debug_inlined_begin", 1);
2875
2876 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2877 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2878
Devang Patel90a0fe32009-11-10 23:06:00 +00002879 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2880 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach652b7432009-11-21 23:12:12 +00002881
Devang Patel90a0fe32009-11-10 23:06:00 +00002882// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2883 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2884 MDNode *Node = *I;
Jim Grosbachb23f2422009-11-22 19:20:36 +00002885 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
2886 = InlineInfo.find(Node);
Devang Patel90a0fe32009-11-10 23:06:00 +00002887 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel15e723d2009-08-28 23:24:31 +00002888 DISubprogram SP(Node);
Devang Patelaaf012e2009-09-29 18:40:58 +00002889 const char *LName = SP.getLinkageName();
2890 const char *Name = SP.getName();
Bill Wendling55fccda2009-05-20 23:21:38 +00002891
Devang Patelaaf012e2009-09-29 18:40:58 +00002892 if (!LName)
Devang Patel76031e82009-07-16 01:01:22 +00002893 Asm->EmitString(Name);
2894 else {
Chris Lattner73266f92009-08-19 05:49:37 +00002895 // Skip special LLVM prefix that is used to inform the asm printer to not
2896 // emit usual symbol prefix before the symbol name. This happens for
2897 // Objective-C symbol names and symbol whose name is replaced using GCC's
2898 // __asm__ attribute.
Devang Patel76031e82009-07-16 01:01:22 +00002899 if (LName[0] == 1)
2900 LName = &LName[1];
Devang Patel90a0fe32009-11-10 23:06:00 +00002901// Asm->EmitString(LName);
2902 EmitSectionOffset("string", "section_str",
2903 StringPool.idFor(LName), false, true);
2904
Devang Patel76031e82009-07-16 01:01:22 +00002905 }
Bill Wendling55fccda2009-05-20 23:21:38 +00002906 Asm->EOL("MIPS linkage name");
Jim Grosbach652b7432009-11-21 23:12:12 +00002907// Asm->EmitString(Name);
Devang Patel90a0fe32009-11-10 23:06:00 +00002908 EmitSectionOffset("string", "section_str",
2909 StringPool.idFor(Name), false, true);
2910 Asm->EOL("Function name");
Bill Wendling55fccda2009-05-20 23:21:38 +00002911 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2912
Devang Patel90a0fe32009-11-10 23:06:00 +00002913 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling55fccda2009-05-20 23:21:38 +00002914 LE = Labels.end(); LI != LE; ++LI) {
Devang Patel90a0fe32009-11-10 23:06:00 +00002915 DIE *SP = LI->second;
Bill Wendling55fccda2009-05-20 23:21:38 +00002916 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
2917
2918 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002919 O << MAI->getData32bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00002920 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002921 O << MAI->getData64bitsDirective();
Bill Wendling55fccda2009-05-20 23:21:38 +00002922
Devang Patel90a0fe32009-11-10 23:06:00 +00002923 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling55fccda2009-05-20 23:21:38 +00002924 }
2925 }
2926
2927 EmitLabel("debug_inlined_end", 1);
2928 Asm->EOL();
2929}