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