blob: 3ce0711a5203c0358a25b60f3eef1268d0ab12b4 [file] [log] [blame]
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001//===-- 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//===----------------------------------------------------------------------===//
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070013
Shih-wei Liaoe264f622010-02-10 11:10:31 -080014#define DEBUG_TYPE "dwarfdebug"
15#include "DwarfDebug.h"
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070016#include "DIE.h"
17#include "llvm/Constants.h"
Shih-wei Liaoe264f622010-02-10 11:10:31 -080018#include "llvm/Module.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineModuleInfo.h"
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070021#include "llvm/MC/MCAsmInfo.h"
Shih-wei Liaoe264f622010-02-10 11:10:31 -080022#include "llvm/MC/MCSection.h"
23#include "llvm/MC/MCStreamer.h"
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070024#include "llvm/MC/MCSymbol.h"
Shih-wei Liaoe264f622010-02-10 11:10:31 -080025#include "llvm/Target/Mangler.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
28#include "llvm/Target/TargetLoweringObjectFile.h"
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070029#include "llvm/Target/TargetMachine.h"
Shih-wei Liaoe264f622010-02-10 11:10:31 -080030#include "llvm/Target/TargetRegisterInfo.h"
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070031#include "llvm/Target/TargetOptions.h"
32#include "llvm/Analysis/DebugInfo.h"
33#include "llvm/ADT/STLExtras.h"
Shih-wei Liaoe264f622010-02-10 11:10:31 -080034#include "llvm/ADT/StringExtras.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/ValueHandle.h"
38#include "llvm/Support/FormattedStream.h"
39#include "llvm/Support/Timer.h"
40#include "llvm/System/Path.h"
41using namespace llvm;
42
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070043namespace {
44 const char *DWARFGroupName = "DWARF Emission";
45 const char *DbgTimerName = "DWARF Debug Writer";
46} // end anonymous namespace
47
Shih-wei Liaoe264f622010-02-10 11:10:31 -080048//===----------------------------------------------------------------------===//
49
50/// Configuration values for initial hash set sizes (log2).
51///
52static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
53
54namespace llvm {
55
56//===----------------------------------------------------------------------===//
57/// CompileUnit - This dwarf writer support class manages information associate
58/// with a source file.
59class CompileUnit {
60 /// ID - File identifier for source.
61 ///
62 unsigned ID;
63
64 /// Die - Compile unit debug information entry.
65 ///
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070066 const OwningPtr<DIE> CUDie;
Shih-wei Liaoe264f622010-02-10 11:10:31 -080067
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070068 /// IndexTyDie - An anonymous type for index type. Owned by CUDie.
Shih-wei Liaoe264f622010-02-10 11:10:31 -080069 DIE *IndexTyDie;
70
71 /// GVToDieMap - Tracks the mapping of unit level debug informaton
72 /// variables to debug information entries.
73 /// FIXME : Rename GVToDieMap -> NodeToDieMap
74 DenseMap<MDNode *, DIE *> GVToDieMap;
75
76 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
77 /// descriptors to debug information entries using a DIEEntry proxy.
78 /// FIXME : Rename
79 DenseMap<MDNode *, DIEEntry *> GVToDIEEntryMap;
80
81 /// Globals - A map of globally visible named entities for this unit.
82 ///
83 StringMap<DIE*> Globals;
84
85 /// GlobalTypes - A map of globally visible types for this unit.
86 ///
87 StringMap<DIE*> GlobalTypes;
88
89public:
90 CompileUnit(unsigned I, DIE *D)
91 : ID(I), CUDie(D), IndexTyDie(0) {}
Shih-wei Liaoe264f622010-02-10 11:10:31 -080092
93 // Accessors.
94 unsigned getID() const { return ID; }
Shih-wei Liao7abe37e2010-04-28 01:47:00 -070095 DIE* getCUDie() const { return CUDie.get(); }
Shih-wei Liaoe264f622010-02-10 11:10:31 -080096 const StringMap<DIE*> &getGlobals() const { return Globals; }
97 const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; }
98
99 /// hasContent - Return true if this compile unit has something to write out.
100 ///
101 bool hasContent() const { return !CUDie->getChildren().empty(); }
102
103 /// addGlobal - Add a new global entity to the compile unit.
104 ///
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700105 void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; }
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800106
107 /// addGlobalType - Add a new global type to the compile unit.
108 ///
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700109 void addGlobalType(StringRef Name, DIE *Die) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800110 GlobalTypes[Name] = Die;
111 }
112
113 /// getDIE - Returns the debug information entry map slot for the
114 /// specified debug variable.
115 DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); }
116
117 /// insertDIE - Insert DIE into the map.
118 void insertDIE(MDNode *N, DIE *D) {
119 GVToDieMap.insert(std::make_pair(N, D));
120 }
121
122 /// getDIEEntry - Returns the debug information entry for the speciefied
123 /// debug variable.
124 DIEEntry *getDIEEntry(MDNode *N) {
125 DenseMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N);
126 if (I == GVToDIEEntryMap.end())
127 return NULL;
128 return I->second;
129 }
130
131 /// insertDIEEntry - Insert debug information entry into the map.
132 void insertDIEEntry(MDNode *N, DIEEntry *E) {
133 GVToDIEEntryMap.insert(std::make_pair(N, E));
134 }
135
136 /// addDie - Adds or interns the DIE to the compile unit.
137 ///
138 void addDie(DIE *Buffer) {
139 this->CUDie->addChild(Buffer);
140 }
141
142 // getIndexTyDie - Get an anonymous type for index type.
143 DIE *getIndexTyDie() {
144 return IndexTyDie;
145 }
146
147 // setIndexTyDie - Set D as anonymous type for index which can be reused
148 // later.
149 void setIndexTyDie(DIE *D) {
150 IndexTyDie = D;
151 }
152
153};
154
155//===----------------------------------------------------------------------===//
156/// DbgVariable - This class is used to track local variable information.
157///
158class DbgVariable {
159 DIVariable Var; // Variable Descriptor.
160 unsigned FrameIndex; // Variable frame index.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700161 const MachineInstr *DbgValueMInsn; // DBG_VALUE
162 // DbgValueLabel - DBG_VALUE is effective from this label.
163 MCSymbol *DbgValueLabel;
164 DbgVariable *const AbstractVar; // Abstract variable for this variable.
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800165 DIE *TheDIE;
166public:
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700167 // AbsVar may be NULL.
168 DbgVariable(DIVariable V, unsigned I, DbgVariable *AbsVar)
169 : Var(V), FrameIndex(I), DbgValueMInsn(0),
170 DbgValueLabel(0), AbstractVar(AbsVar), TheDIE(0) {}
171 DbgVariable(DIVariable V, const MachineInstr *MI, DbgVariable *AbsVar)
172 : Var(V), FrameIndex(0), DbgValueMInsn(MI), DbgValueLabel(0),
173 AbstractVar(AbsVar), TheDIE(0)
174 {}
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800175
176 // Accessors.
177 DIVariable getVariable() const { return Var; }
178 unsigned getFrameIndex() const { return FrameIndex; }
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700179 const MachineInstr *getDbgValue() const { return DbgValueMInsn; }
180 MCSymbol *getDbgValueLabel() const { return DbgValueLabel; }
181 void setDbgValueLabel(MCSymbol *L) { DbgValueLabel = L; }
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800182 DbgVariable *getAbstractVariable() const { return AbstractVar; }
183 void setDIE(DIE *D) { TheDIE = D; }
184 DIE *getDIE() const { return TheDIE; }
185};
186
187//===----------------------------------------------------------------------===//
188/// DbgScope - This class is used to track scope information.
189///
190class DbgScope {
191 DbgScope *Parent; // Parent to this scope.
192 DIDescriptor Desc; // Debug info descriptor for scope.
193 // Location at which this scope is inlined.
194 AssertingVH<MDNode> InlinedAtLocation;
195 bool AbstractScope; // Abstract Scope
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800196 const MachineInstr *LastInsn; // Last instruction of this scope.
197 const MachineInstr *FirstInsn; // First instruction of this scope.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700198 // Scopes defined in scope. Contents not owned.
199 SmallVector<DbgScope *, 4> Scopes;
200 // Variables declared in scope. Contents owned.
201 SmallVector<DbgVariable *, 8> Variables;
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800202
203 // Private state for dump()
204 mutable unsigned IndentLevel;
205public:
206 DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0)
207 : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800208 LastInsn(0), FirstInsn(0), IndentLevel(0) {}
209 virtual ~DbgScope();
210
211 // Accessors.
212 DbgScope *getParent() const { return Parent; }
213 void setParent(DbgScope *P) { Parent = P; }
214 DIDescriptor getDesc() const { return Desc; }
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700215 MDNode *getInlinedAt() const { return InlinedAtLocation; }
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800216 MDNode *getScopeNode() const { return Desc.getNode(); }
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700217 const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
218 const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800219 void setLastInsn(const MachineInstr *MI) { LastInsn = MI; }
220 const MachineInstr *getLastInsn() { return LastInsn; }
221 void setFirstInsn(const MachineInstr *MI) { FirstInsn = MI; }
222 void setAbstractScope() { AbstractScope = true; }
223 bool isAbstractScope() const { return AbstractScope; }
224 const MachineInstr *getFirstInsn() { return FirstInsn; }
225
226 /// addScope - Add a scope to the scope.
227 ///
228 void addScope(DbgScope *S) { Scopes.push_back(S); }
229
230 /// addVariable - Add a variable to the scope.
231 ///
232 void addVariable(DbgVariable *V) { Variables.push_back(V); }
233
234 void fixInstructionMarkers(DenseMap<const MachineInstr *,
235 unsigned> &MIIndexMap) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700236 assert(getFirstInsn() && "First instruction is missing!");
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800237
238 // Use the end of last child scope as end of this scope.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700239 const SmallVector<DbgScope *, 4> &Scopes = getScopes();
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800240 const MachineInstr *LastInsn = getFirstInsn();
241 unsigned LIndex = 0;
242 if (Scopes.empty()) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700243 assert(getLastInsn() && "Inner most scope does not have last insn!");
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800244 return;
245 }
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700246 for (SmallVector<DbgScope *, 4>::const_iterator SI = Scopes.begin(),
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800247 SE = Scopes.end(); SI != SE; ++SI) {
248 DbgScope *DS = *SI;
249 DS->fixInstructionMarkers(MIIndexMap);
250 const MachineInstr *DSLastInsn = DS->getLastInsn();
251 unsigned DSI = MIIndexMap[DSLastInsn];
252 if (DSI > LIndex) {
253 LastInsn = DSLastInsn;
254 LIndex = DSI;
255 }
256 }
Shih-wei Liaoe4454322010-04-07 12:21:42 -0700257
258 unsigned CurrentLastInsnIndex = 0;
259 if (const MachineInstr *CL = getLastInsn())
260 CurrentLastInsnIndex = MIIndexMap[CL];
261 unsigned FIndex = MIIndexMap[getFirstInsn()];
262
263 // Set LastInsn as the last instruction for this scope only if
264 // it follows
265 // 1) this scope's first instruction and
266 // 2) current last instruction for this scope, if any.
267 if (LIndex >= CurrentLastInsnIndex && LIndex >= FIndex)
268 setLastInsn(LastInsn);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800269 }
270
271#ifndef NDEBUG
272 void dump() const;
273#endif
274};
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700275
276} // end llvm namespace
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800277
278#ifndef NDEBUG
279void DbgScope::dump() const {
280 raw_ostream &err = dbgs();
281 err.indent(IndentLevel);
282 MDNode *N = Desc.getNode();
283 N->dump();
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800284 if (AbstractScope)
285 err << "Abstract Scope\n";
286
287 IndentLevel += 2;
288 if (!Scopes.empty())
289 err << "Children ...\n";
290 for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
291 if (Scopes[i] != this)
292 Scopes[i]->dump();
293
294 IndentLevel -= 2;
295}
296#endif
297
298DbgScope::~DbgScope() {
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800299 for (unsigned j = 0, M = Variables.size(); j < M; ++j)
300 delete Variables[j];
301}
302
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700303DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
304 : Asm(A), MMI(Asm->MMI), ModuleCU(0),
305 AbbreviationsSet(InitAbbreviationsSetSize),
306 CurrentFnDbgScope(0), PrevLabel(NULL) {
307 NextStringPoolNumber = 0;
308
309 DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
310 DwarfStrSectionSym = TextSectionSym = 0;
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800311
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700312 if (TimePassesIsEnabled) {
313 NamedRegionTimer T(DbgTimerName, DWARFGroupName);
314 beginModule(M);
315 } else {
316 beginModule(M);
317 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800318}
319DwarfDebug::~DwarfDebug() {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700320 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
321 DIEBlocks[j]->~DIEBlock();
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800322}
323
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700324MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
325 std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
326 if (Entry.first) return Entry.first;
327
328 Entry.second = NextStringPoolNumber++;
329 return Entry.first = Asm->GetTempSymbol("string", Entry.second);
330}
331
332
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800333/// assignAbbrevNumber - Define a unique number for the abbreviation.
334///
335void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
336 // Profile the node so that we can make it unique.
337 FoldingSetNodeID ID;
338 Abbrev.Profile(ID);
339
340 // Check the set for priors.
341 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
342
343 // If it's newly added.
344 if (InSet == &Abbrev) {
345 // Add to abbreviation list.
346 Abbreviations.push_back(&Abbrev);
347
348 // Assign the vector position + 1 as its number.
349 Abbrev.setNumber(Abbreviations.size());
350 } else {
351 // Assign existing abbreviation number.
352 Abbrev.setNumber(InSet->getNumber());
353 }
354}
355
356/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
357/// information entry.
358DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700359 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800360 return Value;
361}
362
363/// addUInt - Add an unsigned integer attribute data and value.
364///
365void DwarfDebug::addUInt(DIE *Die, unsigned Attribute,
366 unsigned Form, uint64_t Integer) {
367 if (!Form) Form = DIEInteger::BestForm(false, Integer);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700368 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800369 Die->addValue(Attribute, Form, Value);
370}
371
372/// addSInt - Add an signed integer attribute data and value.
373///
374void DwarfDebug::addSInt(DIE *Die, unsigned Attribute,
375 unsigned Form, int64_t Integer) {
376 if (!Form) Form = DIEInteger::BestForm(true, Integer);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700377 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800378 Die->addValue(Attribute, Form, Value);
379}
380
381/// addString - Add a string attribute data and value. DIEString only
382/// keeps string reference.
383void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form,
384 StringRef String) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700385 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800386 Die->addValue(Attribute, Form, Value);
387}
388
389/// addLabel - Add a Dwarf label attribute data and value.
390///
391void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700392 const MCSymbol *Label) {
393 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800394 Die->addValue(Attribute, Form, Value);
395}
396
397/// addDelta - Add a label delta attribute data and value.
398///
399void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700400 const MCSymbol *Hi, const MCSymbol *Lo) {
401 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800402 Die->addValue(Attribute, Form, Value);
403}
404
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700405/// addDIEEntry - Add a DIE attribute data and value.
406///
407void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
408 DIE *Entry) {
409 Die->addValue(Attribute, Form, createDIEEntry(Entry));
410}
411
412
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800413/// addBlock - Add block data.
414///
415void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
416 DIEBlock *Block) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700417 Block->ComputeSize(Asm);
418 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800419 Die->addValue(Attribute, Block->BestForm(), Block);
420}
421
422/// addSourceLine - Add location information to specified debug information
423/// entry.
424void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) {
425 // If there is no compile unit specified, don't add a line #.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700426 if (!V->getCompileUnit().Verify())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800427 return;
428
429 unsigned Line = V->getLineNumber();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700430 unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(),
431 V->getContext().getFilename());
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800432 assert(FileID && "Invalid file id");
433 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
434 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
435}
436
437/// addSourceLine - Add location information to specified debug information
438/// entry.
439void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) {
440 // If there is no compile unit specified, don't add a line #.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700441 if (!G->getCompileUnit().Verify())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800442 return;
443
444 unsigned Line = G->getLineNumber();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700445 unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(),
446 G->getContext().getFilename());
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800447 assert(FileID && "Invalid file id");
448 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
449 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
450}
451
452/// addSourceLine - Add location information to specified debug information
453/// entry.
454void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) {
455 // If there is no compile unit specified, don't add a line #.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700456 if (!SP->getCompileUnit().Verify())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800457 return;
458 // If the line number is 0, don't add it.
459 if (SP->getLineNumber() == 0)
460 return;
461
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800462 unsigned Line = SP->getLineNumber();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700463 if (!SP->getContext().Verify())
464 return;
465 unsigned FileID = GetOrCreateSourceID(SP->getDirectory(),
466 SP->getFilename());
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800467 assert(FileID && "Invalid file id");
468 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
469 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
470}
471
472/// addSourceLine - Add location information to specified debug information
473/// entry.
474void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) {
475 // If there is no compile unit specified, don't add a line #.
476 DICompileUnit CU = Ty->getCompileUnit();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700477 if (!CU.Verify())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800478 return;
479
480 unsigned Line = Ty->getLineNumber();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700481 if (!Ty->getContext().Verify())
482 return;
483 unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(),
484 Ty->getContext().getFilename());
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800485 assert(FileID && "Invalid file id");
486 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
487 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
488}
489
490/// addSourceLine - Add location information to specified debug information
491/// entry.
492void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) {
493 // If there is no compile unit specified, don't add a line #.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700494 if (!NS->getCompileUnit().Verify())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800495 return;
496
497 unsigned Line = NS->getLineNumber();
498 StringRef FN = NS->getFilename();
499 StringRef Dir = NS->getDirectory();
500
501 unsigned FileID = GetOrCreateSourceID(Dir, FN);
502 assert(FileID && "Invalid file id");
503 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
504 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
505}
506
507/* Byref variables, in Blocks, are declared by the programmer as
508 "SomeType VarName;", but the compiler creates a
509 __Block_byref_x_VarName struct, and gives the variable VarName
510 either the struct, or a pointer to the struct, as its type. This
511 is necessary for various behind-the-scenes things the compiler
512 needs to do with by-reference variables in blocks.
513
514 However, as far as the original *programmer* is concerned, the
515 variable should still have type 'SomeType', as originally declared.
516
517 The following function dives into the __Block_byref_x_VarName
518 struct to find the original type of the variable. This will be
519 passed back to the code generating the type for the Debug
520 Information Entry for the variable 'VarName'. 'VarName' will then
521 have the original type 'SomeType' in its debug information.
522
523 The original type 'SomeType' will be the type of the field named
524 'VarName' inside the __Block_byref_x_VarName struct.
525
526 NOTE: In order for this to not completely fail on the debugger
527 side, the Debug Information Entry for the variable VarName needs to
528 have a DW_AT_location that tells the debugger how to unwind through
529 the pointers and __Block_byref_x_VarName struct to find the actual
530 value of the variable. The function addBlockByrefType does this. */
531
532/// Find the type the programmer originally declared the variable to be
533/// and return that type.
534///
535DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) {
536
537 DIType subType = Ty;
538 unsigned tag = Ty.getTag();
539
540 if (tag == dwarf::DW_TAG_pointer_type) {
541 DIDerivedType DTy = DIDerivedType(Ty.getNode());
542 subType = DTy.getTypeDerivedFrom();
543 }
544
545 DICompositeType blockStruct = DICompositeType(subType.getNode());
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800546 DIArray Elements = blockStruct.getTypeArray();
547
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800548 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
549 DIDescriptor Element = Elements.getElement(i);
550 DIDerivedType DT = DIDerivedType(Element.getNode());
551 if (Name == DT.getName())
552 return (DT.getTypeDerivedFrom());
553 }
554
555 return Ty;
556}
557
558/// addComplexAddress - Start with the address based on the location provided,
559/// and generate the DWARF information necessary to find the actual variable
560/// given the extra address information encoded in the DIVariable, starting from
561/// the starting location. Add the DWARF information to the die.
562///
563void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die,
564 unsigned Attribute,
565 const MachineLocation &Location) {
566 const DIVariable &VD = DV->getVariable();
567 DIType Ty = VD.getType();
568
569 // Decode the original location, and use that as the start of the byref
570 // variable's location.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700571 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800572 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700573 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800574
575 if (Location.isReg()) {
576 if (Reg < 32) {
577 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
578 } else {
579 Reg = Reg - dwarf::DW_OP_reg0;
580 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
581 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
582 }
583 } else {
584 if (Reg < 32)
585 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
586 else {
587 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
588 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
589 }
590
591 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
592 }
593
594 for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) {
595 uint64_t Element = VD.getAddrElement(i);
596
597 if (Element == DIFactory::OpPlus) {
598 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
599 addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i));
600 } else if (Element == DIFactory::OpDeref) {
601 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
602 } else llvm_unreachable("unknown DIFactory Opcode");
603 }
604
605 // Now attach the location information to the DIE.
606 addBlock(Die, Attribute, 0, Block);
607}
608
609/* Byref variables, in Blocks, are declared by the programmer as "SomeType
610 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
611 gives the variable VarName either the struct, or a pointer to the struct, as
612 its type. This is necessary for various behind-the-scenes things the
613 compiler needs to do with by-reference variables in Blocks.
614
615 However, as far as the original *programmer* is concerned, the variable
616 should still have type 'SomeType', as originally declared.
617
618 The function getBlockByrefType dives into the __Block_byref_x_VarName
619 struct to find the original type of the variable, which is then assigned to
620 the variable's Debug Information Entry as its real type. So far, so good.
621 However now the debugger will expect the variable VarName to have the type
622 SomeType. So we need the location attribute for the variable to be an
623 expression that explains to the debugger how to navigate through the
624 pointers and struct to find the actual variable of type SomeType.
625
626 The following function does just that. We start by getting
627 the "normal" location for the variable. This will be the location
628 of either the struct __Block_byref_x_VarName or the pointer to the
629 struct __Block_byref_x_VarName.
630
631 The struct will look something like:
632
633 struct __Block_byref_x_VarName {
634 ... <various fields>
635 struct __Block_byref_x_VarName *forwarding;
636 ... <various other fields>
637 SomeType VarName;
638 ... <maybe more fields>
639 };
640
641 If we are given the struct directly (as our starting point) we
642 need to tell the debugger to:
643
644 1). Add the offset of the forwarding field.
645
646 2). Follow that pointer to get the real __Block_byref_x_VarName
647 struct to use (the real one may have been copied onto the heap).
648
649 3). Add the offset for the field VarName, to find the actual variable.
650
651 If we started with a pointer to the struct, then we need to
652 dereference that pointer first, before the other steps.
653 Translating this into DWARF ops, we will need to append the following
654 to the current location description for the variable:
655
656 DW_OP_deref -- optional, if we start with a pointer
657 DW_OP_plus_uconst <forward_fld_offset>
658 DW_OP_deref
659 DW_OP_plus_uconst <varName_fld_offset>
660
661 That is what this function does. */
662
663/// addBlockByrefAddress - Start with the address based on the location
664/// provided, and generate the DWARF information necessary to find the
665/// actual Block variable (navigating the Block struct) based on the
666/// starting location. Add the DWARF information to the die. For
667/// more information, read large comment just above here.
668///
669void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
670 unsigned Attribute,
671 const MachineLocation &Location) {
672 const DIVariable &VD = DV->getVariable();
673 DIType Ty = VD.getType();
674 DIType TmpTy = Ty;
675 unsigned Tag = Ty.getTag();
676 bool isPointer = false;
677
678 StringRef varName = VD.getName();
679
680 if (Tag == dwarf::DW_TAG_pointer_type) {
681 DIDerivedType DTy = DIDerivedType(Ty.getNode());
682 TmpTy = DTy.getTypeDerivedFrom();
683 isPointer = true;
684 }
685
686 DICompositeType blockStruct = DICompositeType(TmpTy.getNode());
687
688 // Find the __forwarding field and the variable field in the __Block_byref
689 // struct.
690 DIArray Fields = blockStruct.getTypeArray();
691 DIDescriptor varField = DIDescriptor();
692 DIDescriptor forwardingField = DIDescriptor();
693
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800694 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
695 DIDescriptor Element = Fields.getElement(i);
696 DIDerivedType DT = DIDerivedType(Element.getNode());
697 StringRef fieldName = DT.getName();
698 if (fieldName == "__forwarding")
699 forwardingField = Element;
700 else if (fieldName == varName)
701 varField = Element;
702 }
703
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800704 // Get the offsets for the forwarding field and the variable field.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700705 unsigned forwardingFieldOffset =
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800706 DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700707 unsigned varFieldOffset =
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800708 DIDerivedType(varField.getNode()).getOffsetInBits() >> 3;
709
710 // Decode the original location, and use that as the start of the byref
711 // variable's location.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700712 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800713 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700714 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800715
716 if (Location.isReg()) {
717 if (Reg < 32)
718 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
719 else {
720 Reg = Reg - dwarf::DW_OP_reg0;
721 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
722 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
723 }
724 } else {
725 if (Reg < 32)
726 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
727 else {
728 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
729 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
730 }
731
732 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
733 }
734
735 // If we started with a pointer to the __Block_byref... struct, then
736 // the first thing we need to do is dereference the pointer (DW_OP_deref).
737 if (isPointer)
738 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
739
740 // Next add the offset for the '__forwarding' field:
741 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
742 // adding the offset if it's 0.
743 if (forwardingFieldOffset > 0) {
744 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
745 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
746 }
747
748 // Now dereference the __forwarding field to get to the real __Block_byref
749 // struct: DW_OP_deref.
750 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
751
752 // Now that we've got the real __Block_byref... struct, add the offset
753 // for the variable's field to get to the location of the actual variable:
754 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
755 if (varFieldOffset > 0) {
756 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
757 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
758 }
759
760 // Now attach the location information to the DIE.
761 addBlock(Die, Attribute, 0, Block);
762}
763
764/// addAddress - Add an address attribute to a die based on the location
765/// provided.
766void DwarfDebug::addAddress(DIE *Die, unsigned Attribute,
767 const MachineLocation &Location) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700768 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800769 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700770 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800771
772 if (Location.isReg()) {
773 if (Reg < 32) {
774 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
775 } else {
776 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
777 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
778 }
779 } else {
780 if (Reg < 32) {
781 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
782 } else {
783 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
784 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
785 }
786
787 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
788 }
789
790 addBlock(Die, Attribute, 0, Block);
791}
792
793/// addToContextOwner - Add Die into the list of its context owner's children.
794void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700795 if (Context.isType()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800796 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode()));
797 ContextDIE->addChild(Die);
798 } else if (Context.isNameSpace()) {
799 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode()));
800 ContextDIE->addChild(Die);
801 } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode()))
802 ContextDIE->addChild(Die);
803 else
804 ModuleCU->addDie(Die);
805}
806
807/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
808/// given DIType.
809DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) {
810 DIE *TyDIE = ModuleCU->getDIE(Ty.getNode());
811 if (TyDIE)
812 return TyDIE;
813
814 // Create new type.
815 TyDIE = new DIE(dwarf::DW_TAG_base_type);
816 ModuleCU->insertDIE(Ty.getNode(), TyDIE);
817 if (Ty.isBasicType())
818 constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode()));
819 else if (Ty.isCompositeType())
820 constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode()));
821 else {
822 assert(Ty.isDerivedType() && "Unknown kind of DIType");
823 constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode()));
824 }
825
826 addToContextOwner(TyDIE, Ty.getContext());
827 return TyDIE;
828}
829
830/// addType - Add a new type attribute to the specified entity.
831void DwarfDebug::addType(DIE *Entity, DIType Ty) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700832 if (!Ty.isValid())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800833 return;
834
835 // Check for pre-existence.
836 DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode());
837 // If it exists then use the existing value.
838 if (Entry) {
839 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
840 return;
841 }
842
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800843 // Construct type.
844 DIE *Buffer = getOrCreateTypeDIE(Ty);
845
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700846 // Set up proxy.
847 Entry = createDIEEntry(Buffer);
848 ModuleCU->insertDIEEntry(Ty.getNode(), Entry);
849
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800850 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
851}
852
853/// constructTypeDIE - Construct basic type die from DIBasicType.
854void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
855 // Get core information.
856 StringRef Name = BTy.getName();
857 Buffer.setTag(dwarf::DW_TAG_base_type);
858 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
859 BTy.getEncoding());
860
861 // Add name if not anonymous or intermediate type.
862 if (!Name.empty())
863 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
864 uint64_t Size = BTy.getSizeInBits() >> 3;
865 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
866}
867
868/// constructTypeDIE - Construct derived type die from DIDerivedType.
869void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
870 // Get core information.
871 StringRef Name = DTy.getName();
872 uint64_t Size = DTy.getSizeInBits() >> 3;
873 unsigned Tag = DTy.getTag();
874
875 // FIXME - Workaround for templates.
876 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
877
878 Buffer.setTag(Tag);
879
880 // Map to main type, void will not have a type.
881 DIType FromTy = DTy.getTypeDerivedFrom();
882 addType(&Buffer, FromTy);
883
884 // Add name if not anonymous or intermediate type.
885 if (!Name.empty())
886 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
887
888 // Add size if non-zero (derived types might be zero-sized.)
889 if (Size)
890 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
891
892 // Add source line info if available and TyDesc is not a forward declaration.
893 if (!DTy.isForwardDecl())
894 addSourceLine(&Buffer, &DTy);
895}
896
897/// constructTypeDIE - Construct type DIE from DICompositeType.
898void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
899 // Get core information.
900 StringRef Name = CTy.getName();
901
902 uint64_t Size = CTy.getSizeInBits() >> 3;
903 unsigned Tag = CTy.getTag();
904 Buffer.setTag(Tag);
905
906 switch (Tag) {
907 case dwarf::DW_TAG_vector_type:
908 case dwarf::DW_TAG_array_type:
909 constructArrayTypeDIE(Buffer, &CTy);
910 break;
911 case dwarf::DW_TAG_enumeration_type: {
912 DIArray Elements = CTy.getTypeArray();
913
914 // Add enumerators to enumeration type.
915 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
916 DIE *ElemDie = NULL;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700917 DIDescriptor Enum(Elements.getElement(i).getNode());
918 if (Enum.isEnumerator()) {
919 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum.getNode()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800920 Buffer.addChild(ElemDie);
921 }
922 }
923 }
924 break;
925 case dwarf::DW_TAG_subroutine_type: {
926 // Add return type.
927 DIArray Elements = CTy.getTypeArray();
928 DIDescriptor RTy = Elements.getElement(0);
929 addType(&Buffer, DIType(RTy.getNode()));
930
931 // Add prototype flag.
932 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
933
934 // Add arguments.
935 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
936 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
937 DIDescriptor Ty = Elements.getElement(i);
938 addType(Arg, DIType(Ty.getNode()));
939 Buffer.addChild(Arg);
940 }
941 }
942 break;
943 case dwarf::DW_TAG_structure_type:
944 case dwarf::DW_TAG_union_type:
945 case dwarf::DW_TAG_class_type: {
946 // Add elements to structure type.
947 DIArray Elements = CTy.getTypeArray();
948
949 // A forward struct declared type may not have elements available.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700950 unsigned N = Elements.getNumElements();
951 if (N == 0)
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800952 break;
953
954 // Add elements to structure type.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700955 for (unsigned i = 0; i < N; ++i) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800956 DIDescriptor Element = Elements.getElement(i);
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800957 DIE *ElemDie = NULL;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700958 if (Element.isSubprogram())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800959 ElemDie = createSubprogramDIE(DISubprogram(Element.getNode()));
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700960 else if (Element.isVariable()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800961 DIVariable DV(Element.getNode());
962 ElemDie = new DIE(dwarf::DW_TAG_variable);
963 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
964 DV.getName());
965 addType(ElemDie, DV.getType());
966 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
967 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
968 addSourceLine(ElemDie, &DV);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700969 } else if (Element.isDerivedType())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800970 ElemDie = createMemberDIE(DIDerivedType(Element.getNode()));
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700971 else
972 continue;
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800973 Buffer.addChild(ElemDie);
974 }
975
976 if (CTy.isAppleBlockExtension())
977 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
978
979 unsigned RLang = CTy.getRunTimeLang();
980 if (RLang)
981 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
982 dwarf::DW_FORM_data1, RLang);
983
984 DICompositeType ContainingType = CTy.getContainingType();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -0700985 if (DIDescriptor(ContainingType.getNode()).isCompositeType())
Shih-wei Liaoe264f622010-02-10 11:10:31 -0800986 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
987 getOrCreateTypeDIE(DIType(ContainingType.getNode())));
988 break;
989 }
990 default:
991 break;
992 }
993
994 // Add name if not anonymous or intermediate type.
995 if (!Name.empty())
996 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
997
998 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type ||
999 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
1000 // Add size if non-zero (derived types might be zero-sized.)
1001 if (Size)
1002 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
1003 else {
1004 // Add zero size if it is not a forward declaration.
1005 if (CTy.isForwardDecl())
1006 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1007 else
1008 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1009 }
1010
1011 // Add source line info if available.
1012 if (!CTy.isForwardDecl())
1013 addSourceLine(&Buffer, &CTy);
1014 }
1015}
1016
1017/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1018void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
1019 int64_t L = SR.getLo();
1020 int64_t H = SR.getHi();
1021 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1022
1023 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
1024 if (L)
1025 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
1026 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
1027
1028 Buffer.addChild(DW_Subrange);
1029}
1030
1031/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1032void DwarfDebug::constructArrayTypeDIE(DIE &Buffer,
1033 DICompositeType *CTy) {
1034 Buffer.setTag(dwarf::DW_TAG_array_type);
1035 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
1036 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
1037
1038 // Emit derived type.
1039 addType(&Buffer, CTy->getTypeDerivedFrom());
1040 DIArray Elements = CTy->getTypeArray();
1041
1042 // Get an anonymous type for index type.
1043 DIE *IdxTy = ModuleCU->getIndexTyDie();
1044 if (!IdxTy) {
1045 // Construct an anonymous type for index type.
1046 IdxTy = new DIE(dwarf::DW_TAG_base_type);
1047 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1048 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1049 dwarf::DW_ATE_signed);
1050 ModuleCU->addDie(IdxTy);
1051 ModuleCU->setIndexTyDie(IdxTy);
1052 }
1053
1054 // Add subranges to array type.
1055 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1056 DIDescriptor Element = Elements.getElement(i);
1057 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1058 constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy);
1059 }
1060}
1061
1062/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001063DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001064 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001065 StringRef Name = ETy.getName();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001066 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001067 int64_t Value = ETy.getEnumValue();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001068 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1069 return Enumerator;
1070}
1071
1072/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1073/// printer to not emit usual symbol prefix before the symbol name is used then
1074/// return linkage name after skipping this special LLVM prefix.
1075static StringRef getRealLinkageName(StringRef LinkageName) {
1076 char One = '\1';
1077 if (LinkageName.startswith(StringRef(&One, 1)))
1078 return LinkageName.substr(1);
1079 return LinkageName;
1080}
1081
1082/// createGlobalVariableDIE - Create new DIE using GV.
1083DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) {
1084 // If the global variable was optmized out then no need to create debug info
1085 // entry.
1086 if (!GV.getGlobal()) return NULL;
1087 if (GV.getDisplayName().empty()) return NULL;
1088
1089 DIE *GVDie = new DIE(dwarf::DW_TAG_variable);
1090 addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1091 GV.getDisplayName());
1092
1093 StringRef LinkageName = GV.getLinkageName();
1094 if (!LinkageName.empty())
1095 addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
1096 getRealLinkageName(LinkageName));
1097
1098 addType(GVDie, GV.getType());
1099 if (!GV.isLocalToUnit())
1100 addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1101 addSourceLine(GVDie, &GV);
1102
1103 return GVDie;
1104}
1105
1106/// createMemberDIE - Create new member DIE.
1107DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) {
1108 DIE *MemberDie = new DIE(DT.getTag());
1109 StringRef Name = DT.getName();
1110 if (!Name.empty())
1111 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1112
1113 addType(MemberDie, DT.getTypeDerivedFrom());
1114
1115 addSourceLine(MemberDie, &DT);
1116
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001117 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001118 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1119
1120 uint64_t Size = DT.getSizeInBits();
1121 uint64_t FieldSize = DT.getOriginalTypeSize();
1122
1123 if (Size != FieldSize) {
1124 // Handle bitfield.
1125 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1126 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1127
1128 uint64_t Offset = DT.getOffsetInBits();
1129 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1130 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1131 uint64_t FieldOffset = (HiMark - FieldSize);
1132 Offset -= FieldOffset;
1133
1134 // Maybe we need to work from the other end.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001135 if (Asm->getTargetData().isLittleEndian())
1136 Offset = FieldSize - (Offset + Size);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001137 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1138
1139 // Here WD_AT_data_member_location points to the anonymous
1140 // field that includes this bit field.
1141 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1142
1143 } else
1144 // This is not a bitfield.
1145 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1146
1147 if (DT.getTag() == dwarf::DW_TAG_inheritance
1148 && DT.isVirtual()) {
1149
1150 // For C++, virtual base classes are not at fixed offset. Use following
1151 // expression to extract appropriate offset from vtable.
1152 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1153
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001154 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001155 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1156 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1157 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1158 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1159 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1160 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1161 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1162
1163 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1164 VBaseLocationDie);
1165 } else
1166 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1167
1168 if (DT.isProtected())
1169 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1170 dwarf::DW_ACCESS_protected);
1171 else if (DT.isPrivate())
1172 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1173 dwarf::DW_ACCESS_private);
1174 else if (DT.getTag() == dwarf::DW_TAG_inheritance)
1175 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1176 dwarf::DW_ACCESS_public);
1177 if (DT.isVirtual())
1178 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1179 dwarf::DW_VIRTUALITY_virtual);
1180 return MemberDie;
1181}
1182
1183/// createSubprogramDIE - Create new DIE using SP.
1184DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) {
1185 DIE *SPDie = ModuleCU->getDIE(SP.getNode());
1186 if (SPDie)
1187 return SPDie;
1188
1189 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Shih-wei Liaoe4454322010-04-07 12:21:42 -07001190 // Constructors and operators for anonymous aggregates do not have names.
1191 if (!SP.getName().empty())
1192 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001193
1194 StringRef LinkageName = SP.getLinkageName();
1195 if (!LinkageName.empty())
1196 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
1197 getRealLinkageName(LinkageName));
1198
1199 addSourceLine(SPDie, &SP);
1200
1201 // Add prototyped tag, if C or ObjC.
1202 unsigned Lang = SP.getCompileUnit().getLanguage();
1203 if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 ||
1204 Lang == dwarf::DW_LANG_ObjC)
1205 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
1206
1207 // Add Return Type.
1208 DICompositeType SPTy = SP.getType();
1209 DIArray Args = SPTy.getTypeArray();
1210 unsigned SPTag = SPTy.getTag();
1211
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001212 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001213 addType(SPDie, SPTy);
1214 else
1215 addType(SPDie, DIType(Args.getElement(0).getNode()));
1216
1217 unsigned VK = SP.getVirtuality();
1218 if (VK) {
1219 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001220 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001221 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1222 addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex());
1223 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1224 ContainingTypeMap.insert(std::make_pair(SPDie,
1225 SP.getContainingType().getNode()));
1226 }
1227
1228 if (MakeDecl || !SP.isDefinition()) {
1229 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1230
1231 // Add arguments. Do not add arguments for subprogram definition. They will
1232 // be handled while processing variables.
1233 DICompositeType SPTy = SP.getType();
1234 DIArray Args = SPTy.getTypeArray();
1235 unsigned SPTag = SPTy.getTag();
1236
1237 if (SPTag == dwarf::DW_TAG_subroutine_type)
1238 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1239 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1240 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1241 addType(Arg, ATy);
1242 if (ATy.isArtificial())
1243 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1244 SPDie->addChild(Arg);
1245 }
1246 }
1247
1248 if (SP.isArtificial())
1249 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1250
1251 // DW_TAG_inlined_subroutine may refer to this DIE.
1252 ModuleCU->insertDIE(SP.getNode(), SPDie);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001253
1254 if (!DisableFramePointerElim(*Asm->MF))
1255 addUInt(SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr, dwarf::DW_FORM_flag, 1);
1256
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001257 return SPDie;
1258}
1259
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001260/// getUpdatedDbgScope - Find DbgScope assicated with the instruction.
1261/// Update scope hierarchy. Create abstract scope if required.
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001262DbgScope *DwarfDebug::getUpdatedDbgScope(MDNode *N, const MachineInstr *MI,
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001263 MDNode *InlinedAt) {
1264 assert(N && "Invalid Scope encoding!");
1265 assert(MI && "Missing machine instruction!");
1266 bool isAConcreteScope = InlinedAt != 0;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001267
1268 DbgScope *NScope = NULL;
1269
1270 if (InlinedAt)
1271 NScope = DbgScopeMap.lookup(InlinedAt);
1272 else
1273 NScope = DbgScopeMap.lookup(N);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001274 assert(NScope && "Unable to find working scope!");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001275
1276 if (NScope->getFirstInsn())
1277 return NScope;
1278
1279 DbgScope *Parent = NULL;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001280 if (isAConcreteScope) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001281 DILocation IL(InlinedAt);
1282 Parent = getUpdatedDbgScope(IL.getScope().getNode(), MI,
1283 IL.getOrigLocation().getNode());
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001284 assert(Parent && "Unable to find Parent scope!");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001285 NScope->setParent(Parent);
1286 Parent->addScope(NScope);
1287 } else if (DIDescriptor(N).isLexicalBlock()) {
1288 DILexicalBlock DB(N);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001289 Parent = getUpdatedDbgScope(DB.getContext().getNode(), MI, InlinedAt);
1290 NScope->setParent(Parent);
1291 Parent->addScope(NScope);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001292 }
1293
1294 NScope->setFirstInsn(MI);
1295
1296 if (!Parent && !InlinedAt) {
1297 StringRef SPName = DISubprogram(N).getLinkageName();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001298 if (SPName == Asm->MF->getFunction()->getName())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001299 CurrentFnDbgScope = NScope;
1300 }
1301
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001302 if (isAConcreteScope) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001303 ConcreteScopes[InlinedAt] = NScope;
1304 getOrCreateAbstractScope(N);
1305 }
1306
1307 return NScope;
1308}
1309
1310DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001311 assert(N && "Invalid Scope encoding!");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001312
1313 DbgScope *AScope = AbstractScopes.lookup(N);
1314 if (AScope)
1315 return AScope;
1316
1317 DbgScope *Parent = NULL;
1318
1319 DIDescriptor Scope(N);
1320 if (Scope.isLexicalBlock()) {
1321 DILexicalBlock DB(N);
1322 DIDescriptor ParentDesc = DB.getContext();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001323 Parent = getOrCreateAbstractScope(ParentDesc.getNode());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001324 }
1325
1326 AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
1327
1328 if (Parent)
1329 Parent->addScope(AScope);
1330 AScope->setAbstractScope();
1331 AbstractScopes[N] = AScope;
1332 if (DIDescriptor(N).isSubprogram())
1333 AbstractScopesList.push_back(AScope);
1334 return AScope;
1335}
1336
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001337/// isSubprogramContext - Return true if Context is either a subprogram
1338/// or another context nested inside a subprogram.
1339static bool isSubprogramContext(MDNode *Context) {
1340 if (!Context)
1341 return false;
1342 DIDescriptor D(Context);
1343 if (D.isSubprogram())
1344 return true;
1345 if (D.isType())
1346 return isSubprogramContext(DIType(Context).getContext().getNode());
1347 return false;
1348}
1349
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001350/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
1351/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
1352/// If there are global variables in this scope then create and insert
1353/// DIEs for these variables.
1354DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001355 DIE *SPDie = ModuleCU->getDIE(SPNode);
1356 assert(SPDie && "Unable to find subprogram DIE!");
1357 DISubprogram SP(SPNode);
1358
1359 // There is not any need to generate specification DIE for a function
1360 // defined at compile unit level. If a function is defined inside another
1361 // function then gdb prefers the definition at top level and but does not
1362 // expect specification DIE in parent function. So avoid creating
1363 // specification DIE for a function defined inside a function.
1364 if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
1365 !SP.getContext().isFile() &&
1366 !isSubprogramContext(SP.getContext().getNode())) {
1367 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1368
1369 // Add arguments.
1370 DICompositeType SPTy = SP.getType();
1371 DIArray Args = SPTy.getTypeArray();
1372 unsigned SPTag = SPTy.getTag();
1373 if (SPTag == dwarf::DW_TAG_subroutine_type)
1374 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1375 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1376 DIType ATy = DIType(DIType(Args.getElement(i).getNode()));
1377 addType(Arg, ATy);
1378 if (ATy.isArtificial())
1379 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1380 SPDie->addChild(Arg);
1381 }
1382 DIE *SPDeclDie = SPDie;
1383 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1384 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1385 SPDeclDie);
1386 ModuleCU->addDie(SPDie);
1387 }
1388
1389 addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1390 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
1391 addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
1392 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
1393 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1394 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
1395 addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001396
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001397 if (!DISubprogram(SPNode).isLocalToUnit())
1398 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1399
1400 return SPDie;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001401}
1402
1403/// constructLexicalScope - Construct new DW_TAG_lexical_block
1404/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
1405DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001406
1407 MCSymbol *Start = InsnBeforeLabelMap.lookup(Scope->getFirstInsn());
1408 MCSymbol *End = InsnAfterLabelMap.lookup(Scope->getLastInsn());
1409 if (Start == 0 || End == 0) return 0;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001410
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001411 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
1412 assert(End->isDefined() && "Invalid end label for an inlined scope!");
1413
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001414 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
1415 if (Scope->isAbstractScope())
1416 return ScopeDIE;
1417
1418 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001419 Start ? Start : Asm->GetTempSymbol("func_begin",
1420 Asm->getFunctionNumber()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001421 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001422 End ? End : Asm->GetTempSymbol("func_end",Asm->getFunctionNumber()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001423
1424 return ScopeDIE;
1425}
1426
1427/// constructInlinedScopeDIE - This scope represents inlined body of
1428/// a function. Construct DIE to represent this concrete inlined copy
1429/// of the function.
1430DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001431 MCSymbol *StartLabel = InsnBeforeLabelMap.lookup(Scope->getFirstInsn());
1432 MCSymbol *EndLabel = InsnAfterLabelMap.lookup(Scope->getLastInsn());
1433 if (StartLabel == 0 || EndLabel == 0) return 0;
1434
1435 assert(StartLabel->isDefined() &&
1436 "Invalid starting label for an inlined scope!");
1437 assert(EndLabel->isDefined() &&
1438 "Invalid end label for an inlined scope!");
1439 if (!Scope->getScopeNode())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001440 return NULL;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001441 DIScope DS(Scope->getScopeNode());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001442 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
1443
1444 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
1445 DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode());
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001446 assert(OriginDIE && "Unable to find Origin DIE!");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001447 addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
1448 dwarf::DW_FORM_ref4, OriginDIE);
1449
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001450 addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
1451 addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001452
1453 InlinedSubprogramDIEs.insert(OriginDIE);
1454
1455 // Track the start label for this inlined function.
1456 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
1457 I = InlineInfo.find(InlinedSP.getNode());
1458
1459 if (I == InlineInfo.end()) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001460 InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartLabel,
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001461 ScopeDIE));
1462 InlinedSPNodes.push_back(InlinedSP.getNode());
1463 } else
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001464 I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001465
1466 DILocation DL(Scope->getInlinedAt());
1467 addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID());
1468 addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
1469
1470 return ScopeDIE;
1471}
1472
1473
1474/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1475DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
1476 // Get the descriptor.
1477 const DIVariable &VD = DV->getVariable();
1478 StringRef Name = VD.getName();
1479 if (Name.empty())
1480 return NULL;
1481
1482 // Translate tag to proper Dwarf tag. The result variable is dropped for
1483 // now.
1484 unsigned Tag;
1485 switch (VD.getTag()) {
1486 case dwarf::DW_TAG_return_variable:
1487 return NULL;
1488 case dwarf::DW_TAG_arg_variable:
1489 Tag = dwarf::DW_TAG_formal_parameter;
1490 break;
1491 case dwarf::DW_TAG_auto_variable: // fall thru
1492 default:
1493 Tag = dwarf::DW_TAG_variable;
1494 break;
1495 }
1496
1497 // Define variable debug information entry.
1498 DIE *VariableDie = new DIE(Tag);
1499
1500
1501 DIE *AbsDIE = NULL;
1502 if (DbgVariable *AV = DV->getAbstractVariable())
1503 AbsDIE = AV->getDIE();
1504
1505 if (AbsDIE) {
1506 DIScope DS(Scope->getScopeNode());
1507 DISubprogram InlinedSP = getDISubprogram(DS.getNode());
1508 DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode());
1509 (void) OriginSPDIE;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001510 assert(OriginSPDIE && "Unable to find Origin DIE for the SP!");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001511 DIE *AbsDIE = DV->getAbstractVariable()->getDIE();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001512 assert(AbsDIE && "Unable to find Origin DIE for the Variable!");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001513 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1514 dwarf::DW_FORM_ref4, AbsDIE);
1515 }
1516 else {
1517 addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1518 addSourceLine(VariableDie, &VD);
1519
1520 // Add variable type.
1521 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
1522 // addresses instead.
1523 if (VD.isBlockByrefVariable())
1524 addType(VariableDie, getBlockByrefType(VD.getType(), Name));
1525 else
1526 addType(VariableDie, VD.getType());
1527 }
1528
1529 // Add variable address.
1530 if (!Scope->isAbstractScope()) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001531 // Check if variable is described by DBG_VALUE instruction.
1532 if (const MachineInstr *DbgValueInsn = DV->getDbgValue()) {
1533 if (DbgValueInsn->getNumOperands() == 3) {
1534 // FIXME : Handle getNumOperands != 3
1535 if (DbgValueInsn->getOperand(0).getType()
1536 == MachineOperand::MO_Register
1537 && DbgValueInsn->getOperand(0).getReg()) {
1538 MachineLocation Location;
1539 Location.set(DbgValueInsn->getOperand(0).getReg());
1540 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1541 if (MCSymbol *VS = DV->getDbgValueLabel())
1542 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1543 VS);
1544 } else if (DbgValueInsn->getOperand(0).getType() ==
1545 MachineOperand::MO_Immediate) {
1546 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1547 unsigned Imm = DbgValueInsn->getOperand(0).getImm();
1548 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
1549 addBlock(VariableDie, dwarf::DW_AT_const_value, 0, Block);
1550 if (MCSymbol *VS = DV->getDbgValueLabel())
1551 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1552 VS);
1553 } else if (DbgValueInsn->getOperand(0).getType() ==
1554 MachineOperand::MO_FPImmediate) {
1555 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1556 APFloat FPImm = DbgValueInsn->getOperand(0).getFPImm()->getValueAPF();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001557
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001558 // Get the raw data form of the floating point.
1559 const APInt FltVal = FPImm.bitcastToAPInt();
1560 const char *FltPtr = (const char*)FltVal.getRawData();
1561
1562 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
1563 bool LittleEndian = Asm->getTargetData().isLittleEndian();
1564 int Incr = (LittleEndian ? 1 : -1);
1565 int Start = (LittleEndian ? 0 : NumBytes - 1);
1566 int Stop = (LittleEndian ? NumBytes : -1);
1567
1568 // Output the constant to DWARF one byte at a time.
1569 for (; Start != Stop; Start += Incr)
1570 addUInt(Block, 0, dwarf::DW_FORM_data1,
1571 (unsigned char)0xFF & FltPtr[Start]);
1572
1573 addBlock(VariableDie, dwarf::DW_AT_const_value, 0, Block);
1574
1575 if (MCSymbol *VS = DV->getDbgValueLabel())
1576 addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr,
1577 VS);
1578 } else {
1579 //FIXME : Handle other operand types.
1580 delete VariableDie;
1581 return NULL;
1582 }
1583 }
1584 } else {
1585 MachineLocation Location;
1586 unsigned FrameReg;
1587 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
1588 int Offset = RI->getFrameIndexReference(*Asm->MF, DV->getFrameIndex(),
1589 FrameReg);
1590 Location.set(FrameReg, Offset);
1591
1592 if (VD.hasComplexAddress())
1593 addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1594 else if (VD.isBlockByrefVariable())
1595 addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location);
1596 else
1597 addAddress(VariableDie, dwarf::DW_AT_location, Location);
1598 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001599 }
1600
1601 if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial())
1602 addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1603 DV->setDIE(VariableDie);
1604 return VariableDie;
1605
1606}
1607
1608void DwarfDebug::addPubTypes(DISubprogram SP) {
1609 DICompositeType SPTy = SP.getType();
1610 unsigned SPTag = SPTy.getTag();
1611 if (SPTag != dwarf::DW_TAG_subroutine_type)
1612 return;
1613
1614 DIArray Args = SPTy.getTypeArray();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001615 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1616 DIType ATy(Args.getElement(i).getNode());
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001617 if (!ATy.isValid())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001618 continue;
1619 DICompositeType CATy = getDICompositeType(ATy);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001620 if (DIDescriptor(CATy.getNode()).Verify() && !CATy.getName().empty()
1621 && !CATy.isForwardDecl()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001622 if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode()))
1623 ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry());
1624 }
1625 }
1626}
1627
1628/// constructScopeDIE - Construct a DIE for this scope.
1629DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001630 if (!Scope || !Scope->getScopeNode())
1631 return NULL;
1632
1633 DIScope DS(Scope->getScopeNode());
1634 DIE *ScopeDIE = NULL;
1635 if (Scope->getInlinedAt())
1636 ScopeDIE = constructInlinedScopeDIE(Scope);
1637 else if (DS.isSubprogram()) {
1638 if (Scope->isAbstractScope())
1639 ScopeDIE = ModuleCU->getDIE(DS.getNode());
1640 else
1641 ScopeDIE = updateSubprogramScopeDIE(DS.getNode());
1642 }
1643 else
1644 ScopeDIE = constructLexicalScopeDIE(Scope);
1645 if (!ScopeDIE) return NULL;
1646
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001647 // Add variables to scope.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001648 const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001649 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1650 DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
1651 if (VariableDIE)
1652 ScopeDIE->addChild(VariableDIE);
1653 }
1654
1655 // Add nested scopes.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001656 const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001657 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1658 // Define the Scope debug information entry.
1659 DIE *NestedDIE = constructScopeDIE(Scopes[j]);
1660 if (NestedDIE)
1661 ScopeDIE->addChild(NestedDIE);
1662 }
1663
1664 if (DS.isSubprogram())
1665 addPubTypes(DISubprogram(DS.getNode()));
1666
1667 return ScopeDIE;
1668}
1669
1670/// GetOrCreateSourceID - Look up the source id with the given directory and
1671/// source file names. If none currently exists, create a new id and insert it
1672/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1673/// maps as well.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001674unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001675 unsigned DId;
1676 StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName);
1677 if (DI != DirectoryIdMap.end()) {
1678 DId = DI->getValue();
1679 } else {
1680 DId = DirectoryNames.size() + 1;
1681 DirectoryIdMap[DirName] = DId;
1682 DirectoryNames.push_back(DirName);
1683 }
1684
1685 unsigned FId;
1686 StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName);
1687 if (FI != SourceFileIdMap.end()) {
1688 FId = FI->getValue();
1689 } else {
1690 FId = SourceFileNames.size() + 1;
1691 SourceFileIdMap[FileName] = FId;
1692 SourceFileNames.push_back(FileName);
1693 }
1694
1695 DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI =
1696 SourceIdMap.find(std::make_pair(DId, FId));
1697 if (SI != SourceIdMap.end())
1698 return SI->second;
1699
1700 unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0.
1701 SourceIdMap[std::make_pair(DId, FId)] = SrcId;
1702 SourceIds.push_back(std::make_pair(DId, FId));
1703
1704 return SrcId;
1705}
1706
1707/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1708DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) {
1709 DIE *NDie = ModuleCU->getDIE(NS.getNode());
1710 if (NDie)
1711 return NDie;
1712 NDie = new DIE(dwarf::DW_TAG_namespace);
1713 ModuleCU->insertDIE(NS.getNode(), NDie);
1714 if (!NS.getName().empty())
1715 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
1716 addSourceLine(NDie, &NS);
1717 addToContextOwner(NDie, NS.getContext());
1718 return NDie;
1719}
1720
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001721void DwarfDebug::constructCompileUnit(MDNode *N) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001722 DICompileUnit DIUnit(N);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001723 // Use first compile unit marked as isMain as the compile unit for this
1724 // module.
1725 if (ModuleCU || !DIUnit.isMain())
1726 return;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001727 StringRef FN = DIUnit.getFilename();
1728 StringRef Dir = DIUnit.getDirectory();
1729 unsigned ID = GetOrCreateSourceID(Dir, FN);
1730
1731 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001732 addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
1733 DIUnit.getProducer());
1734 addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1,
1735 DIUnit.getLanguage());
1736 addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001737 // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
1738 // simplifies debug range entries.
1739 addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_data4, 0);
1740 // DW_AT_stmt_list is a offset of line number information for this
1741 // compile unit in debug_line section. It is always zero when only one
1742 // compile unit is emitted in one object file.
1743 addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001744
1745 if (!Dir.empty())
1746 addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
1747 if (DIUnit.isOptimized())
1748 addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1749
1750 StringRef Flags = DIUnit.getFlags();
1751 if (!Flags.empty())
1752 addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
1753
1754 unsigned RVer = DIUnit.getRunTimeVersion();
1755 if (RVer)
1756 addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
1757 dwarf::DW_FORM_data1, RVer);
1758
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001759 assert(!ModuleCU &&
1760 "ModuleCU assigned since the top of constructCompileUnit");
1761 ModuleCU = new CompileUnit(ID, Die);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001762}
1763
1764void DwarfDebug::constructGlobalVariableDIE(MDNode *N) {
1765 DIGlobalVariable DI_GV(N);
1766
1767 // If debug information is malformed then ignore it.
1768 if (DI_GV.Verify() == false)
1769 return;
1770
1771 // Check for pre-existence.
1772 if (ModuleCU->getDIE(DI_GV.getNode()))
1773 return;
1774
1775 DIE *VariableDie = createGlobalVariableDIE(DI_GV);
1776 if (!VariableDie)
1777 return;
1778
1779 // Add to map.
1780 ModuleCU->insertDIE(N, VariableDie);
1781
1782 // Add to context owner.
1783 DIDescriptor GVContext = DI_GV.getContext();
1784 // Do not create specification DIE if context is either compile unit
1785 // or a subprogram.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001786 if (DI_GV.isDefinition() && !GVContext.isCompileUnit() &&
1787 !GVContext.isFile() &&
1788 !isSubprogramContext(GVContext.getNode())) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001789 // Create specification DIE.
1790 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1791 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1792 dwarf::DW_FORM_ref4, VariableDie);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001793 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001794 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001795 addLabel(Block, 0, dwarf::DW_FORM_udata,
1796 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001797 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1798 addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1799 ModuleCU->addDie(VariableSpecDIE);
1800 } else {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001801 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001802 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001803 addLabel(Block, 0, dwarf::DW_FORM_udata,
1804 Asm->Mang->getSymbol(DI_GV.getGlobal()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001805 addBlock(VariableDie, dwarf::DW_AT_location, 0, Block);
1806 }
1807 addToContextOwner(VariableDie, GVContext);
1808
1809 // Expose as global. FIXME - need to check external flag.
1810 ModuleCU->addGlobal(DI_GV.getName(), VariableDie);
1811
1812 DIType GTy = DI_GV.getType();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001813 if (GTy.isCompositeType() && !GTy.getName().empty()
1814 && !GTy.isForwardDecl()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001815 DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode());
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001816 assert(Entry && "Missing global type!");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001817 ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry());
1818 }
1819 return;
1820}
1821
1822void DwarfDebug::constructSubprogramDIE(MDNode *N) {
1823 DISubprogram SP(N);
1824
1825 // Check for pre-existence.
1826 if (ModuleCU->getDIE(N))
1827 return;
1828
1829 if (!SP.isDefinition())
1830 // This is a method declaration which will be handled while constructing
1831 // class type.
1832 return;
1833
1834 DIE *SubprogramDie = createSubprogramDIE(SP);
1835
1836 // Add to map.
1837 ModuleCU->insertDIE(N, SubprogramDie);
1838
1839 // Add to context owner.
1840 addToContextOwner(SubprogramDie, SP.getContext());
1841
1842 // Expose as global.
1843 ModuleCU->addGlobal(SP.getName(), SubprogramDie);
1844
1845 return;
1846}
1847
1848/// beginModule - Emit all Dwarf sections that should come prior to the
1849/// content. Create global DIEs and emit initial debug info sections.
1850/// This is inovked by the target AsmPrinter.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001851void DwarfDebug::beginModule(Module *M) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001852 DebugInfoFinder DbgFinder;
1853 DbgFinder.processModule(*M);
1854
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001855 bool HasDebugInfo = false;
1856
1857 // Scan all the compile-units to see if there are any marked as the main unit.
1858 // if not, we do not generate debug info.
1859 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1860 E = DbgFinder.compile_unit_end(); I != E; ++I) {
1861 if (DICompileUnit(*I).isMain()) {
1862 HasDebugInfo = true;
1863 break;
1864 }
1865 }
1866
1867 if (!HasDebugInfo) return;
1868
1869 // Tell MMI that we have debug info.
1870 MMI->setDebugInfoAvailability(true);
1871
1872 // Emit initial sections.
1873 EmitSectionLabels();
1874
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001875 // Create all the compile unit DIEs.
1876 for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
1877 E = DbgFinder.compile_unit_end(); I != E; ++I)
1878 constructCompileUnit(*I);
1879
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001880 // Create DIEs for each subprogram.
1881 for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
1882 E = DbgFinder.subprogram_end(); I != E; ++I)
1883 constructSubprogramDIE(*I);
1884
1885 // Create DIEs for each global variable.
1886 for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
1887 E = DbgFinder.global_variable_end(); I != E; ++I)
1888 constructGlobalVariableDIE(*I);
1889
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001890 // Prime section data.
1891 SectionMap.insert(Asm->getObjFileLowering().getTextSection());
1892
1893 // Print out .file directives to specify files for .loc directives. These are
1894 // printed out early so that they precede any .loc directives.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001895 if (Asm->MAI->hasDotLocAndDotFile()) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001896 for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) {
1897 // Remember source id starts at 1.
1898 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i);
1899 // FIXME: don't use sys::path for this! This should not depend on the
1900 // host.
1901 sys::Path FullPath(getSourceDirectoryName(Id.first));
1902 bool AppendOk =
1903 FullPath.appendComponent(getSourceFileName(Id.second));
1904 assert(AppendOk && "Could not append filename to directory!");
1905 AppendOk = false;
1906 Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
1907 }
1908 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001909}
1910
1911/// endModule - Emit all Dwarf sections that should come after the content.
1912///
1913void DwarfDebug::endModule() {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001914 if (!ModuleCU) return;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001915
1916 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1917 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
1918 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
1919 DIE *ISP = *AI;
1920 addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
1921 }
1922
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001923 for (DenseMap<DIE *, MDNode *>::iterator CI = ContainingTypeMap.begin(),
1924 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1925 DIE *SPDie = CI->first;
1926 MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
1927 if (!N) continue;
1928 DIE *NDie = ModuleCU->getDIE(N);
1929 if (!NDie) continue;
1930 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001931 }
1932
1933 // Standard sections final addresses.
1934 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001935 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001936 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001937 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001938
1939 // End text sections.
1940 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
1941 Asm->OutStreamer.SwitchSection(SectionMap[i]);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001942 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001943 }
1944
1945 // Emit common frame information.
1946 emitCommonDebugFrame();
1947
1948 // Emit function debug frame information
1949 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
1950 E = DebugFrames.end(); I != E; ++I)
1951 emitFunctionDebugFrame(*I);
1952
1953 // Compute DIE offsets and sizes.
1954 computeSizeAndOffsets();
1955
1956 // Emit all the DIEs into a debug info section
1957 emitDebugInfo();
1958
1959 // Corresponding abbreviations into a abbrev section.
1960 emitAbbreviations();
1961
1962 // Emit source line correspondence into a debug line section.
1963 emitDebugLines();
1964
1965 // Emit info into a debug pubnames section.
1966 emitDebugPubNames();
1967
1968 // Emit info into a debug pubtypes section.
1969 emitDebugPubTypes();
1970
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001971 // Emit info into a debug loc section.
1972 emitDebugLoc();
1973
1974 // Emit info into a debug aranges section.
1975 EmitDebugARanges();
1976
1977 // Emit info into a debug ranges section.
1978 emitDebugRanges();
1979
1980 // Emit info into a debug macinfo section.
1981 emitDebugMacInfo();
1982
1983 // Emit inline info.
1984 emitDebugInlineInfo();
1985
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001986 // Emit info into a debug str section.
1987 emitDebugStr();
1988
1989 delete ModuleCU;
1990 ModuleCU = NULL; // Reset for the next Module, if any.
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001991}
1992
1993/// findAbstractVariable - Find abstract variable, if any, associated with Var.
1994DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
1995 unsigned FrameIdx,
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07001996 DebugLoc ScopeLoc) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001997
1998 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
1999 if (AbsDbgVariable)
2000 return AbsDbgVariable;
2001
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002002 LLVMContext &Ctx = Var.getNode()->getContext();
2003 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002004 if (!Scope)
2005 return NULL;
2006
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002007 AbsDbgVariable = new DbgVariable(Var, FrameIdx,
2008 NULL /* No more-abstract variable*/);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002009 Scope->addVariable(AbsDbgVariable);
2010 AbstractVariables[Var.getNode()] = AbsDbgVariable;
2011 return AbsDbgVariable;
2012}
2013
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002014/// findAbstractVariable - Find abstract variable, if any, associated with Var.
2015/// FIXME : Refactor findAbstractVariable.
2016DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
2017 const MachineInstr *MI,
2018 DebugLoc ScopeLoc) {
2019
2020 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode());
2021 if (AbsDbgVariable)
2022 return AbsDbgVariable;
2023
2024 LLVMContext &Ctx = Var.getNode()->getContext();
2025 DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
2026 if (!Scope)
2027 return NULL;
2028
2029 AbsDbgVariable = new DbgVariable(Var, MI,
2030 NULL /* No more-abstract variable*/);
2031 Scope->addVariable(AbsDbgVariable);
2032 AbstractVariables[Var.getNode()] = AbsDbgVariable;
2033 DbgValueStartMap[MI] = AbsDbgVariable;
2034 return AbsDbgVariable;
2035}
2036
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002037/// collectVariableInfo - Populate DbgScope entries with variables' info.
2038void DwarfDebug::collectVariableInfo() {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002039 const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002040
2041 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
2042 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
2043 VE = VMap.end(); VI != VE; ++VI) {
2044 MDNode *Var = VI->first;
2045 if (!Var) continue;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002046 DIVariable DV(Var);
2047 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002048
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002049 DbgScope *Scope = 0;
2050 if (MDNode *IA = VP.second.getInlinedAt(Ctx))
2051 Scope = ConcreteScopes.lookup(IA);
2052 if (Scope == 0)
2053 Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
2054
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002055 // If variable scope is not found then skip this variable.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002056 if (Scope == 0)
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002057 continue;
2058
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002059 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, VP.second);
2060 DbgVariable *RegVar = new DbgVariable(DV, VP.first, AbsDbgVariable);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002061 Scope->addVariable(RegVar);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002062 }
2063
2064 // Collect variable information from DBG_VALUE machine instructions;
2065 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
2066 I != E; ++I) {
2067 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2068 II != IE; ++II) {
2069 const MachineInstr *MInsn = II;
2070 if (!MInsn->isDebugValue())
2071 continue;
2072
2073 // FIXME : Lift this restriction.
2074 if (MInsn->getNumOperands() != 3)
2075 continue;
2076 DIVariable DV(
2077 const_cast<MDNode *>(MInsn->getOperand(MInsn->getNumOperands() - 1)
2078 .getMetadata()));
2079 if (DV.getTag() == dwarf::DW_TAG_arg_variable) {
2080 // FIXME Handle inlined subroutine arguments.
2081 DbgVariable *ArgVar = new DbgVariable(DV, MInsn, NULL);
2082 CurrentFnDbgScope->addVariable(ArgVar);
2083 DbgValueStartMap[MInsn] = ArgVar;
2084 continue;
2085 }
2086
2087 DebugLoc DL = MInsn->getDebugLoc();
2088 if (DL.isUnknown()) continue;
2089 DbgScope *Scope = 0;
2090 if (MDNode *IA = DL.getInlinedAt(Ctx))
2091 Scope = ConcreteScopes.lookup(IA);
2092 if (Scope == 0)
2093 Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2094
2095 // If variable scope is not found then skip this variable.
2096 if (Scope == 0)
2097 continue;
2098
2099 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn, DL);
2100 DbgVariable *RegVar = new DbgVariable(DV, MInsn, AbsDbgVariable);
2101 DbgValueStartMap[MInsn] = RegVar;
2102 Scope->addVariable(RegVar);
2103 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002104 }
2105}
2106
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002107/// beginScope - Process beginning of a scope.
2108void DwarfDebug::beginScope(const MachineInstr *MI) {
2109 // Check location.
2110 DebugLoc DL = MI->getDebugLoc();
2111 if (DL.isUnknown())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002112 return;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002113
2114 // Check and update last known location info.
2115 if (DL == PrevInstLoc)
2116 return;
2117
2118 MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
2119
2120 // FIXME: Should only verify each scope once!
2121 if (!DIScope(Scope).Verify())
2122 return;
2123
2124 // DBG_VALUE instruction establishes new value.
2125 if (MI->isDebugValue()) {
2126 DenseMap<const MachineInstr *, DbgVariable *>::iterator DI
2127 = DbgValueStartMap.find(MI);
2128 if (DI != DbgValueStartMap.end()) {
2129 MCSymbol *Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2130 PrevInstLoc = DL;
2131 DI->second->setDbgValueLabel(Label);
2132 }
2133 return;
2134 }
2135
2136 // Emit a label to indicate location change. This is used for line
2137 // table even if this instruction does start a new scope.
2138 MCSymbol *Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope);
2139 PrevInstLoc = DL;
2140
2141 // If this instruction begins a scope then note down corresponding label.
2142 if (InsnsBeginScopeSet.count(MI) != 0)
2143 InsnBeforeLabelMap[MI] = Label;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002144}
2145
2146/// endScope - Process end of a scope.
2147void DwarfDebug::endScope(const MachineInstr *MI) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002148 // Ignore DBG_VALUE instruction.
2149 if (MI->isDebugValue())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002150 return;
2151
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002152 // Check location.
2153 DebugLoc DL = MI->getDebugLoc();
2154 if (DL.isUnknown())
2155 return;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002156
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002157 if (InsnsEndScopeSet.count(MI) != 0) {
2158 // Emit a label if this instruction ends a scope.
2159 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2160 Asm->OutStreamer.EmitLabel(Label);
2161 InsnAfterLabelMap[MI] = Label;
2162 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002163}
2164
2165/// createDbgScope - Create DbgScope for the scope.
2166void DwarfDebug::createDbgScope(MDNode *Scope, MDNode *InlinedAt) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002167 if (!InlinedAt) {
2168 DbgScope *WScope = DbgScopeMap.lookup(Scope);
2169 if (WScope)
2170 return;
2171 WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
2172 DbgScopeMap.insert(std::make_pair(Scope, WScope));
2173 if (DIDescriptor(Scope).isLexicalBlock())
2174 createDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL);
2175 return;
2176 }
2177
2178 DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
2179 if (WScope)
2180 return;
2181
2182 WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
2183 DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
2184 DILocation DL(InlinedAt);
2185 createDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode());
2186}
2187
2188/// extractScopeInformation - Scan machine instructions in this function
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002189/// and collect DbgScopes. Return true, if at least one scope was found.
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002190bool DwarfDebug::extractScopeInformation() {
2191 // If scope information was extracted using .dbg intrinsics then there is not
2192 // any need to extract these information by scanning each instruction.
2193 if (!DbgScopeMap.empty())
2194 return false;
2195
2196 DenseMap<const MachineInstr *, unsigned> MIIndexMap;
2197 unsigned MIIndex = 0;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002198 LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
2199
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002200 // Scan each instruction and create scopes. First build working set of scopes.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002201 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002202 I != E; ++I) {
2203 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2204 II != IE; ++II) {
2205 const MachineInstr *MInsn = II;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002206 // FIXME : Remove DBG_VALUE check.
2207 if (MInsn->isDebugValue()) continue;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002208 MIIndexMap[MInsn] = MIIndex++;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002209
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002210 DebugLoc DL = MInsn->getDebugLoc();
2211 if (DL.isUnknown()) continue;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002212
2213 MDNode *Scope = DL.getScope(Ctx);
2214
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002215 // There is no need to create another DIE for compile unit. For all
2216 // other scopes, create one DbgScope now. This will be translated
2217 // into a scope DIE at the end.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002218 if (DIScope(Scope).isCompileUnit()) continue;
2219 createDbgScope(Scope, DL.getInlinedAt(Ctx));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002220 }
2221 }
2222
2223
2224 // Build scope hierarchy using working set of scopes.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002225 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002226 I != E; ++I) {
2227 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
2228 II != IE; ++II) {
2229 const MachineInstr *MInsn = II;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002230 // FIXME : Remove DBG_VALUE check.
2231 if (MInsn->isDebugValue()) continue;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002232 DebugLoc DL = MInsn->getDebugLoc();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002233 if (DL.isUnknown()) continue;
2234
2235 MDNode *Scope = DL.getScope(Ctx);
2236 if (Scope == 0) continue;
2237
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002238 // There is no need to create another DIE for compile unit. For all
2239 // other scopes, create one DbgScope now. This will be translated
2240 // into a scope DIE at the end.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002241 if (DIScope(Scope).isCompileUnit()) continue;
2242 DbgScope *DScope = getUpdatedDbgScope(Scope, MInsn, DL.getInlinedAt(Ctx));
2243 DScope->setLastInsn(MInsn);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002244 }
2245 }
2246
2247 if (!CurrentFnDbgScope)
2248 return false;
2249
2250 CurrentFnDbgScope->fixInstructionMarkers(MIIndexMap);
2251
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002252 identifyScopeMarkers();
2253
2254 return !DbgScopeMap.empty();
2255}
2256
2257/// identifyScopeMarkers() - Indentify instructions that are marking
2258/// beginning of or end of a scope.
2259void DwarfDebug::identifyScopeMarkers() {
2260
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002261 // Each scope has first instruction and last instruction to mark beginning
2262 // and end of a scope respectively. Create an inverse map that list scopes
2263 // starts (and ends) with an instruction. One instruction may start (or end)
2264 // multiple scopes. Ignore scopes that are not reachable.
2265 SmallVector<DbgScope *, 4> WorkList;
2266 WorkList.push_back(CurrentFnDbgScope);
2267 while (!WorkList.empty()) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002268 DbgScope *S = WorkList.pop_back_val();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002269
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002270 const SmallVector<DbgScope *, 4> &Children = S->getScopes();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002271 if (!Children.empty())
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002272 for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002273 SE = Children.end(); SI != SE; ++SI)
2274 WorkList.push_back(*SI);
2275
2276 if (S->isAbstractScope())
2277 continue;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002278 assert(S->getFirstInsn() && "DbgScope does not have first instruction!");
2279 InsnsBeginScopeSet.insert(S->getFirstInsn());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002280
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002281 assert(S->getLastInsn() && "DbgScope does not have last instruction!");
2282 InsnsEndScopeSet.insert(S->getLastInsn());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002283 }
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002284}
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002285
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002286/// FindFirstDebugLoc - Find the first debug location in the function. This
2287/// is intended to be an approximation for the source position of the
2288/// beginning of the function.
2289static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
2290 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
2291 I != E; ++I)
2292 for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
2293 MBBI != MBBE; ++MBBI) {
2294 DebugLoc DL = MBBI->getDebugLoc();
2295 if (!DL.isUnknown())
2296 return DL;
2297 }
2298 return DebugLoc();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002299}
2300
2301/// beginFunction - Gather pre-function debug information. Assumes being
2302/// emitted immediately after the function entry point.
2303void DwarfDebug::beginFunction(const MachineFunction *MF) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002304 if (!MMI->hasDebugInfo()) return;
2305 if (!extractScopeInformation()) return;
2306
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002307 collectVariableInfo();
2308
2309 // Assumes in correct section after the entry point.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002310 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_begin",
2311 Asm->getFunctionNumber()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002312
2313 // Emit label for the implicitly defined dbg.stoppoint at the start of the
2314 // function.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002315 DebugLoc FDL = FindFirstDebugLoc(MF);
2316 if (FDL.isUnknown()) return;
2317
2318 MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
2319
2320 DISubprogram SP = getDISubprogram(Scope);
2321 unsigned Line, Col;
2322 if (SP.Verify()) {
2323 Line = SP.getLineNumber();
2324 Col = 0;
2325 } else {
2326 Line = FDL.getLine();
2327 Col = FDL.getCol();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002328 }
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002329
2330 recordSourceLine(Line, Col, Scope);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002331}
2332
2333/// endFunction - Gather and emit post-function debug information.
2334///
2335void DwarfDebug::endFunction(const MachineFunction *MF) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002336 if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002337
2338 if (CurrentFnDbgScope) {
2339 // Define end label for subprogram.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002340 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end",
2341 Asm->getFunctionNumber()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002342
2343 // Get function line info.
2344 if (!Lines.empty()) {
2345 // Get section line info.
2346 unsigned ID = SectionMap.insert(Asm->getCurrentSection());
2347 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2348 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2349 // Append the function info to section info.
2350 SectionLineInfos.insert(SectionLineInfos.end(),
2351 Lines.begin(), Lines.end());
2352 }
2353
2354 // Construct abstract scopes.
2355 for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2356 AE = AbstractScopesList.end(); AI != AE; ++AI)
2357 constructScopeDIE(*AI);
2358
2359 constructScopeDIE(CurrentFnDbgScope);
2360
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002361 DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002362 MMI->getFrameMoves()));
2363 }
2364
2365 // Clear debug info
2366 CurrentFnDbgScope = NULL;
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002367 DeleteContainerSeconds(DbgScopeMap);
2368 InsnsBeginScopeSet.clear();
2369 InsnsEndScopeSet.clear();
2370 DbgValueStartMap.clear();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002371 ConcreteScopes.clear();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002372 DeleteContainerSeconds(AbstractScopes);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002373 AbstractScopesList.clear();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002374 AbstractVariables.clear();
2375 InsnBeforeLabelMap.clear();
2376 InsnAfterLabelMap.clear();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002377 Lines.clear();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002378 PrevLabel = NULL;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002379}
2380
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002381/// recordSourceLine - Register a source line with debug info. Returns the
2382/// unique label that was emitted and which provides correspondence to
2383/// the source line list.
2384MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, MDNode *S) {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002385 StringRef Dir;
2386 StringRef Fn;
2387
2388 DIDescriptor Scope(S);
2389 if (Scope.isCompileUnit()) {
2390 DICompileUnit CU(S);
2391 Dir = CU.getDirectory();
2392 Fn = CU.getFilename();
2393 } else if (Scope.isSubprogram()) {
2394 DISubprogram SP(S);
2395 Dir = SP.getDirectory();
2396 Fn = SP.getFilename();
2397 } else if (Scope.isLexicalBlock()) {
2398 DILexicalBlock DB(S);
2399 Dir = DB.getDirectory();
2400 Fn = DB.getFilename();
2401 } else
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002402 assert(0 && "Unexpected scope info");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002403
2404 unsigned Src = GetOrCreateSourceID(Dir, Fn);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002405 MCSymbol *Label = MMI->getContext().CreateTempSymbol();
2406 Lines.push_back(SrcLineInfo(Line, Col, Src, Label));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002407
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002408 Asm->OutStreamer.EmitLabel(Label);
2409 return Label;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002410}
2411
2412//===----------------------------------------------------------------------===//
2413// Emit Methods
2414//===----------------------------------------------------------------------===//
2415
2416/// computeSizeAndOffset - Compute the size and offset of a DIE.
2417///
2418unsigned
2419DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
2420 // Get the children.
2421 const std::vector<DIE *> &Children = Die->getChildren();
2422
2423 // If not last sibling and has children then add sibling offset attribute.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002424 if (!Last && !Children.empty())
2425 Die->addSiblingOffset(DIEValueAllocator);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002426
2427 // Record the abbreviation.
2428 assignAbbrevNumber(Die->getAbbrev());
2429
2430 // Get the abbreviation for this DIE.
2431 unsigned AbbrevNumber = Die->getAbbrevNumber();
2432 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2433
2434 // Set DIE offset
2435 Die->setOffset(Offset);
2436
2437 // Start the size with the size of abbreviation code.
2438 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
2439
2440 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2441 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2442
2443 // Size the DIE attribute values.
2444 for (unsigned i = 0, N = Values.size(); i < N; ++i)
2445 // Size attribute value.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002446 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002447
2448 // Size the DIE children if any.
2449 if (!Children.empty()) {
2450 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2451 "Children flag not set");
2452
2453 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2454 Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
2455
2456 // End of children marker.
2457 Offset += sizeof(int8_t);
2458 }
2459
2460 Die->setSize(Offset - Die->getOffset());
2461 return Offset;
2462}
2463
2464/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
2465///
2466void DwarfDebug::computeSizeAndOffsets() {
2467 // Compute size of compile unit header.
2468 static unsigned Offset =
2469 sizeof(int32_t) + // Length of Compilation Unit Info
2470 sizeof(int16_t) + // DWARF version number
2471 sizeof(int32_t) + // Offset Into Abbrev. Section
2472 sizeof(int8_t); // Pointer Size (in bytes)
2473
2474 computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002475}
2476
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002477/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2478/// temporary label to it if SymbolStem is specified.
2479static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
2480 const char *SymbolStem = 0) {
2481 Asm->OutStreamer.SwitchSection(Section);
2482 if (!SymbolStem) return 0;
2483
2484 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2485 Asm->OutStreamer.EmitLabel(TmpSym);
2486 return TmpSym;
2487}
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002488
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002489/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2490/// the start of each one.
2491void DwarfDebug::EmitSectionLabels() {
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002492 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
2493
2494 // Dwarf sections base addresses.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002495 if (Asm->MAI->doesDwarfRequireFrameSection()) {
2496 DwarfFrameSectionSym =
2497 EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame");
2498 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002499
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002500 DwarfInfoSectionSym =
2501 EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2502 DwarfAbbrevSectionSym =
2503 EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
2504 EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
2505
2506 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
2507 EmitSectionSym(Asm, MacroInfo);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002508
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002509 EmitSectionSym(Asm, TLOF.getDwarfLineSection());
2510 EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2511 EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2512 EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
2513 DwarfStrSectionSym =
2514 EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
2515 DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2516 "debug_range");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002517
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002518 TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
2519 EmitSectionSym(Asm, TLOF.getDataSection());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002520}
2521
2522/// emitDIE - Recusively Emits a debug information entry.
2523///
2524void DwarfDebug::emitDIE(DIE *Die) {
2525 // Get the abbreviation for this DIE.
2526 unsigned AbbrevNumber = Die->getAbbrevNumber();
2527 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2528
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002529 // Emit the code (index) for the abbreviation.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002530 if (Asm->isVerbose())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002531 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2532 Twine::utohexstr(Die->getOffset()) + ":0x" +
2533 Twine::utohexstr(Die->getSize()) + " " +
2534 dwarf::TagString(Abbrev->getTag()));
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002535 Asm->EmitULEB128(AbbrevNumber);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002536
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002537 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002538 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2539
2540 // Emit the DIE attribute values.
2541 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2542 unsigned Attr = AbbrevData[i].getAttribute();
2543 unsigned Form = AbbrevData[i].getForm();
2544 assert(Form && "Too many attributes for DIE (check abbreviation)");
2545
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002546 if (Asm->isVerbose())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002547 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2548
2549 switch (Attr) {
2550 case dwarf::DW_AT_sibling:
2551 Asm->EmitInt32(Die->getSiblingOffset());
2552 break;
2553 case dwarf::DW_AT_abstract_origin: {
2554 DIEEntry *E = cast<DIEEntry>(Values[i]);
2555 DIE *Origin = E->getEntry();
2556 unsigned Addr = Origin->getOffset();
2557 Asm->EmitInt32(Addr);
2558 break;
2559 }
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002560 case dwarf::DW_AT_ranges: {
2561 // DW_AT_range Value encodes offset in debug_range section.
2562 DIEInteger *V = cast<DIEInteger>(Values[i]);
2563 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2564 V->getValue(),
2565 DwarfDebugRangeSectionSym,
2566 4);
2567 break;
2568 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002569 default:
2570 // Emit an attribute using the defined form.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002571 Values[i]->EmitValue(Asm, Form);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002572 break;
2573 }
2574 }
2575
2576 // Emit the DIE children if any.
2577 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2578 const std::vector<DIE *> &Children = Die->getChildren();
2579
2580 for (unsigned j = 0, M = Children.size(); j < M; ++j)
2581 emitDIE(Children[j]);
2582
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002583 if (Asm->isVerbose())
2584 Asm->OutStreamer.AddComment("End Of Children Mark");
2585 Asm->EmitInt8(0);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002586 }
2587}
2588
2589/// emitDebugInfo - Emit the debug info section.
2590///
2591void DwarfDebug::emitDebugInfo() {
2592 // Start debug info section.
2593 Asm->OutStreamer.SwitchSection(
2594 Asm->getObjFileLowering().getDwarfInfoSection());
2595 DIE *Die = ModuleCU->getCUDie();
2596
2597 // Emit the compile units header.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002598 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2599 ModuleCU->getID()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002600
2601 // Emit size of content not including length itself
2602 unsigned ContentSize = Die->getSize() +
2603 sizeof(int16_t) + // DWARF version number
2604 sizeof(int32_t) + // Offset Into Abbrev. Section
2605 sizeof(int8_t) + // Pointer Size (in bytes)
2606 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2607
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002608 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2609 Asm->EmitInt32(ContentSize);
2610 Asm->OutStreamer.AddComment("DWARF version number");
2611 Asm->EmitInt16(dwarf::DWARF_VERSION);
2612 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2613 Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2614 DwarfAbbrevSectionSym);
2615 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2616 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002617
2618 emitDIE(Die);
2619 // FIXME - extra padding for gdb bug.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002620 Asm->OutStreamer.AddComment("4 extra padding bytes for GDB");
2621 Asm->EmitInt8(0);
2622 Asm->EmitInt8(0);
2623 Asm->EmitInt8(0);
2624 Asm->EmitInt8(0);
2625 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", ModuleCU->getID()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002626}
2627
2628/// emitAbbreviations - Emit the abbreviation section.
2629///
2630void DwarfDebug::emitAbbreviations() const {
2631 // Check to see if it is worth the effort.
2632 if (!Abbreviations.empty()) {
2633 // Start the debug abbrev section.
2634 Asm->OutStreamer.SwitchSection(
2635 Asm->getObjFileLowering().getDwarfAbbrevSection());
2636
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002637 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002638
2639 // For each abbrevation.
2640 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2641 // Get abbreviation data
2642 const DIEAbbrev *Abbrev = Abbreviations[i];
2643
2644 // Emit the abbrevations code (base 1 index.)
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002645 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002646
2647 // Emit the abbreviations data.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002648 Abbrev->Emit(Asm);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002649 }
2650
2651 // Mark end of abbreviations.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002652 Asm->EmitULEB128(0, "EOM(3)");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002653
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002654 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002655 }
2656}
2657
2658/// emitEndOfLineMatrix - Emit the last address of the section and the end of
2659/// the line matrix.
2660///
2661void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
2662 // Define last address of section.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002663 Asm->OutStreamer.AddComment("Extended Op");
2664 Asm->EmitInt8(0);
2665
2666 Asm->OutStreamer.AddComment("Op size");
2667 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
2668 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2669 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2670
2671 Asm->OutStreamer.AddComment("Section end label");
2672
2673 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
2674 Asm->getTargetData().getPointerSize(),
2675 0/*AddrSpace*/);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002676
2677 // Mark end of matrix.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002678 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2679 Asm->EmitInt8(0);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002680 Asm->EmitInt8(1);
2681 Asm->EmitInt8(1);
2682}
2683
2684/// emitDebugLines - Emit source line information.
2685///
2686void DwarfDebug::emitDebugLines() {
2687 // If the target is using .loc/.file, the assembler will be emitting the
2688 // .debug_line table automatically.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002689 if (Asm->MAI->hasDotLocAndDotFile())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002690 return;
2691
2692 // Minimum line delta, thus ranging from -10..(255-10).
2693 const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1);
2694 // Maximum line delta, thus ranging from -10..(255-10).
2695 const int MaxLineDelta = 255 + MinLineDelta;
2696
2697 // Start the dwarf line section.
2698 Asm->OutStreamer.SwitchSection(
2699 Asm->getObjFileLowering().getDwarfLineSection());
2700
2701 // Construct the section header.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002702 Asm->OutStreamer.AddComment("Length of Source Line Info");
2703 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"),
2704 Asm->GetTempSymbol("line_begin"), 4);
2705 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin"));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002706
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002707 Asm->OutStreamer.AddComment("DWARF version number");
2708 Asm->EmitInt16(dwarf::DWARF_VERSION);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002709
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002710 Asm->OutStreamer.AddComment("Prolog Length");
2711 Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"),
2712 Asm->GetTempSymbol("line_prolog_begin"), 4);
2713 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin"));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002714
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002715 Asm->OutStreamer.AddComment("Minimum Instruction Length");
2716 Asm->EmitInt8(1);
2717 Asm->OutStreamer.AddComment("Default is_stmt_start flag");
2718 Asm->EmitInt8(1);
2719 Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)");
2720 Asm->EmitInt8(MinLineDelta);
2721 Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)");
2722 Asm->EmitInt8(MaxLineDelta);
2723 Asm->OutStreamer.AddComment("Special Opcode Base");
2724 Asm->EmitInt8(-MinLineDelta);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002725
2726 // Line number standard opcode encodings argument count
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002727 Asm->OutStreamer.AddComment("DW_LNS_copy arg count");
2728 Asm->EmitInt8(0);
2729 Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count");
2730 Asm->EmitInt8(1);
2731 Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count");
2732 Asm->EmitInt8(1);
2733 Asm->OutStreamer.AddComment("DW_LNS_set_file arg count");
2734 Asm->EmitInt8(1);
2735 Asm->OutStreamer.AddComment("DW_LNS_set_column arg count");
2736 Asm->EmitInt8(1);
2737 Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count");
2738 Asm->EmitInt8(0);
2739 Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count");
2740 Asm->EmitInt8(0);
2741 Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count");
2742 Asm->EmitInt8(0);
2743 Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count");
2744 Asm->EmitInt8(1);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002745
2746 // Emit directories.
2747 for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) {
2748 const std::string &Dir = getSourceDirectoryName(DI);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002749 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002750 Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0);
2751 }
2752
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002753 Asm->OutStreamer.AddComment("End of directories");
2754 Asm->EmitInt8(0);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002755
2756 // Emit files.
2757 for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) {
2758 // Remember source id starts at 1.
2759 std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI);
2760 const std::string &FN = getSourceFileName(Id.second);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002761 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002762 Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0);
2763
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002764 Asm->EmitULEB128(Id.first, "Directory #");
2765 Asm->EmitULEB128(0, "Mod date");
2766 Asm->EmitULEB128(0, "File size");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002767 }
2768
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002769 Asm->OutStreamer.AddComment("End of files");
2770 Asm->EmitInt8(0);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002771
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002772 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end"));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002773
2774 // A sequence for each text section.
2775 unsigned SecSrcLinesSize = SectionSourceLines.size();
2776
2777 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2778 // Isolate current sections line info.
2779 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2780
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002781 // Dwarf assumes we start with first line of first source file.
2782 unsigned Source = 1;
2783 unsigned Line = 1;
2784
2785 // Construct rows of the address, source, line, column matrix.
2786 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2787 const SrcLineInfo &LineInfo = LineInfos[i];
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002788 MCSymbol *Label = LineInfo.getLabel();
2789 if (!Label->isDefined()) continue; // Not emitted, in dead code.
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002790
2791 if (LineInfo.getLine() == 0) continue;
2792
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002793 if (Asm->isVerbose()) {
2794 std::pair<unsigned, unsigned> SrcID =
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002795 getSourceDirectoryAndFileIds(LineInfo.getSourceID());
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002796 Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) +
2797 "/" +
2798 Twine(getSourceFileName(SrcID.second)) +
2799 ":" + Twine(LineInfo.getLine()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002800 }
2801
2802 // Define the line address.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002803 Asm->OutStreamer.AddComment("Extended Op");
2804 Asm->EmitInt8(0);
2805 Asm->OutStreamer.AddComment("Op size");
2806 Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002807
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002808 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2809 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2810
2811 Asm->OutStreamer.AddComment("Location label");
2812 Asm->OutStreamer.EmitSymbolValue(Label,
2813 Asm->getTargetData().getPointerSize(),
2814 0/*AddrSpace*/);
2815
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002816 // If change of source, then switch to the new source.
2817 if (Source != LineInfo.getSourceID()) {
2818 Source = LineInfo.getSourceID();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002819 Asm->OutStreamer.AddComment("DW_LNS_set_file");
2820 Asm->EmitInt8(dwarf::DW_LNS_set_file);
2821 Asm->EmitULEB128(Source, "New Source");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002822 }
2823
2824 // If change of line.
2825 if (Line != LineInfo.getLine()) {
2826 // Determine offset.
2827 int Offset = LineInfo.getLine() - Line;
2828 int Delta = Offset - MinLineDelta;
2829
2830 // Update line.
2831 Line = LineInfo.getLine();
2832
2833 // If delta is small enough and in range...
2834 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2835 // ... then use fast opcode.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002836 Asm->OutStreamer.AddComment("Line Delta");
2837 Asm->EmitInt8(Delta - MinLineDelta);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002838 } else {
2839 // ... otherwise use long hand.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002840 Asm->OutStreamer.AddComment("DW_LNS_advance_line");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002841 Asm->EmitInt8(dwarf::DW_LNS_advance_line);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002842 Asm->EmitSLEB128(Offset, "Line Offset");
2843 Asm->OutStreamer.AddComment("DW_LNS_copy");
2844 Asm->EmitInt8(dwarf::DW_LNS_copy);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002845 }
2846 } else {
2847 // Copy the previous row (different address or source)
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002848 Asm->OutStreamer.AddComment("DW_LNS_copy");
2849 Asm->EmitInt8(dwarf::DW_LNS_copy);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002850 }
2851 }
2852
2853 emitEndOfLineMatrix(j + 1);
2854 }
2855
2856 if (SecSrcLinesSize == 0)
2857 // Because we're emitting a debug_line section, we still need a line
2858 // table. The linker and friends expect it to exist. If there's nothing to
2859 // put into it, emit an empty table.
2860 emitEndOfLineMatrix(1);
2861
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002862 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end"));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002863}
2864
2865/// emitCommonDebugFrame - Emit common frame info into a debug frame section.
2866///
2867void DwarfDebug::emitCommonDebugFrame() {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002868 if (!Asm->MAI->doesDwarfRequireFrameSection())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002869 return;
2870
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002871 int stackGrowth = Asm->getTargetData().getPointerSize();
2872 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2873 TargetFrameInfo::StackGrowsDown)
2874 stackGrowth *= -1;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002875
2876 // Start the dwarf frame section.
2877 Asm->OutStreamer.SwitchSection(
2878 Asm->getObjFileLowering().getDwarfFrameSection());
2879
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002880 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common"));
2881 Asm->OutStreamer.AddComment("Length of Common Information Entry");
2882 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"),
2883 Asm->GetTempSymbol("debug_frame_common_begin"), 4);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002884
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002885 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin"));
2886 Asm->OutStreamer.AddComment("CIE Identifier Tag");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002887 Asm->EmitInt32((int)dwarf::DW_CIE_ID);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002888 Asm->OutStreamer.AddComment("CIE Version");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002889 Asm->EmitInt8(dwarf::DW_CIE_VERSION);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002890 Asm->OutStreamer.AddComment("CIE Augmentation");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002891 Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002892 Asm->EmitULEB128(1, "CIE Code Alignment Factor");
2893 Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor");
2894 Asm->OutStreamer.AddComment("CIE RA Column");
2895 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002896 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002897
2898 std::vector<MachineMove> Moves;
2899 RI->getInitialFrameState(Moves);
2900
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002901 Asm->EmitFrameMoves(Moves, 0, false);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002902
2903 Asm->EmitAlignment(2, 0, 0, false);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002904 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end"));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002905}
2906
2907/// emitFunctionDebugFrame - Emit per function frame info into a debug frame
2908/// section.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002909void DwarfDebug::
2910emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2911 if (!Asm->MAI->doesDwarfRequireFrameSection())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002912 return;
2913
2914 // Start the dwarf frame section.
2915 Asm->OutStreamer.SwitchSection(
2916 Asm->getObjFileLowering().getDwarfFrameSection());
2917
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002918 Asm->OutStreamer.AddComment("Length of Frame Information Entry");
2919 MCSymbol *DebugFrameBegin =
2920 Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number);
2921 MCSymbol *DebugFrameEnd =
2922 Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number);
2923 Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002924
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002925 Asm->OutStreamer.EmitLabel(DebugFrameBegin);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002926
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002927 Asm->OutStreamer.AddComment("FDE CIE offset");
2928 Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"),
2929 DwarfFrameSectionSym);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002930
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002931 Asm->OutStreamer.AddComment("FDE initial location");
2932 MCSymbol *FuncBeginSym =
2933 Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number);
2934 Asm->OutStreamer.EmitSymbolValue(FuncBeginSym,
2935 Asm->getTargetData().getPointerSize(),
2936 0/*AddrSpace*/);
2937
2938
2939 Asm->OutStreamer.AddComment("FDE address range");
2940 Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number),
2941 FuncBeginSym, Asm->getTargetData().getPointerSize());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002942
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002943 Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002944
2945 Asm->EmitAlignment(2, 0, 0, false);
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002946 Asm->OutStreamer.EmitLabel(DebugFrameEnd);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002947}
2948
2949/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2950///
2951void DwarfDebug::emitDebugPubNames() {
2952 // Start the dwarf pubnames section.
2953 Asm->OutStreamer.SwitchSection(
2954 Asm->getObjFileLowering().getDwarfPubNamesSection());
2955
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002956 Asm->OutStreamer.AddComment("Length of Public Names Info");
2957 Asm->EmitLabelDifference(
2958 Asm->GetTempSymbol("pubnames_end", ModuleCU->getID()),
2959 Asm->GetTempSymbol("pubnames_begin", ModuleCU->getID()), 4);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002960
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002961 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
2962 ModuleCU->getID()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002963
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002964 Asm->OutStreamer.AddComment("DWARF Version");
2965 Asm->EmitInt16(dwarf::DWARF_VERSION);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002966
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002967 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2968 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
2969 DwarfInfoSectionSym);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002970
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002971 Asm->OutStreamer.AddComment("Compilation Unit Length");
2972 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
2973 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
2974 4);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002975
2976 const StringMap<DIE*> &Globals = ModuleCU->getGlobals();
2977 for (StringMap<DIE*>::const_iterator
2978 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2979 const char *Name = GI->getKeyData();
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002980 DIE *Entity = GI->second;
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002981
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002982 Asm->OutStreamer.AddComment("DIE offset");
2983 Asm->EmitInt32(Entity->getOffset());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002984
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002985 if (Asm->isVerbose())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002986 Asm->OutStreamer.AddComment("External Name");
2987 Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
2988 }
2989
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07002990 Asm->OutStreamer.AddComment("End Mark");
2991 Asm->EmitInt32(0);
2992 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
2993 ModuleCU->getID()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08002994}
2995
2996void DwarfDebug::emitDebugPubTypes() {
2997 // Start the dwarf pubnames section.
2998 Asm->OutStreamer.SwitchSection(
2999 Asm->getObjFileLowering().getDwarfPubTypesSection());
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003000 Asm->OutStreamer.AddComment("Length of Public Types Info");
3001 Asm->EmitLabelDifference(
3002 Asm->GetTempSymbol("pubtypes_end", ModuleCU->getID()),
3003 Asm->GetTempSymbol("pubtypes_begin", ModuleCU->getID()), 4);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003004
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003005 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
3006 ModuleCU->getID()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003007
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003008 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003009 Asm->EmitInt16(dwarf::DWARF_VERSION);
3010
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003011 Asm->OutStreamer.AddComment("Offset of Compilation ModuleCU Info");
3012 Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3013 DwarfInfoSectionSym);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003014
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003015 Asm->OutStreamer.AddComment("Compilation ModuleCU Length");
3016 Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()),
3017 Asm->GetTempSymbol("info_begin", ModuleCU->getID()),
3018 4);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003019
3020 const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes();
3021 for (StringMap<DIE*>::const_iterator
3022 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
3023 const char *Name = GI->getKeyData();
3024 DIE * Entity = GI->second;
3025
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003026 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003027 Asm->EmitInt32(Entity->getOffset());
3028
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003029 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003030 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
3031 }
3032
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003033 Asm->OutStreamer.AddComment("End Mark");
3034 Asm->EmitInt32(0);
3035 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
3036 ModuleCU->getID()));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003037}
3038
3039/// emitDebugStr - Emit visible names into a debug str section.
3040///
3041void DwarfDebug::emitDebugStr() {
3042 // Check to see if it is worth the effort.
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003043 if (StringPool.empty()) return;
3044
3045 // Start the dwarf str section.
3046 Asm->OutStreamer.SwitchSection(
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003047 Asm->getObjFileLowering().getDwarfStrSection());
3048
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003049 // Get all of the string pool entries and put them in an array by their ID so
3050 // we can sort them.
3051 SmallVector<std::pair<unsigned,
3052 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
3053
3054 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
3055 I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
3056 Entries.push_back(std::make_pair(I->second.second, &*I));
3057
3058 array_pod_sort(Entries.begin(), Entries.end());
3059
3060 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
3061 // Emit a label for reference from debug information entries.
3062 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
3063
3064 // Emit the string itself.
3065 Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003066 }
3067}
3068
3069/// emitDebugLoc - Emit visible names into a debug loc section.
3070///
3071void DwarfDebug::emitDebugLoc() {
3072 // Start the dwarf loc section.
3073 Asm->OutStreamer.SwitchSection(
3074 Asm->getObjFileLowering().getDwarfLocSection());
3075}
3076
3077/// EmitDebugARanges - Emit visible names into a debug aranges section.
3078///
3079void DwarfDebug::EmitDebugARanges() {
3080 // Start the dwarf aranges section.
3081 Asm->OutStreamer.SwitchSection(
3082 Asm->getObjFileLowering().getDwarfARangesSection());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003083}
3084
3085/// emitDebugRanges - Emit visible names into a debug ranges section.
3086///
3087void DwarfDebug::emitDebugRanges() {
3088 // Start the dwarf ranges section.
3089 Asm->OutStreamer.SwitchSection(
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003090 Asm->getObjFileLowering().getDwarfRangesSection());
3091 for (SmallVector<const MCSymbol *, 8>::const_iterator I = DebugRangeSymbols.begin(),
3092 E = DebugRangeSymbols.end(); I != E; ++I) {
3093 if (*I)
3094 Asm->EmitLabelDifference(*I, TextSectionSym,
3095 Asm->getTargetData().getPointerSize());
3096 else
3097 Asm->OutStreamer.EmitIntValue(0, Asm->getTargetData().getPointerSize(),
3098 /*addrspace*/0);
3099 }
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003100}
3101
3102/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
3103///
3104void DwarfDebug::emitDebugMacInfo() {
3105 if (const MCSection *LineInfo =
3106 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
3107 // Start the dwarf macinfo section.
3108 Asm->OutStreamer.SwitchSection(LineInfo);
3109 }
3110}
3111
3112/// emitDebugInlineInfo - Emit inline info using following format.
3113/// Section Header:
3114/// 1. length of section
3115/// 2. Dwarf version number
3116/// 3. address size.
3117///
3118/// Entries (one "entry" for each function that was inlined):
3119///
3120/// 1. offset into __debug_str section for MIPS linkage name, if exists;
3121/// otherwise offset into __debug_str for regular function name.
3122/// 2. offset into __debug_str section for regular function name.
3123/// 3. an unsigned LEB128 number indicating the number of distinct inlining
3124/// instances for the function.
3125///
3126/// The rest of the entry consists of a {die_offset, low_pc} pair for each
3127/// inlined instance; the die_offset points to the inlined_subroutine die in the
3128/// __debug_info section, and the low_pc is the starting address for the
3129/// inlining instance.
3130void DwarfDebug::emitDebugInlineInfo() {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003131 if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003132 return;
3133
3134 if (!ModuleCU)
3135 return;
3136
3137 Asm->OutStreamer.SwitchSection(
3138 Asm->getObjFileLowering().getDwarfDebugInlineSection());
3139
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003140 Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
3141 Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
3142 Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003143
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003144 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003145
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003146 Asm->OutStreamer.AddComment("Dwarf Version");
3147 Asm->EmitInt16(dwarf::DWARF_VERSION);
3148 Asm->OutStreamer.AddComment("Address Size (in bytes)");
3149 Asm->EmitInt8(Asm->getTargetData().getPointerSize());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003150
3151 for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
3152 E = InlinedSPNodes.end(); I != E; ++I) {
3153
3154 MDNode *Node = *I;
3155 DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
3156 = InlineInfo.find(Node);
3157 SmallVector<InlineInfoLabels, 4> &Labels = II->second;
3158 DISubprogram SP(Node);
3159 StringRef LName = SP.getLinkageName();
3160 StringRef Name = SP.getName();
3161
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003162 Asm->OutStreamer.AddComment("MIPS linkage name");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003163 if (LName.empty()) {
3164 Asm->OutStreamer.EmitBytes(Name, 0);
3165 Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
3166 } else
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003167 Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
3168 DwarfStrSectionSym);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003169
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003170 Asm->OutStreamer.AddComment("Function name");
3171 Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
3172 Asm->EmitULEB128(Labels.size(), "Inline count");
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003173
3174 for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
3175 LE = Labels.end(); LI != LE; ++LI) {
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003176 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
3177 Asm->EmitInt32(LI->second->getOffset());
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003178
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003179 if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
3180 Asm->OutStreamer.EmitSymbolValue(LI->first,
3181 Asm->getTargetData().getPointerSize(),0);
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003182 }
3183 }
3184
Shih-wei Liao7abe37e2010-04-28 01:47:00 -07003185 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
Shih-wei Liaoe264f622010-02-10 11:10:31 -08003186}