blob: 60a8ffe0a62243997948f2811992556f72c79765 [file] [log] [blame]
Bill Wendling2f921f82009-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 Patel80ae3492009-08-28 23:24:31 +000013#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000014#include "DwarfDebug.h"
15#include "llvm/Module.h"
David Greene829b3e82009-08-19 21:52:55 +000016#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000018#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner06fa1762009-08-24 03:52:50 +000025#include "llvm/ADT/StringExtras.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
Chris Lattnerdd5a9892009-09-16 00:08:41 +000028#include "llvm/Support/Mangler.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000029#include "llvm/Support/Timer.h"
30#include "llvm/System/Path.h"
Bill Wendling2f921f82009-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 Wendling2f921f82009-05-15 09:23:25 +000042static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-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 Lewyckyb7993d62009-11-17 08:11:44 +000049class CompileUnit {
Bill Wendling2f921f82009-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 Patel930143b2009-11-21 02:48:08 +000056 DIE *CUDie;
Bill Wendling2f921f82009-05-15 09:23:25 +000057
Devang Patel92e8c652009-11-21 00:31:03 +000058 /// IndexTyDie - An anonymous type for index type.
59 DIE *IndexTyDie;
60
Bill Wendling2f921f82009-05-15 09:23:25 +000061 /// GVToDieMap - Tracks the mapping of unit level debug informaton
62 /// variables to debug information entries.
Devang Patel80ae3492009-08-28 23:24:31 +000063 /// FIXME : Rename GVToDieMap -> NodeToDieMap
Devang Patele064ad42009-11-20 21:37:22 +000064 ValueMap<MDNode *, DIE *> GVToDieMap;
Bill Wendling2f921f82009-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 Patel80ae3492009-08-28 23:24:31 +000068 /// FIXME : Rename
Devang Patele064ad42009-11-20 21:37:22 +000069 ValueMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
Bill Wendling2f921f82009-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 Patel04d2f2d2009-11-24 01:14:22 +000075 /// GlobalTypes - A map of globally visible types for this unit.
76 ///
77 StringMap<DIE*> GlobalTypes;
78
Bill Wendling2f921f82009-05-15 09:23:25 +000079public:
80 CompileUnit(unsigned I, DIE *D)
Devang Patel930143b2009-11-21 02:48:08 +000081 : ID(I), CUDie(D), IndexTyDie(0) {}
82 ~CompileUnit() { delete CUDie; delete IndexTyDie; }
Bill Wendling2f921f82009-05-15 09:23:25 +000083
84 // Accessors.
Devang Patel04d2f2d2009-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 Wendling2f921f82009-05-15 09:23:25 +000089
90 /// hasContent - Return true if this compile unit has something to write out.
91 ///
Devang Patel930143b2009-11-21 02:48:08 +000092 bool hasContent() const { return !CUDie->getChildren().empty(); }
Bill Wendling2f921f82009-05-15 09:23:25 +000093
Devang Patel930143b2009-11-21 02:48:08 +000094 /// addGlobal - Add a new global entity to the compile unit.
Bill Wendling2f921f82009-05-15 09:23:25 +000095 ///
Devang Patel930143b2009-11-21 02:48:08 +000096 void addGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; }
Bill Wendling2f921f82009-05-15 09:23:25 +000097
Devang Patel04d2f2d2009-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 Patele064ad42009-11-20 21:37:22 +0000104 /// getDIE - Returns the debug information entry map slot for the
Bill Wendling2f921f82009-05-15 09:23:25 +0000105 /// specified debug variable.
Devang Patele064ad42009-11-20 21:37:22 +0000106 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
Jim Grosbach042483e2009-11-21 23:12:12 +0000107
Devang Patele064ad42009-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 Wendling2f921f82009-05-15 09:23:25 +0000112
Devang Patele064ad42009-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 Wendling2f921f82009-05-15 09:23:25 +0000120 }
121
Devang Patel930143b2009-11-21 02:48:08 +0000122 /// addDie - Adds or interns the DIE to the compile unit.
Bill Wendling2f921f82009-05-15 09:23:25 +0000123 ///
Devang Patel930143b2009-11-21 02:48:08 +0000124 void addDie(DIE *Buffer) {
125 this->CUDie->addChild(Buffer);
Bill Wendling2f921f82009-05-15 09:23:25 +0000126 }
Devang Patel92e8c652009-11-21 00:31:03 +0000127
128 // getIndexTyDie - Get an anonymous type for index type.
129 DIE *getIndexTyDie() {
130 return IndexTyDie;
131 }
132
Jim Grosbach00e9c612009-11-22 19:20:36 +0000133 // setIndexTyDie - Set D as anonymous type for index which can be reused
134 // later.
Devang Patel92e8c652009-11-21 00:31:03 +0000135 void setIndexTyDie(DIE *D) {
136 IndexTyDie = D;
137 }
138
Bill Wendling2f921f82009-05-15 09:23:25 +0000139};
140
141//===----------------------------------------------------------------------===//
142/// DbgVariable - This class is used to track local variable information.
143///
Devang Patelf3d7c082009-11-16 21:53:40 +0000144class DbgVariable {
Bill Wendling2f921f82009-05-15 09:23:25 +0000145 DIVariable Var; // Variable Descriptor.
146 unsigned FrameIndex; // Variable frame index.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000147 DbgVariable *AbstractVar; // Abstract variable for this variable.
148 DIE *TheDIE;
Bill Wendling2f921f82009-05-15 09:23:25 +0000149public:
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000150 DbgVariable(DIVariable V, unsigned I)
151 : Var(V), FrameIndex(I), AbstractVar(0), TheDIE(0) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000152
153 // Accessors.
Devang Patelf6eeaeb2009-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 Wendling2f921f82009-05-15 09:23:25 +0000160};
161
162//===----------------------------------------------------------------------===//
163/// DbgScope - This class is used to track scope information.
164///
Devang Patelf3d7c082009-11-16 21:53:40 +0000165class DbgScope {
Bill Wendling2f921f82009-05-15 09:23:25 +0000166 DbgScope *Parent; // Parent to this scope.
Jim Grosbach042483e2009-11-21 23:12:12 +0000167 DIDescriptor Desc; // Debug info descriptor for scope.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000168 WeakVH InlinedAtLocation; // Location at which scope is inlined.
169 bool AbstractScope; // Abstract Scope
Bill Wendling2f921f82009-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 Patel787f94c2009-10-01 18:25:23 +0000172 const MachineInstr *LastInsn; // Last instruction of this scope.
173 const MachineInstr *FirstInsn; // First instruction of this scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000174 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
175 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000176
Owen Anderson9becc182009-06-24 22:53:20 +0000177 // Private state for dump()
178 mutable unsigned IndentLevel;
Bill Wendling2f921f82009-05-15 09:23:25 +0000179public:
Devang Patel6875c5eb2009-10-14 21:08:09 +0000180 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000181 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Jim Grosbach042483e2009-11-21 23:12:12 +0000182 StartLabelID(0), EndLabelID(0),
Devang Patel6875c5eb2009-10-14 21:08:09 +0000183 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
Bill Wendling2f921f82009-05-15 09:23:25 +0000184 virtual ~DbgScope();
185
186 // Accessors.
187 DbgScope *getParent() const { return Parent; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000188 void setParent(DbgScope *P) { Parent = P; }
Bill Wendling2f921f82009-05-15 09:23:25 +0000189 DIDescriptor getDesc() const { return Desc; }
Jim Grosbach042483e2009-11-21 23:12:12 +0000190 MDNode *getInlinedAt() const {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000191 return dyn_cast_or_null<MDNode>(InlinedAtLocation);
Devang Patel6875c5eb2009-10-14 21:08:09 +0000192 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000193 MDNode *getScopeNode() const { return Desc.getNode(); }
Bill Wendling2f921f82009-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 Wendling2f921f82009-05-15 09:23:25 +0000198 void setStartLabelID(unsigned S) { StartLabelID = S; }
199 void setEndLabelID(unsigned E) { EndLabelID = E; }
Devang Patel787f94c2009-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 Patelf6eeaeb2009-11-10 23:06:00 +0000203 void setAbstractScope() { AbstractScope = true; }
204 bool isAbstractScope() const { return AbstractScope; }
Devang Patel787f94c2009-10-01 18:25:23 +0000205 const MachineInstr *getFirstInsn() { return FirstInsn; }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000206
Devang Patel930143b2009-11-21 02:48:08 +0000207 /// addScope - Add a scope to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000208 ///
Devang Patel930143b2009-11-21 02:48:08 +0000209 void addScope(DbgScope *S) { Scopes.push_back(S); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000210
Devang Patel930143b2009-11-21 02:48:08 +0000211 /// addVariable - Add a variable to the scope.
Bill Wendling2f921f82009-05-15 09:23:25 +0000212 ///
Devang Patel930143b2009-11-21 02:48:08 +0000213 void addVariable(DbgVariable *V) { Variables.push_back(V); }
Bill Wendling2f921f82009-05-15 09:23:25 +0000214
Devang Patel930143b2009-11-21 02:48:08 +0000215 void fixInstructionMarkers() {
Devang Patel75cc16c2009-10-01 20:31:14 +0000216 assert (getFirstInsn() && "First instruction is missing!");
217 if (getLastInsn())
218 return;
Jim Grosbach042483e2009-11-21 23:12:12 +0000219
Devang Patel75cc16c2009-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 Patel930143b2009-11-21 02:48:08 +0000226 L->fixInstructionMarkers();
Devang Patel75cc16c2009-10-01 20:31:14 +0000227 setLastInsn(L->getLastInsn());
228 }
229
Bill Wendling2f921f82009-05-15 09:23:25 +0000230#ifndef NDEBUG
231 void dump() const;
232#endif
233};
234
235#ifndef NDEBUG
236void DbgScope::dump() const {
Chris Lattner81e8e022009-08-23 00:51:00 +0000237 raw_ostream &err = errs();
238 err.indent(IndentLevel);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000239 MDNode *N = Desc.getNode();
240 N->dump();
Chris Lattner81e8e022009-08-23 00:51:00 +0000241 err << " [" << StartLabelID << ", " << EndLabelID << "]\n";
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000242 if (AbstractScope)
243 err << "Abstract Scope\n";
Bill Wendling2f921f82009-05-15 09:23:25 +0000244
245 IndentLevel += 2;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000246 if (!Scopes.empty())
247 err << "Children ...\n";
Bill Wendling2f921f82009-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 Wendling2f921f82009-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 Wendling2f921f82009-05-15 09:23:25 +0000261}
262
263} // end llvm namespace
264
Chris Lattner7b26fce2009-08-22 20:48:53 +0000265DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T)
Devang Patel40d78412009-06-29 20:45:18 +0000266 : Dwarf(OS, A, T, "dbg"), ModuleCU(0),
Bill Wendling2f921f82009-05-15 09:23:25 +0000267 AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(),
Devang Patel930143b2009-11-21 02:48:08 +0000268 DIEValues(), StringPool(),
Bill Wendling2f921f82009-05-15 09:23:25 +0000269 SectionSourceLines(), didInitial(false), shouldEmit(false),
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000270 CurrentFnDbgScope(0), DebugTimer(0) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000271 if (TimePassesIsEnabled)
272 DebugTimer = new Timer("Dwarf Debug Writer",
273 getDwarfTimerGroup());
274}
275DwarfDebug::~DwarfDebug() {
Devang Patel930143b2009-11-21 02:48:08 +0000276 for (unsigned j = 0, M = DIEValues.size(); j < M; ++j)
277 delete DIEValues[j];
Bill Wendling2f921f82009-05-15 09:23:25 +0000278
Bill Wendling2f921f82009-05-15 09:23:25 +0000279 delete DebugTimer;
280}
281
Devang Patel930143b2009-11-21 02:48:08 +0000282/// assignAbbrevNumber - Define a unique number for the abbreviation.
Bill Wendling2f921f82009-05-15 09:23:25 +0000283///
Devang Patel930143b2009-11-21 02:48:08 +0000284void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-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 Patel930143b2009-11-21 02:48:08 +0000305/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
Bill Wendlingbcad77a2009-05-20 23:24:48 +0000306/// information entry.
Devang Patel930143b2009-11-21 02:48:08 +0000307DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Devang Patel92e8c652009-11-21 00:31:03 +0000308 DIEEntry *Value = new DIEEntry(Entry);
Devang Patel930143b2009-11-21 02:48:08 +0000309 DIEValues.push_back(Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000310 return Value;
311}
312
Devang Patel930143b2009-11-21 02:48:08 +0000313/// addUInt - Add an unsigned integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000314///
Devang Patel930143b2009-11-21 02:48:08 +0000315void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000316 unsigned Form, uint64_t Integer) {
317 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Devang Patel92e8c652009-11-21 00:31:03 +0000318 DIEValue *Value = new DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000319 DIEValues.push_back(Value);
320 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000321}
322
Devang Patel930143b2009-11-21 02:48:08 +0000323/// addSInt - Add an signed integer attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000324///
Devang Patel930143b2009-11-21 02:48:08 +0000325void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-05-15 09:23:25 +0000326 unsigned Form, int64_t Integer) {
327 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Devang Patel92e8c652009-11-21 00:31:03 +0000328 DIEValue *Value = new DIEInteger(Integer);
Devang Patel930143b2009-11-21 02:48:08 +0000329 DIEValues.push_back(Value);
330 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000331}
332
Devang Patel8c339592009-12-02 15:25:16 +0000333/// addString - Add a string attribute data and value. DIEString only
334/// keeps string reference.
Devang Patel930143b2009-11-21 02:48:08 +0000335void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
Devang Patelb3e01682009-11-24 19:42:17 +0000336 const StringRef String) {
Devang Patel92e8c652009-11-21 00:31:03 +0000337 DIEValue *Value = new DIEString(String);
Devang Patel930143b2009-11-21 02:48:08 +0000338 DIEValues.push_back(Value);
339 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000340}
341
Devang Patel930143b2009-11-21 02:48:08 +0000342/// addLabel - Add a Dwarf label attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000343///
Devang Patel930143b2009-11-21 02:48:08 +0000344void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling2f921f82009-05-15 09:23:25 +0000345 const DWLabel &Label) {
Devang Patel92e8c652009-11-21 00:31:03 +0000346 DIEValue *Value = new DIEDwarfLabel(Label);
Devang Patel930143b2009-11-21 02:48:08 +0000347 DIEValues.push_back(Value);
348 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000349}
350
Devang Patel930143b2009-11-21 02:48:08 +0000351/// addObjectLabel - Add an non-Dwarf label attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000352///
Devang Patel930143b2009-11-21 02:48:08 +0000353void DwarfDebug::addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling2f921f82009-05-15 09:23:25 +0000354 const std::string &Label) {
Devang Patel92e8c652009-11-21 00:31:03 +0000355 DIEValue *Value = new DIEObjectLabel(Label);
Devang Patel930143b2009-11-21 02:48:08 +0000356 DIEValues.push_back(Value);
357 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000358}
359
Devang Patel930143b2009-11-21 02:48:08 +0000360/// addSectionOffset - Add a section offset label attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000361///
Devang Patel930143b2009-11-21 02:48:08 +0000362void DwarfDebug::addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling2f921f82009-05-15 09:23:25 +0000363 const DWLabel &Label, const DWLabel &Section,
364 bool isEH, bool useSet) {
Devang Patel92e8c652009-11-21 00:31:03 +0000365 DIEValue *Value = new DIESectionOffset(Label, Section, isEH, useSet);
Devang Patel930143b2009-11-21 02:48:08 +0000366 DIEValues.push_back(Value);
367 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000368}
369
Devang Patel930143b2009-11-21 02:48:08 +0000370/// addDelta - Add a label delta attribute data and value.
Bill Wendling2f921f82009-05-15 09:23:25 +0000371///
Devang Patel930143b2009-11-21 02:48:08 +0000372void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling2f921f82009-05-15 09:23:25 +0000373 const DWLabel &Hi, const DWLabel &Lo) {
Devang Patel92e8c652009-11-21 00:31:03 +0000374 DIEValue *Value = new DIEDelta(Hi, Lo);
Devang Patel930143b2009-11-21 02:48:08 +0000375 DIEValues.push_back(Value);
376 Die->addValue(Attribute, Form, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000377}
378
Devang Patel930143b2009-11-21 02:48:08 +0000379/// addBlock - Add block data.
Bill Wendling2f921f82009-05-15 09:23:25 +0000380///
Devang Patel930143b2009-11-21 02:48:08 +0000381void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
Bill Wendling2f921f82009-05-15 09:23:25 +0000382 DIEBlock *Block) {
383 Block->ComputeSize(TD);
Devang Patel930143b2009-11-21 02:48:08 +0000384 DIEValues.push_back(Block);
385 Die->addValue(Attribute, Block->BestForm(), Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000386}
387
Devang Patel930143b2009-11-21 02:48:08 +0000388/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000389/// entry.
Devang Patel930143b2009-11-21 02:48:08 +0000390void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
Bill Wendling2f921f82009-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 Patel930143b2009-11-21 02:48:08 +0000396 unsigned FileID = findCompileUnit(V->getCompileUnit()).getID();
Bill Wendling2f921f82009-05-15 09:23:25 +0000397 assert(FileID && "Invalid file id");
Devang Patel930143b2009-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 Wendling2f921f82009-05-15 09:23:25 +0000400}
401
Devang Patel930143b2009-11-21 02:48:08 +0000402/// addSourceLine - Add location information to specified debug information
Bill Wendling2f921f82009-05-15 09:23:25 +0000403/// entry.
Devang Patel930143b2009-11-21 02:48:08 +0000404void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
Bill Wendling2f921f82009-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 Patel930143b2009-11-21 02:48:08 +0000410 unsigned FileID = findCompileUnit(G->getCompileUnit()).getID();
Bill Wendling2f921f82009-05-15 09:23:25 +0000411 assert(FileID && "Invalid file id");
Devang Patel930143b2009-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 Wendling2f921f82009-05-15 09:23:25 +0000414}
Devang Patelb2de5fa2009-08-31 22:47:13 +0000415
Devang Patel930143b2009-11-21 02:48:08 +0000416/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000417/// entry.
Devang Patel930143b2009-11-21 02:48:08 +0000418void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
Devang Patelb2de5fa2009-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 Tice183a5192009-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 Patelb2de5fa2009-08-31 22:47:13 +0000426
427 unsigned Line = SP->getLineNumber();
Devang Patel930143b2009-11-21 02:48:08 +0000428 unsigned FileID = findCompileUnit(SP->getCompileUnit()).getID();
Devang Patelb2de5fa2009-08-31 22:47:13 +0000429 assert(FileID && "Invalid file id");
Devang Patel930143b2009-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 Patelb2de5fa2009-08-31 22:47:13 +0000432}
433
Devang Patel930143b2009-11-21 02:48:08 +0000434/// addSourceLine - Add location information to specified debug information
Devang Patelb2de5fa2009-08-31 22:47:13 +0000435/// entry.
Devang Patel930143b2009-11-21 02:48:08 +0000436void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
Bill Wendling2f921f82009-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 Patel930143b2009-11-21 02:48:08 +0000443 unsigned FileID = findCompileUnit(CU).getID();
Bill Wendling2f921f82009-05-15 09:23:25 +0000444 assert(FileID && "Invalid file id");
Devang Patel930143b2009-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 Wendling2f921f82009-05-15 09:23:25 +0000447}
448
Caroline Ticec87c1e22009-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 Patel930143b2009-11-21 02:48:08 +0000472 value of the variable. The function addBlockByrefType does this. */
Caroline Ticec87c1e22009-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 Patel930143b2009-11-21 02:48:08 +0000477DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
Caroline Ticec87c1e22009-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 Stump14cf8ec2009-09-30 00:08:22 +0000483 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticec87c1e22009-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 Patel2d9caf92009-11-25 17:36:49 +0000497 if (Name == DT.getName())
Caroline Ticec87c1e22009-08-31 21:19:37 +0000498 return (DT.getTypeDerivedFrom());
499 }
500
501 return Ty;
502}
503
Devang Patel930143b2009-11-21 02:48:08 +0000504/// addComplexAddress - Start with the address based on the location provided,
Mike Stump14cf8ec2009-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 Patel930143b2009-11-21 02:48:08 +0000509void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
Mike Stump14cf8ec2009-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 Patel930143b2009-11-21 02:48:08 +0000522 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000523 } else {
524 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel930143b2009-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 Stump14cf8ec2009-09-30 00:08:22 +0000527 }
528 } else {
529 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000530 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000531 else {
Devang Patel930143b2009-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 Stump14cf8ec2009-09-30 00:08:22 +0000534 }
535
Devang Patel930143b2009-11-21 02:48:08 +0000536 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Mike Stump14cf8ec2009-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 Patel930143b2009-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 Stump14cf8ec2009-09-30 00:08:22 +0000545 } else if (Element == DIFactory::OpDeref) {
Devang Patel930143b2009-11-21 02:48:08 +0000546 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000547 } else llvm_unreachable("unknown DIFactory Opcode");
548 }
549
550 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000551 addBlock(Die, Attribute, 0, Block);
Mike Stump14cf8ec2009-09-30 00:08:22 +0000552}
553
Caroline Ticec87c1e22009-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 Patel930143b2009-11-21 02:48:08 +0000563 The function getBlockByrefType dives into the __Block_byref_x_VarName
Caroline Ticec87c1e22009-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 Dunbarc418d6b2009-09-19 20:40:05 +0000568 expression that explains to the debugger how to navigate through the
Caroline Ticec87c1e22009-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 Patel930143b2009-11-21 02:48:08 +0000608/// addBlockByrefAddress - Start with the address based on the location
Caroline Ticec87c1e22009-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 Patel930143b2009-11-21 02:48:08 +0000614void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000615 unsigned Attribute,
616 const MachineLocation &Location) {
Caroline Ticec87c1e22009-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 Patel2d9caf92009-11-25 17:36:49 +0000623 StringRef varName = VD.getName();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000624
625 if (Tag == dwarf::DW_TAG_pointer_type) {
Mike Stump14cf8ec2009-09-30 00:08:22 +0000626 DIDerivedType DTy = DIDerivedType(Ty.getNode());
Caroline Ticec87c1e22009-08-31 21:19:37 +0000627 TmpTy = DTy.getTypeDerivedFrom();
628 isPointer = true;
629 }
630
631 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
632
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000633 // Find the __forwarding field and the variable field in the __Block_byref
634 // struct.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000635 DIArray Fields = blockStruct.getTypeArray();
636 DIDescriptor varField = DIDescriptor();
637 DIDescriptor forwardingField = DIDescriptor();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000638
639
Daniel Dunbarbe22ec42009-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 Patel2d9caf92009-11-25 17:36:49 +0000643 StringRef fieldName = DT.getName();
644 if (fieldName == "__forwarding")
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000645 forwardingField = Element;
Devang Patel2d9caf92009-11-25 17:36:49 +0000646 else if (fieldName == varName)
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000647 varField = Element;
648 }
Daniel Dunbarc418d6b2009-09-19 20:40:05 +0000649
Mike Stump944fa252009-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 Ticec87c1e22009-08-31 21:19:37 +0000653
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000654 // Get the offsets for the forwarding field and the variable field.
Daniel Dunbarbe22ec42009-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 Ticec87c1e22009-08-31 21:19:37 +0000659
Mike Stump944fa252009-09-24 23:21:26 +0000660 // Decode the original location, and use that as the start of the byref
661 // variable's location.
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000662 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
663 DIEBlock *Block = new DIEBlock();
Caroline Ticec87c1e22009-08-31 21:19:37 +0000664
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000665 if (Location.isReg()) {
666 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000667 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000668 else {
669 Reg = Reg - dwarf::DW_OP_reg0;
Devang Patel930143b2009-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 Dunbarbe22ec42009-09-19 20:40:14 +0000672 }
673 } else {
674 if (Reg < 32)
Devang Patel930143b2009-11-21 02:48:08 +0000675 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000676 else {
Devang Patel930143b2009-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 Dunbarbe22ec42009-09-19 20:40:14 +0000679 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000680
Devang Patel930143b2009-11-21 02:48:08 +0000681 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000682 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000683
Mike Stump944fa252009-09-24 23:21:26 +0000684 // If we started with a pointer to the __Block_byref... struct, then
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000685 // the first thing we need to do is dereference the pointer (DW_OP_deref).
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000686 if (isPointer)
Devang Patel930143b2009-11-21 02:48:08 +0000687 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000688
Daniel Dunbarbe22ec42009-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 Dunbarbe22ec42009-09-19 20:40:14 +0000692 if (forwardingFieldOffset > 0) {
Devang Patel930143b2009-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 Dunbarbe22ec42009-09-19 20:40:14 +0000695 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000696
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000697 // Now dereference the __forwarding field to get to the real __Block_byref
698 // struct: DW_OP_deref.
Devang Patel930143b2009-11-21 02:48:08 +0000699 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000700
Daniel Dunbarbe22ec42009-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 Dunbarbe22ec42009-09-19 20:40:14 +0000704 if (varFieldOffset > 0) {
Devang Patel930143b2009-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 Dunbarbe22ec42009-09-19 20:40:14 +0000707 }
Caroline Ticec87c1e22009-08-31 21:19:37 +0000708
Daniel Dunbarbe22ec42009-09-19 20:40:14 +0000709 // Now attach the location information to the DIE.
Devang Patel930143b2009-11-21 02:48:08 +0000710 addBlock(Die, Attribute, 0, Block);
Caroline Ticec87c1e22009-08-31 21:19:37 +0000711}
712
Devang Patel930143b2009-11-21 02:48:08 +0000713/// addAddress - Add an address attribute to a die based on the location
Bill Wendling2f921f82009-05-15 09:23:25 +0000714/// provided.
Devang Patel930143b2009-11-21 02:48:08 +0000715void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
Bill Wendling2f921f82009-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 Patel930143b2009-11-21 02:48:08 +0000722 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000723 } else {
Devang Patel930143b2009-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 Wendling2f921f82009-05-15 09:23:25 +0000726 }
727 } else {
728 if (Reg < 32) {
Devang Patel930143b2009-11-21 02:48:08 +0000729 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000730 } else {
Devang Patel930143b2009-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 Wendling2f921f82009-05-15 09:23:25 +0000733 }
734
Devang Patel930143b2009-11-21 02:48:08 +0000735 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
Bill Wendling2f921f82009-05-15 09:23:25 +0000736 }
737
Devang Patel930143b2009-11-21 02:48:08 +0000738 addBlock(Die, Attribute, 0, Block);
Bill Wendling2f921f82009-05-15 09:23:25 +0000739}
740
Devang Patel930143b2009-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 Wendling2f921f82009-05-15 09:23:25 +0000743 if (Ty.isNull())
744 return;
745
746 // Check for pre-existence.
Devang Patel930143b2009-11-21 02:48:08 +0000747 DIEEntry *Entry = DW_Unit->getDIEEntry(Ty.getNode());
Bill Wendling2f921f82009-05-15 09:23:25 +0000748
749 // If it exists then use the existing value.
Devang Patel930143b2009-11-21 02:48:08 +0000750 if (Entry) {
751 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000752 return;
753 }
754
755 // Set up proxy.
Devang Patel930143b2009-11-21 02:48:08 +0000756 Entry = createDIEEntry();
757 DW_Unit->insertDIEEntry(Ty.getNode(), Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000758
759 // Construct type.
Devang Patel92e8c652009-11-21 00:31:03 +0000760 DIE *Buffer = new DIE(dwarf::DW_TAG_base_type);
Devang Patel3b666fe2009-12-03 23:46:57 +0000761 ModuleCU->insertDIE(Ty.getNode(), Buffer);
Devang Patel9fda4bd2009-08-31 18:49:10 +0000762 if (Ty.isBasicType())
Devang Patel930143b2009-11-21 02:48:08 +0000763 constructTypeDIE(DW_Unit, *Buffer, DIBasicType(Ty.getNode()));
Devang Patel9fda4bd2009-08-31 18:49:10 +0000764 else if (Ty.isCompositeType())
Devang Patel930143b2009-11-21 02:48:08 +0000765 constructTypeDIE(DW_Unit, *Buffer, DICompositeType(Ty.getNode()));
Bill Wendling2f921f82009-05-15 09:23:25 +0000766 else {
Devang Patel9fda4bd2009-08-31 18:49:10 +0000767 assert(Ty.isDerivedType() && "Unknown kind of DIType");
Devang Patel930143b2009-11-21 02:48:08 +0000768 constructTypeDIE(DW_Unit, *Buffer, DIDerivedType(Ty.getNode()));
Bill Wendling2f921f82009-05-15 09:23:25 +0000769 }
770
771 // Add debug information entry to entity and appropriate context.
772 DIE *Die = NULL;
773 DIDescriptor Context = Ty.getContext();
774 if (!Context.isNull())
Devang Patele064ad42009-11-20 21:37:22 +0000775 Die = DW_Unit->getDIE(Context.getNode());
Bill Wendling2f921f82009-05-15 09:23:25 +0000776
Jim Grosbach042483e2009-11-21 23:12:12 +0000777 if (Die)
Devang Patel930143b2009-11-21 02:48:08 +0000778 Die->addChild(Buffer);
Jim Grosbach042483e2009-11-21 23:12:12 +0000779 else
Devang Patel930143b2009-11-21 02:48:08 +0000780 DW_Unit->addDie(Buffer);
781 Entry->setEntry(Buffer);
782 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Bill Wendling2f921f82009-05-15 09:23:25 +0000783}
784
Devang Patel930143b2009-11-21 02:48:08 +0000785/// constructTypeDIE - Construct basic type die from DIBasicType.
786void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling2f921f82009-05-15 09:23:25 +0000787 DIBasicType BTy) {
788 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000789 StringRef Name = BTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000790 Buffer.setTag(dwarf::DW_TAG_base_type);
Devang Patel930143b2009-11-21 02:48:08 +0000791 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Bill Wendling2f921f82009-05-15 09:23:25 +0000792 BTy.getEncoding());
793
794 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +0000795 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000796 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000797 uint64_t Size = BTy.getSizeInBits() >> 3;
Devang Patel930143b2009-11-21 02:48:08 +0000798 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000799}
800
Devang Patel930143b2009-11-21 02:48:08 +0000801/// constructTypeDIE - Construct derived type die from DIDerivedType.
802void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling2f921f82009-05-15 09:23:25 +0000803 DIDerivedType DTy) {
804 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000805 StringRef Name = DTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000806 uint64_t Size = DTy.getSizeInBits() >> 3;
807 unsigned Tag = DTy.getTag();
808
809 // FIXME - Workaround for templates.
810 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
811
812 Buffer.setTag(Tag);
813
814 // Map to main type, void will not have a type.
815 DIType FromTy = DTy.getTypeDerivedFrom();
Devang Patel930143b2009-11-21 02:48:08 +0000816 addType(DW_Unit, &Buffer, FromTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000817
818 // Add name if not anonymous or intermediate type.
Devang Patelae466ef2009-11-30 23:56:56 +0000819 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000820 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000821
822 // Add size if non-zero (derived types might be zero-sized.)
823 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +0000824 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000825
826 // Add source line info if available and TyDesc is not a forward declaration.
Devang Patelb5b51592009-11-23 18:43:37 +0000827 if (!DTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +0000828 addSourceLine(&Buffer, &DTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000829}
830
Devang Patel930143b2009-11-21 02:48:08 +0000831/// constructTypeDIE - Construct type DIE from DICompositeType.
832void DwarfDebug::constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling2f921f82009-05-15 09:23:25 +0000833 DICompositeType CTy) {
834 // Get core information.
Devang Patel2d9caf92009-11-25 17:36:49 +0000835 StringRef Name = CTy.getName();
Bill Wendling2f921f82009-05-15 09:23:25 +0000836
837 uint64_t Size = CTy.getSizeInBits() >> 3;
838 unsigned Tag = CTy.getTag();
839 Buffer.setTag(Tag);
840
841 switch (Tag) {
842 case dwarf::DW_TAG_vector_type:
843 case dwarf::DW_TAG_array_type:
Devang Patel930143b2009-11-21 02:48:08 +0000844 constructArrayTypeDIE(DW_Unit, Buffer, &CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000845 break;
846 case dwarf::DW_TAG_enumeration_type: {
847 DIArray Elements = CTy.getTypeArray();
848
849 // Add enumerators to enumeration type.
850 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
851 DIE *ElemDie = NULL;
Devang Patel80ae3492009-08-28 23:24:31 +0000852 DIEnumerator Enum(Elements.getElement(i).getNode());
Devang Patelfafa1fe2009-10-09 17:51:49 +0000853 if (!Enum.isNull()) {
Devang Patel930143b2009-11-21 02:48:08 +0000854 ElemDie = constructEnumTypeDIE(DW_Unit, &Enum);
855 Buffer.addChild(ElemDie);
Devang Patelfafa1fe2009-10-09 17:51:49 +0000856 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000857 }
858 }
859 break;
860 case dwarf::DW_TAG_subroutine_type: {
861 // Add return type.
862 DIArray Elements = CTy.getTypeArray();
863 DIDescriptor RTy = Elements.getElement(0);
Devang Patel930143b2009-11-21 02:48:08 +0000864 addType(DW_Unit, &Buffer, DIType(RTy.getNode()));
Bill Wendling2f921f82009-05-15 09:23:25 +0000865
866 // Add prototype flag.
Devang Patel930143b2009-11-21 02:48:08 +0000867 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +0000868
869 // Add arguments.
870 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
871 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
872 DIDescriptor Ty = Elements.getElement(i);
Devang Patel930143b2009-11-21 02:48:08 +0000873 addType(DW_Unit, Arg, DIType(Ty.getNode()));
874 Buffer.addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +0000875 }
876 }
877 break;
878 case dwarf::DW_TAG_structure_type:
879 case dwarf::DW_TAG_union_type:
880 case dwarf::DW_TAG_class_type: {
881 // Add elements to structure type.
882 DIArray Elements = CTy.getTypeArray();
883
884 // A forward struct declared type may not have elements available.
885 if (Elements.isNull())
886 break;
887
888 // Add elements to structure type.
889 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
890 DIDescriptor Element = Elements.getElement(i);
Devang Patel80ae3492009-08-28 23:24:31 +0000891 if (Element.isNull())
892 continue;
Bill Wendling2f921f82009-05-15 09:23:25 +0000893 DIE *ElemDie = NULL;
894 if (Element.getTag() == dwarf::DW_TAG_subprogram)
Devang Patel236526d2009-12-03 01:25:38 +0000895 ElemDie = createMemberSubprogramDIE(DW_Unit,
896 DISubprogram(Element.getNode()));
Bill Wendling2f921f82009-05-15 09:23:25 +0000897 else
Devang Patel930143b2009-11-21 02:48:08 +0000898 ElemDie = createMemberDIE(DW_Unit,
Devang Patel80ae3492009-08-28 23:24:31 +0000899 DIDerivedType(Element.getNode()));
Devang Patel930143b2009-11-21 02:48:08 +0000900 Buffer.addChild(ElemDie);
Bill Wendling2f921f82009-05-15 09:23:25 +0000901 }
902
Devang Patel3082c012009-08-27 23:51:51 +0000903 if (CTy.isAppleBlockExtension())
Devang Patel930143b2009-11-21 02:48:08 +0000904 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +0000905
906 unsigned RLang = CTy.getRunTimeLang();
907 if (RLang)
Devang Patel930143b2009-11-21 02:48:08 +0000908 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
Bill Wendling2f921f82009-05-15 09:23:25 +0000909 dwarf::DW_FORM_data1, RLang);
910 break;
911 }
912 default:
913 break;
914 }
915
916 // Add name if not anonymous or intermediate type.
Devang Patel2d9caf92009-11-25 17:36:49 +0000917 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +0000918 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000919
920 if (Tag == dwarf::DW_TAG_enumeration_type ||
921 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
922 // Add size if non-zero (derived types might be zero-sized.)
923 if (Size)
Devang Patel930143b2009-11-21 02:48:08 +0000924 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Bill Wendling2f921f82009-05-15 09:23:25 +0000925 else {
926 // Add zero size if it is not a forward declaration.
927 if (CTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +0000928 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +0000929 else
Devang Patel930143b2009-11-21 02:48:08 +0000930 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
Bill Wendling2f921f82009-05-15 09:23:25 +0000931 }
932
933 // Add source line info if available.
934 if (!CTy.isForwardDecl())
Devang Patel930143b2009-11-21 02:48:08 +0000935 addSourceLine(&Buffer, &CTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000936 }
937}
938
Devang Patel930143b2009-11-21 02:48:08 +0000939/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
940void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
Bill Wendling2f921f82009-05-15 09:23:25 +0000941 int64_t L = SR.getLo();
942 int64_t H = SR.getHi();
943 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
944
Devang Patel930143b2009-11-21 02:48:08 +0000945 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patelf691df32009-08-14 20:59:16 +0000946 if (L)
Devang Patel930143b2009-11-21 02:48:08 +0000947 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Devang Patel8f046022009-12-04 23:10:24 +0000948 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Bill Wendling2f921f82009-05-15 09:23:25 +0000949
Devang Patel930143b2009-11-21 02:48:08 +0000950 Buffer.addChild(DW_Subrange);
Bill Wendling2f921f82009-05-15 09:23:25 +0000951}
952
Devang Patel930143b2009-11-21 02:48:08 +0000953/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
954void DwarfDebug::constructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
Bill Wendling2f921f82009-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 Patel930143b2009-11-21 02:48:08 +0000958 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +0000959
960 // Emit derived type.
Devang Patel930143b2009-11-21 02:48:08 +0000961 addType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +0000962 DIArray Elements = CTy->getTypeArray();
963
Devang Patel92e8c652009-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 Patel930143b2009-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 Patel92e8c652009-11-21 00:31:03 +0000971 dwarf::DW_ATE_signed);
Devang Patel930143b2009-11-21 02:48:08 +0000972 DW_Unit->addDie(IdxTy);
Devang Patel92e8c652009-11-21 00:31:03 +0000973 DW_Unit->setIndexTyDie(IdxTy);
974 }
Bill Wendling2f921f82009-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 Patel930143b2009-11-21 02:48:08 +0000980 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
Bill Wendling2f921f82009-05-15 09:23:25 +0000981 }
982}
983
Devang Patel930143b2009-11-21 02:48:08 +0000984/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
985DIE *DwarfDebug::constructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000986 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Devang Patel2d9caf92009-11-25 17:36:49 +0000987 StringRef Name = ETy->getName();
Devang Patel930143b2009-11-21 02:48:08 +0000988 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +0000989 int64_t Value = ETy->getEnumValue();
Devang Patel930143b2009-11-21 02:48:08 +0000990 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
Bill Wendling2f921f82009-05-15 09:23:25 +0000991 return Enumerator;
992}
993
Devang Patel930143b2009-11-21 02:48:08 +0000994/// createGlobalVariableDIE - Create new DIE using GV.
995DIE *DwarfDebug::createGlobalVariableDIE(CompileUnit *DW_Unit,
Bill Wendling2f921f82009-05-15 09:23:25 +0000996 const DIGlobalVariable &GV) {
Jim Grosbach00e9c612009-11-22 19:20:36 +0000997 // If the global variable was optmized out then no need to create debug info
998 // entry.
Devang Patelcc11371b2009-11-06 17:58:12 +0000999 if (!GV.getGlobal()) return NULL;
Devang Patel2d9caf92009-11-25 17:36:49 +00001000 if (GV.getDisplayName().empty()) return NULL;
Devang Patel06ce6502009-11-06 01:30:04 +00001001
Bill Wendling2f921f82009-05-15 09:23:25 +00001002 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
Jim Grosbach042483e2009-11-21 23:12:12 +00001003 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
Devang Patelb2969422009-09-29 18:40:58 +00001004 GV.getDisplayName());
1005
Devang Patel2d9caf92009-11-25 17:36:49 +00001006 StringRef LinkageName = GV.getLinkageName();
1007 if (!LinkageName.empty()) {
Chris Lattner4b7dadb2009-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 Patelf281bf82009-07-16 01:01:22 +00001012 if (LinkageName[0] == 1)
Benjamin Kramer4cd30812009-11-25 18:26:09 +00001013 LinkageName = LinkageName.substr(1);
Devang Patel930143b2009-11-21 02:48:08 +00001014 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelcdfc8582009-07-14 00:55:28 +00001015 LinkageName);
Devang Patelf281bf82009-07-16 01:01:22 +00001016 }
Devang Patel930143b2009-11-21 02:48:08 +00001017 addType(DW_Unit, GVDie, GV.getType());
Bill Wendling2f921f82009-05-15 09:23:25 +00001018 if (!GV.isLocalToUnit())
Devang Patel930143b2009-11-21 02:48:08 +00001019 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1020 addSourceLine(GVDie, &GV);
Devang Patel4144a822009-10-05 23:22:08 +00001021
1022 // Add address.
1023 DIEBlock *Block = new DIEBlock();
Devang Patel930143b2009-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 Patel4144a822009-10-05 23:22:08 +00001026 Asm->Mang->getMangledName(GV.getGlobal()));
Devang Patel930143b2009-11-21 02:48:08 +00001027 addBlock(GVDie, dwarf::DW_AT_location, 0, Block);
Devang Patel4144a822009-10-05 23:22:08 +00001028
Bill Wendling2f921f82009-05-15 09:23:25 +00001029 return GVDie;
1030}
1031
Devang Patel930143b2009-11-21 02:48:08 +00001032/// createMemberDIE - Create new member DIE.
1033DIE *DwarfDebug::createMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){
Bill Wendling2f921f82009-05-15 09:23:25 +00001034 DIE *MemberDie = new DIE(DT.getTag());
Devang Patel2d9caf92009-11-25 17:36:49 +00001035 StringRef Name = DT.getName();
1036 if (!Name.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001037 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Devang Patel2d9caf92009-11-25 17:36:49 +00001038
Devang Patel930143b2009-11-21 02:48:08 +00001039 addType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
Bill Wendling2f921f82009-05-15 09:23:25 +00001040
Devang Patel930143b2009-11-21 02:48:08 +00001041 addSourceLine(MemberDie, &DT);
Bill Wendling2f921f82009-05-15 09:23:25 +00001042
Devang Patel67f56f02009-11-04 22:06:12 +00001043 DIEBlock *MemLocationDie = new DIEBlock();
Devang Patel930143b2009-11-21 02:48:08 +00001044 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel67f56f02009-11-04 22:06:12 +00001045
Bill Wendling2f921f82009-05-15 09:23:25 +00001046 uint64_t Size = DT.getSizeInBits();
Devang Patelf05d5722009-11-04 23:48:00 +00001047 uint64_t FieldSize = DT.getOriginalTypeSize();
Bill Wendling2f921f82009-05-15 09:23:25 +00001048
1049 if (Size != FieldSize) {
1050 // Handle bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001051 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1052 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
Bill Wendling2f921f82009-05-15 09:23:25 +00001053
1054 uint64_t Offset = DT.getOffsetInBits();
1055 uint64_t FieldOffset = Offset;
1056 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1057 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1058 FieldOffset = (HiMark - FieldSize);
1059 Offset -= FieldOffset;
1060
1061 // Maybe we need to work from the other end.
1062 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
Devang Patel930143b2009-11-21 02:48:08 +00001063 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
Bill Wendling2f921f82009-05-15 09:23:25 +00001064
Devang Patel67f56f02009-11-04 22:06:12 +00001065 // Here WD_AT_data_member_location points to the anonymous
1066 // field that includes this bit field.
Devang Patel930143b2009-11-21 02:48:08 +00001067 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001068
1069 } else
1070 // This is not a bitfield.
Devang Patel930143b2009-11-21 02:48:08 +00001071 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
Devang Patel67f56f02009-11-04 22:06:12 +00001072
Devang Patel930143b2009-11-21 02:48:08 +00001073 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001074
1075 if (DT.isProtected())
Devang Pateleb57c592009-12-03 19:11:07 +00001076 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001077 dwarf::DW_ACCESS_protected);
1078 else if (DT.isPrivate())
Devang Pateleb57c592009-12-03 19:11:07 +00001079 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
Bill Wendling2f921f82009-05-15 09:23:25 +00001080 dwarf::DW_ACCESS_private);
Devang Pateleb57c592009-12-03 19:11:07 +00001081 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1082 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1083 dwarf::DW_ACCESS_public);
1084 if (DT.isVirtual())
1085 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1086 dwarf::DW_VIRTUALITY_virtual);
Bill Wendling2f921f82009-05-15 09:23:25 +00001087 return MemberDie;
1088}
1089
Devang Patel236526d2009-12-03 01:25:38 +00001090/// createRawSubprogramDIE - Create new partially incomplete DIE. This is
1091/// a helper routine used by createMemberSubprogramDIE and
1092/// createSubprogramDIE.
1093DIE *DwarfDebug::createRawSubprogramDIE(CompileUnit *DW_Unit,
1094 const DISubprogram &SP) {
1095 DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel2d9caf92009-11-25 17:36:49 +00001096 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Bill Wendling2f921f82009-05-15 09:23:25 +00001097
Devang Patel2d9caf92009-11-25 17:36:49 +00001098 StringRef LinkageName = SP.getLinkageName();
1099 if (!LinkageName.empty()) {
Jim Grosbach00e9c612009-11-22 19:20:36 +00001100 // Skip special LLVM prefix that is used to inform the asm printer to not
1101 // emit usual symbol prefix before the symbol name. This happens for
1102 // Objective-C symbol names and symbol whose name is replaced using GCC's
1103 // __asm__ attribute.
Devang Patelf281bf82009-07-16 01:01:22 +00001104 if (LinkageName[0] == 1)
Benjamin Kramer4cd30812009-11-25 18:26:09 +00001105 LinkageName = LinkageName.substr(1);
Devang Patel930143b2009-11-21 02:48:08 +00001106 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
Devang Patelcdfc8582009-07-14 00:55:28 +00001107 LinkageName);
Devang Patelf281bf82009-07-16 01:01:22 +00001108 }
Devang Patel930143b2009-11-21 02:48:08 +00001109 addSourceLine(SPDie, &SP);
Bill Wendling2f921f82009-05-15 09:23:25 +00001110
Bill Wendling2f921f82009-05-15 09:23:25 +00001111 // Add prototyped tag, if C or ObjC.
1112 unsigned Lang = SP.getCompileUnit().getLanguage();
1113 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1114 Lang == dwarf::DW_LANG_ObjC)
Devang Patel930143b2009-11-21 02:48:08 +00001115 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001116
1117 // Add Return Type.
Devang Patel236526d2009-12-03 01:25:38 +00001118 DICompositeType SPTy = SP.getType();
1119 DIArray Args = SPTy.getTypeArray();
Bill Wendling2f921f82009-05-15 09:23:25 +00001120 unsigned SPTag = SPTy.getTag();
Devang Pateleb57c592009-12-03 19:11:07 +00001121
Devang Patel236526d2009-12-03 01:25:38 +00001122 if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type)
1123 addType(DW_Unit, SPDie, SPTy);
1124 else
1125 addType(DW_Unit, SPDie, DIType(Args.getElement(0).getNode()));
1126
Devang Pateleb57c592009-12-03 19:11:07 +00001127 unsigned VK = SP.getVirtuality();
1128 if (VK) {
1129 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
1130 DIEBlock *Block = new DIEBlock();
1131 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1132 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1133 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1134 ContainingTypeMap.insert(std::make_pair(SPDie, WeakVH(SP.getContainingType().getNode())));
1135 }
1136
Devang Patel236526d2009-12-03 01:25:38 +00001137 return SPDie;
1138}
1139
1140/// createMemberSubprogramDIE - Create new member DIE using SP. This routine
1141/// always returns a die with DW_AT_declaration attribute.
1142DIE *DwarfDebug::createMemberSubprogramDIE(CompileUnit *DW_Unit,
1143 const DISubprogram &SP) {
1144 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1145 if (!SPDie)
1146 SPDie = createSubprogramDIE(DW_Unit, SP);
1147
1148 // If SPDie has DW_AT_declaration then reuse it.
1149 if (!SP.isDefinition())
1150 return SPDie;
1151
1152 // Otherwise create new DIE for the declaration. First push definition
1153 // DIE at the top level.
1154 if (TopLevelDIEs.insert(SPDie))
1155 TopLevelDIEsVector.push_back(SPDie);
1156
1157 SPDie = createRawSubprogramDIE(DW_Unit, SP);
1158
1159 // Add arguments.
1160 DICompositeType SPTy = SP.getType();
1161 DIArray Args = SPTy.getTypeArray();
1162 unsigned SPTag = SPTy.getTag();
1163 if (SPTag == dwarf::DW_TAG_subroutine_type)
1164 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1165 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1166 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
1167 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1168 SPDie->addChild(Arg);
1169 }
1170
1171 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1172 return SPDie;
1173}
1174
1175/// createSubprogramDIE - Create new DIE using SP.
1176DIE *DwarfDebug::createSubprogramDIE(CompileUnit *DW_Unit,
1177 const DISubprogram &SP) {
1178 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1179 if (SPDie)
1180 return SPDie;
1181
1182 SPDie = createRawSubprogramDIE(DW_Unit, SP);
Bill Wendling2f921f82009-05-15 09:23:25 +00001183
1184 if (!SP.isDefinition()) {
Devang Patel930143b2009-11-21 02:48:08 +00001185 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
Bill Wendling2f921f82009-05-15 09:23:25 +00001186
1187 // Add arguments. Do not add arguments for subprogram definition. They will
Devang Patel236526d2009-12-03 01:25:38 +00001188 // be handled while processing variables.
1189 DICompositeType SPTy = SP.getType();
1190 DIArray Args = SPTy.getTypeArray();
1191 unsigned SPTag = SPTy.getTag();
1192
Bill Wendling2f921f82009-05-15 09:23:25 +00001193 if (SPTag == dwarf::DW_TAG_subroutine_type)
1194 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1195 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Devang Patel930143b2009-11-21 02:48:08 +00001196 addType(DW_Unit, Arg, DIType(Args.getElement(i).getNode()));
1197 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ??
1198 SPDie->addChild(Arg);
Bill Wendling2f921f82009-05-15 09:23:25 +00001199 }
1200 }
1201
Bill Wendling2f921f82009-05-15 09:23:25 +00001202 // DW_TAG_inlined_subroutine may refer to this DIE.
Devang Patele064ad42009-11-20 21:37:22 +00001203 DW_Unit->insertDIE(SP.getNode(), SPDie);
Bill Wendling2f921f82009-05-15 09:23:25 +00001204 return SPDie;
1205}
1206
Devang Patel930143b2009-11-21 02:48:08 +00001207/// findCompileUnit - Get the compile unit for the given descriptor.
Bill Wendling2f921f82009-05-15 09:23:25 +00001208///
Devang Patel930143b2009-11-21 02:48:08 +00001209CompileUnit &DwarfDebug::findCompileUnit(DICompileUnit Unit) const {
Bill Wendling2f921f82009-05-15 09:23:25 +00001210 DenseMap<Value *, CompileUnit *>::const_iterator I =
Devang Patel80ae3492009-08-28 23:24:31 +00001211 CompileUnitMap.find(Unit.getNode());
Bill Wendling2f921f82009-05-15 09:23:25 +00001212 assert(I != CompileUnitMap.end() && "Missing compile unit.");
1213 return *I->second;
1214}
1215
Devang Patel930143b2009-11-21 02:48:08 +00001216/// createDbgScopeVariable - Create a new scope variable.
Bill Wendling2f921f82009-05-15 09:23:25 +00001217///
Devang Patel930143b2009-11-21 02:48:08 +00001218DIE *DwarfDebug::createDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
Bill Wendling2f921f82009-05-15 09:23:25 +00001219 // Get the descriptor.
1220 const DIVariable &VD = DV->getVariable();
Devang Patel2d9caf92009-11-25 17:36:49 +00001221 StringRef Name = VD.getName();
1222 if (Name.empty())
Devang Patelffa7c072009-11-03 18:30:27 +00001223 return NULL;
Bill Wendling2f921f82009-05-15 09:23:25 +00001224
1225 // Translate tag to proper Dwarf tag. The result variable is dropped for
1226 // now.
1227 unsigned Tag;
1228 switch (VD.getTag()) {
1229 case dwarf::DW_TAG_return_variable:
1230 return NULL;
1231 case dwarf::DW_TAG_arg_variable:
1232 Tag = dwarf::DW_TAG_formal_parameter;
1233 break;
1234 case dwarf::DW_TAG_auto_variable: // fall thru
1235 default:
1236 Tag = dwarf::DW_TAG_variable;
1237 break;
1238 }
1239
1240 // Define variable debug information entry.
1241 DIE *VariableDie = new DIE(Tag);
Devang Patel930143b2009-11-21 02:48:08 +00001242 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Bill Wendling2f921f82009-05-15 09:23:25 +00001243
1244 // Add source line info if available.
Devang Patel930143b2009-11-21 02:48:08 +00001245 addSourceLine(VariableDie, &VD);
Bill Wendling2f921f82009-05-15 09:23:25 +00001246
1247 // Add variable type.
Jim Grosbach042483e2009-11-21 23:12:12 +00001248 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001249 // addresses instead.
Caroline Ticec87c1e22009-08-31 21:19:37 +00001250 if (VD.isBlockByrefVariable())
Devang Patel930143b2009-11-21 02:48:08 +00001251 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Caroline Ticec87c1e22009-08-31 21:19:37 +00001252 else
Devang Patel930143b2009-11-21 02:48:08 +00001253 addType(Unit, VariableDie, VD.getType());
Bill Wendling2f921f82009-05-15 09:23:25 +00001254
1255 // Add variable address.
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001256 // Variables for abstract instances of inlined functions don't get a
1257 // location.
1258 MachineLocation Location;
Jim Grosbachbf6d3582009-11-22 20:14:00 +00001259 unsigned FrameReg;
1260 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1261 Location.set(FrameReg, Offset);
Jim Grosbach042483e2009-11-21 23:12:12 +00001262
1263
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001264 if (VD.hasComplexAddress())
Devang Patel930143b2009-11-21 02:48:08 +00001265 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001266 else if (VD.isBlockByrefVariable())
Devang Patel930143b2009-11-21 02:48:08 +00001267 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001268 else
Devang Patel930143b2009-11-21 02:48:08 +00001269 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Bill Wendling2f921f82009-05-15 09:23:25 +00001270
1271 return VariableDie;
1272}
1273
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001274/// getUpdatedDbgScope - Find or create DbgScope assicated with the instruction.
1275/// Initialize scope and update scope hierarchy.
1276DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
1277 MDNode *InlinedAt) {
1278 assert (N && "Invalid Scope encoding!");
1279 assert (MI && "Missing machine instruction!");
1280 bool GetConcreteScope = (MI && InlinedAt);
1281
1282 DbgScope *NScope = NULL;
1283
1284 if (InlinedAt)
1285 NScope = DbgScopeMap.lookup(InlinedAt);
1286 else
1287 NScope = DbgScopeMap.lookup(N);
1288 assert (NScope && "Unable to find working scope!");
1289
1290 if (NScope->getFirstInsn())
1291 return NScope;
Devang Patel75cc16c2009-10-01 20:31:14 +00001292
1293 DbgScope *Parent = NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001294 if (GetConcreteScope) {
Devang Patel6875c5eb2009-10-14 21:08:09 +00001295 DILocation IL(InlinedAt);
Jim Grosbach042483e2009-11-21 23:12:12 +00001296 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001297 IL.getOrigLocation().getNode());
1298 assert (Parent && "Unable to find Parent scope!");
1299 NScope->setParent(Parent);
Devang Patel930143b2009-11-21 02:48:08 +00001300 Parent->addScope(NScope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001301 } else if (DIDescriptor(N).isLexicalBlock()) {
1302 DILexicalBlock DB(N);
1303 if (!DB.getContext().isNull()) {
1304 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1305 NScope->setParent(Parent);
Devang Patel930143b2009-11-21 02:48:08 +00001306 Parent->addScope(NScope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001307 }
Devang Patel6875c5eb2009-10-14 21:08:09 +00001308 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001309
Devang Patelcfeaa482009-10-27 20:47:17 +00001310 NScope->setFirstInsn(MI);
Devang Patel75cc16c2009-10-01 20:31:14 +00001311
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001312 if (!Parent && !InlinedAt) {
Devang Patel78319c62009-11-11 00:31:36 +00001313 StringRef SPName = DISubprogram(N).getLinkageName();
1314 if (SPName == MF->getFunction()->getName())
1315 CurrentFnDbgScope = NScope;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001316 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001317
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001318 if (GetConcreteScope) {
1319 ConcreteScopes[InlinedAt] = NScope;
1320 getOrCreateAbstractScope(N);
1321 }
1322
Devang Patelcfeaa482009-10-27 20:47:17 +00001323 return NScope;
Devang Patel75cc16c2009-10-01 20:31:14 +00001324}
1325
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001326DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
1327 assert (N && "Invalid Scope encoding!");
1328
1329 DbgScope *AScope = AbstractScopes.lookup(N);
1330 if (AScope)
1331 return AScope;
Jim Grosbach042483e2009-11-21 23:12:12 +00001332
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001333 DbgScope *Parent = NULL;
1334
1335 DIDescriptor Scope(N);
1336 if (Scope.isLexicalBlock()) {
1337 DILexicalBlock DB(N);
1338 DIDescriptor ParentDesc = DB.getContext();
1339 if (!ParentDesc.isNull())
1340 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
1341 }
1342
1343 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1344
1345 if (Parent)
Devang Patel930143b2009-11-21 02:48:08 +00001346 Parent->addScope(AScope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001347 AScope->setAbstractScope();
1348 AbstractScopes[N] = AScope;
1349 if (DIDescriptor(N).isSubprogram())
1350 AbstractScopesList.push_back(AScope);
1351 return AScope;
1352}
Devang Patel75cc16c2009-10-01 20:31:14 +00001353
Jim Grosbach042483e2009-11-21 23:12:12 +00001354/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
Devang Patel930143b2009-11-21 02:48:08 +00001355/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1356/// If there are global variables in this scope then create and insert
1357/// DIEs for these variables.
1358DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001359
Devang Patele064ad42009-11-20 21:37:22 +00001360 DIE *SPDie = ModuleCU->getDIE(SPNode);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001361 assert (SPDie && "Unable to find subprogram DIE!");
Devang Patel930143b2009-11-21 02:48:08 +00001362 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001363 DWLabel("func_begin", SubprogramCount));
Devang Patel930143b2009-11-21 02:48:08 +00001364 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001365 DWLabel("func_end", SubprogramCount));
1366 MachineLocation Location(RI->getFrameRegister(*MF));
Devang Patel930143b2009-11-21 02:48:08 +00001367 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Jim Grosbach042483e2009-11-21 23:12:12 +00001368
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001369 if (!DISubprogram(SPNode).isLocalToUnit())
Devang Patel930143b2009-11-21 02:48:08 +00001370 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001371
1372 // If there are global variables at this scope then add their dies.
Jim Grosbach042483e2009-11-21 23:12:12 +00001373 for (SmallVector<WeakVH, 4>::iterator SGI = ScopedGVs.begin(),
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001374 SGE = ScopedGVs.end(); SGI != SGE; ++SGI) {
1375 MDNode *N = dyn_cast_or_null<MDNode>(*SGI);
1376 if (!N) continue;
1377 DIGlobalVariable GV(N);
1378 if (GV.getContext().getNode() == SPNode) {
Devang Patel930143b2009-11-21 02:48:08 +00001379 DIE *ScopedGVDie = createGlobalVariableDIE(ModuleCU, GV);
Devang Patelcdb7d442009-11-10 23:20:04 +00001380 if (ScopedGVDie)
Devang Patel930143b2009-11-21 02:48:08 +00001381 SPDie->addChild(ScopedGVDie);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001382 }
1383 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001384
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001385 return SPDie;
1386}
1387
Jim Grosbach042483e2009-11-21 23:12:12 +00001388/// constructLexicalScope - Construct new DW_TAG_lexical_block
Devang Patel930143b2009-11-21 02:48:08 +00001389/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1390DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001391 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1392 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1393
1394 // Ignore empty scopes.
1395 if (StartID == EndID && StartID != 0)
1396 return NULL;
1397
1398 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1399 if (Scope->isAbstractScope())
1400 return ScopeDIE;
1401
Devang Patel930143b2009-11-21 02:48:08 +00001402 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Jim Grosbach042483e2009-11-21 23:12:12 +00001403 StartID ?
1404 DWLabel("label", StartID)
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001405 : DWLabel("func_begin", SubprogramCount));
Devang Patel930143b2009-11-21 02:48:08 +00001406 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Jim Grosbach042483e2009-11-21 23:12:12 +00001407 EndID ?
1408 DWLabel("label", EndID)
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001409 : DWLabel("func_end", SubprogramCount));
1410
1411
1412
1413 return ScopeDIE;
1414}
1415
Devang Patel930143b2009-11-21 02:48:08 +00001416/// constructInlinedScopeDIE - This scope represents inlined body of
1417/// a function. Construct DIE to represent this concrete inlined copy
1418/// of the function.
1419DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001420 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1421 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1422 assert (StartID && "Invalid starting label for an inlined scope!");
1423 assert (EndID && "Invalid end label for an inlined scope!");
1424 // Ignore empty scopes.
1425 if (StartID == EndID && StartID != 0)
1426 return NULL;
1427
1428 DIScope DS(Scope->getScopeNode());
1429 if (DS.isNull())
1430 return NULL;
1431 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1432
1433 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patele064ad42009-11-20 21:37:22 +00001434 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001435 assert (OriginDIE && "Unable to find Origin DIE!");
Devang Patel930143b2009-11-21 02:48:08 +00001436 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001437 dwarf::DW_FORM_ref4, OriginDIE);
1438
Devang Patel930143b2009-11-21 02:48:08 +00001439 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001440 DWLabel("label", StartID));
Devang Patel930143b2009-11-21 02:48:08 +00001441 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001442 DWLabel("label", EndID));
1443
1444 InlinedSubprogramDIEs.insert(OriginDIE);
1445
1446 // Track the start label for this inlined function.
1447 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1448 I = InlineInfo.find(InlinedSP.getNode());
1449
1450 if (I == InlineInfo.end()) {
Jim Grosbach00e9c612009-11-22 19:20:36 +00001451 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartID,
1452 ScopeDIE));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001453 InlinedSPNodes.push_back(InlinedSP.getNode());
1454 } else
1455 I->second.push_back(std::make_pair(StartID, ScopeDIE));
1456
1457 StringPool.insert(InlinedSP.getName());
1458 StringPool.insert(InlinedSP.getLinkageName());
1459 DILocation DL(Scope->getInlinedAt());
Devang Patel930143b2009-11-21 02:48:08 +00001460 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1461 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001462
1463 return ScopeDIE;
1464}
1465
Devang Patel930143b2009-11-21 02:48:08 +00001466
1467/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Jim Grosbach042483e2009-11-21 23:12:12 +00001468DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001469 DbgScope *Scope, CompileUnit *Unit) {
1470 // Get the descriptor.
1471 const DIVariable &VD = DV->getVariable();
Devang Patel2d9caf92009-11-25 17:36:49 +00001472 StringRef Name = VD.getName();
1473 if (Name.empty())
Devang Patel97f99fa2009-11-13 02:25:26 +00001474 return NULL;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001475
1476 // Translate tag to proper Dwarf tag. The result variable is dropped for
1477 // now.
1478 unsigned Tag;
1479 switch (VD.getTag()) {
1480 case dwarf::DW_TAG_return_variable:
1481 return NULL;
1482 case dwarf::DW_TAG_arg_variable:
1483 Tag = dwarf::DW_TAG_formal_parameter;
1484 break;
1485 case dwarf::DW_TAG_auto_variable: // fall thru
1486 default:
1487 Tag = dwarf::DW_TAG_variable;
1488 break;
1489 }
1490
1491 // Define variable debug information entry.
1492 DIE *VariableDie = new DIE(Tag);
1493
1494
1495 DIE *AbsDIE = NULL;
1496 if (DbgVariable *AV = DV->getAbstractVariable())
1497 AbsDIE = AV->getDIE();
Jim Grosbach042483e2009-11-21 23:12:12 +00001498
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001499 if (AbsDIE) {
1500 DIScope DS(Scope->getScopeNode());
1501 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
Devang Patele064ad42009-11-20 21:37:22 +00001502 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
Daniel Dunbar75b4d352009-11-11 03:09:50 +00001503 (void) OriginSPDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001504 assert (OriginSPDIE && "Unable to find Origin DIE for the SP!");
1505 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
1506 assert (AbsDIE && "Unable to find Origin DIE for the Variable!");
Devang Patel930143b2009-11-21 02:48:08 +00001507 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001508 dwarf::DW_FORM_ref4, AbsDIE);
1509 }
1510 else {
Devang Patel930143b2009-11-21 02:48:08 +00001511 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1512 addSourceLine(VariableDie, &VD);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001513
1514 // Add variable type.
Jim Grosbach042483e2009-11-21 23:12:12 +00001515 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001516 // addresses instead.
1517 if (VD.isBlockByrefVariable())
Devang Patel930143b2009-11-21 02:48:08 +00001518 addType(Unit, VariableDie, getBlockByrefType(VD.getType(), Name));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001519 else
Devang Patel930143b2009-11-21 02:48:08 +00001520 addType(Unit, VariableDie, VD.getType());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001521 }
1522
1523 // Add variable address.
1524 if (!Scope->isAbstractScope()) {
1525 MachineLocation Location;
Jim Grosbachbf6d3582009-11-22 20:14:00 +00001526 unsigned FrameReg;
1527 int Offset = RI->getFrameIndexReference(*MF, DV->getFrameIndex(), FrameReg);
1528 Location.set(FrameReg, Offset);
Jim Grosbach042483e2009-11-21 23:12:12 +00001529
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001530 if (VD.hasComplexAddress())
Devang Patel930143b2009-11-21 02:48:08 +00001531 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001532 else if (VD.isBlockByrefVariable())
Devang Patel930143b2009-11-21 02:48:08 +00001533 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001534 else
Devang Patel930143b2009-11-21 02:48:08 +00001535 addAddress(VariableDie, dwarf::DW_AT_location, Location);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001536 }
1537 DV->setDIE(VariableDie);
1538 return VariableDie;
1539
1540}
Devang Patel930143b2009-11-21 02:48:08 +00001541
Devang Patel04d2f2d2009-11-24 01:14:22 +00001542void DwarfDebug::addPubTypes(DISubprogram SP) {
1543 DICompositeType SPTy = SP.getType();
1544 unsigned SPTag = SPTy.getTag();
1545 if (SPTag != dwarf::DW_TAG_subroutine_type)
1546 return;
1547
1548 DIArray Args = SPTy.getTypeArray();
1549 if (Args.isNull())
1550 return;
1551
1552 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1553 DIType ATy(Args.getElement(i).getNode());
1554 if (ATy.isNull())
1555 continue;
1556 DICompositeType CATy = getDICompositeType(ATy);
Devang Patel2d9caf92009-11-25 17:36:49 +00001557 if (!CATy.isNull() && !CATy.getName().empty()) {
Devang Patel04d2f2d2009-11-24 01:14:22 +00001558 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1559 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1560 }
1561 }
1562}
1563
Devang Patel930143b2009-11-21 02:48:08 +00001564/// constructScopeDIE - Construct a DIE for this scope.
1565DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001566 if (!Scope)
1567 return NULL;
1568 DIScope DS(Scope->getScopeNode());
1569 if (DS.isNull())
1570 return NULL;
1571
1572 DIE *ScopeDIE = NULL;
1573 if (Scope->getInlinedAt())
Devang Patel930143b2009-11-21 02:48:08 +00001574 ScopeDIE = constructInlinedScopeDIE(Scope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001575 else if (DS.isSubprogram()) {
1576 if (Scope->isAbstractScope())
Devang Patele064ad42009-11-20 21:37:22 +00001577 ScopeDIE = ModuleCU->getDIE(DS.getNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001578 else
Devang Patel930143b2009-11-21 02:48:08 +00001579 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001580 }
1581 else {
Devang Patel930143b2009-11-21 02:48:08 +00001582 ScopeDIE = constructLexicalScopeDIE(Scope);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001583 if (!ScopeDIE) return NULL;
1584 }
1585
1586 // Add variables to scope.
1587 SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
1588 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
Devang Patel930143b2009-11-21 02:48:08 +00001589 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope, ModuleCU);
Jim Grosbach042483e2009-11-21 23:12:12 +00001590 if (VariableDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001591 ScopeDIE->addChild(VariableDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001592 }
1593
1594 // Add nested scopes.
1595 SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
1596 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1597 // Define the Scope debug information entry.
Devang Patel930143b2009-11-21 02:48:08 +00001598 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
Jim Grosbach042483e2009-11-21 23:12:12 +00001599 if (NestedDIE)
Devang Patel930143b2009-11-21 02:48:08 +00001600 ScopeDIE->addChild(NestedDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001601 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001602
1603 if (DS.isSubprogram())
1604 addPubTypes(DISubprogram(DS.getNode()));
1605
1606 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001607}
1608
Bill Wendling2b128d72009-05-20 23:19:06 +00001609/// GetOrCreateSourceID - Look up the source id with the given directory and
1610/// source file names. If none currently exists, create a new id and insert it
1611/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1612/// maps as well.
Devang Patel2d9caf92009-11-25 17:36:49 +00001613unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName) {
Bill Wendling2b128d72009-05-20 23:19:06 +00001614 unsigned DId;
1615 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1616 if (DI != DirectoryIdMap.end()) {
1617 DId = DI->getValue();
1618 } else {
1619 DId = DirectoryNames.size() + 1;
1620 DirectoryIdMap[DirName] = DId;
1621 DirectoryNames.push_back(DirName);
1622 }
1623
1624 unsigned FId;
1625 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1626 if (FI != SourceFileIdMap.end()) {
1627 FId = FI->getValue();
1628 } else {
1629 FId = SourceFileNames.size() + 1;
1630 SourceFileIdMap[FileName] = FId;
1631 SourceFileNames.push_back(FileName);
1632 }
1633
1634 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1635 SourceIdMap.find(std::make_pair(DId, FId));
1636 if (SI != SourceIdMap.end())
1637 return SI->second;
1638
1639 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1640 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1641 SourceIds.push_back(std::make_pair(DId, FId));
1642
1643 return SrcId;
1644}
1645
Devang Patel930143b2009-11-21 02:48:08 +00001646void DwarfDebug::constructCompileUnit(MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001647 DICompileUnit DIUnit(N);
Devang Patel2d9caf92009-11-25 17:36:49 +00001648 StringRef FN = DIUnit.getFilename();
1649 StringRef Dir = DIUnit.getDirectory();
Devang Patelb2969422009-09-29 18:40:58 +00001650 unsigned ID = GetOrCreateSourceID(Dir, FN);
Bill Wendling2b128d72009-05-20 23:19:06 +00001651
1652 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Devang Patel930143b2009-11-21 02:48:08 +00001653 addSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
Bill Wendling2b128d72009-05-20 23:19:06 +00001654 DWLabel("section_line", 0), DWLabel("section_line", 0),
1655 false);
Devang Patel930143b2009-11-21 02:48:08 +00001656 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
Devang Patelb2969422009-09-29 18:40:58 +00001657 DIUnit.getProducer());
Devang Patel930143b2009-11-21 02:48:08 +00001658 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
Bill Wendling2b128d72009-05-20 23:19:06 +00001659 DIUnit.getLanguage());
Devang Patel930143b2009-11-21 02:48:08 +00001660 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Bill Wendling2b128d72009-05-20 23:19:06 +00001661
Devang Patel2d9caf92009-11-25 17:36:49 +00001662 if (!Dir.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001663 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
Bill Wendling2b128d72009-05-20 23:19:06 +00001664 if (DIUnit.isOptimized())
Devang Patel930143b2009-11-21 02:48:08 +00001665 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
Bill Wendling2b128d72009-05-20 23:19:06 +00001666
Devang Patel2d9caf92009-11-25 17:36:49 +00001667 StringRef Flags = DIUnit.getFlags();
1668 if (!Flags.empty())
Devang Patel930143b2009-11-21 02:48:08 +00001669 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
Bill Wendling2b128d72009-05-20 23:19:06 +00001670
1671 unsigned RVer = DIUnit.getRunTimeVersion();
1672 if (RVer)
Devang Patel930143b2009-11-21 02:48:08 +00001673 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendling2b128d72009-05-20 23:19:06 +00001674 dwarf::DW_FORM_data1, RVer);
1675
1676 CompileUnit *Unit = new CompileUnit(ID, Die);
Devang Patel40d78412009-06-29 20:45:18 +00001677 if (!ModuleCU && DIUnit.isMain()) {
Devang Patel86353452009-06-29 20:38:13 +00001678 // Use first compile unit marked as isMain as the compile unit
1679 // for this module.
Devang Patel40d78412009-06-29 20:45:18 +00001680 ModuleCU = Unit;
Devang Patel86353452009-06-29 20:38:13 +00001681 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001682
Devang Patel80ae3492009-08-28 23:24:31 +00001683 CompileUnitMap[DIUnit.getNode()] = Unit;
Bill Wendling2b128d72009-05-20 23:19:06 +00001684 CompileUnits.push_back(Unit);
1685}
1686
Devang Patel930143b2009-11-21 02:48:08 +00001687void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001688 DIGlobalVariable DI_GV(N);
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001689
Devang Patelf5d53602009-09-04 23:59:07 +00001690 // If debug information is malformed then ignore it.
1691 if (DI_GV.Verify() == false)
1692 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001693
1694 // Check for pre-existence.
Devang Patele064ad42009-11-20 21:37:22 +00001695 if (ModuleCU->getDIE(DI_GV.getNode()))
Devang Patel0751a282009-06-26 01:49:18 +00001696 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001697
Devang Patel930143b2009-11-21 02:48:08 +00001698 DIE *VariableDie = createGlobalVariableDIE(ModuleCU, DI_GV);
Bill Wendling2b128d72009-05-20 23:19:06 +00001699
Bill Wendling2b128d72009-05-20 23:19:06 +00001700 // Add to map.
Devang Patele064ad42009-11-20 21:37:22 +00001701 ModuleCU->insertDIE(N, VariableDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001702
1703 // Add to context owner.
Devang Patel236526d2009-12-03 01:25:38 +00001704 if (TopLevelDIEs.insert(VariableDie))
1705 TopLevelDIEsVector.push_back(VariableDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001706
1707 // Expose as global. FIXME - need to check external flag.
Devang Patel930143b2009-11-21 02:48:08 +00001708 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001709
1710 DIType GTy = DI_GV.getType();
Devang Patel2d9caf92009-11-25 17:36:49 +00001711 if (GTy.isCompositeType() && !GTy.getName().empty()) {
Devang Patel04d2f2d2009-11-24 01:14:22 +00001712 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
1713 assert (Entry && "Missing global type!");
1714 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1715 }
Devang Patel0751a282009-06-26 01:49:18 +00001716 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001717}
1718
Devang Patel930143b2009-11-21 02:48:08 +00001719void DwarfDebug::constructSubprogramDIE(MDNode *N) {
Devang Patel80ae3492009-08-28 23:24:31 +00001720 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +00001721
1722 // Check for pre-existence.
Devang Patele064ad42009-11-20 21:37:22 +00001723 if (ModuleCU->getDIE(N))
Devang Patel0751a282009-06-26 01:49:18 +00001724 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001725
1726 if (!SP.isDefinition())
1727 // This is a method declaration which will be handled while constructing
1728 // class type.
Devang Patel0751a282009-06-26 01:49:18 +00001729 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001730
Devang Patel930143b2009-11-21 02:48:08 +00001731 DIE *SubprogramDie = createSubprogramDIE(ModuleCU, SP);
Bill Wendling2b128d72009-05-20 23:19:06 +00001732
1733 // Add to map.
Devang Patele064ad42009-11-20 21:37:22 +00001734 ModuleCU->insertDIE(N, SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001735
1736 // Add to context owner.
Devang Patela605b912009-12-01 23:07:59 +00001737 if (SP.getContext().getNode() == SP.getCompileUnit().getNode())
Devang Patel236526d2009-12-03 01:25:38 +00001738 if (TopLevelDIEs.insert(SubprogramDie))
1739 TopLevelDIEsVector.push_back(SubprogramDie);
Bill Wendling2b128d72009-05-20 23:19:06 +00001740
1741 // Expose as global.
Devang Patel930143b2009-11-21 02:48:08 +00001742 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001743
Devang Patel0751a282009-06-26 01:49:18 +00001744 return;
Bill Wendling2b128d72009-05-20 23:19:06 +00001745}
1746
Devang Patel930143b2009-11-21 02:48:08 +00001747/// beginModule - Emit all Dwarf sections that should come prior to the
Daniel Dunbarbe22ec42009-09-19 20:40:14 +00001748/// content. Create global DIEs and emit initial debug info sections.
1749/// This is inovked by the target AsmPrinter.
Devang Patel930143b2009-11-21 02:48:08 +00001750void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
Devang Patel0c044ec2009-06-25 22:36:02 +00001751 this->M = M;
1752
Bill Wendling2b128d72009-05-20 23:19:06 +00001753 if (TimePassesIsEnabled)
1754 DebugTimer->startTimer();
1755
Devang Pateld41f1192009-11-11 19:55:08 +00001756 if (!MAI->doesSupportDebugInformation())
1757 return;
1758
Devang Patel63524442009-07-30 18:56:46 +00001759 DebugInfoFinder DbgFinder;
1760 DbgFinder.processModule(*M);
Devang Patel0751a282009-06-26 01:49:18 +00001761
Bill Wendling2b128d72009-05-20 23:19:06 +00001762 // Create all the compile unit DIEs.
Devang Patel63524442009-07-30 18:56:46 +00001763 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1764 E = DbgFinder.compile_unit_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001765 constructCompileUnit(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00001766
1767 if (CompileUnits.empty()) {
1768 if (TimePassesIsEnabled)
1769 DebugTimer->stopTimer();
1770
1771 return;
1772 }
1773
Devang Patel86353452009-06-29 20:38:13 +00001774 // If main compile unit for this module is not seen than randomly
1775 // select first compile unit.
Devang Patel40d78412009-06-29 20:45:18 +00001776 if (!ModuleCU)
1777 ModuleCU = CompileUnits[0];
Devang Patel86353452009-06-29 20:38:13 +00001778
Devang Patel0751a282009-06-26 01:49:18 +00001779 // Create DIEs for each of the externally visible global variables.
Devang Patel63524442009-07-30 18:56:46 +00001780 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
Devang Patel4c420ec2009-10-05 23:40:42 +00001781 E = DbgFinder.global_variable_end(); I != E; ++I) {
1782 DIGlobalVariable GV(*I);
1783 if (GV.getContext().getNode() != GV.getCompileUnit().getNode())
1784 ScopedGVs.push_back(*I);
1785 else
Devang Patel930143b2009-11-21 02:48:08 +00001786 constructGlobalVariableDIE(*I);
Devang Patel4c420ec2009-10-05 23:40:42 +00001787 }
Devang Patel0751a282009-06-26 01:49:18 +00001788
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001789 // Create DIEs for each subprogram.
Devang Patel63524442009-07-30 18:56:46 +00001790 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1791 E = DbgFinder.subprogram_end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001792 constructSubprogramDIE(*I);
Devang Patel0751a282009-06-26 01:49:18 +00001793
Bill Wendling2b128d72009-05-20 23:19:06 +00001794 MMI = mmi;
1795 shouldEmit = true;
1796 MMI->setDebugInfoAvailability(true);
1797
1798 // Prime section data.
Chris Lattner5e693ed2009-07-28 03:13:23 +00001799 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00001800
1801 // Print out .file directives to specify files for .loc directives. These are
1802 // printed out early so that they precede any .loc directives.
Chris Lattnere9a75a62009-08-22 21:43:10 +00001803 if (MAI->hasDotLocAndDotFile()) {
Bill Wendling2b128d72009-05-20 23:19:06 +00001804 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1805 // Remember source id starts at 1.
1806 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1807 sys::Path FullPath(getSourceDirectoryName(Id.first));
1808 bool AppendOk =
1809 FullPath.appendComponent(getSourceFileName(Id.second));
1810 assert(AppendOk && "Could not append filename to directory!");
1811 AppendOk = false;
Chris Lattnerc521f542009-08-23 22:45:37 +00001812 Asm->EmitFile(i, FullPath.str());
Bill Wendling2b128d72009-05-20 23:19:06 +00001813 Asm->EOL();
1814 }
1815 }
1816
1817 // Emit initial sections
Devang Patel930143b2009-11-21 02:48:08 +00001818 emitInitial();
Bill Wendling2b128d72009-05-20 23:19:06 +00001819
1820 if (TimePassesIsEnabled)
1821 DebugTimer->stopTimer();
1822}
1823
Devang Patel930143b2009-11-21 02:48:08 +00001824/// endModule - Emit all Dwarf sections that should come after the content.
Bill Wendling2b128d72009-05-20 23:19:06 +00001825///
Devang Patel930143b2009-11-21 02:48:08 +00001826void DwarfDebug::endModule() {
Devang Pateld859d862009-10-06 00:03:14 +00001827 if (!ModuleCU)
Bill Wendling2b128d72009-05-20 23:19:06 +00001828 return;
1829
1830 if (TimePassesIsEnabled)
1831 DebugTimer->startTimer();
1832
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001833 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1834 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1835 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1836 DIE *ISP = *AI;
Devang Patel930143b2009-11-21 02:48:08 +00001837 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001838 }
1839
Devang Patel236526d2009-12-03 01:25:38 +00001840 // Insert top level DIEs.
1841 for (SmallVector<DIE *, 4>::iterator TI = TopLevelDIEsVector.begin(),
1842 TE = TopLevelDIEsVector.end(); TI != TE; ++TI)
1843 ModuleCU->getCUDie()->addChild(*TI);
1844
Devang Pateleb57c592009-12-03 19:11:07 +00001845 for (DenseMap<DIE *, WeakVH>::iterator CI = ContainingTypeMap.begin(),
1846 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1847 DIE *SPDie = CI->first;
1848 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1849 if (!N) continue;
1850 DIE *NDie = ModuleCU->getDIE(N);
1851 if (!NDie) continue;
1852 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1853 addDIEEntry(NDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1854 }
1855
Bill Wendling2b128d72009-05-20 23:19:06 +00001856 // Standard sections final addresses.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001857 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00001858 EmitLabel("text_end", 0);
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001859 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00001860 EmitLabel("data_end", 0);
1861
1862 // End text sections.
1863 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001864 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Bill Wendling2b128d72009-05-20 23:19:06 +00001865 EmitLabel("section_end", i);
1866 }
1867
1868 // Emit common frame information.
Devang Patel930143b2009-11-21 02:48:08 +00001869 emitCommonDebugFrame();
Bill Wendling2b128d72009-05-20 23:19:06 +00001870
1871 // Emit function debug frame information
1872 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1873 E = DebugFrames.end(); I != E; ++I)
Devang Patel930143b2009-11-21 02:48:08 +00001874 emitFunctionDebugFrame(*I);
Bill Wendling2b128d72009-05-20 23:19:06 +00001875
1876 // Compute DIE offsets and sizes.
Devang Patel930143b2009-11-21 02:48:08 +00001877 computeSizeAndOffsets();
Bill Wendling2b128d72009-05-20 23:19:06 +00001878
1879 // Emit all the DIEs into a debug info section
Devang Patel930143b2009-11-21 02:48:08 +00001880 emitDebugInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001881
1882 // Corresponding abbreviations into a abbrev section.
Devang Patel930143b2009-11-21 02:48:08 +00001883 emitAbbreviations();
Bill Wendling2b128d72009-05-20 23:19:06 +00001884
1885 // Emit source line correspondence into a debug line section.
Devang Patel930143b2009-11-21 02:48:08 +00001886 emitDebugLines();
Bill Wendling2b128d72009-05-20 23:19:06 +00001887
1888 // Emit info into a debug pubnames section.
Devang Patel930143b2009-11-21 02:48:08 +00001889 emitDebugPubNames();
Bill Wendling2b128d72009-05-20 23:19:06 +00001890
Devang Patel04d2f2d2009-11-24 01:14:22 +00001891 // Emit info into a debug pubtypes section.
1892 emitDebugPubTypes();
1893
Bill Wendling2b128d72009-05-20 23:19:06 +00001894 // Emit info into a debug str section.
Devang Patel930143b2009-11-21 02:48:08 +00001895 emitDebugStr();
Bill Wendling2b128d72009-05-20 23:19:06 +00001896
1897 // Emit info into a debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00001898 emitDebugLoc();
Bill Wendling2b128d72009-05-20 23:19:06 +00001899
1900 // Emit info into a debug aranges section.
1901 EmitDebugARanges();
1902
1903 // Emit info into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00001904 emitDebugRanges();
Bill Wendling2b128d72009-05-20 23:19:06 +00001905
1906 // Emit info into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00001907 emitDebugMacInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001908
1909 // Emit inline info.
Devang Patel930143b2009-11-21 02:48:08 +00001910 emitDebugInlineInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001911
1912 if (TimePassesIsEnabled)
1913 DebugTimer->stopTimer();
1914}
1915
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001916/// findAbstractVariable - Find abstract variable, if any, associated with Var.
Jim Grosbach00e9c612009-11-22 19:20:36 +00001917DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1918 unsigned FrameIdx,
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001919 DILocation &ScopeLoc) {
1920
1921 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1922 if (AbsDbgVariable)
1923 return AbsDbgVariable;
1924
1925 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope().getNode());
1926 if (!Scope)
1927 return NULL;
1928
1929 AbsDbgVariable = new DbgVariable(Var, FrameIdx);
Devang Patel930143b2009-11-21 02:48:08 +00001930 Scope->addVariable(AbsDbgVariable);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001931 AbstractVariables[Var.getNode()] = AbsDbgVariable;
1932 return AbsDbgVariable;
1933}
1934
Devang Patel930143b2009-11-21 02:48:08 +00001935/// collectVariableInfo - Populate DbgScope entries with variables' info.
1936void DwarfDebug::collectVariableInfo() {
Devang Pateldf45c7f2009-10-09 22:42:28 +00001937 if (!MMI) return;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001938
Devang Patel475d32a2009-10-06 01:26:37 +00001939 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1940 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1941 VE = VMap.end(); VI != VE; ++VI) {
Devang Pateldf45c7f2009-10-09 22:42:28 +00001942 MetadataBase *MB = VI->first;
1943 MDNode *Var = dyn_cast_or_null<MDNode>(MB);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001944 if (!Var) continue;
Devang Patel20b2a772009-10-08 18:48:03 +00001945 DIVariable DV (Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001946 std::pair< unsigned, MDNode *> VP = VI->second;
1947 DILocation ScopeLoc(VP.second);
1948
1949 DbgScope *Scope =
1950 ConcreteScopes.lookup(ScopeLoc.getOrigLocation().getNode());
1951 if (!Scope)
Jim Grosbach042483e2009-11-21 23:12:12 +00001952 Scope = DbgScopeMap.lookup(ScopeLoc.getScope().getNode());
Devang Patelcdb7d442009-11-10 23:20:04 +00001953 // If variable scope is not found then skip this variable.
1954 if (!Scope)
1955 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001956
1957 DbgVariable *RegVar = new DbgVariable(DV, VP.first);
Devang Patel930143b2009-11-21 02:48:08 +00001958 Scope->addVariable(RegVar);
Jim Grosbach00e9c612009-11-22 19:20:36 +00001959 if (DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first,
1960 ScopeLoc))
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001961 RegVar->setAbstractVariable(AbsDbgVariable);
Devang Patel475d32a2009-10-06 01:26:37 +00001962 }
1963}
1964
Devang Patel930143b2009-11-21 02:48:08 +00001965/// beginScope - Process beginning of a scope starting at Label.
1966void DwarfDebug::beginScope(const MachineInstr *MI, unsigned Label) {
Devang Patel8db360d2009-10-06 01:50:42 +00001967 InsnToDbgScopeMapTy::iterator I = DbgScopeBeginMap.find(MI);
1968 if (I == DbgScopeBeginMap.end())
1969 return;
Dan Gohman3650f4e2009-11-23 21:30:55 +00001970 ScopeVector &SD = I->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001971 for (ScopeVector::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach042483e2009-11-21 23:12:12 +00001972 SDI != SDE; ++SDI)
Devang Patel8db360d2009-10-06 01:50:42 +00001973 (*SDI)->setStartLabelID(Label);
1974}
1975
Devang Patel930143b2009-11-21 02:48:08 +00001976/// endScope - Process end of a scope.
1977void DwarfDebug::endScope(const MachineInstr *MI) {
Devang Patel8db360d2009-10-06 01:50:42 +00001978 InsnToDbgScopeMapTy::iterator I = DbgScopeEndMap.find(MI);
Devang Patel7d838bb2009-10-06 03:15:38 +00001979 if (I == DbgScopeEndMap.end())
Devang Patel8db360d2009-10-06 01:50:42 +00001980 return;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001981
1982 unsigned Label = MMI->NextLabelID();
1983 Asm->printLabel(Label);
1984
Devang Patel8db360d2009-10-06 01:50:42 +00001985 SmallVector<DbgScope *, 2> &SD = I->second;
1986 for (SmallVector<DbgScope *, 2>::iterator SDI = SD.begin(), SDE = SD.end();
Jim Grosbach042483e2009-11-21 23:12:12 +00001987 SDI != SDE; ++SDI)
Devang Patel8db360d2009-10-06 01:50:42 +00001988 (*SDI)->setEndLabelID(Label);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001989 return;
1990}
1991
1992/// createDbgScope - Create DbgScope for the scope.
1993void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
1994
1995 if (!InlinedAt) {
1996 DbgScope *WScope = DbgScopeMap.lookup(Scope);
1997 if (WScope)
1998 return;
1999 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2000 DbgScopeMap.insert(std::make_pair(Scope, WScope));
Jim Grosbach042483e2009-11-21 23:12:12 +00002001 if (DIDescriptor(Scope).isLexicalBlock())
Devang Patel4450f262009-11-11 00:18:40 +00002002 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002003 return;
2004 }
2005
2006 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2007 if (WScope)
2008 return;
2009
2010 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2011 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2012 DILocation DL(InlinedAt);
2013 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
Devang Patel8db360d2009-10-06 01:50:42 +00002014}
2015
Devang Patel930143b2009-11-21 02:48:08 +00002016/// extractScopeInformation - Scan machine instructions in this function
Devang Patel75cc16c2009-10-01 20:31:14 +00002017/// and collect DbgScopes. Return true, if atleast one scope was found.
Devang Patel930143b2009-11-21 02:48:08 +00002018bool DwarfDebug::extractScopeInformation(MachineFunction *MF) {
Devang Patel75cc16c2009-10-01 20:31:14 +00002019 // If scope information was extracted using .dbg intrinsics then there is not
2020 // any need to extract these information by scanning each instruction.
2021 if (!DbgScopeMap.empty())
2022 return false;
2023
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002024 // Scan each instruction and create scopes. First build working set of scopes.
Devang Patel75cc16c2009-10-01 20:31:14 +00002025 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2026 I != E; ++I) {
2027 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2028 II != IE; ++II) {
2029 const MachineInstr *MInsn = II;
2030 DebugLoc DL = MInsn->getDebugLoc();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002031 if (DL.isUnknown()) continue;
Devang Patel75cc16c2009-10-01 20:31:14 +00002032 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002033 if (!DLT.Scope) continue;
Devang Patel75cc16c2009-10-01 20:31:14 +00002034 // There is no need to create another DIE for compile unit. For all
Jim Grosbach042483e2009-11-21 23:12:12 +00002035 // other scopes, create one DbgScope now. This will be translated
Devang Patel75cc16c2009-10-01 20:31:14 +00002036 // into a scope DIE at the end.
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002037 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2038 createDbgScope(DLT.Scope, DLT.InlinedAtLoc);
2039 }
2040 }
2041
2042
2043 // Build scope hierarchy using working set of scopes.
2044 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2045 I != E; ++I) {
2046 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2047 II != IE; ++II) {
2048 const MachineInstr *MInsn = II;
2049 DebugLoc DL = MInsn->getDebugLoc();
2050 if (DL.isUnknown()) continue;
2051 DebugLocTuple DLT = MF->getDebugLocTuple(DL);
2052 if (!DLT.Scope) continue;
2053 // There is no need to create another DIE for compile unit. For all
Jim Grosbach042483e2009-11-21 23:12:12 +00002054 // other scopes, create one DbgScope now. This will be translated
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002055 // into a scope DIE at the end.
2056 if (DIDescriptor(DLT.Scope).isCompileUnit()) continue;
2057 DbgScope *Scope = getUpdatedDbgScope(DLT.Scope, MInsn, DLT.InlinedAtLoc);
2058 Scope->setLastInsn(MInsn);
Devang Patel75cc16c2009-10-01 20:31:14 +00002059 }
2060 }
2061
2062 // If a scope's last instruction is not set then use its child scope's
2063 // last instruction as this scope's last instrunction.
Devang Patelcfeaa482009-10-27 20:47:17 +00002064 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel75cc16c2009-10-01 20:31:14 +00002065 DE = DbgScopeMap.end(); DI != DE; ++DI) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002066 if (DI->second->isAbstractScope())
2067 continue;
Devang Patel75cc16c2009-10-01 20:31:14 +00002068 assert (DI->second->getFirstInsn() && "Invalid first instruction!");
Devang Patel930143b2009-11-21 02:48:08 +00002069 DI->second->fixInstructionMarkers();
Devang Patel75cc16c2009-10-01 20:31:14 +00002070 assert (DI->second->getLastInsn() && "Invalid last instruction!");
2071 }
2072
2073 // Each scope has first instruction and last instruction to mark beginning
2074 // and end of a scope respectively. Create an inverse map that list scopes
2075 // starts (and ends) with an instruction. One instruction may start (or end)
2076 // multiple scopes.
Devang Patelcfeaa482009-10-27 20:47:17 +00002077 for (ValueMap<MDNode *, DbgScope *>::iterator DI = DbgScopeMap.begin(),
Devang Patel75cc16c2009-10-01 20:31:14 +00002078 DE = DbgScopeMap.end(); DI != DE; ++DI) {
2079 DbgScope *S = DI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002080 if (S->isAbstractScope())
2081 continue;
Devang Patel75cc16c2009-10-01 20:31:14 +00002082 const MachineInstr *MI = S->getFirstInsn();
2083 assert (MI && "DbgScope does not have first instruction!");
2084
2085 InsnToDbgScopeMapTy::iterator IDI = DbgScopeBeginMap.find(MI);
2086 if (IDI != DbgScopeBeginMap.end())
2087 IDI->second.push_back(S);
2088 else
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002089 DbgScopeBeginMap[MI].push_back(S);
Devang Patel75cc16c2009-10-01 20:31:14 +00002090
2091 MI = S->getLastInsn();
2092 assert (MI && "DbgScope does not have last instruction!");
2093 IDI = DbgScopeEndMap.find(MI);
2094 if (IDI != DbgScopeEndMap.end())
2095 IDI->second.push_back(S);
2096 else
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002097 DbgScopeEndMap[MI].push_back(S);
Devang Patel75cc16c2009-10-01 20:31:14 +00002098 }
2099
2100 return !DbgScopeMap.empty();
2101}
2102
Devang Patel930143b2009-11-21 02:48:08 +00002103/// beginFunction - Gather pre-function debug information. Assumes being
Bill Wendling2b128d72009-05-20 23:19:06 +00002104/// emitted immediately after the function entry point.
Devang Patel930143b2009-11-21 02:48:08 +00002105void DwarfDebug::beginFunction(MachineFunction *MF) {
Bill Wendling2b128d72009-05-20 23:19:06 +00002106 this->MF = MF;
2107
2108 if (!ShouldEmitDwarfDebug()) return;
2109
2110 if (TimePassesIsEnabled)
2111 DebugTimer->startTimer();
2112
Devang Patel930143b2009-11-21 02:48:08 +00002113 if (!extractScopeInformation(MF))
Devang Patel4598eb62009-10-06 18:37:31 +00002114 return;
Devang Patel930143b2009-11-21 02:48:08 +00002115
2116 collectVariableInfo();
Devang Patel4598eb62009-10-06 18:37:31 +00002117
Bill Wendling2b128d72009-05-20 23:19:06 +00002118 // Begin accumulating function debug information.
2119 MMI->BeginFunction(MF);
2120
2121 // Assumes in correct section after the entry point.
2122 EmitLabel("func_begin", ++SubprogramCount);
2123
2124 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2125 // function.
Devang Pateldf45c7f2009-10-09 22:42:28 +00002126 DebugLoc FDL = MF->getDefaultDebugLoc();
2127 if (!FDL.isUnknown()) {
2128 DebugLocTuple DLT = MF->getDebugLocTuple(FDL);
2129 unsigned LabelID = 0;
Devang Pateld7ebfe32009-10-13 23:28:53 +00002130 DISubprogram SP = getDISubprogram(DLT.Scope);
Devang Pateldf45c7f2009-10-09 22:42:28 +00002131 if (!SP.isNull())
Devang Patel930143b2009-11-21 02:48:08 +00002132 LabelID = recordSourceLine(SP.getLineNumber(), 0, DLT.Scope);
Devang Pateldf45c7f2009-10-09 22:42:28 +00002133 else
Devang Patel930143b2009-11-21 02:48:08 +00002134 LabelID = recordSourceLine(DLT.Line, DLT.Col, DLT.Scope);
Devang Pateldf45c7f2009-10-09 22:42:28 +00002135 Asm->printLabel(LabelID);
2136 O << '\n';
Bill Wendling2b128d72009-05-20 23:19:06 +00002137 }
Bill Wendling2b128d72009-05-20 23:19:06 +00002138 if (TimePassesIsEnabled)
2139 DebugTimer->stopTimer();
2140}
2141
Devang Patel930143b2009-11-21 02:48:08 +00002142/// endFunction - Gather and emit post-function debug information.
Bill Wendling2b128d72009-05-20 23:19:06 +00002143///
Devang Patel930143b2009-11-21 02:48:08 +00002144void DwarfDebug::endFunction(MachineFunction *MF) {
Bill Wendling2b128d72009-05-20 23:19:06 +00002145 if (!ShouldEmitDwarfDebug()) return;
2146
2147 if (TimePassesIsEnabled)
2148 DebugTimer->startTimer();
2149
Devang Pateldf45c7f2009-10-09 22:42:28 +00002150 if (DbgScopeMap.empty())
2151 return;
Devang Patel2904aa92009-11-12 19:02:56 +00002152
Bill Wendling2b128d72009-05-20 23:19:06 +00002153 // Define end label for subprogram.
2154 EmitLabel("func_end", SubprogramCount);
2155
2156 // Get function line info.
2157 if (!Lines.empty()) {
2158 // Get section line info.
Chris Lattner8ce12532009-08-03 23:20:21 +00002159 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
Bill Wendling2b128d72009-05-20 23:19:06 +00002160 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2161 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2162 // Append the function info to section info.
2163 SectionLineInfos.insert(SectionLineInfos.end(),
2164 Lines.begin(), Lines.end());
2165 }
2166
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002167 // Construct abstract scopes.
2168 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
Jim Grosbach042483e2009-11-21 23:12:12 +00002169 AE = AbstractScopesList.end(); AI != AE; ++AI)
Devang Patel930143b2009-11-21 02:48:08 +00002170 constructScopeDIE(*AI);
Bill Wendling2b128d72009-05-20 23:19:06 +00002171
Devang Patel930143b2009-11-21 02:48:08 +00002172 constructScopeDIE(CurrentFnDbgScope);
Devang Patel2904aa92009-11-12 19:02:56 +00002173
Bill Wendling2b128d72009-05-20 23:19:06 +00002174 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2175 MMI->getFrameMoves()));
2176
2177 // Clear debug info
Devang Patel0a2c0bc2009-12-01 18:13:48 +00002178 CurrentFnDbgScope = NULL;
2179 DbgScopeMap.clear();
2180 DbgScopeBeginMap.clear();
2181 DbgScopeEndMap.clear();
2182 ConcreteScopes.clear();
2183 AbstractScopesList.clear();
Bill Wendling2b128d72009-05-20 23:19:06 +00002184
2185 Lines.clear();
Devang Patel0a2c0bc2009-12-01 18:13:48 +00002186
Bill Wendling2b128d72009-05-20 23:19:06 +00002187 if (TimePassesIsEnabled)
2188 DebugTimer->stopTimer();
2189}
2190
Devang Patel930143b2009-11-21 02:48:08 +00002191/// recordSourceLine - Records location information and associates it with a
Bill Wendling2b128d72009-05-20 23:19:06 +00002192/// label. Returns a unique label ID used to generate a label and provide
2193/// correspondence to the source line list.
Jim Grosbach042483e2009-11-21 23:12:12 +00002194unsigned DwarfDebug::recordSourceLine(unsigned Line, unsigned Col,
Devang Patel2089d162009-10-05 18:03:19 +00002195 MDNode *S) {
Devang Patel80ae3492009-08-28 23:24:31 +00002196 if (!MMI)
2197 return 0;
2198
Bill Wendling2b128d72009-05-20 23:19:06 +00002199 if (TimePassesIsEnabled)
2200 DebugTimer->startTimer();
2201
Devang Patel2d9caf92009-11-25 17:36:49 +00002202 StringRef Dir;
2203 StringRef Fn;
Devang Patel2089d162009-10-05 18:03:19 +00002204
2205 DIDescriptor Scope(S);
2206 if (Scope.isCompileUnit()) {
2207 DICompileUnit CU(S);
2208 Dir = CU.getDirectory();
2209 Fn = CU.getFilename();
2210 } else if (Scope.isSubprogram()) {
2211 DISubprogram SP(S);
2212 Dir = SP.getDirectory();
2213 Fn = SP.getFilename();
2214 } else if (Scope.isLexicalBlock()) {
2215 DILexicalBlock DB(S);
2216 Dir = DB.getDirectory();
2217 Fn = DB.getFilename();
2218 } else
2219 assert (0 && "Unexpected scope info");
2220
2221 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00002222 unsigned ID = MMI->NextLabelID();
2223 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
2224
2225 if (TimePassesIsEnabled)
2226 DebugTimer->stopTimer();
2227
2228 return ID;
2229}
2230
2231/// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
2232/// timed. Look up the source id with the given directory and source file
2233/// names. If none currently exists, create a new id and insert it in the
2234/// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
2235/// well.
2236unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName,
2237 const std::string &FileName) {
2238 if (TimePassesIsEnabled)
2239 DebugTimer->startTimer();
2240
Devang Patelb2969422009-09-29 18:40:58 +00002241 unsigned SrcId = GetOrCreateSourceID(DirName.c_str(), FileName.c_str());
Bill Wendling2b128d72009-05-20 23:19:06 +00002242
2243 if (TimePassesIsEnabled)
2244 DebugTimer->stopTimer();
2245
2246 return SrcId;
2247}
2248
Bill Wendling806535f2009-05-20 23:22:40 +00002249//===----------------------------------------------------------------------===//
2250// Emit Methods
2251//===----------------------------------------------------------------------===//
2252
Devang Patel930143b2009-11-21 02:48:08 +00002253/// computeSizeAndOffset - Compute the size and offset of a DIE.
Bill Wendling480ff322009-05-20 23:21:38 +00002254///
Jim Grosbach00e9c612009-11-22 19:20:36 +00002255unsigned
2256DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
Bill Wendling480ff322009-05-20 23:21:38 +00002257 // Get the children.
2258 const std::vector<DIE *> &Children = Die->getChildren();
2259
2260 // If not last sibling and has children then add sibling offset attribute.
Devang Patel930143b2009-11-21 02:48:08 +00002261 if (!Last && !Children.empty()) Die->addSiblingOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00002262
2263 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00002264 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00002265
2266 // Get the abbreviation for this DIE.
2267 unsigned AbbrevNumber = Die->getAbbrevNumber();
2268 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2269
2270 // Set DIE offset
2271 Die->setOffset(Offset);
2272
2273 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00002274 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00002275
2276 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2277 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2278
2279 // Size the DIE attribute values.
2280 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2281 // Size attribute value.
2282 Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm());
2283
2284 // Size the DIE children if any.
2285 if (!Children.empty()) {
2286 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2287 "Children flag not set");
2288
2289 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00002290 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
Bill Wendling480ff322009-05-20 23:21:38 +00002291
2292 // End of children marker.
2293 Offset += sizeof(int8_t);
2294 }
2295
2296 Die->setSize(Offset - Die->getOffset());
2297 return Offset;
2298}
2299
Devang Patel930143b2009-11-21 02:48:08 +00002300/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
Bill Wendling480ff322009-05-20 23:21:38 +00002301///
Devang Patel930143b2009-11-21 02:48:08 +00002302void DwarfDebug::computeSizeAndOffsets() {
Bill Wendling480ff322009-05-20 23:21:38 +00002303 // Compute size of compile unit header.
2304 static unsigned Offset =
2305 sizeof(int32_t) + // Length of Compilation Unit Info
2306 sizeof(int16_t) + // DWARF version number
2307 sizeof(int32_t) + // Offset Into Abbrev. Section
2308 sizeof(int8_t); // Pointer Size (in bytes)
2309
Devang Patel930143b2009-11-21 02:48:08 +00002310 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Devang Patel40d78412009-06-29 20:45:18 +00002311 CompileUnitOffsets[ModuleCU] = 0;
Bill Wendling480ff322009-05-20 23:21:38 +00002312}
2313
Devang Patel930143b2009-11-21 02:48:08 +00002314/// emitInitial - Emit initial Dwarf declarations. This is necessary for cc
Bill Wendling480ff322009-05-20 23:21:38 +00002315/// tools to recognize the object file contains Dwarf information.
Devang Patel930143b2009-11-21 02:48:08 +00002316void DwarfDebug::emitInitial() {
Bill Wendling480ff322009-05-20 23:21:38 +00002317 // Check to see if we already emitted intial headers.
2318 if (didInitial) return;
2319 didInitial = true;
2320
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002321 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002322
Bill Wendling480ff322009-05-20 23:21:38 +00002323 // Dwarf sections base addresses.
Chris Lattnere9a75a62009-08-22 21:43:10 +00002324 if (MAI->doesDwarfRequireFrameSection()) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002325 Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002326 EmitLabel("section_debug_frame", 0);
2327 }
2328
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002329 Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002330 EmitLabel("section_info", 0);
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002331 Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002332 EmitLabel("section_abbrev", 0);
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002333 Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002334 EmitLabel("section_aranges", 0);
2335
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002336 if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) {
2337 Asm->OutStreamer.SwitchSection(LineInfoDirective);
Bill Wendling480ff322009-05-20 23:21:38 +00002338 EmitLabel("section_macinfo", 0);
2339 }
2340
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002341 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002342 EmitLabel("section_line", 0);
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002343 Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002344 EmitLabel("section_loc", 0);
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002345 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002346 EmitLabel("section_pubnames", 0);
Devang Patel04d2f2d2009-11-24 01:14:22 +00002347 Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubTypesSection());
2348 EmitLabel("section_pubtypes", 0);
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002349 Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002350 EmitLabel("section_str", 0);
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002351 Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002352 EmitLabel("section_ranges", 0);
2353
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002354 Asm->OutStreamer.SwitchSection(TLOF.getTextSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002355 EmitLabel("text_begin", 0);
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002356 Asm->OutStreamer.SwitchSection(TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002357 EmitLabel("data_begin", 0);
2358}
2359
Devang Patel930143b2009-11-21 02:48:08 +00002360/// emitDIE - Recusively Emits a debug information entry.
Bill Wendling480ff322009-05-20 23:21:38 +00002361///
Devang Patel930143b2009-11-21 02:48:08 +00002362void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00002363 // Get the abbreviation for this DIE.
2364 unsigned AbbrevNumber = Die->getAbbrevNumber();
2365 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2366
2367 Asm->EOL();
2368
2369 // Emit the code (index) for the abbreviation.
2370 Asm->EmitULEB128Bytes(AbbrevNumber);
2371
2372 if (Asm->isVerbose())
2373 Asm->EOL(std::string("Abbrev [" +
2374 utostr(AbbrevNumber) +
2375 "] 0x" + utohexstr(Die->getOffset()) +
2376 ":0x" + utohexstr(Die->getSize()) + " " +
2377 dwarf::TagString(Abbrev->getTag())));
2378 else
2379 Asm->EOL();
2380
2381 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2382 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2383
2384 // Emit the DIE attribute values.
2385 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2386 unsigned Attr = AbbrevData[i].getAttribute();
2387 unsigned Form = AbbrevData[i].getForm();
2388 assert(Form && "Too many attributes for DIE (check abbreviation)");
2389
2390 switch (Attr) {
2391 case dwarf::DW_AT_sibling:
Devang Patel930143b2009-11-21 02:48:08 +00002392 Asm->EmitInt32(Die->getSiblingOffset());
Bill Wendling480ff322009-05-20 23:21:38 +00002393 break;
2394 case dwarf::DW_AT_abstract_origin: {
2395 DIEEntry *E = cast<DIEEntry>(Values[i]);
2396 DIE *Origin = E->getEntry();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002397 unsigned Addr = Origin->getOffset();
Bill Wendling480ff322009-05-20 23:21:38 +00002398 Asm->EmitInt32(Addr);
2399 break;
2400 }
2401 default:
2402 // Emit an attribute using the defined form.
2403 Values[i]->EmitValue(this, Form);
2404 break;
2405 }
2406
2407 Asm->EOL(dwarf::AttributeString(Attr));
2408 }
2409
2410 // Emit the DIE children if any.
2411 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2412 const std::vector<DIE *> &Children = Die->getChildren();
2413
2414 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Devang Patel930143b2009-11-21 02:48:08 +00002415 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00002416
2417 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2418 }
2419}
2420
Devang Patel930143b2009-11-21 02:48:08 +00002421/// emitDebugInfo / emitDebugInfoPerCU - Emit the debug info section.
Bill Wendling480ff322009-05-20 23:21:38 +00002422///
Devang Patel930143b2009-11-21 02:48:08 +00002423void DwarfDebug::emitDebugInfoPerCU(CompileUnit *Unit) {
Devang Patele064ad42009-11-20 21:37:22 +00002424 DIE *Die = Unit->getCUDie();
Bill Wendling480ff322009-05-20 23:21:38 +00002425
2426 // Emit the compile units header.
2427 EmitLabel("info_begin", Unit->getID());
2428
2429 // Emit size of content not including length itself
2430 unsigned ContentSize = Die->getSize() +
2431 sizeof(int16_t) + // DWARF version number
2432 sizeof(int32_t) + // Offset Into Abbrev. Section
2433 sizeof(int8_t) + // Pointer Size (in bytes)
2434 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2435
2436 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2437 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2438 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2439 Asm->EOL("Offset Into Abbrev. Section");
2440 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2441
Devang Patel930143b2009-11-21 02:48:08 +00002442 emitDIE(Die);
Bill Wendling480ff322009-05-20 23:21:38 +00002443 // FIXME - extra padding for gdb bug.
2444 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2445 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2446 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2447 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2448 EmitLabel("info_end", Unit->getID());
2449
2450 Asm->EOL();
2451}
2452
Devang Patel930143b2009-11-21 02:48:08 +00002453void DwarfDebug::emitDebugInfo() {
Bill Wendling480ff322009-05-20 23:21:38 +00002454 // Start debug info section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002455 Asm->OutStreamer.SwitchSection(
2456 Asm->getObjFileLowering().getDwarfInfoSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002457
Devang Patel930143b2009-11-21 02:48:08 +00002458 emitDebugInfoPerCU(ModuleCU);
Bill Wendling480ff322009-05-20 23:21:38 +00002459}
2460
Devang Patel930143b2009-11-21 02:48:08 +00002461/// emitAbbreviations - Emit the abbreviation section.
Bill Wendling480ff322009-05-20 23:21:38 +00002462///
Devang Patel930143b2009-11-21 02:48:08 +00002463void DwarfDebug::emitAbbreviations() const {
Bill Wendling480ff322009-05-20 23:21:38 +00002464 // Check to see if it is worth the effort.
2465 if (!Abbreviations.empty()) {
2466 // Start the debug abbrev section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002467 Asm->OutStreamer.SwitchSection(
2468 Asm->getObjFileLowering().getDwarfAbbrevSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002469
2470 EmitLabel("abbrev_begin", 0);
2471
2472 // For each abbrevation.
2473 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2474 // Get abbreviation data
2475 const DIEAbbrev *Abbrev = Abbreviations[i];
2476
2477 // Emit the abbrevations code (base 1 index.)
2478 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2479 Asm->EOL("Abbreviation Code");
2480
2481 // Emit the abbreviations data.
2482 Abbrev->Emit(Asm);
2483
2484 Asm->EOL();
2485 }
2486
2487 // Mark end of abbreviations.
2488 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2489
2490 EmitLabel("abbrev_end", 0);
2491 Asm->EOL();
2492 }
2493}
2494
Devang Patel930143b2009-11-21 02:48:08 +00002495/// emitEndOfLineMatrix - Emit the last address of the section and the end of
Bill Wendling480ff322009-05-20 23:21:38 +00002496/// the line matrix.
2497///
Devang Patel930143b2009-11-21 02:48:08 +00002498void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002499 // Define last address of section.
2500 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2501 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2502 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2503 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2504
2505 // Mark end of matrix.
2506 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2507 Asm->EmitULEB128Bytes(1); Asm->EOL();
2508 Asm->EmitInt8(1); Asm->EOL();
2509}
2510
Devang Patel930143b2009-11-21 02:48:08 +00002511/// emitDebugLines - Emit source line information.
Bill Wendling480ff322009-05-20 23:21:38 +00002512///
Devang Patel930143b2009-11-21 02:48:08 +00002513void DwarfDebug::emitDebugLines() {
Bill Wendling480ff322009-05-20 23:21:38 +00002514 // If the target is using .loc/.file, the assembler will be emitting the
2515 // .debug_line table automatically.
Chris Lattnere9a75a62009-08-22 21:43:10 +00002516 if (MAI->hasDotLocAndDotFile())
Bill Wendling480ff322009-05-20 23:21:38 +00002517 return;
2518
2519 // Minimum line delta, thus ranging from -10..(255-10).
2520 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2521 // Maximum line delta, thus ranging from -10..(255-10).
2522 const int MaxLineDelta = 255 + MinLineDelta;
2523
2524 // Start the dwarf line section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002525 Asm->OutStreamer.SwitchSection(
2526 Asm->getObjFileLowering().getDwarfLineSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002527
2528 // Construct the section header.
2529 EmitDifference("line_end", 0, "line_begin", 0, true);
2530 Asm->EOL("Length of Source Line Info");
2531 EmitLabel("line_begin", 0);
2532
2533 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number");
2534
2535 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2536 Asm->EOL("Prolog Length");
2537 EmitLabel("line_prolog_begin", 0);
2538
2539 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2540
2541 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2542
2543 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2544
2545 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2546
2547 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2548
2549 // Line number standard opcode encodings argument count
2550 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2551 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2552 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2553 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2554 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2555 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2556 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2557 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2558 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2559
2560 // Emit directories.
2561 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2562 Asm->EmitString(getSourceDirectoryName(DI));
2563 Asm->EOL("Directory");
2564 }
2565
2566 Asm->EmitInt8(0); Asm->EOL("End of directories");
2567
2568 // Emit files.
2569 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2570 // Remember source id starts at 1.
2571 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2572 Asm->EmitString(getSourceFileName(Id.second));
2573 Asm->EOL("Source");
2574 Asm->EmitULEB128Bytes(Id.first);
2575 Asm->EOL("Directory #");
2576 Asm->EmitULEB128Bytes(0);
2577 Asm->EOL("Mod date");
2578 Asm->EmitULEB128Bytes(0);
2579 Asm->EOL("File size");
2580 }
2581
2582 Asm->EmitInt8(0); Asm->EOL("End of files");
2583
2584 EmitLabel("line_prolog_end", 0);
2585
2586 // A sequence for each text section.
2587 unsigned SecSrcLinesSize = SectionSourceLines.size();
2588
2589 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2590 // Isolate current sections line info.
2591 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2592
Chris Lattner591105c2009-08-08 23:39:42 +00002593 /*if (Asm->isVerbose()) {
Chris Lattner4d2c0f92009-07-31 18:48:30 +00002594 const MCSection *S = SectionMap[j + 1];
Chris Lattnere9a75a62009-08-22 21:43:10 +00002595 O << '\t' << MAI->getCommentString() << " Section"
Bill Wendling480ff322009-05-20 23:21:38 +00002596 << S->getName() << '\n';
Chris Lattner591105c2009-08-08 23:39:42 +00002597 }*/
2598 Asm->EOL();
Bill Wendling480ff322009-05-20 23:21:38 +00002599
2600 // Dwarf assumes we start with first line of first source file.
2601 unsigned Source = 1;
2602 unsigned Line = 1;
2603
2604 // Construct rows of the address, source, line, column matrix.
2605 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2606 const SrcLineInfo &LineInfo = LineInfos[i];
2607 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2608 if (!LabelID) continue;
2609
Caroline Tice183a5192009-09-11 18:25:54 +00002610 if (LineInfo.getLine() == 0) continue;
2611
Bill Wendling480ff322009-05-20 23:21:38 +00002612 if (!Asm->isVerbose())
2613 Asm->EOL();
2614 else {
2615 std::pair<unsigned, unsigned> SourceID =
2616 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Chris Lattnere9a75a62009-08-22 21:43:10 +00002617 O << '\t' << MAI->getCommentString() << ' '
Bill Wendling480ff322009-05-20 23:21:38 +00002618 << getSourceDirectoryName(SourceID.first) << ' '
2619 << getSourceFileName(SourceID.second)
2620 <<" :" << utostr_32(LineInfo.getLine()) << '\n';
2621 }
2622
2623 // Define the line address.
2624 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2625 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2626 Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2627 EmitReference("label", LabelID); Asm->EOL("Location label");
2628
2629 // If change of source, then switch to the new source.
2630 if (Source != LineInfo.getSourceID()) {
2631 Source = LineInfo.getSourceID();
2632 Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2633 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2634 }
2635
2636 // If change of line.
2637 if (Line != LineInfo.getLine()) {
2638 // Determine offset.
2639 int Offset = LineInfo.getLine() - Line;
2640 int Delta = Offset - MinLineDelta;
2641
2642 // Update line.
2643 Line = LineInfo.getLine();
2644
2645 // If delta is small enough and in range...
2646 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2647 // ... then use fast opcode.
2648 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2649 } else {
2650 // ... otherwise use long hand.
2651 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
2652 Asm->EOL("DW_LNS_advance_line");
2653 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2654 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2655 }
2656 } else {
2657 // Copy the previous row (different address or source)
2658 Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2659 }
2660 }
2661
Devang Patel930143b2009-11-21 02:48:08 +00002662 emitEndOfLineMatrix(j + 1);
Bill Wendling480ff322009-05-20 23:21:38 +00002663 }
2664
2665 if (SecSrcLinesSize == 0)
2666 // Because we're emitting a debug_line section, we still need a line
2667 // table. The linker and friends expect it to exist. If there's nothing to
2668 // put into it, emit an empty table.
Devang Patel930143b2009-11-21 02:48:08 +00002669 emitEndOfLineMatrix(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002670
2671 EmitLabel("line_end", 0);
2672 Asm->EOL();
2673}
2674
Devang Patel930143b2009-11-21 02:48:08 +00002675/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
Bill Wendling480ff322009-05-20 23:21:38 +00002676///
Devang Patel930143b2009-11-21 02:48:08 +00002677void DwarfDebug::emitCommonDebugFrame() {
Chris Lattnere9a75a62009-08-22 21:43:10 +00002678 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002679 return;
2680
2681 int stackGrowth =
2682 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2683 TargetFrameInfo::StackGrowsUp ?
2684 TD->getPointerSize() : -TD->getPointerSize();
2685
2686 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002687 Asm->OutStreamer.SwitchSection(
2688 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002689
2690 EmitLabel("debug_frame_common", 0);
2691 EmitDifference("debug_frame_common_end", 0,
2692 "debug_frame_common_begin", 0, true);
2693 Asm->EOL("Length of Common Information Entry");
2694
2695 EmitLabel("debug_frame_common_begin", 0);
2696 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
2697 Asm->EOL("CIE Identifier Tag");
2698 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
2699 Asm->EOL("CIE Version");
2700 Asm->EmitString("");
2701 Asm->EOL("CIE Augmentation");
2702 Asm->EmitULEB128Bytes(1);
2703 Asm->EOL("CIE Code Alignment Factor");
2704 Asm->EmitSLEB128Bytes(stackGrowth);
2705 Asm->EOL("CIE Data Alignment Factor");
2706 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2707 Asm->EOL("CIE RA Column");
2708
2709 std::vector<MachineMove> Moves;
2710 RI->getInitialFrameState(Moves);
2711
2712 EmitFrameMoves(NULL, 0, Moves, false);
2713
2714 Asm->EmitAlignment(2, 0, 0, false);
2715 EmitLabel("debug_frame_common_end", 0);
2716
2717 Asm->EOL();
2718}
2719
Devang Patel930143b2009-11-21 02:48:08 +00002720/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
Bill Wendling480ff322009-05-20 23:21:38 +00002721/// section.
2722void
Devang Patel930143b2009-11-21 02:48:08 +00002723DwarfDebug::emitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){
Chris Lattnere9a75a62009-08-22 21:43:10 +00002724 if (!MAI->doesDwarfRequireFrameSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002725 return;
2726
2727 // Start the dwarf frame section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002728 Asm->OutStreamer.SwitchSection(
2729 Asm->getObjFileLowering().getDwarfFrameSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002730
2731 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2732 "debug_frame_begin", DebugFrameInfo.Number, true);
2733 Asm->EOL("Length of Frame Information Entry");
2734
2735 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2736
2737 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2738 0, 0, true, false);
2739 Asm->EOL("FDE CIE offset");
2740
2741 EmitReference("func_begin", DebugFrameInfo.Number);
2742 Asm->EOL("FDE initial location");
2743 EmitDifference("func_end", DebugFrameInfo.Number,
2744 "func_begin", DebugFrameInfo.Number);
2745 Asm->EOL("FDE address range");
2746
2747 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2748 false);
2749
2750 Asm->EmitAlignment(2, 0, 0, false);
2751 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2752
2753 Asm->EOL();
2754}
2755
Devang Patel930143b2009-11-21 02:48:08 +00002756void DwarfDebug::emitDebugPubNamesPerCU(CompileUnit *Unit) {
Bill Wendling480ff322009-05-20 23:21:38 +00002757 EmitDifference("pubnames_end", Unit->getID(),
2758 "pubnames_begin", Unit->getID(), true);
2759 Asm->EOL("Length of Public Names Info");
2760
2761 EmitLabel("pubnames_begin", Unit->getID());
2762
2763 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2764
2765 EmitSectionOffset("info_begin", "section_info",
2766 Unit->getID(), 0, true, false);
2767 Asm->EOL("Offset of Compilation Unit Info");
2768
2769 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2770 true);
2771 Asm->EOL("Compilation Unit Length");
2772
Devang Patel04d2f2d2009-11-24 01:14:22 +00002773 const StringMap<DIE*> &Globals = Unit->getGlobals();
Bill Wendling480ff322009-05-20 23:21:38 +00002774 for (StringMap<DIE*>::const_iterator
2775 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2776 const char *Name = GI->getKeyData();
2777 DIE * Entity = GI->second;
2778
2779 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2780 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2781 }
2782
2783 Asm->EmitInt32(0); Asm->EOL("End Mark");
2784 EmitLabel("pubnames_end", Unit->getID());
2785
2786 Asm->EOL();
2787}
2788
Devang Patel930143b2009-11-21 02:48:08 +00002789/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Bill Wendling480ff322009-05-20 23:21:38 +00002790///
Devang Patel930143b2009-11-21 02:48:08 +00002791void DwarfDebug::emitDebugPubNames() {
Bill Wendling480ff322009-05-20 23:21:38 +00002792 // Start the dwarf pubnames section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002793 Asm->OutStreamer.SwitchSection(
2794 Asm->getObjFileLowering().getDwarfPubNamesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002795
Devang Patel930143b2009-11-21 02:48:08 +00002796 emitDebugPubNamesPerCU(ModuleCU);
Bill Wendling480ff322009-05-20 23:21:38 +00002797}
2798
Devang Patel04d2f2d2009-11-24 01:14:22 +00002799void DwarfDebug::emitDebugPubTypes() {
Devang Patelc8654eb2009-11-24 19:18:41 +00002800 // Start the dwarf pubnames section.
2801 Asm->OutStreamer.SwitchSection(
2802 Asm->getObjFileLowering().getDwarfPubTypesSection());
Devang Patel04d2f2d2009-11-24 01:14:22 +00002803 EmitDifference("pubtypes_end", ModuleCU->getID(),
2804 "pubtypes_begin", ModuleCU->getID(), true);
2805 Asm->EOL("Length of Public Types Info");
2806
2807 EmitLabel("pubtypes_begin", ModuleCU->getID());
2808
2809 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version");
2810
2811 EmitSectionOffset("info_begin", "section_info",
2812 ModuleCU->getID(), 0, true, false);
2813 Asm->EOL("Offset of Compilation ModuleCU Info");
2814
2815 EmitDifference("info_end", ModuleCU->getID(), "info_begin", ModuleCU->getID(),
2816 true);
2817 Asm->EOL("Compilation ModuleCU Length");
2818
2819 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
2820 for (StringMap<DIE*>::const_iterator
2821 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2822 const char *Name = GI->getKeyData();
2823 DIE * Entity = GI->second;
2824
2825 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2826 Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name");
2827 }
2828
2829 Asm->EmitInt32(0); Asm->EOL("End Mark");
2830 EmitLabel("pubtypes_end", ModuleCU->getID());
2831
2832 Asm->EOL();
2833}
2834
Devang Patel930143b2009-11-21 02:48:08 +00002835/// emitDebugStr - Emit visible names into a debug str section.
Bill Wendling480ff322009-05-20 23:21:38 +00002836///
Devang Patel930143b2009-11-21 02:48:08 +00002837void DwarfDebug::emitDebugStr() {
Bill Wendling480ff322009-05-20 23:21:38 +00002838 // Check to see if it is worth the effort.
2839 if (!StringPool.empty()) {
2840 // Start the dwarf str section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002841 Asm->OutStreamer.SwitchSection(
2842 Asm->getObjFileLowering().getDwarfStrSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002843
2844 // For each of strings in the string pool.
2845 for (unsigned StringID = 1, N = StringPool.size();
2846 StringID <= N; ++StringID) {
2847 // Emit a label for reference from debug information entries.
2848 EmitLabel("string", StringID);
2849
2850 // Emit the string itself.
2851 const std::string &String = StringPool[StringID];
2852 Asm->EmitString(String); Asm->EOL();
2853 }
2854
2855 Asm->EOL();
2856 }
2857}
2858
Devang Patel930143b2009-11-21 02:48:08 +00002859/// emitDebugLoc - Emit visible names into a debug loc section.
Bill Wendling480ff322009-05-20 23:21:38 +00002860///
Devang Patel930143b2009-11-21 02:48:08 +00002861void DwarfDebug::emitDebugLoc() {
Bill Wendling480ff322009-05-20 23:21:38 +00002862 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002863 Asm->OutStreamer.SwitchSection(
2864 Asm->getObjFileLowering().getDwarfLocSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002865 Asm->EOL();
2866}
2867
2868/// EmitDebugARanges - Emit visible names into a debug aranges section.
2869///
2870void DwarfDebug::EmitDebugARanges() {
2871 // Start the dwarf aranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002872 Asm->OutStreamer.SwitchSection(
2873 Asm->getObjFileLowering().getDwarfARangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002874
2875 // FIXME - Mock up
2876#if 0
2877 CompileUnit *Unit = GetBaseCompileUnit();
2878
2879 // Don't include size of length
2880 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2881
2882 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2883
2884 EmitReference("info_begin", Unit->getID());
2885 Asm->EOL("Offset of Compilation Unit Info");
2886
2887 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2888
2889 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2890
2891 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2892 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2893
2894 // Range 1
2895 EmitReference("text_begin", 0); Asm->EOL("Address");
2896 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2897
2898 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2899 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2900#endif
2901
2902 Asm->EOL();
2903}
2904
Devang Patel930143b2009-11-21 02:48:08 +00002905/// emitDebugRanges - Emit visible names into a debug ranges section.
Bill Wendling480ff322009-05-20 23:21:38 +00002906///
Devang Patel930143b2009-11-21 02:48:08 +00002907void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002908 // Start the dwarf ranges section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002909 Asm->OutStreamer.SwitchSection(
2910 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002911 Asm->EOL();
2912}
2913
Devang Patel930143b2009-11-21 02:48:08 +00002914/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
Bill Wendling480ff322009-05-20 23:21:38 +00002915///
Devang Patel930143b2009-11-21 02:48:08 +00002916void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002917 if (const MCSection *LineInfo =
Chris Lattner1472cf52009-08-02 07:24:22 +00002918 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002919 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002920 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002921 Asm->EOL();
2922 }
2923}
2924
Devang Patel930143b2009-11-21 02:48:08 +00002925/// emitDebugInlineInfo - Emit inline info using following format.
Bill Wendling480ff322009-05-20 23:21:38 +00002926/// Section Header:
2927/// 1. length of section
2928/// 2. Dwarf version number
2929/// 3. address size.
2930///
2931/// Entries (one "entry" for each function that was inlined):
2932///
2933/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2934/// otherwise offset into __debug_str for regular function name.
2935/// 2. offset into __debug_str section for regular function name.
2936/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2937/// instances for the function.
2938///
2939/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2940/// inlined instance; the die_offset points to the inlined_subroutine die in the
2941/// __debug_info section, and the low_pc is the starting address for the
2942/// inlining instance.
Devang Patel930143b2009-11-21 02:48:08 +00002943void DwarfDebug::emitDebugInlineInfo() {
Chris Lattnere9a75a62009-08-22 21:43:10 +00002944 if (!MAI->doesDwarfUsesInlineInfoSection())
Bill Wendling480ff322009-05-20 23:21:38 +00002945 return;
2946
Devang Patel40d78412009-06-29 20:45:18 +00002947 if (!ModuleCU)
Bill Wendling480ff322009-05-20 23:21:38 +00002948 return;
2949
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002950 Asm->OutStreamer.SwitchSection(
2951 Asm->getObjFileLowering().getDwarfDebugInlineSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002952 Asm->EOL();
2953 EmitDifference("debug_inlined_end", 1,
2954 "debug_inlined_begin", 1, true);
2955 Asm->EOL("Length of Debug Inlined Information Entry");
2956
2957 EmitLabel("debug_inlined_begin", 1);
2958
2959 Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version");
2960 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2961
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002962 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2963 E = InlinedSPNodes.end(); I != E; ++I) {
Jim Grosbach042483e2009-11-21 23:12:12 +00002964
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002965// for (ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
2966 // I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) {
2967 MDNode *Node = *I;
Jim Grosbach00e9c612009-11-22 19:20:36 +00002968 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
2969 = InlineInfo.find(Node);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002970 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
Devang Patel80ae3492009-08-28 23:24:31 +00002971 DISubprogram SP(Node);
Devang Patel2d9caf92009-11-25 17:36:49 +00002972 StringRef LName = SP.getLinkageName();
2973 StringRef Name = SP.getName();
Bill Wendling480ff322009-05-20 23:21:38 +00002974
Devang Patel2d9caf92009-11-25 17:36:49 +00002975 if (LName.empty())
Devang Patelf281bf82009-07-16 01:01:22 +00002976 Asm->EmitString(Name);
2977 else {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002978 // Skip special LLVM prefix that is used to inform the asm printer to not
2979 // emit usual symbol prefix before the symbol name. This happens for
2980 // Objective-C symbol names and symbol whose name is replaced using GCC's
2981 // __asm__ attribute.
Devang Patelf281bf82009-07-16 01:01:22 +00002982 if (LName[0] == 1)
Benjamin Kramer4cd30812009-11-25 18:26:09 +00002983 LName = LName.substr(1);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002984// Asm->EmitString(LName);
2985 EmitSectionOffset("string", "section_str",
2986 StringPool.idFor(LName), false, true);
2987
Devang Patelf281bf82009-07-16 01:01:22 +00002988 }
Bill Wendling480ff322009-05-20 23:21:38 +00002989 Asm->EOL("MIPS linkage name");
Jim Grosbach042483e2009-11-21 23:12:12 +00002990// Asm->EmitString(Name);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002991 EmitSectionOffset("string", "section_str",
2992 StringPool.idFor(Name), false, true);
2993 Asm->EOL("Function name");
Bill Wendling480ff322009-05-20 23:21:38 +00002994 Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count");
2995
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002996 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
Bill Wendling480ff322009-05-20 23:21:38 +00002997 LE = Labels.end(); LI != LE; ++LI) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +00002998 DIE *SP = LI->second;
Bill Wendling480ff322009-05-20 23:21:38 +00002999 Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset");
3000
3001 if (TD->getPointerSize() == sizeof(int32_t))
Chris Lattnere9a75a62009-08-22 21:43:10 +00003002 O << MAI->getData32bitsDirective();
Bill Wendling480ff322009-05-20 23:21:38 +00003003 else
Chris Lattnere9a75a62009-08-22 21:43:10 +00003004 O << MAI->getData64bitsDirective();
Bill Wendling480ff322009-05-20 23:21:38 +00003005
Devang Patelf6eeaeb2009-11-10 23:06:00 +00003006 PrintLabelName("label", LI->first); Asm->EOL("low_pc");
Bill Wendling480ff322009-05-20 23:21:38 +00003007 }
3008 }
3009
3010 EmitLabel("debug_inlined_end", 1);
3011 Asm->EOL();
3012}