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