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