Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains support for writing dwarf debug info into asm files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 13 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 14 | #include "DwarfDebug.h" |
| 15 | #include "llvm/Module.h" |
David Greene | b2c66fc | 2009-08-19 21:52:55 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSection.h" |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmInfo.h" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
| 22 | #include "llvm/Target/TargetFrameInfo.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 24 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 23132b1 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Timer.h" |
| 27 | #include "llvm/System/Path.h" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | static TimerGroup &getDwarfTimerGroup() { |
| 31 | static TimerGroup DwarfTimerGroup("Dwarf Debugging"); |
| 32 | return DwarfTimerGroup; |
| 33 | } |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
| 37 | /// Configuration values for initial hash set sizes (log2). |
| 38 | /// |
| 39 | static const unsigned InitDiesSetSize = 9; // log2(512) |
| 40 | static const unsigned InitAbbreviationsSetSize = 9; // log2(512) |
| 41 | static const unsigned InitValuesSetSize = 9; // log2(512) |
| 42 | |
| 43 | namespace llvm { |
| 44 | |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | /// CompileUnit - This dwarf writer support class manages information associate |
| 47 | /// with a source file. |
| 48 | class VISIBILITY_HIDDEN CompileUnit { |
| 49 | /// ID - File identifier for source. |
| 50 | /// |
| 51 | unsigned ID; |
| 52 | |
| 53 | /// Die - Compile unit debug information entry. |
| 54 | /// |
| 55 | DIE *Die; |
| 56 | |
| 57 | /// GVToDieMap - Tracks the mapping of unit level debug informaton |
| 58 | /// variables to debug information entries. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 59 | std::map<GlobalVariable *, DIE *> GVToDieMap; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 60 | |
| 61 | /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton |
| 62 | /// descriptors to debug information entries using a DIEEntry proxy. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 63 | std::map<GlobalVariable *, DIEEntry *> GVToDIEEntryMap; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 64 | |
| 65 | /// Globals - A map of globally visible named entities for this unit. |
| 66 | /// |
| 67 | StringMap<DIE*> Globals; |
| 68 | |
| 69 | /// DiesSet - Used to uniquely define dies within the compile unit. |
| 70 | /// |
| 71 | FoldingSet<DIE> DiesSet; |
| 72 | public: |
| 73 | CompileUnit(unsigned I, DIE *D) |
Bill Wendling | 39dd696 | 2009-05-20 23:31:45 +0000 | [diff] [blame] | 74 | : ID(I), Die(D), DiesSet(InitDiesSetSize) {} |
| 75 | ~CompileUnit() { delete Die; } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 76 | |
| 77 | // Accessors. |
Bill Wendling | 39dd696 | 2009-05-20 23:31:45 +0000 | [diff] [blame] | 78 | unsigned getID() const { return ID; } |
| 79 | DIE* getDie() const { return Die; } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 80 | StringMap<DIE*> &getGlobals() { return Globals; } |
| 81 | |
| 82 | /// hasContent - Return true if this compile unit has something to write out. |
| 83 | /// |
Bill Wendling | 39dd696 | 2009-05-20 23:31:45 +0000 | [diff] [blame] | 84 | bool hasContent() const { return !Die->getChildren().empty(); } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 85 | |
| 86 | /// AddGlobal - Add a new global entity to the compile unit. |
| 87 | /// |
Bill Wendling | 39dd696 | 2009-05-20 23:31:45 +0000 | [diff] [blame] | 88 | void AddGlobal(const std::string &Name, DIE *Die) { Globals[Name] = Die; } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 89 | |
| 90 | /// getDieMapSlotFor - Returns the debug information entry map slot for the |
| 91 | /// specified debug variable. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 92 | DIE *&getDieMapSlotFor(GlobalVariable *GV) { return GVToDieMap[GV]; } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 1cda87c | 2009-07-14 04:50:12 +0000 | [diff] [blame] | 94 | /// getDIEEntrySlotFor - Returns the debug information entry proxy slot for |
| 95 | /// the specified debug variable. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 96 | DIEEntry *&getDIEEntrySlotFor(GlobalVariable *GV) { |
| 97 | return GVToDIEEntryMap[GV]; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /// AddDie - Adds or interns the DIE to the compile unit. |
| 101 | /// |
| 102 | DIE *AddDie(DIE &Buffer) { |
| 103 | FoldingSetNodeID ID; |
| 104 | Buffer.Profile(ID); |
| 105 | void *Where; |
| 106 | DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where); |
| 107 | |
| 108 | if (!Die) { |
| 109 | Die = new DIE(Buffer); |
| 110 | DiesSet.InsertNode(Die, Where); |
| 111 | this->Die->AddChild(Die); |
| 112 | Buffer.Detach(); |
| 113 | } |
| 114 | |
| 115 | return Die; |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | //===----------------------------------------------------------------------===// |
| 120 | /// DbgVariable - This class is used to track local variable information. |
| 121 | /// |
| 122 | class VISIBILITY_HIDDEN DbgVariable { |
| 123 | DIVariable Var; // Variable Descriptor. |
| 124 | unsigned FrameIndex; // Variable frame index. |
Bill Wendling | 1180c78 | 2009-05-18 23:08:55 +0000 | [diff] [blame] | 125 | bool InlinedFnVar; // Variable for an inlined function. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 126 | public: |
Bill Wendling | 1180c78 | 2009-05-18 23:08:55 +0000 | [diff] [blame] | 127 | DbgVariable(DIVariable V, unsigned I, bool IFV) |
| 128 | : Var(V), FrameIndex(I), InlinedFnVar(IFV) {} |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 129 | |
| 130 | // Accessors. |
Bill Wendling | 1180c78 | 2009-05-18 23:08:55 +0000 | [diff] [blame] | 131 | DIVariable getVariable() const { return Var; } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 132 | unsigned getFrameIndex() const { return FrameIndex; } |
Bill Wendling | 1180c78 | 2009-05-18 23:08:55 +0000 | [diff] [blame] | 133 | bool isInlinedFnVar() const { return InlinedFnVar; } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | //===----------------------------------------------------------------------===// |
| 137 | /// DbgScope - This class is used to track scope information. |
| 138 | /// |
| 139 | class DbgConcreteScope; |
| 140 | class VISIBILITY_HIDDEN DbgScope { |
| 141 | DbgScope *Parent; // Parent to this scope. |
| 142 | DIDescriptor Desc; // Debug info descriptor for scope. |
| 143 | // Either subprogram or block. |
| 144 | unsigned StartLabelID; // Label ID of the beginning of scope. |
| 145 | unsigned EndLabelID; // Label ID of the end of scope. |
| 146 | SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope. |
| 147 | SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope. |
| 148 | SmallVector<DbgConcreteScope *, 8> ConcreteInsts;// Concrete insts of funcs. |
Owen Anderson | 04c05f7 | 2009-06-24 22:53:20 +0000 | [diff] [blame] | 149 | |
| 150 | // Private state for dump() |
| 151 | mutable unsigned IndentLevel; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 152 | public: |
| 153 | DbgScope(DbgScope *P, DIDescriptor D) |
Owen Anderson | 04c05f7 | 2009-06-24 22:53:20 +0000 | [diff] [blame] | 154 | : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), IndentLevel(0) {} |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 155 | virtual ~DbgScope(); |
| 156 | |
| 157 | // Accessors. |
| 158 | DbgScope *getParent() const { return Parent; } |
| 159 | DIDescriptor getDesc() const { return Desc; } |
| 160 | unsigned getStartLabelID() const { return StartLabelID; } |
| 161 | unsigned getEndLabelID() const { return EndLabelID; } |
| 162 | SmallVector<DbgScope *, 4> &getScopes() { return Scopes; } |
| 163 | SmallVector<DbgVariable *, 8> &getVariables() { return Variables; } |
| 164 | SmallVector<DbgConcreteScope*,8> &getConcreteInsts() { return ConcreteInsts; } |
| 165 | void setStartLabelID(unsigned S) { StartLabelID = S; } |
| 166 | void setEndLabelID(unsigned E) { EndLabelID = E; } |
| 167 | |
| 168 | /// AddScope - Add a scope to the scope. |
| 169 | /// |
| 170 | void AddScope(DbgScope *S) { Scopes.push_back(S); } |
| 171 | |
| 172 | /// AddVariable - Add a variable to the scope. |
| 173 | /// |
| 174 | void AddVariable(DbgVariable *V) { Variables.push_back(V); } |
| 175 | |
| 176 | /// AddConcreteInst - Add a concrete instance to the scope. |
| 177 | /// |
| 178 | void AddConcreteInst(DbgConcreteScope *C) { ConcreteInsts.push_back(C); } |
| 179 | |
| 180 | #ifndef NDEBUG |
| 181 | void dump() const; |
| 182 | #endif |
| 183 | }; |
| 184 | |
| 185 | #ifndef NDEBUG |
| 186 | void DbgScope::dump() const { |
Chris Lattner | c281de1 | 2009-08-23 00:51:00 +0000 | [diff] [blame] | 187 | raw_ostream &err = errs(); |
| 188 | err.indent(IndentLevel); |
| 189 | Desc.dump(); |
| 190 | err << " [" << StartLabelID << ", " << EndLabelID << "]\n"; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 191 | |
| 192 | IndentLevel += 2; |
| 193 | |
| 194 | for (unsigned i = 0, e = Scopes.size(); i != e; ++i) |
| 195 | if (Scopes[i] != this) |
| 196 | Scopes[i]->dump(); |
| 197 | |
| 198 | IndentLevel -= 2; |
| 199 | } |
| 200 | #endif |
| 201 | |
| 202 | //===----------------------------------------------------------------------===// |
| 203 | /// DbgConcreteScope - This class is used to track a scope that holds concrete |
| 204 | /// instance information. |
| 205 | /// |
| 206 | class VISIBILITY_HIDDEN DbgConcreteScope : public DbgScope { |
| 207 | CompileUnit *Unit; |
| 208 | DIE *Die; // Debug info for this concrete scope. |
| 209 | public: |
| 210 | DbgConcreteScope(DIDescriptor D) : DbgScope(NULL, D) {} |
| 211 | |
| 212 | // Accessors. |
| 213 | DIE *getDie() const { return Die; } |
| 214 | void setDie(DIE *D) { Die = D; } |
| 215 | }; |
| 216 | |
| 217 | DbgScope::~DbgScope() { |
| 218 | for (unsigned i = 0, N = Scopes.size(); i < N; ++i) |
| 219 | delete Scopes[i]; |
| 220 | for (unsigned j = 0, M = Variables.size(); j < M; ++j) |
| 221 | delete Variables[j]; |
| 222 | for (unsigned k = 0, O = ConcreteInsts.size(); k < O; ++k) |
| 223 | delete ConcreteInsts[k]; |
| 224 | } |
| 225 | |
| 226 | } // end llvm namespace |
| 227 | |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 228 | DwarfDebug::DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T) |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 229 | : Dwarf(OS, A, T, "dbg"), ModuleCU(0), |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 230 | AbbreviationsSet(InitAbbreviationsSetSize), Abbreviations(), |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 231 | ValuesSet(InitValuesSetSize), Values(), StringPool(), |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 232 | SectionSourceLines(), didInitial(false), shouldEmit(false), |
Devang Patel | 43da8fb | 2009-07-13 21:26:33 +0000 | [diff] [blame] | 233 | FunctionDbgScope(0), DebugTimer(0) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 234 | if (TimePassesIsEnabled) |
| 235 | DebugTimer = new Timer("Dwarf Debug Writer", |
| 236 | getDwarfTimerGroup()); |
| 237 | } |
| 238 | DwarfDebug::~DwarfDebug() { |
| 239 | for (unsigned j = 0, M = Values.size(); j < M; ++j) |
| 240 | delete Values[j]; |
| 241 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 242 | for (DenseMap<const GlobalVariable *, DbgScope *>::iterator |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 243 | I = AbstractInstanceRootMap.begin(), |
| 244 | E = AbstractInstanceRootMap.end(); I != E;++I) |
| 245 | delete I->second; |
| 246 | |
| 247 | delete DebugTimer; |
| 248 | } |
| 249 | |
| 250 | /// AssignAbbrevNumber - Define a unique number for the abbreviation. |
| 251 | /// |
| 252 | void DwarfDebug::AssignAbbrevNumber(DIEAbbrev &Abbrev) { |
| 253 | // Profile the node so that we can make it unique. |
| 254 | FoldingSetNodeID ID; |
| 255 | Abbrev.Profile(ID); |
| 256 | |
| 257 | // Check the set for priors. |
| 258 | DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev); |
| 259 | |
| 260 | // If it's newly added. |
| 261 | if (InSet == &Abbrev) { |
| 262 | // Add to abbreviation list. |
| 263 | Abbreviations.push_back(&Abbrev); |
| 264 | |
| 265 | // Assign the vector position + 1 as its number. |
| 266 | Abbrev.setNumber(Abbreviations.size()); |
| 267 | } else { |
| 268 | // Assign existing abbreviation number. |
| 269 | Abbrev.setNumber(InSet->getNumber()); |
| 270 | } |
| 271 | } |
| 272 | |
Bill Wendling | 995f80a | 2009-05-20 23:24:48 +0000 | [diff] [blame] | 273 | /// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug |
| 274 | /// information entry. |
| 275 | DIEEntry *DwarfDebug::CreateDIEEntry(DIE *Entry) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 276 | DIEEntry *Value; |
| 277 | |
| 278 | if (Entry) { |
| 279 | FoldingSetNodeID ID; |
| 280 | DIEEntry::Profile(ID, Entry); |
| 281 | void *Where; |
| 282 | Value = static_cast<DIEEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where)); |
| 283 | |
| 284 | if (Value) return Value; |
| 285 | |
| 286 | Value = new DIEEntry(Entry); |
| 287 | ValuesSet.InsertNode(Value, Where); |
| 288 | } else { |
| 289 | Value = new DIEEntry(Entry); |
| 290 | } |
| 291 | |
| 292 | Values.push_back(Value); |
| 293 | return Value; |
| 294 | } |
| 295 | |
| 296 | /// SetDIEEntry - Set a DIEEntry once the debug information entry is defined. |
| 297 | /// |
| 298 | void DwarfDebug::SetDIEEntry(DIEEntry *Value, DIE *Entry) { |
| 299 | Value->setEntry(Entry); |
| 300 | |
| 301 | // Add to values set if not already there. If it is, we merely have a |
| 302 | // duplicate in the values list (no harm.) |
| 303 | ValuesSet.GetOrInsertNode(Value); |
| 304 | } |
| 305 | |
| 306 | /// AddUInt - Add an unsigned integer attribute data and value. |
| 307 | /// |
| 308 | void DwarfDebug::AddUInt(DIE *Die, unsigned Attribute, |
| 309 | unsigned Form, uint64_t Integer) { |
| 310 | if (!Form) Form = DIEInteger::BestForm(false, Integer); |
| 311 | |
| 312 | FoldingSetNodeID ID; |
| 313 | DIEInteger::Profile(ID, Integer); |
| 314 | void *Where; |
| 315 | DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); |
| 316 | |
| 317 | if (!Value) { |
| 318 | Value = new DIEInteger(Integer); |
| 319 | ValuesSet.InsertNode(Value, Where); |
| 320 | Values.push_back(Value); |
| 321 | } |
| 322 | |
| 323 | Die->AddValue(Attribute, Form, Value); |
| 324 | } |
| 325 | |
| 326 | /// AddSInt - Add an signed integer attribute data and value. |
| 327 | /// |
| 328 | void DwarfDebug::AddSInt(DIE *Die, unsigned Attribute, |
| 329 | unsigned Form, int64_t Integer) { |
| 330 | if (!Form) Form = DIEInteger::BestForm(true, Integer); |
| 331 | |
| 332 | FoldingSetNodeID ID; |
| 333 | DIEInteger::Profile(ID, (uint64_t)Integer); |
| 334 | void *Where; |
| 335 | DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); |
| 336 | |
| 337 | if (!Value) { |
| 338 | Value = new DIEInteger(Integer); |
| 339 | ValuesSet.InsertNode(Value, Where); |
| 340 | Values.push_back(Value); |
| 341 | } |
| 342 | |
| 343 | Die->AddValue(Attribute, Form, Value); |
| 344 | } |
| 345 | |
| 346 | /// AddString - Add a string attribute data and value. |
| 347 | /// |
| 348 | void DwarfDebug::AddString(DIE *Die, unsigned Attribute, unsigned Form, |
| 349 | const std::string &String) { |
| 350 | FoldingSetNodeID ID; |
| 351 | DIEString::Profile(ID, String); |
| 352 | void *Where; |
| 353 | DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); |
| 354 | |
| 355 | if (!Value) { |
| 356 | Value = new DIEString(String); |
| 357 | ValuesSet.InsertNode(Value, Where); |
| 358 | Values.push_back(Value); |
| 359 | } |
| 360 | |
| 361 | Die->AddValue(Attribute, Form, Value); |
| 362 | } |
| 363 | |
| 364 | /// AddLabel - Add a Dwarf label attribute data and value. |
| 365 | /// |
| 366 | void DwarfDebug::AddLabel(DIE *Die, unsigned Attribute, unsigned Form, |
| 367 | const DWLabel &Label) { |
| 368 | FoldingSetNodeID ID; |
| 369 | DIEDwarfLabel::Profile(ID, Label); |
| 370 | void *Where; |
| 371 | DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); |
| 372 | |
| 373 | if (!Value) { |
| 374 | Value = new DIEDwarfLabel(Label); |
| 375 | ValuesSet.InsertNode(Value, Where); |
| 376 | Values.push_back(Value); |
| 377 | } |
| 378 | |
| 379 | Die->AddValue(Attribute, Form, Value); |
| 380 | } |
| 381 | |
| 382 | /// AddObjectLabel - Add an non-Dwarf label attribute data and value. |
| 383 | /// |
| 384 | void DwarfDebug::AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form, |
| 385 | const std::string &Label) { |
| 386 | FoldingSetNodeID ID; |
| 387 | DIEObjectLabel::Profile(ID, Label); |
| 388 | void *Where; |
| 389 | DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); |
| 390 | |
| 391 | if (!Value) { |
| 392 | Value = new DIEObjectLabel(Label); |
| 393 | ValuesSet.InsertNode(Value, Where); |
| 394 | Values.push_back(Value); |
| 395 | } |
| 396 | |
| 397 | Die->AddValue(Attribute, Form, Value); |
| 398 | } |
| 399 | |
| 400 | /// AddSectionOffset - Add a section offset label attribute data and value. |
| 401 | /// |
| 402 | void DwarfDebug::AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form, |
| 403 | const DWLabel &Label, const DWLabel &Section, |
| 404 | bool isEH, bool useSet) { |
| 405 | FoldingSetNodeID ID; |
| 406 | DIESectionOffset::Profile(ID, Label, Section); |
| 407 | void *Where; |
| 408 | DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); |
| 409 | |
| 410 | if (!Value) { |
| 411 | Value = new DIESectionOffset(Label, Section, isEH, useSet); |
| 412 | ValuesSet.InsertNode(Value, Where); |
| 413 | Values.push_back(Value); |
| 414 | } |
| 415 | |
| 416 | Die->AddValue(Attribute, Form, Value); |
| 417 | } |
| 418 | |
| 419 | /// AddDelta - Add a label delta attribute data and value. |
| 420 | /// |
| 421 | void DwarfDebug::AddDelta(DIE *Die, unsigned Attribute, unsigned Form, |
| 422 | const DWLabel &Hi, const DWLabel &Lo) { |
| 423 | FoldingSetNodeID ID; |
| 424 | DIEDelta::Profile(ID, Hi, Lo); |
| 425 | void *Where; |
| 426 | DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); |
| 427 | |
| 428 | if (!Value) { |
| 429 | Value = new DIEDelta(Hi, Lo); |
| 430 | ValuesSet.InsertNode(Value, Where); |
| 431 | Values.push_back(Value); |
| 432 | } |
| 433 | |
| 434 | Die->AddValue(Attribute, Form, Value); |
| 435 | } |
| 436 | |
| 437 | /// AddBlock - Add block data. |
| 438 | /// |
| 439 | void DwarfDebug::AddBlock(DIE *Die, unsigned Attribute, unsigned Form, |
| 440 | DIEBlock *Block) { |
| 441 | Block->ComputeSize(TD); |
| 442 | FoldingSetNodeID ID; |
| 443 | Block->Profile(ID); |
| 444 | void *Where; |
| 445 | DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where); |
| 446 | |
| 447 | if (!Value) { |
| 448 | Value = Block; |
| 449 | ValuesSet.InsertNode(Value, Where); |
| 450 | Values.push_back(Value); |
| 451 | } else { |
| 452 | // Already exists, reuse the previous one. |
| 453 | delete Block; |
| 454 | Block = cast<DIEBlock>(Value); |
| 455 | } |
| 456 | |
| 457 | Die->AddValue(Attribute, Block->BestForm(), Value); |
| 458 | } |
| 459 | |
| 460 | /// AddSourceLine - Add location information to specified debug information |
| 461 | /// entry. |
| 462 | void DwarfDebug::AddSourceLine(DIE *Die, const DIVariable *V) { |
| 463 | // If there is no compile unit specified, don't add a line #. |
| 464 | if (V->getCompileUnit().isNull()) |
| 465 | return; |
| 466 | |
| 467 | unsigned Line = V->getLineNumber(); |
| 468 | unsigned FileID = FindCompileUnit(V->getCompileUnit()).getID(); |
| 469 | assert(FileID && "Invalid file id"); |
| 470 | AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 471 | AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
| 472 | } |
| 473 | |
| 474 | /// AddSourceLine - Add location information to specified debug information |
| 475 | /// entry. |
| 476 | void DwarfDebug::AddSourceLine(DIE *Die, const DIGlobal *G) { |
| 477 | // If there is no compile unit specified, don't add a line #. |
| 478 | if (G->getCompileUnit().isNull()) |
| 479 | return; |
| 480 | |
| 481 | unsigned Line = G->getLineNumber(); |
| 482 | unsigned FileID = FindCompileUnit(G->getCompileUnit()).getID(); |
| 483 | assert(FileID && "Invalid file id"); |
| 484 | AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 485 | AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
| 486 | } |
| 487 | void DwarfDebug::AddSourceLine(DIE *Die, const DIType *Ty) { |
| 488 | // If there is no compile unit specified, don't add a line #. |
| 489 | DICompileUnit CU = Ty->getCompileUnit(); |
| 490 | if (CU.isNull()) |
| 491 | return; |
| 492 | |
| 493 | unsigned Line = Ty->getLineNumber(); |
| 494 | unsigned FileID = FindCompileUnit(CU).getID(); |
| 495 | assert(FileID && "Invalid file id"); |
| 496 | AddUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 497 | AddUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
| 498 | } |
| 499 | |
| 500 | /// AddAddress - Add an address attribute to a die based on the location |
| 501 | /// provided. |
| 502 | void DwarfDebug::AddAddress(DIE *Die, unsigned Attribute, |
| 503 | const MachineLocation &Location) { |
| 504 | unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false); |
| 505 | DIEBlock *Block = new DIEBlock(); |
| 506 | |
| 507 | if (Location.isReg()) { |
| 508 | if (Reg < 32) { |
| 509 | AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg); |
| 510 | } else { |
| 511 | AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); |
| 512 | AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg); |
| 513 | } |
| 514 | } else { |
| 515 | if (Reg < 32) { |
| 516 | AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); |
| 517 | } else { |
| 518 | AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); |
| 519 | AddUInt(Block, 0, dwarf::DW_FORM_udata, Reg); |
| 520 | } |
| 521 | |
| 522 | AddUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset()); |
| 523 | } |
| 524 | |
| 525 | AddBlock(Die, Attribute, 0, Block); |
| 526 | } |
| 527 | |
| 528 | /// AddType - Add a new type attribute to the specified entity. |
| 529 | void DwarfDebug::AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) { |
| 530 | if (Ty.isNull()) |
| 531 | return; |
| 532 | |
| 533 | // Check for pre-existence. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 534 | DIEEntry *&Slot = DW_Unit->getDIEEntrySlotFor(Ty.getGV()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 535 | |
| 536 | // If it exists then use the existing value. |
| 537 | if (Slot) { |
| 538 | Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot); |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | // Set up proxy. |
Bill Wendling | 995f80a | 2009-05-20 23:24:48 +0000 | [diff] [blame] | 543 | Slot = CreateDIEEntry(); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 544 | |
| 545 | // Construct type. |
| 546 | DIE Buffer(dwarf::DW_TAG_base_type); |
| 547 | if (Ty.isBasicType(Ty.getTag())) |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 548 | ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV())); |
| 549 | else if (Ty.isDerivedType(Ty.getTag())) |
| 550 | ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV())); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 551 | else { |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 552 | assert(Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType"); |
| 553 | ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV())); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | // Add debug information entry to entity and appropriate context. |
| 557 | DIE *Die = NULL; |
| 558 | DIDescriptor Context = Ty.getContext(); |
| 559 | if (!Context.isNull()) |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 560 | Die = DW_Unit->getDieMapSlotFor(Context.getGV()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 561 | |
| 562 | if (Die) { |
| 563 | DIE *Child = new DIE(Buffer); |
| 564 | Die->AddChild(Child); |
| 565 | Buffer.Detach(); |
| 566 | SetDIEEntry(Slot, Child); |
| 567 | } else { |
| 568 | Die = DW_Unit->AddDie(Buffer); |
| 569 | SetDIEEntry(Slot, Die); |
| 570 | } |
| 571 | |
| 572 | Entity->AddValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Slot); |
| 573 | } |
| 574 | |
| 575 | /// ConstructTypeDIE - Construct basic type die from DIBasicType. |
| 576 | void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, |
| 577 | DIBasicType BTy) { |
| 578 | // Get core information. |
| 579 | std::string Name; |
| 580 | BTy.getName(Name); |
| 581 | Buffer.setTag(dwarf::DW_TAG_base_type); |
| 582 | AddUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, |
| 583 | BTy.getEncoding()); |
| 584 | |
| 585 | // Add name if not anonymous or intermediate type. |
| 586 | if (!Name.empty()) |
| 587 | AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
| 588 | uint64_t Size = BTy.getSizeInBits() >> 3; |
| 589 | AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); |
| 590 | } |
| 591 | |
| 592 | /// ConstructTypeDIE - Construct derived type die from DIDerivedType. |
| 593 | void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, |
| 594 | DIDerivedType DTy) { |
| 595 | // Get core information. |
| 596 | std::string Name; |
| 597 | DTy.getName(Name); |
| 598 | uint64_t Size = DTy.getSizeInBits() >> 3; |
| 599 | unsigned Tag = DTy.getTag(); |
| 600 | |
| 601 | // FIXME - Workaround for templates. |
| 602 | if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type; |
| 603 | |
| 604 | Buffer.setTag(Tag); |
| 605 | |
| 606 | // Map to main type, void will not have a type. |
| 607 | DIType FromTy = DTy.getTypeDerivedFrom(); |
| 608 | AddType(DW_Unit, &Buffer, FromTy); |
| 609 | |
| 610 | // Add name if not anonymous or intermediate type. |
| 611 | if (!Name.empty()) |
| 612 | AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
| 613 | |
| 614 | // Add size if non-zero (derived types might be zero-sized.) |
| 615 | if (Size) |
| 616 | AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); |
| 617 | |
| 618 | // Add source line info if available and TyDesc is not a forward declaration. |
| 619 | if (!DTy.isForwardDecl()) |
| 620 | AddSourceLine(&Buffer, &DTy); |
| 621 | } |
| 622 | |
| 623 | /// ConstructTypeDIE - Construct type DIE from DICompositeType. |
| 624 | void DwarfDebug::ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, |
| 625 | DICompositeType CTy) { |
| 626 | // Get core information. |
| 627 | std::string Name; |
| 628 | CTy.getName(Name); |
| 629 | |
| 630 | uint64_t Size = CTy.getSizeInBits() >> 3; |
| 631 | unsigned Tag = CTy.getTag(); |
| 632 | Buffer.setTag(Tag); |
| 633 | |
| 634 | switch (Tag) { |
| 635 | case dwarf::DW_TAG_vector_type: |
| 636 | case dwarf::DW_TAG_array_type: |
| 637 | ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy); |
| 638 | break; |
| 639 | case dwarf::DW_TAG_enumeration_type: { |
| 640 | DIArray Elements = CTy.getTypeArray(); |
| 641 | |
| 642 | // Add enumerators to enumeration type. |
| 643 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
| 644 | DIE *ElemDie = NULL; |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 645 | DIEnumerator Enum(Elements.getElement(i).getGV()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 646 | ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum); |
| 647 | Buffer.AddChild(ElemDie); |
| 648 | } |
| 649 | } |
| 650 | break; |
| 651 | case dwarf::DW_TAG_subroutine_type: { |
| 652 | // Add return type. |
| 653 | DIArray Elements = CTy.getTypeArray(); |
| 654 | DIDescriptor RTy = Elements.getElement(0); |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 655 | AddType(DW_Unit, &Buffer, DIType(RTy.getGV())); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 656 | |
| 657 | // Add prototype flag. |
| 658 | AddUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1); |
| 659 | |
| 660 | // Add arguments. |
| 661 | for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) { |
| 662 | DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); |
| 663 | DIDescriptor Ty = Elements.getElement(i); |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 664 | AddType(DW_Unit, Arg, DIType(Ty.getGV())); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 665 | Buffer.AddChild(Arg); |
| 666 | } |
| 667 | } |
| 668 | break; |
| 669 | case dwarf::DW_TAG_structure_type: |
| 670 | case dwarf::DW_TAG_union_type: |
| 671 | case dwarf::DW_TAG_class_type: { |
| 672 | // Add elements to structure type. |
| 673 | DIArray Elements = CTy.getTypeArray(); |
| 674 | |
| 675 | // A forward struct declared type may not have elements available. |
| 676 | if (Elements.isNull()) |
| 677 | break; |
| 678 | |
| 679 | // Add elements to structure type. |
| 680 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
| 681 | DIDescriptor Element = Elements.getElement(i); |
| 682 | DIE *ElemDie = NULL; |
| 683 | if (Element.getTag() == dwarf::DW_TAG_subprogram) |
| 684 | ElemDie = CreateSubprogramDIE(DW_Unit, |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 685 | DISubprogram(Element.getGV())); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 686 | else |
| 687 | ElemDie = CreateMemberDIE(DW_Unit, |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 688 | DIDerivedType(Element.getGV())); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 689 | Buffer.AddChild(ElemDie); |
| 690 | } |
| 691 | |
Devang Patel | a1ba269 | 2009-08-27 23:51:51 +0000 | [diff] [blame^] | 692 | if (CTy.isAppleBlockExtension()) |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 693 | AddUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1); |
| 694 | |
| 695 | unsigned RLang = CTy.getRunTimeLang(); |
| 696 | if (RLang) |
| 697 | AddUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, |
| 698 | dwarf::DW_FORM_data1, RLang); |
| 699 | break; |
| 700 | } |
| 701 | default: |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | // Add name if not anonymous or intermediate type. |
| 706 | if (!Name.empty()) |
| 707 | AddString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
| 708 | |
| 709 | if (Tag == dwarf::DW_TAG_enumeration_type || |
| 710 | Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) { |
| 711 | // Add size if non-zero (derived types might be zero-sized.) |
| 712 | if (Size) |
| 713 | AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); |
| 714 | else { |
| 715 | // Add zero size if it is not a forward declaration. |
| 716 | if (CTy.isForwardDecl()) |
| 717 | AddUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); |
| 718 | else |
| 719 | AddUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0); |
| 720 | } |
| 721 | |
| 722 | // Add source line info if available. |
| 723 | if (!CTy.isForwardDecl()) |
| 724 | AddSourceLine(&Buffer, &CTy); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange. |
| 729 | void DwarfDebug::ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){ |
| 730 | int64_t L = SR.getLo(); |
| 731 | int64_t H = SR.getHi(); |
| 732 | DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type); |
| 733 | |
Devang Patel | 6325a53 | 2009-08-14 20:59:16 +0000 | [diff] [blame] | 734 | AddDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy); |
| 735 | if (L) |
| 736 | AddSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L); |
| 737 | if (H) |
| 738 | AddSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 739 | |
| 740 | Buffer.AddChild(DW_Subrange); |
| 741 | } |
| 742 | |
| 743 | /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType. |
| 744 | void DwarfDebug::ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, |
| 745 | DICompositeType *CTy) { |
| 746 | Buffer.setTag(dwarf::DW_TAG_array_type); |
| 747 | if (CTy->getTag() == dwarf::DW_TAG_vector_type) |
| 748 | AddUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1); |
| 749 | |
| 750 | // Emit derived type. |
| 751 | AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom()); |
| 752 | DIArray Elements = CTy->getTypeArray(); |
| 753 | |
| 754 | // Construct an anonymous type for index type. |
| 755 | DIE IdxBuffer(dwarf::DW_TAG_base_type); |
| 756 | AddUInt(&IdxBuffer, dwarf::DW_AT_byte_size, 0, sizeof(int32_t)); |
| 757 | AddUInt(&IdxBuffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, |
| 758 | dwarf::DW_ATE_signed); |
| 759 | DIE *IndexTy = DW_Unit->AddDie(IdxBuffer); |
| 760 | |
| 761 | // Add subranges to array type. |
| 762 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
| 763 | DIDescriptor Element = Elements.getElement(i); |
| 764 | if (Element.getTag() == dwarf::DW_TAG_subrange_type) |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 765 | ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
| 769 | /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator. |
| 770 | DIE *DwarfDebug::ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) { |
| 771 | DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator); |
| 772 | std::string Name; |
| 773 | ETy->getName(Name); |
| 774 | AddString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
| 775 | int64_t Value = ETy->getEnumValue(); |
| 776 | AddSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value); |
| 777 | return Enumerator; |
| 778 | } |
| 779 | |
| 780 | /// CreateGlobalVariableDIE - Create new DIE using GV. |
| 781 | DIE *DwarfDebug::CreateGlobalVariableDIE(CompileUnit *DW_Unit, |
| 782 | const DIGlobalVariable &GV) { |
| 783 | DIE *GVDie = new DIE(dwarf::DW_TAG_variable); |
| 784 | std::string Name; |
| 785 | GV.getDisplayName(Name); |
| 786 | AddString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
| 787 | std::string LinkageName; |
| 788 | GV.getLinkageName(LinkageName); |
Devang Patel | 53cb17d | 2009-07-16 01:01:22 +0000 | [diff] [blame] | 789 | if (!LinkageName.empty()) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 790 | // Skip special LLVM prefix that is used to inform the asm printer to not |
| 791 | // emit usual symbol prefix before the symbol name. This happens for |
| 792 | // Objective-C symbol names and symbol whose name is replaced using GCC's |
| 793 | // __asm__ attribute. |
Devang Patel | 53cb17d | 2009-07-16 01:01:22 +0000 | [diff] [blame] | 794 | if (LinkageName[0] == 1) |
| 795 | LinkageName = &LinkageName[1]; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 796 | AddString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string, |
Devang Patel | 1a8d2d2 | 2009-07-14 00:55:28 +0000 | [diff] [blame] | 797 | LinkageName); |
Devang Patel | 53cb17d | 2009-07-16 01:01:22 +0000 | [diff] [blame] | 798 | } |
| 799 | AddType(DW_Unit, GVDie, GV.getType()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 800 | if (!GV.isLocalToUnit()) |
| 801 | AddUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1); |
| 802 | AddSourceLine(GVDie, &GV); |
| 803 | return GVDie; |
| 804 | } |
| 805 | |
| 806 | /// CreateMemberDIE - Create new member DIE. |
| 807 | DIE *DwarfDebug::CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT){ |
| 808 | DIE *MemberDie = new DIE(DT.getTag()); |
| 809 | std::string Name; |
| 810 | DT.getName(Name); |
| 811 | if (!Name.empty()) |
| 812 | AddString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
| 813 | |
| 814 | AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom()); |
| 815 | |
| 816 | AddSourceLine(MemberDie, &DT); |
| 817 | |
| 818 | uint64_t Size = DT.getSizeInBits(); |
| 819 | uint64_t FieldSize = DT.getOriginalTypeSize(); |
| 820 | |
| 821 | if (Size != FieldSize) { |
| 822 | // Handle bitfield. |
| 823 | AddUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3); |
| 824 | AddUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits()); |
| 825 | |
| 826 | uint64_t Offset = DT.getOffsetInBits(); |
| 827 | uint64_t FieldOffset = Offset; |
| 828 | uint64_t AlignMask = ~(DT.getAlignInBits() - 1); |
| 829 | uint64_t HiMark = (Offset + FieldSize) & AlignMask; |
| 830 | FieldOffset = (HiMark - FieldSize); |
| 831 | Offset -= FieldOffset; |
| 832 | |
| 833 | // Maybe we need to work from the other end. |
| 834 | if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size); |
| 835 | AddUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset); |
| 836 | } |
| 837 | |
| 838 | DIEBlock *Block = new DIEBlock(); |
| 839 | AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 840 | AddUInt(Block, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3); |
| 841 | AddBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, Block); |
| 842 | |
| 843 | if (DT.isProtected()) |
| 844 | AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0, |
| 845 | dwarf::DW_ACCESS_protected); |
| 846 | else if (DT.isPrivate()) |
| 847 | AddUInt(MemberDie, dwarf::DW_AT_accessibility, 0, |
| 848 | dwarf::DW_ACCESS_private); |
| 849 | |
| 850 | return MemberDie; |
| 851 | } |
| 852 | |
| 853 | /// CreateSubprogramDIE - Create new DIE using SP. |
| 854 | DIE *DwarfDebug::CreateSubprogramDIE(CompileUnit *DW_Unit, |
| 855 | const DISubprogram &SP, |
Bill Wendling | 6679ee4 | 2009-05-18 22:02:36 +0000 | [diff] [blame] | 856 | bool IsConstructor, |
| 857 | bool IsInlined) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 858 | DIE *SPDie = new DIE(dwarf::DW_TAG_subprogram); |
| 859 | |
| 860 | std::string Name; |
| 861 | SP.getName(Name); |
| 862 | AddString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
| 863 | |
| 864 | std::string LinkageName; |
| 865 | SP.getLinkageName(LinkageName); |
Devang Patel | 53cb17d | 2009-07-16 01:01:22 +0000 | [diff] [blame] | 866 | if (!LinkageName.empty()) { |
| 867 | // Skip special LLVM prefix that is used to inform the asm printer to not emit |
| 868 | // usual symbol prefix before the symbol name. This happens for Objective-C |
| 869 | // symbol names and symbol whose name is replaced using GCC's __asm__ attribute. |
| 870 | if (LinkageName[0] == 1) |
| 871 | LinkageName = &LinkageName[1]; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 872 | AddString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string, |
Devang Patel | 1a8d2d2 | 2009-07-14 00:55:28 +0000 | [diff] [blame] | 873 | LinkageName); |
Devang Patel | 53cb17d | 2009-07-16 01:01:22 +0000 | [diff] [blame] | 874 | } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 875 | AddSourceLine(SPDie, &SP); |
| 876 | |
| 877 | DICompositeType SPTy = SP.getType(); |
| 878 | DIArray Args = SPTy.getTypeArray(); |
| 879 | |
| 880 | // Add prototyped tag, if C or ObjC. |
| 881 | unsigned Lang = SP.getCompileUnit().getLanguage(); |
| 882 | if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 || |
| 883 | Lang == dwarf::DW_LANG_ObjC) |
| 884 | AddUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1); |
| 885 | |
| 886 | // Add Return Type. |
| 887 | unsigned SPTag = SPTy.getTag(); |
| 888 | if (!IsConstructor) { |
| 889 | if (Args.isNull() || SPTag != dwarf::DW_TAG_subroutine_type) |
| 890 | AddType(DW_Unit, SPDie, SPTy); |
| 891 | else |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 892 | AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV())); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | if (!SP.isDefinition()) { |
| 896 | AddUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); |
| 897 | |
| 898 | // Add arguments. Do not add arguments for subprogram definition. They will |
| 899 | // be handled through RecordVariable. |
| 900 | if (SPTag == dwarf::DW_TAG_subroutine_type) |
| 901 | for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { |
| 902 | DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 903 | AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV())); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 904 | AddUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); // ?? |
| 905 | SPDie->AddChild(Arg); |
| 906 | } |
| 907 | } |
| 908 | |
Bill Wendling | 6679ee4 | 2009-05-18 22:02:36 +0000 | [diff] [blame] | 909 | if (!SP.isLocalToUnit() && !IsInlined) |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 910 | AddUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1); |
| 911 | |
| 912 | // DW_TAG_inlined_subroutine may refer to this DIE. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 913 | DIE *&Slot = DW_Unit->getDieMapSlotFor(SP.getGV()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 914 | Slot = SPDie; |
| 915 | return SPDie; |
| 916 | } |
| 917 | |
| 918 | /// FindCompileUnit - Get the compile unit for the given descriptor. |
| 919 | /// |
| 920 | CompileUnit &DwarfDebug::FindCompileUnit(DICompileUnit Unit) const { |
| 921 | DenseMap<Value *, CompileUnit *>::const_iterator I = |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 922 | CompileUnitMap.find(Unit.getGV()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 923 | assert(I != CompileUnitMap.end() && "Missing compile unit."); |
| 924 | return *I->second; |
| 925 | } |
| 926 | |
Bill Wendling | 995f80a | 2009-05-20 23:24:48 +0000 | [diff] [blame] | 927 | /// CreateDbgScopeVariable - Create a new scope variable. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 928 | /// |
Bill Wendling | 995f80a | 2009-05-20 23:24:48 +0000 | [diff] [blame] | 929 | DIE *DwarfDebug::CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 930 | // Get the descriptor. |
| 931 | const DIVariable &VD = DV->getVariable(); |
| 932 | |
| 933 | // Translate tag to proper Dwarf tag. The result variable is dropped for |
| 934 | // now. |
| 935 | unsigned Tag; |
| 936 | switch (VD.getTag()) { |
| 937 | case dwarf::DW_TAG_return_variable: |
| 938 | return NULL; |
| 939 | case dwarf::DW_TAG_arg_variable: |
| 940 | Tag = dwarf::DW_TAG_formal_parameter; |
| 941 | break; |
| 942 | case dwarf::DW_TAG_auto_variable: // fall thru |
| 943 | default: |
| 944 | Tag = dwarf::DW_TAG_variable; |
| 945 | break; |
| 946 | } |
| 947 | |
| 948 | // Define variable debug information entry. |
| 949 | DIE *VariableDie = new DIE(Tag); |
| 950 | std::string Name; |
| 951 | VD.getName(Name); |
| 952 | AddString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
| 953 | |
| 954 | // Add source line info if available. |
| 955 | AddSourceLine(VariableDie, &VD); |
| 956 | |
| 957 | // Add variable type. |
| 958 | AddType(Unit, VariableDie, VD.getType()); |
| 959 | |
| 960 | // Add variable address. |
Bill Wendling | 1180c78 | 2009-05-18 23:08:55 +0000 | [diff] [blame] | 961 | if (!DV->isInlinedFnVar()) { |
| 962 | // Variables for abstract instances of inlined functions don't get a |
| 963 | // location. |
| 964 | MachineLocation Location; |
| 965 | Location.set(RI->getFrameRegister(*MF), |
| 966 | RI->getFrameIndexOffset(*MF, DV->getFrameIndex())); |
| 967 | AddAddress(VariableDie, dwarf::DW_AT_location, Location); |
| 968 | } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 969 | |
| 970 | return VariableDie; |
| 971 | } |
| 972 | |
| 973 | /// getOrCreateScope - Returns the scope associated with the given descriptor. |
| 974 | /// |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 975 | DbgScope *DwarfDebug::getOrCreateScope(GlobalVariable *V) { |
| 976 | DbgScope *&Slot = DbgScopeMap[V]; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 977 | if (Slot) return Slot; |
| 978 | |
| 979 | DbgScope *Parent = NULL; |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 980 | DIBlock Block(V); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 981 | |
Bill Wendling | 8fff19b | 2009-06-01 20:18:46 +0000 | [diff] [blame] | 982 | // Don't create a new scope if we already created one for an inlined function. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 983 | DenseMap<const GlobalVariable *, DbgScope *>::iterator |
| 984 | II = AbstractInstanceRootMap.find(V); |
Bill Wendling | 8fff19b | 2009-06-01 20:18:46 +0000 | [diff] [blame] | 985 | if (II != AbstractInstanceRootMap.end()) |
| 986 | return LexicalScopeStack.back(); |
| 987 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 988 | if (!Block.isNull()) { |
| 989 | DIDescriptor ParentDesc = Block.getContext(); |
| 990 | Parent = |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 991 | ParentDesc.isNull() ? NULL : getOrCreateScope(ParentDesc.getGV()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 994 | Slot = new DbgScope(Parent, DIDescriptor(V)); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 995 | |
| 996 | if (Parent) |
| 997 | Parent->AddScope(Slot); |
| 998 | else |
| 999 | // First function is top level function. |
| 1000 | FunctionDbgScope = Slot; |
| 1001 | |
| 1002 | return Slot; |
| 1003 | } |
| 1004 | |
| 1005 | /// ConstructDbgScope - Construct the components of a scope. |
| 1006 | /// |
| 1007 | void DwarfDebug::ConstructDbgScope(DbgScope *ParentScope, |
| 1008 | unsigned ParentStartID, |
| 1009 | unsigned ParentEndID, |
| 1010 | DIE *ParentDie, CompileUnit *Unit) { |
| 1011 | // Add variables to scope. |
| 1012 | SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables(); |
| 1013 | for (unsigned i = 0, N = Variables.size(); i < N; ++i) { |
Bill Wendling | 995f80a | 2009-05-20 23:24:48 +0000 | [diff] [blame] | 1014 | DIE *VariableDie = CreateDbgScopeVariable(Variables[i], Unit); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1015 | if (VariableDie) ParentDie->AddChild(VariableDie); |
| 1016 | } |
| 1017 | |
| 1018 | // Add concrete instances to scope. |
| 1019 | SmallVector<DbgConcreteScope *, 8> &ConcreteInsts = |
| 1020 | ParentScope->getConcreteInsts(); |
| 1021 | for (unsigned i = 0, N = ConcreteInsts.size(); i < N; ++i) { |
| 1022 | DbgConcreteScope *ConcreteInst = ConcreteInsts[i]; |
| 1023 | DIE *Die = ConcreteInst->getDie(); |
| 1024 | |
| 1025 | unsigned StartID = ConcreteInst->getStartLabelID(); |
| 1026 | unsigned EndID = ConcreteInst->getEndLabelID(); |
| 1027 | |
| 1028 | // Add the scope bounds. |
| 1029 | if (StartID) |
| 1030 | AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, |
| 1031 | DWLabel("label", StartID)); |
| 1032 | else |
| 1033 | AddLabel(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, |
| 1034 | DWLabel("func_begin", SubprogramCount)); |
| 1035 | |
| 1036 | if (EndID) |
| 1037 | AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, |
| 1038 | DWLabel("label", EndID)); |
| 1039 | else |
| 1040 | AddLabel(Die, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, |
| 1041 | DWLabel("func_end", SubprogramCount)); |
| 1042 | |
| 1043 | ParentDie->AddChild(Die); |
| 1044 | } |
| 1045 | |
| 1046 | // Add nested scopes. |
| 1047 | SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes(); |
| 1048 | for (unsigned j = 0, M = Scopes.size(); j < M; ++j) { |
| 1049 | // Define the Scope debug information entry. |
| 1050 | DbgScope *Scope = Scopes[j]; |
| 1051 | |
| 1052 | unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID()); |
| 1053 | unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID()); |
| 1054 | |
| 1055 | // Ignore empty scopes. |
| 1056 | if (StartID == EndID && StartID != 0) continue; |
| 1057 | |
| 1058 | // Do not ignore inlined scopes even if they don't have any variables or |
| 1059 | // scopes. |
| 1060 | if (Scope->getScopes().empty() && Scope->getVariables().empty() && |
| 1061 | Scope->getConcreteInsts().empty()) |
| 1062 | continue; |
| 1063 | |
| 1064 | if (StartID == ParentStartID && EndID == ParentEndID) { |
| 1065 | // Just add stuff to the parent scope. |
| 1066 | ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit); |
| 1067 | } else { |
| 1068 | DIE *ScopeDie = new DIE(dwarf::DW_TAG_lexical_block); |
| 1069 | |
| 1070 | // Add the scope bounds. |
| 1071 | if (StartID) |
| 1072 | AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, |
| 1073 | DWLabel("label", StartID)); |
| 1074 | else |
| 1075 | AddLabel(ScopeDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, |
| 1076 | DWLabel("func_begin", SubprogramCount)); |
| 1077 | |
| 1078 | if (EndID) |
| 1079 | AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, |
| 1080 | DWLabel("label", EndID)); |
| 1081 | else |
| 1082 | AddLabel(ScopeDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, |
| 1083 | DWLabel("func_end", SubprogramCount)); |
| 1084 | |
| 1085 | // Add the scope's contents. |
| 1086 | ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit); |
| 1087 | ParentDie->AddChild(ScopeDie); |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | /// ConstructFunctionDbgScope - Construct the scope for the subprogram. |
| 1093 | /// |
Bill Wendling | 1795616 | 2009-05-20 23:28:48 +0000 | [diff] [blame] | 1094 | void DwarfDebug::ConstructFunctionDbgScope(DbgScope *RootScope, |
| 1095 | bool AbstractScope) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1096 | // Exit if there is no root scope. |
| 1097 | if (!RootScope) return; |
| 1098 | DIDescriptor Desc = RootScope->getDesc(); |
| 1099 | if (Desc.isNull()) |
| 1100 | return; |
| 1101 | |
| 1102 | // Get the subprogram debug information entry. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1103 | DISubprogram SPD(Desc.getGV()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1104 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1105 | // Get the subprogram die. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1106 | DIE *SPDie = ModuleCU->getDieMapSlotFor(SPD.getGV()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1107 | assert(SPDie && "Missing subprogram descriptor"); |
| 1108 | |
Bill Wendling | 1795616 | 2009-05-20 23:28:48 +0000 | [diff] [blame] | 1109 | if (!AbstractScope) { |
| 1110 | // Add the function bounds. |
| 1111 | AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, |
| 1112 | DWLabel("func_begin", SubprogramCount)); |
| 1113 | AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, |
| 1114 | DWLabel("func_end", SubprogramCount)); |
| 1115 | MachineLocation Location(RI->getFrameRegister(*MF)); |
| 1116 | AddAddress(SPDie, dwarf::DW_AT_frame_base, Location); |
| 1117 | } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1118 | |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1119 | ConstructDbgScope(RootScope, 0, 0, SPDie, ModuleCU); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1122 | /// ConstructDefaultDbgScope - Construct a default scope for the subprogram. |
| 1123 | /// |
| 1124 | void DwarfDebug::ConstructDefaultDbgScope(MachineFunction *MF) { |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1125 | StringMap<DIE*> &Globals = ModuleCU->getGlobals(); |
Daniel Dunbar | 460f656 | 2009-07-26 09:48:23 +0000 | [diff] [blame] | 1126 | StringMap<DIE*>::iterator GI = Globals.find(MF->getFunction()->getName()); |
Devang Patel | 70f4426 | 2009-06-29 20:38:13 +0000 | [diff] [blame] | 1127 | if (GI != Globals.end()) { |
| 1128 | DIE *SPDie = GI->second; |
| 1129 | |
| 1130 | // Add the function bounds. |
| 1131 | AddLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, |
| 1132 | DWLabel("func_begin", SubprogramCount)); |
| 1133 | AddLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, |
| 1134 | DWLabel("func_end", SubprogramCount)); |
| 1135 | |
| 1136 | MachineLocation Location(RI->getFrameRegister(*MF)); |
| 1137 | AddAddress(SPDie, dwarf::DW_AT_frame_base, Location); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1138 | } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1141 | /// GetOrCreateSourceID - Look up the source id with the given directory and |
| 1142 | /// source file names. If none currently exists, create a new id and insert it |
| 1143 | /// in the SourceIds map. This can update DirectoryNames and SourceFileNames |
| 1144 | /// maps as well. |
| 1145 | unsigned DwarfDebug::GetOrCreateSourceID(const std::string &DirName, |
| 1146 | const std::string &FileName) { |
| 1147 | unsigned DId; |
| 1148 | StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName); |
| 1149 | if (DI != DirectoryIdMap.end()) { |
| 1150 | DId = DI->getValue(); |
| 1151 | } else { |
| 1152 | DId = DirectoryNames.size() + 1; |
| 1153 | DirectoryIdMap[DirName] = DId; |
| 1154 | DirectoryNames.push_back(DirName); |
| 1155 | } |
| 1156 | |
| 1157 | unsigned FId; |
| 1158 | StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName); |
| 1159 | if (FI != SourceFileIdMap.end()) { |
| 1160 | FId = FI->getValue(); |
| 1161 | } else { |
| 1162 | FId = SourceFileNames.size() + 1; |
| 1163 | SourceFileIdMap[FileName] = FId; |
| 1164 | SourceFileNames.push_back(FileName); |
| 1165 | } |
| 1166 | |
| 1167 | DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI = |
| 1168 | SourceIdMap.find(std::make_pair(DId, FId)); |
| 1169 | if (SI != SourceIdMap.end()) |
| 1170 | return SI->second; |
| 1171 | |
| 1172 | unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0. |
| 1173 | SourceIdMap[std::make_pair(DId, FId)] = SrcId; |
| 1174 | SourceIds.push_back(std::make_pair(DId, FId)); |
| 1175 | |
| 1176 | return SrcId; |
| 1177 | } |
| 1178 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1179 | void DwarfDebug::ConstructCompileUnit(GlobalVariable *GV) { |
| 1180 | DICompileUnit DIUnit(GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1181 | std::string Dir, FN, Prod; |
| 1182 | unsigned ID = GetOrCreateSourceID(DIUnit.getDirectory(Dir), |
| 1183 | DIUnit.getFilename(FN)); |
| 1184 | |
| 1185 | DIE *Die = new DIE(dwarf::DW_TAG_compile_unit); |
| 1186 | AddSectionOffset(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, |
| 1187 | DWLabel("section_line", 0), DWLabel("section_line", 0), |
| 1188 | false); |
| 1189 | AddString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string, |
| 1190 | DIUnit.getProducer(Prod)); |
| 1191 | AddUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1, |
| 1192 | DIUnit.getLanguage()); |
| 1193 | AddString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN); |
| 1194 | |
| 1195 | if (!Dir.empty()) |
| 1196 | AddString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir); |
| 1197 | if (DIUnit.isOptimized()) |
| 1198 | AddUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1); |
| 1199 | |
| 1200 | std::string Flags; |
| 1201 | DIUnit.getFlags(Flags); |
| 1202 | if (!Flags.empty()) |
| 1203 | AddString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags); |
| 1204 | |
| 1205 | unsigned RVer = DIUnit.getRunTimeVersion(); |
| 1206 | if (RVer) |
| 1207 | AddUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, |
| 1208 | dwarf::DW_FORM_data1, RVer); |
| 1209 | |
| 1210 | CompileUnit *Unit = new CompileUnit(ID, Die); |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1211 | if (!ModuleCU && DIUnit.isMain()) { |
Devang Patel | 70f4426 | 2009-06-29 20:38:13 +0000 | [diff] [blame] | 1212 | // Use first compile unit marked as isMain as the compile unit |
| 1213 | // for this module. |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1214 | ModuleCU = Unit; |
Devang Patel | 70f4426 | 2009-06-29 20:38:13 +0000 | [diff] [blame] | 1215 | } |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1216 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1217 | CompileUnitMap[DIUnit.getGV()] = Unit; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1218 | CompileUnits.push_back(Unit); |
| 1219 | } |
| 1220 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1221 | void DwarfDebug::ConstructGlobalVariableDIE(GlobalVariable *GV) { |
| 1222 | DIGlobalVariable DI_GV(GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1223 | |
| 1224 | // Check for pre-existence. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1225 | DIE *&Slot = ModuleCU->getDieMapSlotFor(DI_GV.getGV()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1226 | if (Slot) |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1227 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1228 | |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1229 | DIE *VariableDie = CreateGlobalVariableDIE(ModuleCU, DI_GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1230 | |
| 1231 | // Add address. |
| 1232 | DIEBlock *Block = new DIEBlock(); |
| 1233 | AddUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); |
| 1234 | std::string GLN; |
| 1235 | AddObjectLabel(Block, 0, dwarf::DW_FORM_udata, |
| 1236 | Asm->getGlobalLinkName(DI_GV.getGlobal(), GLN)); |
| 1237 | AddBlock(VariableDie, dwarf::DW_AT_location, 0, Block); |
| 1238 | |
| 1239 | // Add to map. |
| 1240 | Slot = VariableDie; |
| 1241 | |
| 1242 | // Add to context owner. |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1243 | ModuleCU->getDie()->AddChild(VariableDie); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1244 | |
| 1245 | // Expose as global. FIXME - need to check external flag. |
| 1246 | std::string Name; |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1247 | ModuleCU->AddGlobal(DI_GV.getName(Name), VariableDie); |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1248 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1251 | void DwarfDebug::ConstructSubprogram(GlobalVariable *GV) { |
| 1252 | DISubprogram SP(GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1253 | |
| 1254 | // Check for pre-existence. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1255 | DIE *&Slot = ModuleCU->getDieMapSlotFor(GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1256 | if (Slot) |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1257 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (!SP.isDefinition()) |
| 1260 | // This is a method declaration which will be handled while constructing |
| 1261 | // class type. |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1262 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1263 | |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1264 | DIE *SubprogramDie = CreateSubprogramDIE(ModuleCU, SP); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1265 | |
| 1266 | // Add to map. |
| 1267 | Slot = SubprogramDie; |
| 1268 | |
| 1269 | // Add to context owner. |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1270 | ModuleCU->getDie()->AddChild(SubprogramDie); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1271 | |
| 1272 | // Expose as global. |
| 1273 | std::string Name; |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1274 | ModuleCU->AddGlobal(SP.getName(Name), SubprogramDie); |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1275 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Devang Patel | 208622d | 2009-06-25 22:36:02 +0000 | [diff] [blame] | 1278 | /// BeginModule - Emit all Dwarf sections that should come prior to the |
| 1279 | /// content. Create global DIEs and emit initial debug info sections. |
| 1280 | /// This is inovked by the target AsmPrinter. |
| 1281 | void DwarfDebug::BeginModule(Module *M, MachineModuleInfo *mmi) { |
| 1282 | this->M = M; |
| 1283 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1284 | if (TimePassesIsEnabled) |
| 1285 | DebugTimer->startTimer(); |
| 1286 | |
Devang Patel | 78ab9e2 | 2009-07-30 18:56:46 +0000 | [diff] [blame] | 1287 | DebugInfoFinder DbgFinder; |
| 1288 | DbgFinder.processModule(*M); |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1289 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1290 | // Create all the compile unit DIEs. |
Devang Patel | 78ab9e2 | 2009-07-30 18:56:46 +0000 | [diff] [blame] | 1291 | for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(), |
| 1292 | E = DbgFinder.compile_unit_end(); I != E; ++I) |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1293 | ConstructCompileUnit(*I); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1294 | |
| 1295 | if (CompileUnits.empty()) { |
| 1296 | if (TimePassesIsEnabled) |
| 1297 | DebugTimer->stopTimer(); |
| 1298 | |
| 1299 | return; |
| 1300 | } |
| 1301 | |
Devang Patel | 70f4426 | 2009-06-29 20:38:13 +0000 | [diff] [blame] | 1302 | // If main compile unit for this module is not seen than randomly |
| 1303 | // select first compile unit. |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1304 | if (!ModuleCU) |
| 1305 | ModuleCU = CompileUnits[0]; |
Devang Patel | 70f4426 | 2009-06-29 20:38:13 +0000 | [diff] [blame] | 1306 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1307 | // If there is not any debug info available for any global variables and any |
| 1308 | // subprograms then there is not any debug info to emit. |
Devang Patel | 78ab9e2 | 2009-07-30 18:56:46 +0000 | [diff] [blame] | 1309 | if (DbgFinder.global_variable_count() == 0 |
| 1310 | && DbgFinder.subprogram_count() == 0) { |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1311 | if (TimePassesIsEnabled) |
| 1312 | DebugTimer->stopTimer(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1313 | return; |
| 1314 | } |
Devang Patel | 78ab9e2 | 2009-07-30 18:56:46 +0000 | [diff] [blame] | 1315 | |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1316 | // Create DIEs for each of the externally visible global variables. |
Devang Patel | 78ab9e2 | 2009-07-30 18:56:46 +0000 | [diff] [blame] | 1317 | for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(), |
| 1318 | E = DbgFinder.global_variable_end(); I != E; ++I) |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1319 | ConstructGlobalVariableDIE(*I); |
| 1320 | |
| 1321 | // Create DIEs for each of the externally visible subprograms. |
Devang Patel | 78ab9e2 | 2009-07-30 18:56:46 +0000 | [diff] [blame] | 1322 | for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(), |
| 1323 | E = DbgFinder.subprogram_end(); I != E; ++I) |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1324 | ConstructSubprogram(*I); |
| 1325 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1326 | MMI = mmi; |
| 1327 | shouldEmit = true; |
| 1328 | MMI->setDebugInfoAvailability(true); |
| 1329 | |
| 1330 | // Prime section data. |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1331 | SectionMap.insert(Asm->getObjFileLowering().getTextSection()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1332 | |
| 1333 | // Print out .file directives to specify files for .loc directives. These are |
| 1334 | // printed out early so that they precede any .loc directives. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1335 | if (MAI->hasDotLocAndDotFile()) { |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1336 | for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) { |
| 1337 | // Remember source id starts at 1. |
| 1338 | std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i); |
| 1339 | sys::Path FullPath(getSourceDirectoryName(Id.first)); |
| 1340 | bool AppendOk = |
| 1341 | FullPath.appendComponent(getSourceFileName(Id.second)); |
| 1342 | assert(AppendOk && "Could not append filename to directory!"); |
| 1343 | AppendOk = false; |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 1344 | Asm->EmitFile(i, FullPath.str()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1345 | Asm->EOL(); |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | // Emit initial sections |
| 1350 | EmitInitial(); |
| 1351 | |
| 1352 | if (TimePassesIsEnabled) |
| 1353 | DebugTimer->stopTimer(); |
| 1354 | } |
| 1355 | |
| 1356 | /// EndModule - Emit all Dwarf sections that should come after the content. |
| 1357 | /// |
| 1358 | void DwarfDebug::EndModule() { |
| 1359 | if (!ShouldEmitDwarfDebug()) |
| 1360 | return; |
| 1361 | |
| 1362 | if (TimePassesIsEnabled) |
| 1363 | DebugTimer->startTimer(); |
| 1364 | |
| 1365 | // Standard sections final addresses. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1366 | Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1367 | EmitLabel("text_end", 0); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1368 | Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1369 | EmitLabel("data_end", 0); |
| 1370 | |
| 1371 | // End text sections. |
| 1372 | for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1373 | Asm->OutStreamer.SwitchSection(SectionMap[i]); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1374 | EmitLabel("section_end", i); |
| 1375 | } |
| 1376 | |
| 1377 | // Emit common frame information. |
| 1378 | EmitCommonDebugFrame(); |
| 1379 | |
| 1380 | // Emit function debug frame information |
| 1381 | for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(), |
| 1382 | E = DebugFrames.end(); I != E; ++I) |
| 1383 | EmitFunctionDebugFrame(*I); |
| 1384 | |
| 1385 | // Compute DIE offsets and sizes. |
| 1386 | SizeAndOffsets(); |
| 1387 | |
| 1388 | // Emit all the DIEs into a debug info section |
| 1389 | EmitDebugInfo(); |
| 1390 | |
| 1391 | // Corresponding abbreviations into a abbrev section. |
| 1392 | EmitAbbreviations(); |
| 1393 | |
| 1394 | // Emit source line correspondence into a debug line section. |
| 1395 | EmitDebugLines(); |
| 1396 | |
| 1397 | // Emit info into a debug pubnames section. |
| 1398 | EmitDebugPubNames(); |
| 1399 | |
| 1400 | // Emit info into a debug str section. |
| 1401 | EmitDebugStr(); |
| 1402 | |
| 1403 | // Emit info into a debug loc section. |
| 1404 | EmitDebugLoc(); |
| 1405 | |
| 1406 | // Emit info into a debug aranges section. |
| 1407 | EmitDebugARanges(); |
| 1408 | |
| 1409 | // Emit info into a debug ranges section. |
| 1410 | EmitDebugRanges(); |
| 1411 | |
| 1412 | // Emit info into a debug macinfo section. |
| 1413 | EmitDebugMacInfo(); |
| 1414 | |
| 1415 | // Emit inline info. |
| 1416 | EmitDebugInlineInfo(); |
| 1417 | |
| 1418 | if (TimePassesIsEnabled) |
| 1419 | DebugTimer->stopTimer(); |
| 1420 | } |
| 1421 | |
| 1422 | /// BeginFunction - Gather pre-function debug information. Assumes being |
| 1423 | /// emitted immediately after the function entry point. |
| 1424 | void DwarfDebug::BeginFunction(MachineFunction *MF) { |
| 1425 | this->MF = MF; |
| 1426 | |
| 1427 | if (!ShouldEmitDwarfDebug()) return; |
| 1428 | |
| 1429 | if (TimePassesIsEnabled) |
| 1430 | DebugTimer->startTimer(); |
| 1431 | |
| 1432 | // Begin accumulating function debug information. |
| 1433 | MMI->BeginFunction(MF); |
| 1434 | |
| 1435 | // Assumes in correct section after the entry point. |
| 1436 | EmitLabel("func_begin", ++SubprogramCount); |
| 1437 | |
| 1438 | // Emit label for the implicitly defined dbg.stoppoint at the start of the |
| 1439 | // function. |
| 1440 | DebugLoc FDL = MF->getDefaultDebugLoc(); |
| 1441 | if (!FDL.isUnknown()) { |
| 1442 | DebugLocTuple DLT = MF->getDebugLocTuple(FDL); |
| 1443 | unsigned LabelID = RecordSourceLine(DLT.Line, DLT.Col, |
| 1444 | DICompileUnit(DLT.CompileUnit)); |
| 1445 | Asm->printLabel(LabelID); |
| 1446 | } |
| 1447 | |
| 1448 | if (TimePassesIsEnabled) |
| 1449 | DebugTimer->stopTimer(); |
| 1450 | } |
| 1451 | |
| 1452 | /// EndFunction - Gather and emit post-function debug information. |
| 1453 | /// |
| 1454 | void DwarfDebug::EndFunction(MachineFunction *MF) { |
| 1455 | if (!ShouldEmitDwarfDebug()) return; |
| 1456 | |
| 1457 | if (TimePassesIsEnabled) |
| 1458 | DebugTimer->startTimer(); |
| 1459 | |
| 1460 | // Define end label for subprogram. |
| 1461 | EmitLabel("func_end", SubprogramCount); |
| 1462 | |
| 1463 | // Get function line info. |
| 1464 | if (!Lines.empty()) { |
| 1465 | // Get section line info. |
Chris Lattner | 290c2f5 | 2009-08-03 23:20:21 +0000 | [diff] [blame] | 1466 | unsigned ID = SectionMap.insert(Asm->getCurrentSection()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1467 | if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID); |
| 1468 | std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1]; |
| 1469 | // Append the function info to section info. |
| 1470 | SectionLineInfos.insert(SectionLineInfos.end(), |
| 1471 | Lines.begin(), Lines.end()); |
| 1472 | } |
| 1473 | |
| 1474 | // Construct the DbgScope for abstract instances. |
| 1475 | for (SmallVector<DbgScope *, 32>::iterator |
| 1476 | I = AbstractInstanceRootList.begin(), |
| 1477 | E = AbstractInstanceRootList.end(); I != E; ++I) |
Bill Wendling | 1795616 | 2009-05-20 23:28:48 +0000 | [diff] [blame] | 1478 | ConstructFunctionDbgScope(*I); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1479 | |
| 1480 | // Construct scopes for subprogram. |
| 1481 | if (FunctionDbgScope) |
| 1482 | ConstructFunctionDbgScope(FunctionDbgScope); |
| 1483 | else |
| 1484 | // FIXME: This is wrong. We are essentially getting past a problem with |
| 1485 | // debug information not being able to handle unreachable blocks that have |
| 1486 | // debug information in them. In particular, those unreachable blocks that |
| 1487 | // have "region end" info in them. That situation results in the "root |
| 1488 | // scope" not being created. If that's the case, then emit a "default" |
| 1489 | // scope, i.e., one that encompasses the whole function. This isn't |
| 1490 | // desirable. And a better way of handling this (and all of the debugging |
| 1491 | // information) needs to be explored. |
| 1492 | ConstructDefaultDbgScope(MF); |
| 1493 | |
| 1494 | DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount, |
| 1495 | MMI->getFrameMoves())); |
| 1496 | |
| 1497 | // Clear debug info |
| 1498 | if (FunctionDbgScope) { |
| 1499 | delete FunctionDbgScope; |
| 1500 | DbgScopeMap.clear(); |
| 1501 | DbgAbstractScopeMap.clear(); |
| 1502 | DbgConcreteScopeMap.clear(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1503 | FunctionDbgScope = NULL; |
| 1504 | LexicalScopeStack.clear(); |
| 1505 | AbstractInstanceRootList.clear(); |
Devang Patel | 9217f79 | 2009-06-12 19:24:05 +0000 | [diff] [blame] | 1506 | AbstractInstanceRootMap.clear(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | Lines.clear(); |
| 1510 | |
| 1511 | if (TimePassesIsEnabled) |
| 1512 | DebugTimer->stopTimer(); |
| 1513 | } |
| 1514 | |
| 1515 | /// RecordSourceLine - Records location information and associates it with a |
| 1516 | /// label. Returns a unique label ID used to generate a label and provide |
| 1517 | /// correspondence to the source line list. |
| 1518 | unsigned DwarfDebug::RecordSourceLine(Value *V, unsigned Line, unsigned Col) { |
| 1519 | if (TimePassesIsEnabled) |
| 1520 | DebugTimer->startTimer(); |
| 1521 | |
| 1522 | CompileUnit *Unit = CompileUnitMap[V]; |
| 1523 | assert(Unit && "Unable to find CompileUnit"); |
| 1524 | unsigned ID = MMI->NextLabelID(); |
| 1525 | Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID)); |
| 1526 | |
| 1527 | if (TimePassesIsEnabled) |
| 1528 | DebugTimer->stopTimer(); |
| 1529 | |
| 1530 | return ID; |
| 1531 | } |
| 1532 | |
| 1533 | /// RecordSourceLine - Records location information and associates it with a |
| 1534 | /// label. Returns a unique label ID used to generate a label and provide |
| 1535 | /// correspondence to the source line list. |
| 1536 | unsigned DwarfDebug::RecordSourceLine(unsigned Line, unsigned Col, |
| 1537 | DICompileUnit CU) { |
| 1538 | if (TimePassesIsEnabled) |
| 1539 | DebugTimer->startTimer(); |
| 1540 | |
| 1541 | std::string Dir, Fn; |
| 1542 | unsigned Src = GetOrCreateSourceID(CU.getDirectory(Dir), |
| 1543 | CU.getFilename(Fn)); |
| 1544 | unsigned ID = MMI->NextLabelID(); |
| 1545 | Lines.push_back(SrcLineInfo(Line, Col, Src, ID)); |
| 1546 | |
| 1547 | if (TimePassesIsEnabled) |
| 1548 | DebugTimer->stopTimer(); |
| 1549 | |
| 1550 | return ID; |
| 1551 | } |
| 1552 | |
| 1553 | /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be |
| 1554 | /// timed. Look up the source id with the given directory and source file |
| 1555 | /// names. If none currently exists, create a new id and insert it in the |
| 1556 | /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as |
| 1557 | /// well. |
| 1558 | unsigned DwarfDebug::getOrCreateSourceID(const std::string &DirName, |
| 1559 | const std::string &FileName) { |
| 1560 | if (TimePassesIsEnabled) |
| 1561 | DebugTimer->startTimer(); |
| 1562 | |
| 1563 | unsigned SrcId = GetOrCreateSourceID(DirName, FileName); |
| 1564 | |
| 1565 | if (TimePassesIsEnabled) |
| 1566 | DebugTimer->stopTimer(); |
| 1567 | |
| 1568 | return SrcId; |
| 1569 | } |
| 1570 | |
| 1571 | /// RecordRegionStart - Indicate the start of a region. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1572 | unsigned DwarfDebug::RecordRegionStart(GlobalVariable *V) { |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1573 | if (TimePassesIsEnabled) |
| 1574 | DebugTimer->startTimer(); |
| 1575 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1576 | DbgScope *Scope = getOrCreateScope(V); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1577 | unsigned ID = MMI->NextLabelID(); |
| 1578 | if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID); |
| 1579 | LexicalScopeStack.push_back(Scope); |
| 1580 | |
| 1581 | if (TimePassesIsEnabled) |
| 1582 | DebugTimer->stopTimer(); |
| 1583 | |
| 1584 | return ID; |
| 1585 | } |
| 1586 | |
| 1587 | /// RecordRegionEnd - Indicate the end of a region. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1588 | unsigned DwarfDebug::RecordRegionEnd(GlobalVariable *V) { |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1589 | if (TimePassesIsEnabled) |
| 1590 | DebugTimer->startTimer(); |
| 1591 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1592 | DbgScope *Scope = getOrCreateScope(V); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1593 | unsigned ID = MMI->NextLabelID(); |
| 1594 | Scope->setEndLabelID(ID); |
Devang Patel | daf9e02 | 2009-06-13 02:16:18 +0000 | [diff] [blame] | 1595 | // FIXME : region.end() may not be in the last basic block. |
| 1596 | // For now, do not pop last lexical scope because next basic |
| 1597 | // block may start new inlined function's body. |
| 1598 | unsigned LSSize = LexicalScopeStack.size(); |
| 1599 | if (LSSize != 0 && LSSize != 1) |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1600 | LexicalScopeStack.pop_back(); |
| 1601 | |
| 1602 | if (TimePassesIsEnabled) |
| 1603 | DebugTimer->stopTimer(); |
| 1604 | |
| 1605 | return ID; |
| 1606 | } |
| 1607 | |
| 1608 | /// RecordVariable - Indicate the declaration of a local variable. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1609 | void DwarfDebug::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) { |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1610 | if (TimePassesIsEnabled) |
| 1611 | DebugTimer->startTimer(); |
| 1612 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1613 | DIDescriptor Desc(GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1614 | DbgScope *Scope = NULL; |
| 1615 | bool InlinedFnVar = false; |
| 1616 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1617 | if (Desc.getTag() == dwarf::DW_TAG_variable) { |
| 1618 | // GV is a global variable. |
| 1619 | DIGlobalVariable DG(GV); |
| 1620 | Scope = getOrCreateScope(DG.getContext().getGV()); |
| 1621 | } else { |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 1622 | bool InlinedVar = false; |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1623 | DIVariable DV(GV); |
| 1624 | GlobalVariable *V = DV.getContext().getGV(); |
| 1625 | DISubprogram SP(V); |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 1626 | if (!SP.isNull()) { |
| 1627 | // SP is inserted into DbgAbstractScopeMap when inlined function |
| 1628 | // start was recorded by RecordInlineFnStart. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1629 | DenseMap<GlobalVariable *, DbgScope *>::iterator |
| 1630 | I = DbgAbstractScopeMap.find(SP.getGV()); |
Devang Patel | 24f20e0 | 2009-08-22 17:12:53 +0000 | [diff] [blame] | 1631 | if (I != DbgAbstractScopeMap.end()) { |
| 1632 | InlinedVar = true; |
| 1633 | Scope = I->second; |
| 1634 | } |
| 1635 | } |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1636 | if (!InlinedVar) { |
| 1637 | // GV is a local variable. |
| 1638 | Scope = getOrCreateScope(V); |
| 1639 | } |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | assert(Scope && "Unable to find the variable's scope"); |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1643 | DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex, InlinedFnVar); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1644 | Scope->AddVariable(DV); |
| 1645 | |
| 1646 | if (TimePassesIsEnabled) |
| 1647 | DebugTimer->stopTimer(); |
| 1648 | } |
| 1649 | |
| 1650 | //// RecordInlinedFnStart - Indicate the start of inlined subroutine. |
| 1651 | unsigned DwarfDebug::RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU, |
| 1652 | unsigned Line, unsigned Col) { |
| 1653 | unsigned LabelID = MMI->NextLabelID(); |
| 1654 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1655 | if (!MAI->doesDwarfUsesInlineInfoSection()) |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1656 | return LabelID; |
| 1657 | |
| 1658 | if (TimePassesIsEnabled) |
| 1659 | DebugTimer->startTimer(); |
| 1660 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1661 | GlobalVariable *GV = SP.getGV(); |
| 1662 | DenseMap<const GlobalVariable *, DbgScope *>::iterator |
| 1663 | II = AbstractInstanceRootMap.find(GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1664 | |
| 1665 | if (II == AbstractInstanceRootMap.end()) { |
| 1666 | // Create an abstract instance entry for this inlined function if it doesn't |
| 1667 | // already exist. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1668 | DbgScope *Scope = new DbgScope(NULL, DIDescriptor(GV)); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1669 | |
| 1670 | // Get the compile unit context. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1671 | DIE *SPDie = ModuleCU->getDieMapSlotFor(GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1672 | if (!SPDie) |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1673 | SPDie = CreateSubprogramDIE(ModuleCU, SP, false, true); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1674 | |
| 1675 | // Mark as being inlined. This makes this subprogram entry an abstract |
| 1676 | // instance root. |
| 1677 | // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only |
| 1678 | // that it's defined. That probably won't change in the future. However, |
| 1679 | // this could be more elegant. |
| 1680 | AddUInt(SPDie, dwarf::DW_AT_inline, 0, dwarf::DW_INL_declared_not_inlined); |
| 1681 | |
| 1682 | // Keep track of the abstract scope for this function. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1683 | DbgAbstractScopeMap[GV] = Scope; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1684 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1685 | AbstractInstanceRootMap[GV] = Scope; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1686 | AbstractInstanceRootList.push_back(Scope); |
| 1687 | } |
| 1688 | |
| 1689 | // Create a concrete inlined instance for this inlined function. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1690 | DbgConcreteScope *ConcreteScope = new DbgConcreteScope(DIDescriptor(GV)); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1691 | DIE *ScopeDie = new DIE(dwarf::DW_TAG_inlined_subroutine); |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1692 | ScopeDie->setAbstractCompileUnit(ModuleCU); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1693 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1694 | DIE *Origin = ModuleCU->getDieMapSlotFor(GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1695 | AddDIEEntry(ScopeDie, dwarf::DW_AT_abstract_origin, |
| 1696 | dwarf::DW_FORM_ref4, Origin); |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1697 | AddUInt(ScopeDie, dwarf::DW_AT_call_file, 0, ModuleCU->getID()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1698 | AddUInt(ScopeDie, dwarf::DW_AT_call_line, 0, Line); |
| 1699 | AddUInt(ScopeDie, dwarf::DW_AT_call_column, 0, Col); |
| 1700 | |
| 1701 | ConcreteScope->setDie(ScopeDie); |
| 1702 | ConcreteScope->setStartLabelID(LabelID); |
| 1703 | MMI->RecordUsedDbgLabel(LabelID); |
| 1704 | |
| 1705 | LexicalScopeStack.back()->AddConcreteInst(ConcreteScope); |
| 1706 | |
| 1707 | // Keep track of the concrete scope that's inlined into this function. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1708 | DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator |
| 1709 | SI = DbgConcreteScopeMap.find(GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1710 | |
| 1711 | if (SI == DbgConcreteScopeMap.end()) |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1712 | DbgConcreteScopeMap[GV].push_back(ConcreteScope); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1713 | else |
| 1714 | SI->second.push_back(ConcreteScope); |
| 1715 | |
| 1716 | // Track the start label for this inlined function. |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1717 | DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator |
| 1718 | I = InlineInfo.find(GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1719 | |
| 1720 | if (I == InlineInfo.end()) |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1721 | InlineInfo[GV].push_back(LabelID); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1722 | else |
| 1723 | I->second.push_back(LabelID); |
| 1724 | |
| 1725 | if (TimePassesIsEnabled) |
| 1726 | DebugTimer->stopTimer(); |
| 1727 | |
| 1728 | return LabelID; |
| 1729 | } |
| 1730 | |
| 1731 | /// RecordInlinedFnEnd - Indicate the end of inlined subroutine. |
| 1732 | unsigned DwarfDebug::RecordInlinedFnEnd(DISubprogram &SP) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1733 | if (!MAI->doesDwarfUsesInlineInfoSection()) |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1734 | return 0; |
| 1735 | |
| 1736 | if (TimePassesIsEnabled) |
| 1737 | DebugTimer->startTimer(); |
| 1738 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 1739 | GlobalVariable *GV = SP.getGV(); |
| 1740 | DenseMap<GlobalVariable *, SmallVector<DbgScope *, 8> >::iterator |
| 1741 | I = DbgConcreteScopeMap.find(GV); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1742 | |
| 1743 | if (I == DbgConcreteScopeMap.end()) { |
| 1744 | // FIXME: Can this situation actually happen? And if so, should it? |
| 1745 | if (TimePassesIsEnabled) |
| 1746 | DebugTimer->stopTimer(); |
| 1747 | |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | SmallVector<DbgScope *, 8> &Scopes = I->second; |
Devang Patel | 11a407f | 2009-06-15 21:45:50 +0000 | [diff] [blame] | 1752 | if (Scopes.empty()) { |
| 1753 | // Returned ID is 0 if this is unbalanced "end of inlined |
| 1754 | // scope". This could happen if optimizer eats dbg intrinsics |
| 1755 | // or "beginning of inlined scope" is not recoginized due to |
| 1756 | // missing location info. In such cases, ignore this region.end. |
| 1757 | return 0; |
| 1758 | } |
| 1759 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1760 | DbgScope *Scope = Scopes.back(); Scopes.pop_back(); |
| 1761 | unsigned ID = MMI->NextLabelID(); |
| 1762 | MMI->RecordUsedDbgLabel(ID); |
| 1763 | Scope->setEndLabelID(ID); |
| 1764 | |
| 1765 | if (TimePassesIsEnabled) |
| 1766 | DebugTimer->stopTimer(); |
| 1767 | |
| 1768 | return ID; |
| 1769 | } |
| 1770 | |
Bill Wendling | 829e67b | 2009-05-20 23:22:40 +0000 | [diff] [blame] | 1771 | //===----------------------------------------------------------------------===// |
| 1772 | // Emit Methods |
| 1773 | //===----------------------------------------------------------------------===// |
| 1774 | |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1775 | /// SizeAndOffsetDie - Compute the size and offset of a DIE. |
| 1776 | /// |
| 1777 | unsigned DwarfDebug::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) { |
| 1778 | // Get the children. |
| 1779 | const std::vector<DIE *> &Children = Die->getChildren(); |
| 1780 | |
| 1781 | // If not last sibling and has children then add sibling offset attribute. |
| 1782 | if (!Last && !Children.empty()) Die->AddSiblingOffset(); |
| 1783 | |
| 1784 | // Record the abbreviation. |
| 1785 | AssignAbbrevNumber(Die->getAbbrev()); |
| 1786 | |
| 1787 | // Get the abbreviation for this DIE. |
| 1788 | unsigned AbbrevNumber = Die->getAbbrevNumber(); |
| 1789 | const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1]; |
| 1790 | |
| 1791 | // Set DIE offset |
| 1792 | Die->setOffset(Offset); |
| 1793 | |
| 1794 | // Start the size with the size of abbreviation code. |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 1795 | Offset += MCAsmInfo::getULEB128Size(AbbrevNumber); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1796 | |
| 1797 | const SmallVector<DIEValue*, 32> &Values = Die->getValues(); |
| 1798 | const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData(); |
| 1799 | |
| 1800 | // Size the DIE attribute values. |
| 1801 | for (unsigned i = 0, N = Values.size(); i < N; ++i) |
| 1802 | // Size attribute value. |
| 1803 | Offset += Values[i]->SizeOf(TD, AbbrevData[i].getForm()); |
| 1804 | |
| 1805 | // Size the DIE children if any. |
| 1806 | if (!Children.empty()) { |
| 1807 | assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes && |
| 1808 | "Children flag not set"); |
| 1809 | |
| 1810 | for (unsigned j = 0, M = Children.size(); j < M; ++j) |
| 1811 | Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M); |
| 1812 | |
| 1813 | // End of children marker. |
| 1814 | Offset += sizeof(int8_t); |
| 1815 | } |
| 1816 | |
| 1817 | Die->setSize(Offset - Die->getOffset()); |
| 1818 | return Offset; |
| 1819 | } |
| 1820 | |
| 1821 | /// SizeAndOffsets - Compute the size and offset of all the DIEs. |
| 1822 | /// |
| 1823 | void DwarfDebug::SizeAndOffsets() { |
| 1824 | // Compute size of compile unit header. |
| 1825 | static unsigned Offset = |
| 1826 | sizeof(int32_t) + // Length of Compilation Unit Info |
| 1827 | sizeof(int16_t) + // DWARF version number |
| 1828 | sizeof(int32_t) + // Offset Into Abbrev. Section |
| 1829 | sizeof(int8_t); // Pointer Size (in bytes) |
| 1830 | |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1831 | SizeAndOffsetDie(ModuleCU->getDie(), Offset, true); |
| 1832 | CompileUnitOffsets[ModuleCU] = 0; |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc |
| 1836 | /// tools to recognize the object file contains Dwarf information. |
| 1837 | void DwarfDebug::EmitInitial() { |
| 1838 | // Check to see if we already emitted intial headers. |
| 1839 | if (didInitial) return; |
| 1840 | didInitial = true; |
| 1841 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1842 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 1843 | |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1844 | // Dwarf sections base addresses. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 1845 | if (MAI->doesDwarfRequireFrameSection()) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1846 | Asm->OutStreamer.SwitchSection(TLOF.getDwarfFrameSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1847 | EmitLabel("section_debug_frame", 0); |
| 1848 | } |
| 1849 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1850 | Asm->OutStreamer.SwitchSection(TLOF.getDwarfInfoSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1851 | EmitLabel("section_info", 0); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1852 | Asm->OutStreamer.SwitchSection(TLOF.getDwarfAbbrevSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1853 | EmitLabel("section_abbrev", 0); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1854 | Asm->OutStreamer.SwitchSection(TLOF.getDwarfARangesSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1855 | EmitLabel("section_aranges", 0); |
| 1856 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1857 | if (const MCSection *LineInfoDirective = TLOF.getDwarfMacroInfoSection()) { |
| 1858 | Asm->OutStreamer.SwitchSection(LineInfoDirective); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1859 | EmitLabel("section_macinfo", 0); |
| 1860 | } |
| 1861 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1862 | Asm->OutStreamer.SwitchSection(TLOF.getDwarfLineSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1863 | EmitLabel("section_line", 0); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1864 | Asm->OutStreamer.SwitchSection(TLOF.getDwarfLocSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1865 | EmitLabel("section_loc", 0); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1866 | Asm->OutStreamer.SwitchSection(TLOF.getDwarfPubNamesSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1867 | EmitLabel("section_pubnames", 0); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1868 | Asm->OutStreamer.SwitchSection(TLOF.getDwarfStrSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1869 | EmitLabel("section_str", 0); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1870 | Asm->OutStreamer.SwitchSection(TLOF.getDwarfRangesSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1871 | EmitLabel("section_ranges", 0); |
| 1872 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1873 | Asm->OutStreamer.SwitchSection(TLOF.getTextSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1874 | EmitLabel("text_begin", 0); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1875 | Asm->OutStreamer.SwitchSection(TLOF.getDataSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1876 | EmitLabel("data_begin", 0); |
| 1877 | } |
| 1878 | |
| 1879 | /// EmitDIE - Recusively Emits a debug information entry. |
| 1880 | /// |
| 1881 | void DwarfDebug::EmitDIE(DIE *Die) { |
| 1882 | // Get the abbreviation for this DIE. |
| 1883 | unsigned AbbrevNumber = Die->getAbbrevNumber(); |
| 1884 | const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1]; |
| 1885 | |
| 1886 | Asm->EOL(); |
| 1887 | |
| 1888 | // Emit the code (index) for the abbreviation. |
| 1889 | Asm->EmitULEB128Bytes(AbbrevNumber); |
| 1890 | |
| 1891 | if (Asm->isVerbose()) |
| 1892 | Asm->EOL(std::string("Abbrev [" + |
| 1893 | utostr(AbbrevNumber) + |
| 1894 | "] 0x" + utohexstr(Die->getOffset()) + |
| 1895 | ":0x" + utohexstr(Die->getSize()) + " " + |
| 1896 | dwarf::TagString(Abbrev->getTag()))); |
| 1897 | else |
| 1898 | Asm->EOL(); |
| 1899 | |
| 1900 | SmallVector<DIEValue*, 32> &Values = Die->getValues(); |
| 1901 | const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData(); |
| 1902 | |
| 1903 | // Emit the DIE attribute values. |
| 1904 | for (unsigned i = 0, N = Values.size(); i < N; ++i) { |
| 1905 | unsigned Attr = AbbrevData[i].getAttribute(); |
| 1906 | unsigned Form = AbbrevData[i].getForm(); |
| 1907 | assert(Form && "Too many attributes for DIE (check abbreviation)"); |
| 1908 | |
| 1909 | switch (Attr) { |
| 1910 | case dwarf::DW_AT_sibling: |
| 1911 | Asm->EmitInt32(Die->SiblingOffset()); |
| 1912 | break; |
| 1913 | case dwarf::DW_AT_abstract_origin: { |
| 1914 | DIEEntry *E = cast<DIEEntry>(Values[i]); |
| 1915 | DIE *Origin = E->getEntry(); |
| 1916 | unsigned Addr = |
| 1917 | CompileUnitOffsets[Die->getAbstractCompileUnit()] + |
| 1918 | Origin->getOffset(); |
| 1919 | |
| 1920 | Asm->EmitInt32(Addr); |
| 1921 | break; |
| 1922 | } |
| 1923 | default: |
| 1924 | // Emit an attribute using the defined form. |
| 1925 | Values[i]->EmitValue(this, Form); |
| 1926 | break; |
| 1927 | } |
| 1928 | |
| 1929 | Asm->EOL(dwarf::AttributeString(Attr)); |
| 1930 | } |
| 1931 | |
| 1932 | // Emit the DIE children if any. |
| 1933 | if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) { |
| 1934 | const std::vector<DIE *> &Children = Die->getChildren(); |
| 1935 | |
| 1936 | for (unsigned j = 0, M = Children.size(); j < M; ++j) |
| 1937 | EmitDIE(Children[j]); |
| 1938 | |
| 1939 | Asm->EmitInt8(0); Asm->EOL("End Of Children Mark"); |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section. |
| 1944 | /// |
| 1945 | void DwarfDebug::EmitDebugInfoPerCU(CompileUnit *Unit) { |
| 1946 | DIE *Die = Unit->getDie(); |
| 1947 | |
| 1948 | // Emit the compile units header. |
| 1949 | EmitLabel("info_begin", Unit->getID()); |
| 1950 | |
| 1951 | // Emit size of content not including length itself |
| 1952 | unsigned ContentSize = Die->getSize() + |
| 1953 | sizeof(int16_t) + // DWARF version number |
| 1954 | sizeof(int32_t) + // Offset Into Abbrev. Section |
| 1955 | sizeof(int8_t) + // Pointer Size (in bytes) |
| 1956 | sizeof(int32_t); // FIXME - extra pad for gdb bug. |
| 1957 | |
| 1958 | Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info"); |
| 1959 | Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number"); |
| 1960 | EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false); |
| 1961 | Asm->EOL("Offset Into Abbrev. Section"); |
| 1962 | Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)"); |
| 1963 | |
| 1964 | EmitDIE(Die); |
| 1965 | // FIXME - extra padding for gdb bug. |
| 1966 | Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); |
| 1967 | Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); |
| 1968 | Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); |
| 1969 | Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB"); |
| 1970 | EmitLabel("info_end", Unit->getID()); |
| 1971 | |
| 1972 | Asm->EOL(); |
| 1973 | } |
| 1974 | |
| 1975 | void DwarfDebug::EmitDebugInfo() { |
| 1976 | // Start debug info section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1977 | Asm->OutStreamer.SwitchSection( |
| 1978 | Asm->getObjFileLowering().getDwarfInfoSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1979 | |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 1980 | EmitDebugInfoPerCU(ModuleCU); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /// EmitAbbreviations - Emit the abbreviation section. |
| 1984 | /// |
| 1985 | void DwarfDebug::EmitAbbreviations() const { |
| 1986 | // Check to see if it is worth the effort. |
| 1987 | if (!Abbreviations.empty()) { |
| 1988 | // Start the debug abbrev section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1989 | Asm->OutStreamer.SwitchSection( |
| 1990 | Asm->getObjFileLowering().getDwarfAbbrevSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1991 | |
| 1992 | EmitLabel("abbrev_begin", 0); |
| 1993 | |
| 1994 | // For each abbrevation. |
| 1995 | for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) { |
| 1996 | // Get abbreviation data |
| 1997 | const DIEAbbrev *Abbrev = Abbreviations[i]; |
| 1998 | |
| 1999 | // Emit the abbrevations code (base 1 index.) |
| 2000 | Asm->EmitULEB128Bytes(Abbrev->getNumber()); |
| 2001 | Asm->EOL("Abbreviation Code"); |
| 2002 | |
| 2003 | // Emit the abbreviations data. |
| 2004 | Abbrev->Emit(Asm); |
| 2005 | |
| 2006 | Asm->EOL(); |
| 2007 | } |
| 2008 | |
| 2009 | // Mark end of abbreviations. |
| 2010 | Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)"); |
| 2011 | |
| 2012 | EmitLabel("abbrev_end", 0); |
| 2013 | Asm->EOL(); |
| 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | /// EmitEndOfLineMatrix - Emit the last address of the section and the end of |
| 2018 | /// the line matrix. |
| 2019 | /// |
| 2020 | void DwarfDebug::EmitEndOfLineMatrix(unsigned SectionEnd) { |
| 2021 | // Define last address of section. |
| 2022 | Asm->EmitInt8(0); Asm->EOL("Extended Op"); |
| 2023 | Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size"); |
| 2024 | Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address"); |
| 2025 | EmitReference("section_end", SectionEnd); Asm->EOL("Section end label"); |
| 2026 | |
| 2027 | // Mark end of matrix. |
| 2028 | Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence"); |
| 2029 | Asm->EmitULEB128Bytes(1); Asm->EOL(); |
| 2030 | Asm->EmitInt8(1); Asm->EOL(); |
| 2031 | } |
| 2032 | |
| 2033 | /// EmitDebugLines - Emit source line information. |
| 2034 | /// |
| 2035 | void DwarfDebug::EmitDebugLines() { |
| 2036 | // If the target is using .loc/.file, the assembler will be emitting the |
| 2037 | // .debug_line table automatically. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 2038 | if (MAI->hasDotLocAndDotFile()) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2039 | return; |
| 2040 | |
| 2041 | // Minimum line delta, thus ranging from -10..(255-10). |
| 2042 | const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1); |
| 2043 | // Maximum line delta, thus ranging from -10..(255-10). |
| 2044 | const int MaxLineDelta = 255 + MinLineDelta; |
| 2045 | |
| 2046 | // Start the dwarf line section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2047 | Asm->OutStreamer.SwitchSection( |
| 2048 | Asm->getObjFileLowering().getDwarfLineSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2049 | |
| 2050 | // Construct the section header. |
| 2051 | EmitDifference("line_end", 0, "line_begin", 0, true); |
| 2052 | Asm->EOL("Length of Source Line Info"); |
| 2053 | EmitLabel("line_begin", 0); |
| 2054 | |
| 2055 | Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF version number"); |
| 2056 | |
| 2057 | EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true); |
| 2058 | Asm->EOL("Prolog Length"); |
| 2059 | EmitLabel("line_prolog_begin", 0); |
| 2060 | |
| 2061 | Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length"); |
| 2062 | |
| 2063 | Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag"); |
| 2064 | |
| 2065 | Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)"); |
| 2066 | |
| 2067 | Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)"); |
| 2068 | |
| 2069 | Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base"); |
| 2070 | |
| 2071 | // Line number standard opcode encodings argument count |
| 2072 | Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count"); |
| 2073 | Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count"); |
| 2074 | Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count"); |
| 2075 | Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count"); |
| 2076 | Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count"); |
| 2077 | Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count"); |
| 2078 | Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count"); |
| 2079 | Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count"); |
| 2080 | Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count"); |
| 2081 | |
| 2082 | // Emit directories. |
| 2083 | for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) { |
| 2084 | Asm->EmitString(getSourceDirectoryName(DI)); |
| 2085 | Asm->EOL("Directory"); |
| 2086 | } |
| 2087 | |
| 2088 | Asm->EmitInt8(0); Asm->EOL("End of directories"); |
| 2089 | |
| 2090 | // Emit files. |
| 2091 | for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) { |
| 2092 | // Remember source id starts at 1. |
| 2093 | std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI); |
| 2094 | Asm->EmitString(getSourceFileName(Id.second)); |
| 2095 | Asm->EOL("Source"); |
| 2096 | Asm->EmitULEB128Bytes(Id.first); |
| 2097 | Asm->EOL("Directory #"); |
| 2098 | Asm->EmitULEB128Bytes(0); |
| 2099 | Asm->EOL("Mod date"); |
| 2100 | Asm->EmitULEB128Bytes(0); |
| 2101 | Asm->EOL("File size"); |
| 2102 | } |
| 2103 | |
| 2104 | Asm->EmitInt8(0); Asm->EOL("End of files"); |
| 2105 | |
| 2106 | EmitLabel("line_prolog_end", 0); |
| 2107 | |
| 2108 | // A sequence for each text section. |
| 2109 | unsigned SecSrcLinesSize = SectionSourceLines.size(); |
| 2110 | |
| 2111 | for (unsigned j = 0; j < SecSrcLinesSize; ++j) { |
| 2112 | // Isolate current sections line info. |
| 2113 | const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j]; |
| 2114 | |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 2115 | /*if (Asm->isVerbose()) { |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 2116 | const MCSection *S = SectionMap[j + 1]; |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 2117 | O << '\t' << MAI->getCommentString() << " Section" |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2118 | << S->getName() << '\n'; |
Chris Lattner | 93b6db3 | 2009-08-08 23:39:42 +0000 | [diff] [blame] | 2119 | }*/ |
| 2120 | Asm->EOL(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2121 | |
| 2122 | // Dwarf assumes we start with first line of first source file. |
| 2123 | unsigned Source = 1; |
| 2124 | unsigned Line = 1; |
| 2125 | |
| 2126 | // Construct rows of the address, source, line, column matrix. |
| 2127 | for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) { |
| 2128 | const SrcLineInfo &LineInfo = LineInfos[i]; |
| 2129 | unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID()); |
| 2130 | if (!LabelID) continue; |
| 2131 | |
| 2132 | if (!Asm->isVerbose()) |
| 2133 | Asm->EOL(); |
| 2134 | else { |
| 2135 | std::pair<unsigned, unsigned> SourceID = |
| 2136 | getSourceDirectoryAndFileIds(LineInfo.getSourceID()); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 2137 | O << '\t' << MAI->getCommentString() << ' ' |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2138 | << getSourceDirectoryName(SourceID.first) << ' ' |
| 2139 | << getSourceFileName(SourceID.second) |
| 2140 | <<" :" << utostr_32(LineInfo.getLine()) << '\n'; |
| 2141 | } |
| 2142 | |
| 2143 | // Define the line address. |
| 2144 | Asm->EmitInt8(0); Asm->EOL("Extended Op"); |
| 2145 | Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size"); |
| 2146 | Asm->EmitInt8(dwarf::DW_LNE_set_address); Asm->EOL("DW_LNE_set_address"); |
| 2147 | EmitReference("label", LabelID); Asm->EOL("Location label"); |
| 2148 | |
| 2149 | // If change of source, then switch to the new source. |
| 2150 | if (Source != LineInfo.getSourceID()) { |
| 2151 | Source = LineInfo.getSourceID(); |
| 2152 | Asm->EmitInt8(dwarf::DW_LNS_set_file); Asm->EOL("DW_LNS_set_file"); |
| 2153 | Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source"); |
| 2154 | } |
| 2155 | |
| 2156 | // If change of line. |
| 2157 | if (Line != LineInfo.getLine()) { |
| 2158 | // Determine offset. |
| 2159 | int Offset = LineInfo.getLine() - Line; |
| 2160 | int Delta = Offset - MinLineDelta; |
| 2161 | |
| 2162 | // Update line. |
| 2163 | Line = LineInfo.getLine(); |
| 2164 | |
| 2165 | // If delta is small enough and in range... |
| 2166 | if (Delta >= 0 && Delta < (MaxLineDelta - 1)) { |
| 2167 | // ... then use fast opcode. |
| 2168 | Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta"); |
| 2169 | } else { |
| 2170 | // ... otherwise use long hand. |
| 2171 | Asm->EmitInt8(dwarf::DW_LNS_advance_line); |
| 2172 | Asm->EOL("DW_LNS_advance_line"); |
| 2173 | Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset"); |
| 2174 | Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy"); |
| 2175 | } |
| 2176 | } else { |
| 2177 | // Copy the previous row (different address or source) |
| 2178 | Asm->EmitInt8(dwarf::DW_LNS_copy); Asm->EOL("DW_LNS_copy"); |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | EmitEndOfLineMatrix(j + 1); |
| 2183 | } |
| 2184 | |
| 2185 | if (SecSrcLinesSize == 0) |
| 2186 | // Because we're emitting a debug_line section, we still need a line |
| 2187 | // table. The linker and friends expect it to exist. If there's nothing to |
| 2188 | // put into it, emit an empty table. |
| 2189 | EmitEndOfLineMatrix(1); |
| 2190 | |
| 2191 | EmitLabel("line_end", 0); |
| 2192 | Asm->EOL(); |
| 2193 | } |
| 2194 | |
| 2195 | /// EmitCommonDebugFrame - Emit common frame info into a debug frame section. |
| 2196 | /// |
| 2197 | void DwarfDebug::EmitCommonDebugFrame() { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 2198 | if (!MAI->doesDwarfRequireFrameSection()) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2199 | return; |
| 2200 | |
| 2201 | int stackGrowth = |
| 2202 | Asm->TM.getFrameInfo()->getStackGrowthDirection() == |
| 2203 | TargetFrameInfo::StackGrowsUp ? |
| 2204 | TD->getPointerSize() : -TD->getPointerSize(); |
| 2205 | |
| 2206 | // Start the dwarf frame section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2207 | Asm->OutStreamer.SwitchSection( |
| 2208 | Asm->getObjFileLowering().getDwarfFrameSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2209 | |
| 2210 | EmitLabel("debug_frame_common", 0); |
| 2211 | EmitDifference("debug_frame_common_end", 0, |
| 2212 | "debug_frame_common_begin", 0, true); |
| 2213 | Asm->EOL("Length of Common Information Entry"); |
| 2214 | |
| 2215 | EmitLabel("debug_frame_common_begin", 0); |
| 2216 | Asm->EmitInt32((int)dwarf::DW_CIE_ID); |
| 2217 | Asm->EOL("CIE Identifier Tag"); |
| 2218 | Asm->EmitInt8(dwarf::DW_CIE_VERSION); |
| 2219 | Asm->EOL("CIE Version"); |
| 2220 | Asm->EmitString(""); |
| 2221 | Asm->EOL("CIE Augmentation"); |
| 2222 | Asm->EmitULEB128Bytes(1); |
| 2223 | Asm->EOL("CIE Code Alignment Factor"); |
| 2224 | Asm->EmitSLEB128Bytes(stackGrowth); |
| 2225 | Asm->EOL("CIE Data Alignment Factor"); |
| 2226 | Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false)); |
| 2227 | Asm->EOL("CIE RA Column"); |
| 2228 | |
| 2229 | std::vector<MachineMove> Moves; |
| 2230 | RI->getInitialFrameState(Moves); |
| 2231 | |
| 2232 | EmitFrameMoves(NULL, 0, Moves, false); |
| 2233 | |
| 2234 | Asm->EmitAlignment(2, 0, 0, false); |
| 2235 | EmitLabel("debug_frame_common_end", 0); |
| 2236 | |
| 2237 | Asm->EOL(); |
| 2238 | } |
| 2239 | |
| 2240 | /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame |
| 2241 | /// section. |
| 2242 | void |
| 2243 | DwarfDebug::EmitFunctionDebugFrame(const FunctionDebugFrameInfo&DebugFrameInfo){ |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 2244 | if (!MAI->doesDwarfRequireFrameSection()) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2245 | return; |
| 2246 | |
| 2247 | // Start the dwarf frame section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2248 | Asm->OutStreamer.SwitchSection( |
| 2249 | Asm->getObjFileLowering().getDwarfFrameSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2250 | |
| 2251 | EmitDifference("debug_frame_end", DebugFrameInfo.Number, |
| 2252 | "debug_frame_begin", DebugFrameInfo.Number, true); |
| 2253 | Asm->EOL("Length of Frame Information Entry"); |
| 2254 | |
| 2255 | EmitLabel("debug_frame_begin", DebugFrameInfo.Number); |
| 2256 | |
| 2257 | EmitSectionOffset("debug_frame_common", "section_debug_frame", |
| 2258 | 0, 0, true, false); |
| 2259 | Asm->EOL("FDE CIE offset"); |
| 2260 | |
| 2261 | EmitReference("func_begin", DebugFrameInfo.Number); |
| 2262 | Asm->EOL("FDE initial location"); |
| 2263 | EmitDifference("func_end", DebugFrameInfo.Number, |
| 2264 | "func_begin", DebugFrameInfo.Number); |
| 2265 | Asm->EOL("FDE address range"); |
| 2266 | |
| 2267 | EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves, |
| 2268 | false); |
| 2269 | |
| 2270 | Asm->EmitAlignment(2, 0, 0, false); |
| 2271 | EmitLabel("debug_frame_end", DebugFrameInfo.Number); |
| 2272 | |
| 2273 | Asm->EOL(); |
| 2274 | } |
| 2275 | |
| 2276 | void DwarfDebug::EmitDebugPubNamesPerCU(CompileUnit *Unit) { |
| 2277 | EmitDifference("pubnames_end", Unit->getID(), |
| 2278 | "pubnames_begin", Unit->getID(), true); |
| 2279 | Asm->EOL("Length of Public Names Info"); |
| 2280 | |
| 2281 | EmitLabel("pubnames_begin", Unit->getID()); |
| 2282 | |
| 2283 | Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("DWARF Version"); |
| 2284 | |
| 2285 | EmitSectionOffset("info_begin", "section_info", |
| 2286 | Unit->getID(), 0, true, false); |
| 2287 | Asm->EOL("Offset of Compilation Unit Info"); |
| 2288 | |
| 2289 | EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(), |
| 2290 | true); |
| 2291 | Asm->EOL("Compilation Unit Length"); |
| 2292 | |
| 2293 | StringMap<DIE*> &Globals = Unit->getGlobals(); |
| 2294 | for (StringMap<DIE*>::const_iterator |
| 2295 | GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) { |
| 2296 | const char *Name = GI->getKeyData(); |
| 2297 | DIE * Entity = GI->second; |
| 2298 | |
| 2299 | Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset"); |
| 2300 | Asm->EmitString(Name, strlen(Name)); Asm->EOL("External Name"); |
| 2301 | } |
| 2302 | |
| 2303 | Asm->EmitInt32(0); Asm->EOL("End Mark"); |
| 2304 | EmitLabel("pubnames_end", Unit->getID()); |
| 2305 | |
| 2306 | Asm->EOL(); |
| 2307 | } |
| 2308 | |
| 2309 | /// EmitDebugPubNames - Emit visible names into a debug pubnames section. |
| 2310 | /// |
| 2311 | void DwarfDebug::EmitDebugPubNames() { |
| 2312 | // Start the dwarf pubnames section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2313 | Asm->OutStreamer.SwitchSection( |
| 2314 | Asm->getObjFileLowering().getDwarfPubNamesSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2315 | |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 2316 | EmitDebugPubNamesPerCU(ModuleCU); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | /// EmitDebugStr - Emit visible names into a debug str section. |
| 2320 | /// |
| 2321 | void DwarfDebug::EmitDebugStr() { |
| 2322 | // Check to see if it is worth the effort. |
| 2323 | if (!StringPool.empty()) { |
| 2324 | // Start the dwarf str section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2325 | Asm->OutStreamer.SwitchSection( |
| 2326 | Asm->getObjFileLowering().getDwarfStrSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2327 | |
| 2328 | // For each of strings in the string pool. |
| 2329 | for (unsigned StringID = 1, N = StringPool.size(); |
| 2330 | StringID <= N; ++StringID) { |
| 2331 | // Emit a label for reference from debug information entries. |
| 2332 | EmitLabel("string", StringID); |
| 2333 | |
| 2334 | // Emit the string itself. |
| 2335 | const std::string &String = StringPool[StringID]; |
| 2336 | Asm->EmitString(String); Asm->EOL(); |
| 2337 | } |
| 2338 | |
| 2339 | Asm->EOL(); |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | /// EmitDebugLoc - Emit visible names into a debug loc section. |
| 2344 | /// |
| 2345 | void DwarfDebug::EmitDebugLoc() { |
| 2346 | // Start the dwarf loc section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2347 | Asm->OutStreamer.SwitchSection( |
| 2348 | Asm->getObjFileLowering().getDwarfLocSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2349 | Asm->EOL(); |
| 2350 | } |
| 2351 | |
| 2352 | /// EmitDebugARanges - Emit visible names into a debug aranges section. |
| 2353 | /// |
| 2354 | void DwarfDebug::EmitDebugARanges() { |
| 2355 | // Start the dwarf aranges section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2356 | Asm->OutStreamer.SwitchSection( |
| 2357 | Asm->getObjFileLowering().getDwarfARangesSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2358 | |
| 2359 | // FIXME - Mock up |
| 2360 | #if 0 |
| 2361 | CompileUnit *Unit = GetBaseCompileUnit(); |
| 2362 | |
| 2363 | // Don't include size of length |
| 2364 | Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info"); |
| 2365 | |
| 2366 | Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version"); |
| 2367 | |
| 2368 | EmitReference("info_begin", Unit->getID()); |
| 2369 | Asm->EOL("Offset of Compilation Unit Info"); |
| 2370 | |
| 2371 | Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address"); |
| 2372 | |
| 2373 | Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor"); |
| 2374 | |
| 2375 | Asm->EmitInt16(0); Asm->EOL("Pad (1)"); |
| 2376 | Asm->EmitInt16(0); Asm->EOL("Pad (2)"); |
| 2377 | |
| 2378 | // Range 1 |
| 2379 | EmitReference("text_begin", 0); Asm->EOL("Address"); |
| 2380 | EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length"); |
| 2381 | |
| 2382 | Asm->EmitInt32(0); Asm->EOL("EOM (1)"); |
| 2383 | Asm->EmitInt32(0); Asm->EOL("EOM (2)"); |
| 2384 | #endif |
| 2385 | |
| 2386 | Asm->EOL(); |
| 2387 | } |
| 2388 | |
| 2389 | /// EmitDebugRanges - Emit visible names into a debug ranges section. |
| 2390 | /// |
| 2391 | void DwarfDebug::EmitDebugRanges() { |
| 2392 | // Start the dwarf ranges section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2393 | Asm->OutStreamer.SwitchSection( |
| 2394 | Asm->getObjFileLowering().getDwarfRangesSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2395 | Asm->EOL(); |
| 2396 | } |
| 2397 | |
| 2398 | /// EmitDebugMacInfo - Emit visible names into a debug macinfo section. |
| 2399 | /// |
| 2400 | void DwarfDebug::EmitDebugMacInfo() { |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 2401 | if (const MCSection *LineInfo = |
| 2402 | Asm->getObjFileLowering().getDwarfMacroInfoSection()) { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2403 | // Start the dwarf macinfo section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2404 | Asm->OutStreamer.SwitchSection(LineInfo); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2405 | Asm->EOL(); |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | /// EmitDebugInlineInfo - Emit inline info using following format. |
| 2410 | /// Section Header: |
| 2411 | /// 1. length of section |
| 2412 | /// 2. Dwarf version number |
| 2413 | /// 3. address size. |
| 2414 | /// |
| 2415 | /// Entries (one "entry" for each function that was inlined): |
| 2416 | /// |
| 2417 | /// 1. offset into __debug_str section for MIPS linkage name, if exists; |
| 2418 | /// otherwise offset into __debug_str for regular function name. |
| 2419 | /// 2. offset into __debug_str section for regular function name. |
| 2420 | /// 3. an unsigned LEB128 number indicating the number of distinct inlining |
| 2421 | /// instances for the function. |
| 2422 | /// |
| 2423 | /// The rest of the entry consists of a {die_offset, low_pc} pair for each |
| 2424 | /// inlined instance; the die_offset points to the inlined_subroutine die in the |
| 2425 | /// __debug_info section, and the low_pc is the starting address for the |
| 2426 | /// inlining instance. |
| 2427 | void DwarfDebug::EmitDebugInlineInfo() { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 2428 | if (!MAI->doesDwarfUsesInlineInfoSection()) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2429 | return; |
| 2430 | |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 2431 | if (!ModuleCU) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2432 | return; |
| 2433 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2434 | Asm->OutStreamer.SwitchSection( |
| 2435 | Asm->getObjFileLowering().getDwarfDebugInlineSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2436 | Asm->EOL(); |
| 2437 | EmitDifference("debug_inlined_end", 1, |
| 2438 | "debug_inlined_begin", 1, true); |
| 2439 | Asm->EOL("Length of Debug Inlined Information Entry"); |
| 2440 | |
| 2441 | EmitLabel("debug_inlined_begin", 1); |
| 2442 | |
| 2443 | Asm->EmitInt16(dwarf::DWARF_VERSION); Asm->EOL("Dwarf Version"); |
| 2444 | Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)"); |
| 2445 | |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 2446 | for (DenseMap<GlobalVariable *, SmallVector<unsigned, 4> >::iterator |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2447 | I = InlineInfo.begin(), E = InlineInfo.end(); I != E; ++I) { |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 2448 | GlobalVariable *GV = I->first; |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2449 | SmallVector<unsigned, 4> &Labels = I->second; |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 2450 | DISubprogram SP(GV); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2451 | std::string Name; |
| 2452 | std::string LName; |
| 2453 | |
| 2454 | SP.getLinkageName(LName); |
| 2455 | SP.getName(Name); |
| 2456 | |
Devang Patel | 53cb17d | 2009-07-16 01:01:22 +0000 | [diff] [blame] | 2457 | if (LName.empty()) |
| 2458 | Asm->EmitString(Name); |
| 2459 | else { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2460 | // Skip special LLVM prefix that is used to inform the asm printer to not |
| 2461 | // emit usual symbol prefix before the symbol name. This happens for |
| 2462 | // Objective-C symbol names and symbol whose name is replaced using GCC's |
| 2463 | // __asm__ attribute. |
Devang Patel | 53cb17d | 2009-07-16 01:01:22 +0000 | [diff] [blame] | 2464 | if (LName[0] == 1) |
| 2465 | LName = &LName[1]; |
| 2466 | Asm->EmitString(LName); |
| 2467 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2468 | Asm->EOL("MIPS linkage name"); |
| 2469 | |
| 2470 | Asm->EmitString(Name); Asm->EOL("Function name"); |
| 2471 | |
| 2472 | Asm->EmitULEB128Bytes(Labels.size()); Asm->EOL("Inline count"); |
| 2473 | |
| 2474 | for (SmallVector<unsigned, 4>::iterator LI = Labels.begin(), |
| 2475 | LE = Labels.end(); LI != LE; ++LI) { |
Devang Patel | 8245988 | 2009-08-26 05:01:18 +0000 | [diff] [blame] | 2476 | DIE *SP = ModuleCU->getDieMapSlotFor(GV); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2477 | Asm->EmitInt32(SP->getOffset()); Asm->EOL("DIE offset"); |
| 2478 | |
| 2479 | if (TD->getPointerSize() == sizeof(int32_t)) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 2480 | O << MAI->getData32bitsDirective(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2481 | else |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 2482 | O << MAI->getData64bitsDirective(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2483 | |
| 2484 | PrintLabelName("label", *LI); Asm->EOL("low_pc"); |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | EmitLabel("debug_inlined_end", 1); |
| 2489 | Asm->EOL(); |
| 2490 | } |