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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6cde3e6 | 2010-03-09 00:39:24 +0000 | [diff] [blame] | 13 | |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "dwarfdebug" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 15 | #include "DwarfDebug.h" |
Chris Lattner | 74e41f9 | 2010-04-05 05:24:55 +0000 | [diff] [blame] | 16 | #include "DIE.h" |
Bill Wendling | 57fbba4 | 2010-04-05 22:59:21 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
David Greene | b2c66fc | 2009-08-19 21:52:55 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunction.h" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | b7db733 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCSection.h" |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | b7db733 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | 45111d1 | 2010-01-16 21:57:06 +0000 | [diff] [blame] | 25 | #include "llvm/Target/Mangler.h" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
| 27 | #include "llvm/Target/TargetFrameInfo.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Chris Lattner | 9d1c1ad | 2010-04-04 18:06:11 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetRegisterInfo.h" |
Devang Patel | 2a4a3b7 | 2010-04-19 19:14:02 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 74e41f9 | 2010-04-05 05:24:55 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/DebugInfo.h" |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 23132b1 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringExtras.h" |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 6e4bdfc | 2009-10-13 06:47:08 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
| 37 | #include "llvm/Support/ErrorHandling.h" |
Devang Patel | 3139fcf | 2010-01-26 21:39:14 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ValueHandle.h" |
Chris Lattner | 0ad9c91 | 2010-01-22 22:09:00 +0000 | [diff] [blame] | 39 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Timer.h" |
| 41 | #include "llvm/System/Path.h" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 44 | static cl::opt<bool> PrintDbgScope("print-dbgscope", cl::Hidden, |
| 45 | cl::desc("Print DbgScope information for each machine instruction")); |
| 46 | |
| 47 | static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, |
| 48 | cl::desc("Disable debug info printing")); |
| 49 | |
Bill Wendling | 5f017e8 | 2010-04-07 09:28:04 +0000 | [diff] [blame] | 50 | namespace { |
| 51 | const char *DWARFGroupName = "DWARF Emission"; |
| 52 | const char *DbgTimerName = "DWARF Debug Writer"; |
| 53 | } // end anonymous namespace |
| 54 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// |
| 56 | |
| 57 | /// Configuration values for initial hash set sizes (log2). |
| 58 | /// |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 59 | static const unsigned InitAbbreviationsSetSize = 9; // log2(512) |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 60 | |
| 61 | namespace llvm { |
| 62 | |
| 63 | //===----------------------------------------------------------------------===// |
| 64 | /// CompileUnit - This dwarf writer support class manages information associate |
| 65 | /// with a source file. |
Nick Lewycky | 5f9843f | 2009-11-17 08:11:44 +0000 | [diff] [blame] | 66 | class CompileUnit { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 67 | /// ID - File identifier for source. |
| 68 | /// |
| 69 | unsigned ID; |
| 70 | |
| 71 | /// Die - Compile unit debug information entry. |
| 72 | /// |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 73 | const OwningPtr<DIE> CUDie; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 74 | |
Jeffrey Yasskin | d0f393d | 2010-03-11 18:29:55 +0000 | [diff] [blame] | 75 | /// IndexTyDie - An anonymous type for index type. Owned by CUDie. |
Devang Patel | 6f01d9c | 2009-11-21 00:31:03 +0000 | [diff] [blame] | 76 | DIE *IndexTyDie; |
| 77 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 78 | /// GVToDieMap - Tracks the mapping of unit level debug informaton |
| 79 | /// variables to debug information entries. |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 80 | /// FIXME : Rename GVToDieMap -> NodeToDieMap |
Devang Patel | 622b026 | 2010-01-19 06:19:05 +0000 | [diff] [blame] | 81 | DenseMap<MDNode *, DIE *> GVToDieMap; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 82 | |
| 83 | /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton |
| 84 | /// descriptors to debug information entries using a DIEEntry proxy. |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 85 | /// FIXME : Rename |
Devang Patel | 622b026 | 2010-01-19 06:19:05 +0000 | [diff] [blame] | 86 | DenseMap<MDNode *, DIEEntry *> GVToDIEEntryMap; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 87 | |
| 88 | /// Globals - A map of globally visible named entities for this unit. |
| 89 | /// |
| 90 | StringMap<DIE*> Globals; |
| 91 | |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 92 | /// GlobalTypes - A map of globally visible types for this unit. |
| 93 | /// |
| 94 | StringMap<DIE*> GlobalTypes; |
| 95 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 96 | public: |
| 97 | CompileUnit(unsigned I, DIE *D) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 98 | : ID(I), CUDie(D), IndexTyDie(0) {} |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 99 | |
| 100 | // Accessors. |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 101 | unsigned getID() const { return ID; } |
Jeffrey Yasskin | d0f393d | 2010-03-11 18:29:55 +0000 | [diff] [blame] | 102 | DIE* getCUDie() const { return CUDie.get(); } |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 103 | const StringMap<DIE*> &getGlobals() const { return Globals; } |
| 104 | const StringMap<DIE*> &getGlobalTypes() const { return GlobalTypes; } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 105 | |
| 106 | /// hasContent - Return true if this compile unit has something to write out. |
| 107 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 108 | bool hasContent() const { return !CUDie->getChildren().empty(); } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 109 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 110 | /// addGlobal - Add a new global entity to the compile unit. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 111 | /// |
Benjamin Kramer | bbb88db | 2010-03-31 20:15:45 +0000 | [diff] [blame] | 112 | void addGlobal(StringRef Name, DIE *Die) { Globals[Name] = Die; } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 113 | |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 114 | /// addGlobalType - Add a new global type to the compile unit. |
| 115 | /// |
Benjamin Kramer | bbb88db | 2010-03-31 20:15:45 +0000 | [diff] [blame] | 116 | void addGlobalType(StringRef Name, DIE *Die) { |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 117 | GlobalTypes[Name] = Die; |
| 118 | } |
| 119 | |
Devang Patel | 017d121 | 2009-11-20 21:37:22 +0000 | [diff] [blame] | 120 | /// getDIE - Returns the debug information entry map slot for the |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 121 | /// specified debug variable. |
Devang Patel | 017d121 | 2009-11-20 21:37:22 +0000 | [diff] [blame] | 122 | DIE *getDIE(MDNode *N) { return GVToDieMap.lookup(N); } |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 123 | |
Devang Patel | 017d121 | 2009-11-20 21:37:22 +0000 | [diff] [blame] | 124 | /// insertDIE - Insert DIE into the map. |
| 125 | void insertDIE(MDNode *N, DIE *D) { |
| 126 | GVToDieMap.insert(std::make_pair(N, D)); |
| 127 | } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 128 | |
Devang Patel | 017d121 | 2009-11-20 21:37:22 +0000 | [diff] [blame] | 129 | /// getDIEEntry - Returns the debug information entry for the speciefied |
| 130 | /// debug variable. |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 131 | DIEEntry *getDIEEntry(MDNode *N) { |
Devang Patel | 622b026 | 2010-01-19 06:19:05 +0000 | [diff] [blame] | 132 | DenseMap<MDNode *, DIEEntry *>::iterator I = GVToDIEEntryMap.find(N); |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 133 | if (I == GVToDIEEntryMap.end()) |
| 134 | return NULL; |
| 135 | return I->second; |
| 136 | } |
Devang Patel | 017d121 | 2009-11-20 21:37:22 +0000 | [diff] [blame] | 137 | |
| 138 | /// insertDIEEntry - Insert debug information entry into the map. |
| 139 | void insertDIEEntry(MDNode *N, DIEEntry *E) { |
| 140 | GVToDIEEntryMap.insert(std::make_pair(N, E)); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 143 | /// addDie - Adds or interns the DIE to the compile unit. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 144 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 145 | void addDie(DIE *Buffer) { |
| 146 | this->CUDie->addChild(Buffer); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 147 | } |
Devang Patel | 6f01d9c | 2009-11-21 00:31:03 +0000 | [diff] [blame] | 148 | |
| 149 | // getIndexTyDie - Get an anonymous type for index type. |
| 150 | DIE *getIndexTyDie() { |
| 151 | return IndexTyDie; |
| 152 | } |
| 153 | |
Jim Grosbach | 7ab38df | 2009-11-22 19:20:36 +0000 | [diff] [blame] | 154 | // setIndexTyDie - Set D as anonymous type for index which can be reused |
| 155 | // later. |
Devang Patel | 6f01d9c | 2009-11-21 00:31:03 +0000 | [diff] [blame] | 156 | void setIndexTyDie(DIE *D) { |
| 157 | IndexTyDie = D; |
| 158 | } |
| 159 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | /// DbgVariable - This class is used to track local variable information. |
| 164 | /// |
Devang Patel | f76a3d6 | 2009-11-16 21:53:40 +0000 | [diff] [blame] | 165 | class DbgVariable { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 166 | DIVariable Var; // Variable Descriptor. |
| 167 | unsigned FrameIndex; // Variable frame index. |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 168 | const MachineInstr *DbgValueMInsn; // DBG_VALUE |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 169 | // DbgValueLabel - DBG_VALUE is effective from this label. |
| 170 | MCSymbol *DbgValueLabel; |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 171 | DbgVariable *const AbstractVar; // Abstract variable for this variable. |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 172 | DIE *TheDIE; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 173 | public: |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 174 | // AbsVar may be NULL. |
| 175 | DbgVariable(DIVariable V, unsigned I, DbgVariable *AbsVar) |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 176 | : Var(V), FrameIndex(I), DbgValueMInsn(0), |
| 177 | DbgValueLabel(0), AbstractVar(AbsVar), TheDIE(0) {} |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 178 | DbgVariable(DIVariable V, const MachineInstr *MI, DbgVariable *AbsVar) |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 179 | : Var(V), FrameIndex(0), DbgValueMInsn(MI), DbgValueLabel(0), |
| 180 | AbstractVar(AbsVar), TheDIE(0) |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 181 | {} |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 182 | |
| 183 | // Accessors. |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 184 | DIVariable getVariable() const { return Var; } |
| 185 | unsigned getFrameIndex() const { return FrameIndex; } |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 186 | const MachineInstr *getDbgValue() const { return DbgValueMInsn; } |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 187 | MCSymbol *getDbgValueLabel() const { return DbgValueLabel; } |
| 188 | void setDbgValueLabel(MCSymbol *L) { DbgValueLabel = L; } |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 189 | DbgVariable *getAbstractVariable() const { return AbstractVar; } |
| 190 | void setDIE(DIE *D) { TheDIE = D; } |
| 191 | DIE *getDIE() const { return TheDIE; } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | //===----------------------------------------------------------------------===// |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 195 | /// DbgRange - This is used to track range of instructions with identical |
| 196 | /// debug info scope. |
| 197 | /// |
| 198 | typedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange; |
| 199 | |
| 200 | //===----------------------------------------------------------------------===// |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 201 | /// DbgScope - This class is used to track scope information. |
| 202 | /// |
Devang Patel | f76a3d6 | 2009-11-16 21:53:40 +0000 | [diff] [blame] | 203 | class DbgScope { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 204 | DbgScope *Parent; // Parent to this scope. |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 205 | DIDescriptor Desc; // Debug info descriptor for scope. |
Devang Patel | 3139fcf | 2010-01-26 21:39:14 +0000 | [diff] [blame] | 206 | // Location at which this scope is inlined. |
| 207 | AssertingVH<MDNode> InlinedAtLocation; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 208 | bool AbstractScope; // Abstract Scope |
Devang Patel | d38dd11 | 2009-10-01 18:25:23 +0000 | [diff] [blame] | 209 | const MachineInstr *LastInsn; // Last instruction of this scope. |
| 210 | const MachineInstr *FirstInsn; // First instruction of this scope. |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 211 | unsigned DFSIn, DFSOut; |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 212 | // Scopes defined in scope. Contents not owned. |
| 213 | SmallVector<DbgScope *, 4> Scopes; |
| 214 | // Variables declared in scope. Contents owned. |
| 215 | SmallVector<DbgVariable *, 8> Variables; |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 216 | SmallVector<DbgRange, 4> Ranges; |
Owen Anderson | 04c05f7 | 2009-06-24 22:53:20 +0000 | [diff] [blame] | 217 | // Private state for dump() |
| 218 | mutable unsigned IndentLevel; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 219 | public: |
Devang Patel | c90aefe | 2009-10-14 21:08:09 +0000 | [diff] [blame] | 220 | DbgScope(DbgScope *P, DIDescriptor D, MDNode *I = 0) |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 221 | : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false), |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 222 | LastInsn(0), FirstInsn(0), |
| 223 | DFSIn(0), DFSOut(0), IndentLevel(0) {} |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 224 | virtual ~DbgScope(); |
| 225 | |
| 226 | // Accessors. |
| 227 | DbgScope *getParent() const { return Parent; } |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 228 | void setParent(DbgScope *P) { Parent = P; } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 229 | DIDescriptor getDesc() const { return Desc; } |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 230 | MDNode *getInlinedAt() const { return InlinedAtLocation; } |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 231 | MDNode *getScopeNode() const { return Desc.getNode(); } |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 232 | const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; } |
| 233 | const SmallVector<DbgVariable *, 8> &getVariables() { return Variables; } |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 234 | const SmallVector<DbgRange, 4> &getRanges() { return Ranges; } |
| 235 | |
| 236 | /// openInsnRange - This scope covers instruction range starting from MI. |
| 237 | void openInsnRange(const MachineInstr *MI) { |
| 238 | if (!FirstInsn) |
| 239 | FirstInsn = MI; |
| 240 | |
| 241 | if (Parent) |
| 242 | Parent->openInsnRange(MI); |
| 243 | } |
| 244 | |
| 245 | /// extendInsnRange - Extend the current instruction range covered by |
| 246 | /// this scope. |
| 247 | void extendInsnRange(const MachineInstr *MI) { |
| 248 | assert (FirstInsn && "MI Range is not open!"); |
| 249 | LastInsn = MI; |
| 250 | if (Parent) |
| 251 | Parent->extendInsnRange(MI); |
| 252 | } |
| 253 | |
| 254 | /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected |
| 255 | /// until now. This is used when a new scope is encountered while walking |
| 256 | /// machine instructions. |
| 257 | void closeInsnRange(DbgScope *NewScope = NULL) { |
| 258 | assert (LastInsn && "Last insn missing!"); |
| 259 | Ranges.push_back(DbgRange(FirstInsn, LastInsn)); |
| 260 | FirstInsn = NULL; |
| 261 | LastInsn = NULL; |
| 262 | // If Parent dominates NewScope then do not close Parent's instruction |
| 263 | // range. |
| 264 | if (Parent && (!NewScope || !Parent->dominates(NewScope))) |
| 265 | Parent->closeInsnRange(NewScope); |
| 266 | } |
| 267 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 268 | void setAbstractScope() { AbstractScope = true; } |
| 269 | bool isAbstractScope() const { return AbstractScope; } |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 270 | |
| 271 | // Depth First Search support to walk and mainpluate DbgScope hierarchy. |
| 272 | unsigned getDFSOut() const { return DFSOut; } |
| 273 | void setDFSOut(unsigned O) { DFSOut = O; } |
| 274 | unsigned getDFSIn() const { return DFSIn; } |
| 275 | void setDFSIn(unsigned I) { DFSIn = I; } |
| 276 | bool dominates(const DbgScope *S) { |
| 277 | if (S == this) |
| 278 | return true; |
| 279 | if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut()) |
| 280 | return true; |
| 281 | return false; |
| 282 | } |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 283 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 284 | /// addScope - Add a scope to the scope. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 285 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 286 | void addScope(DbgScope *S) { Scopes.push_back(S); } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 287 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 288 | /// addVariable - Add a variable to the scope. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 289 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 290 | void addVariable(DbgVariable *V) { Variables.push_back(V); } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 291 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 292 | #ifndef NDEBUG |
| 293 | void dump() const; |
| 294 | #endif |
| 295 | }; |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 296 | |
Chris Lattner | ea76186 | 2010-04-05 04:09:20 +0000 | [diff] [blame] | 297 | } // end llvm namespace |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 298 | |
| 299 | #ifndef NDEBUG |
| 300 | void DbgScope::dump() const { |
David Greene | f83adbc | 2009-12-24 00:31:35 +0000 | [diff] [blame] | 301 | raw_ostream &err = dbgs(); |
Chris Lattner | c281de1 | 2009-08-23 00:51:00 +0000 | [diff] [blame] | 302 | err.indent(IndentLevel); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 303 | MDNode *N = Desc.getNode(); |
| 304 | N->dump(); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 305 | if (AbstractScope) |
| 306 | err << "Abstract Scope\n"; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 307 | |
| 308 | IndentLevel += 2; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 309 | if (!Scopes.empty()) |
| 310 | err << "Children ...\n"; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 311 | for (unsigned i = 0, e = Scopes.size(); i != e; ++i) |
| 312 | if (Scopes[i] != this) |
| 313 | Scopes[i]->dump(); |
| 314 | |
| 315 | IndentLevel -= 2; |
| 316 | } |
| 317 | #endif |
| 318 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 319 | DbgScope::~DbgScope() { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 320 | for (unsigned j = 0, M = Variables.size(); j < M; ++j) |
| 321 | delete Variables[j]; |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Chris Lattner | 49cd664 | 2010-04-05 05:11:15 +0000 | [diff] [blame] | 324 | DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M) |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 325 | : Asm(A), MMI(Asm->MMI), ModuleCU(0), |
Chris Lattner | 994cb12 | 2010-04-05 03:52:55 +0000 | [diff] [blame] | 326 | AbbreviationsSet(InitAbbreviationsSetSize), |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 327 | CurrentFnDbgScope(0), PrevLabel(NULL) { |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 328 | NextStringPoolNumber = 0; |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 4ad1efe | 2010-04-04 23:10:38 +0000 | [diff] [blame] | 330 | DwarfFrameSectionSym = DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0; |
| 331 | DwarfStrSectionSym = TextSectionSym = 0; |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 332 | DwarfDebugRangeSectionSym = 0; |
| 333 | FunctionBeginSym = 0; |
Torok Edwin | 9c42107 | 2010-04-07 10:44:46 +0000 | [diff] [blame] | 334 | if (TimePassesIsEnabled) { |
| 335 | NamedRegionTimer T(DbgTimerName, DWARFGroupName); |
| 336 | beginModule(M); |
| 337 | } else { |
| 338 | beginModule(M); |
| 339 | } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 340 | } |
| 341 | DwarfDebug::~DwarfDebug() { |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 342 | for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j) |
| 343 | DIEBlocks[j]->~DIEBlock(); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 346 | MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) { |
| 347 | std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str]; |
| 348 | if (Entry.first) return Entry.first; |
| 349 | |
| 350 | Entry.second = NextStringPoolNumber++; |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 351 | return Entry.first = Asm->GetTempSymbol("string", Entry.second); |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 355 | /// assignAbbrevNumber - Define a unique number for the abbreviation. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 356 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 357 | void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 358 | // Profile the node so that we can make it unique. |
| 359 | FoldingSetNodeID ID; |
| 360 | Abbrev.Profile(ID); |
| 361 | |
| 362 | // Check the set for priors. |
| 363 | DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev); |
| 364 | |
| 365 | // If it's newly added. |
| 366 | if (InSet == &Abbrev) { |
| 367 | // Add to abbreviation list. |
| 368 | Abbreviations.push_back(&Abbrev); |
| 369 | |
| 370 | // Assign the vector position + 1 as its number. |
| 371 | Abbrev.setNumber(Abbreviations.size()); |
| 372 | } else { |
| 373 | // Assign existing abbreviation number. |
| 374 | Abbrev.setNumber(InSet->getNumber()); |
| 375 | } |
| 376 | } |
| 377 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 378 | /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug |
Bill Wendling | 995f80a | 2009-05-20 23:24:48 +0000 | [diff] [blame] | 379 | /// information entry. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 380 | DIEEntry *DwarfDebug::createDIEEntry(DIE *Entry) { |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 381 | DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 382 | return Value; |
| 383 | } |
| 384 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 385 | /// addUInt - Add an unsigned integer attribute data and value. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 386 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 387 | void DwarfDebug::addUInt(DIE *Die, unsigned Attribute, |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 388 | unsigned Form, uint64_t Integer) { |
| 389 | if (!Form) Form = DIEInteger::BestForm(false, Integer); |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 390 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 391 | Die->addValue(Attribute, Form, Value); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 394 | /// addSInt - Add an signed integer attribute data and value. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 395 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 396 | void DwarfDebug::addSInt(DIE *Die, unsigned Attribute, |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 397 | unsigned Form, int64_t Integer) { |
| 398 | if (!Form) Form = DIEInteger::BestForm(true, Integer); |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 399 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 400 | Die->addValue(Attribute, Form, Value); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Devang Patel | 69f57b1 | 2009-12-02 15:25:16 +0000 | [diff] [blame] | 403 | /// addString - Add a string attribute data and value. DIEString only |
| 404 | /// keeps string reference. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 405 | void DwarfDebug::addString(DIE *Die, unsigned Attribute, unsigned Form, |
Chris Lattner | 4cf202b | 2010-01-23 03:11:46 +0000 | [diff] [blame] | 406 | StringRef String) { |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 407 | DIEValue *Value = new (DIEValueAllocator) DIEString(String); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 408 | Die->addValue(Attribute, Form, Value); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 411 | /// addLabel - Add a Dwarf label attribute data and value. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 412 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 413 | void DwarfDebug::addLabel(DIE *Die, unsigned Attribute, unsigned Form, |
Chris Lattner | b98b1bf | 2010-03-08 22:23:36 +0000 | [diff] [blame] | 414 | const MCSymbol *Label) { |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 415 | DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 416 | Die->addValue(Attribute, Form, Value); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 419 | /// addDelta - Add a label delta attribute data and value. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 420 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 421 | void DwarfDebug::addDelta(DIE *Die, unsigned Attribute, unsigned Form, |
Chris Lattner | b98b1bf | 2010-03-08 22:23:36 +0000 | [diff] [blame] | 422 | const MCSymbol *Hi, const MCSymbol *Lo) { |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 423 | DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 424 | Die->addValue(Attribute, Form, Value); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Chris Lattner | 74e41f9 | 2010-04-05 05:24:55 +0000 | [diff] [blame] | 427 | /// addDIEEntry - Add a DIE attribute data and value. |
| 428 | /// |
| 429 | void DwarfDebug::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, |
| 430 | DIE *Entry) { |
| 431 | Die->addValue(Attribute, Form, createDIEEntry(Entry)); |
| 432 | } |
| 433 | |
| 434 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 435 | /// addBlock - Add block data. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 436 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 437 | void DwarfDebug::addBlock(DIE *Die, unsigned Attribute, unsigned Form, |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 438 | DIEBlock *Block) { |
Chris Lattner | a37d538 | 2010-04-05 00:18:22 +0000 | [diff] [blame] | 439 | Block->ComputeSize(Asm); |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 440 | DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 441 | Die->addValue(Attribute, Block->BestForm(), Block); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 444 | /// addSourceLine - Add location information to specified debug information |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 445 | /// entry. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 446 | void DwarfDebug::addSourceLine(DIE *Die, const DIVariable *V) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 447 | // If there is no compile unit specified, don't add a line #. |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 448 | if (!V->getCompileUnit().Verify()) |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 449 | return; |
| 450 | |
| 451 | unsigned Line = V->getLineNumber(); |
Devang Patel | 77bf295 | 2010-03-08 22:02:50 +0000 | [diff] [blame] | 452 | unsigned FileID = GetOrCreateSourceID(V->getContext().getDirectory(), |
| 453 | V->getContext().getFilename()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 454 | assert(FileID && "Invalid file id"); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 455 | addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 456 | addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 459 | /// addSourceLine - Add location information to specified debug information |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 460 | /// entry. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 461 | void DwarfDebug::addSourceLine(DIE *Die, const DIGlobal *G) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 462 | // If there is no compile unit specified, don't add a line #. |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 463 | if (!G->getCompileUnit().Verify()) |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 464 | return; |
| 465 | |
| 466 | unsigned Line = G->getLineNumber(); |
Devang Patel | 77bf295 | 2010-03-08 22:02:50 +0000 | [diff] [blame] | 467 | unsigned FileID = GetOrCreateSourceID(G->getContext().getDirectory(), |
| 468 | G->getContext().getFilename()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 469 | assert(FileID && "Invalid file id"); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 470 | addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 471 | addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 472 | } |
Devang Patel | 82dfc0c | 2009-08-31 22:47:13 +0000 | [diff] [blame] | 473 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 474 | /// addSourceLine - Add location information to specified debug information |
Devang Patel | 82dfc0c | 2009-08-31 22:47:13 +0000 | [diff] [blame] | 475 | /// entry. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 476 | void DwarfDebug::addSourceLine(DIE *Die, const DISubprogram *SP) { |
Devang Patel | 82dfc0c | 2009-08-31 22:47:13 +0000 | [diff] [blame] | 477 | // If there is no compile unit specified, don't add a line #. |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 478 | if (!SP->getCompileUnit().Verify()) |
Devang Patel | 82dfc0c | 2009-08-31 22:47:13 +0000 | [diff] [blame] | 479 | return; |
Caroline Tice | c6f9d62 | 2009-09-11 18:25:54 +0000 | [diff] [blame] | 480 | // If the line number is 0, don't add it. |
| 481 | if (SP->getLineNumber() == 0) |
| 482 | return; |
| 483 | |
Devang Patel | 82dfc0c | 2009-08-31 22:47:13 +0000 | [diff] [blame] | 484 | unsigned Line = SP->getLineNumber(); |
Devang Patel | 77bf295 | 2010-03-08 22:02:50 +0000 | [diff] [blame] | 485 | if (!SP->getContext().Verify()) |
| 486 | return; |
Devang Patel | 9bb59a2 | 2010-03-24 21:30:35 +0000 | [diff] [blame] | 487 | unsigned FileID = GetOrCreateSourceID(SP->getDirectory(), |
| 488 | SP->getFilename()); |
Devang Patel | 82dfc0c | 2009-08-31 22:47:13 +0000 | [diff] [blame] | 489 | assert(FileID && "Invalid file id"); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 490 | addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 491 | addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
Devang Patel | 82dfc0c | 2009-08-31 22:47:13 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 494 | /// addSourceLine - Add location information to specified debug information |
Devang Patel | 82dfc0c | 2009-08-31 22:47:13 +0000 | [diff] [blame] | 495 | /// entry. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 496 | void DwarfDebug::addSourceLine(DIE *Die, const DIType *Ty) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 497 | // If there is no compile unit specified, don't add a line #. |
| 498 | DICompileUnit CU = Ty->getCompileUnit(); |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 499 | if (!CU.Verify()) |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 500 | return; |
| 501 | |
| 502 | unsigned Line = Ty->getLineNumber(); |
Devang Patel | 77bf295 | 2010-03-08 22:02:50 +0000 | [diff] [blame] | 503 | if (!Ty->getContext().Verify()) |
| 504 | return; |
| 505 | unsigned FileID = GetOrCreateSourceID(Ty->getContext().getDirectory(), |
| 506 | Ty->getContext().getFilename()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 507 | assert(FileID && "Invalid file id"); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 508 | addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 509 | addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 512 | /// addSourceLine - Add location information to specified debug information |
| 513 | /// entry. |
| 514 | void DwarfDebug::addSourceLine(DIE *Die, const DINameSpace *NS) { |
| 515 | // If there is no compile unit specified, don't add a line #. |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 516 | if (!NS->getCompileUnit().Verify()) |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 517 | return; |
| 518 | |
| 519 | unsigned Line = NS->getLineNumber(); |
| 520 | StringRef FN = NS->getFilename(); |
| 521 | StringRef Dir = NS->getDirectory(); |
| 522 | |
| 523 | unsigned FileID = GetOrCreateSourceID(Dir, FN); |
| 524 | assert(FileID && "Invalid file id"); |
| 525 | addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 526 | addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
| 527 | } |
| 528 | |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 529 | /* Byref variables, in Blocks, are declared by the programmer as |
| 530 | "SomeType VarName;", but the compiler creates a |
| 531 | __Block_byref_x_VarName struct, and gives the variable VarName |
| 532 | either the struct, or a pointer to the struct, as its type. This |
| 533 | is necessary for various behind-the-scenes things the compiler |
| 534 | needs to do with by-reference variables in blocks. |
| 535 | |
| 536 | However, as far as the original *programmer* is concerned, the |
| 537 | variable should still have type 'SomeType', as originally declared. |
| 538 | |
| 539 | The following function dives into the __Block_byref_x_VarName |
| 540 | struct to find the original type of the variable. This will be |
| 541 | passed back to the code generating the type for the Debug |
| 542 | Information Entry for the variable 'VarName'. 'VarName' will then |
| 543 | have the original type 'SomeType' in its debug information. |
| 544 | |
| 545 | The original type 'SomeType' will be the type of the field named |
| 546 | 'VarName' inside the __Block_byref_x_VarName struct. |
| 547 | |
| 548 | NOTE: In order for this to not completely fail on the debugger |
| 549 | side, the Debug Information Entry for the variable VarName needs to |
| 550 | have a DW_AT_location that tells the debugger how to unwind through |
| 551 | the pointers and __Block_byref_x_VarName struct to find the actual |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 552 | value of the variable. The function addBlockByrefType does this. */ |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 553 | |
| 554 | /// Find the type the programmer originally declared the variable to be |
| 555 | /// and return that type. |
| 556 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 557 | DIType DwarfDebug::getBlockByrefType(DIType Ty, std::string Name) { |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 558 | |
| 559 | DIType subType = Ty; |
| 560 | unsigned tag = Ty.getTag(); |
| 561 | |
| 562 | if (tag == dwarf::DW_TAG_pointer_type) { |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 563 | DIDerivedType DTy = DIDerivedType(Ty.getNode()); |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 564 | subType = DTy.getTypeDerivedFrom(); |
| 565 | } |
| 566 | |
| 567 | DICompositeType blockStruct = DICompositeType(subType.getNode()); |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 568 | DIArray Elements = blockStruct.getTypeArray(); |
| 569 | |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 570 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
| 571 | DIDescriptor Element = Elements.getElement(i); |
| 572 | DIDerivedType DT = DIDerivedType(Element.getNode()); |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 573 | if (Name == DT.getName()) |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 574 | return (DT.getTypeDerivedFrom()); |
| 575 | } |
| 576 | |
| 577 | return Ty; |
| 578 | } |
| 579 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 580 | /// addComplexAddress - Start with the address based on the location provided, |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 581 | /// and generate the DWARF information necessary to find the actual variable |
| 582 | /// given the extra address information encoded in the DIVariable, starting from |
| 583 | /// the starting location. Add the DWARF information to the die. |
| 584 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 585 | void DwarfDebug::addComplexAddress(DbgVariable *&DV, DIE *Die, |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 586 | unsigned Attribute, |
| 587 | const MachineLocation &Location) { |
| 588 | const DIVariable &VD = DV->getVariable(); |
| 589 | DIType Ty = VD.getType(); |
| 590 | |
| 591 | // Decode the original location, and use that as the start of the byref |
| 592 | // variable's location. |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 593 | const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 594 | unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false); |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 595 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 596 | |
| 597 | if (Location.isReg()) { |
| 598 | if (Reg < 32) { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 599 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg); |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 600 | } else { |
| 601 | Reg = Reg - dwarf::DW_OP_reg0; |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 602 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); |
| 603 | addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 604 | } |
| 605 | } else { |
| 606 | if (Reg < 32) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 607 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 608 | else { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 609 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); |
| 610 | addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 613 | addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset()); |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | for (unsigned i = 0, N = VD.getNumAddrElements(); i < N; ++i) { |
| 617 | uint64_t Element = VD.getAddrElement(i); |
| 618 | |
| 619 | if (Element == DIFactory::OpPlus) { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 620 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 621 | addUInt(Block, 0, dwarf::DW_FORM_udata, VD.getAddrElement(++i)); |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 622 | } else if (Element == DIFactory::OpDeref) { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 623 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 624 | } else llvm_unreachable("unknown DIFactory Opcode"); |
| 625 | } |
| 626 | |
| 627 | // Now attach the location information to the DIE. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 628 | addBlock(Die, Attribute, 0, Block); |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 631 | /* Byref variables, in Blocks, are declared by the programmer as "SomeType |
| 632 | VarName;", but the compiler creates a __Block_byref_x_VarName struct, and |
| 633 | gives the variable VarName either the struct, or a pointer to the struct, as |
| 634 | its type. This is necessary for various behind-the-scenes things the |
| 635 | compiler needs to do with by-reference variables in Blocks. |
| 636 | |
| 637 | However, as far as the original *programmer* is concerned, the variable |
| 638 | should still have type 'SomeType', as originally declared. |
| 639 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 640 | The function getBlockByrefType dives into the __Block_byref_x_VarName |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 641 | struct to find the original type of the variable, which is then assigned to |
| 642 | the variable's Debug Information Entry as its real type. So far, so good. |
| 643 | However now the debugger will expect the variable VarName to have the type |
| 644 | 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] | 645 | expression that explains to the debugger how to navigate through the |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 646 | pointers and struct to find the actual variable of type SomeType. |
| 647 | |
| 648 | The following function does just that. We start by getting |
| 649 | the "normal" location for the variable. This will be the location |
| 650 | of either the struct __Block_byref_x_VarName or the pointer to the |
| 651 | struct __Block_byref_x_VarName. |
| 652 | |
| 653 | The struct will look something like: |
| 654 | |
| 655 | struct __Block_byref_x_VarName { |
| 656 | ... <various fields> |
| 657 | struct __Block_byref_x_VarName *forwarding; |
| 658 | ... <various other fields> |
| 659 | SomeType VarName; |
| 660 | ... <maybe more fields> |
| 661 | }; |
| 662 | |
| 663 | If we are given the struct directly (as our starting point) we |
| 664 | need to tell the debugger to: |
| 665 | |
| 666 | 1). Add the offset of the forwarding field. |
| 667 | |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 668 | 2). Follow that pointer to get the real __Block_byref_x_VarName |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 669 | struct to use (the real one may have been copied onto the heap). |
| 670 | |
| 671 | 3). Add the offset for the field VarName, to find the actual variable. |
| 672 | |
| 673 | If we started with a pointer to the struct, then we need to |
| 674 | dereference that pointer first, before the other steps. |
| 675 | Translating this into DWARF ops, we will need to append the following |
| 676 | to the current location description for the variable: |
| 677 | |
| 678 | DW_OP_deref -- optional, if we start with a pointer |
| 679 | DW_OP_plus_uconst <forward_fld_offset> |
| 680 | DW_OP_deref |
| 681 | DW_OP_plus_uconst <varName_fld_offset> |
| 682 | |
| 683 | That is what this function does. */ |
| 684 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 685 | /// addBlockByrefAddress - Start with the address based on the location |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 686 | /// provided, and generate the DWARF information necessary to find the |
| 687 | /// actual Block variable (navigating the Block struct) based on the |
| 688 | /// starting location. Add the DWARF information to the die. For |
| 689 | /// more information, read large comment just above here. |
| 690 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 691 | void DwarfDebug::addBlockByrefAddress(DbgVariable *&DV, DIE *Die, |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 692 | unsigned Attribute, |
| 693 | const MachineLocation &Location) { |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 694 | const DIVariable &VD = DV->getVariable(); |
| 695 | DIType Ty = VD.getType(); |
| 696 | DIType TmpTy = Ty; |
| 697 | unsigned Tag = Ty.getTag(); |
| 698 | bool isPointer = false; |
| 699 | |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 700 | StringRef varName = VD.getName(); |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 701 | |
| 702 | if (Tag == dwarf::DW_TAG_pointer_type) { |
Mike Stump | 3e4c9bd | 2009-09-30 00:08:22 +0000 | [diff] [blame] | 703 | DIDerivedType DTy = DIDerivedType(Ty.getNode()); |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 704 | TmpTy = DTy.getTypeDerivedFrom(); |
| 705 | isPointer = true; |
| 706 | } |
| 707 | |
| 708 | DICompositeType blockStruct = DICompositeType(TmpTy.getNode()); |
| 709 | |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 710 | // Find the __forwarding field and the variable field in the __Block_byref |
| 711 | // struct. |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 712 | DIArray Fields = blockStruct.getTypeArray(); |
| 713 | DIDescriptor varField = DIDescriptor(); |
| 714 | DIDescriptor forwardingField = DIDescriptor(); |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 715 | |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 716 | for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) { |
| 717 | DIDescriptor Element = Fields.getElement(i); |
| 718 | DIDerivedType DT = DIDerivedType(Element.getNode()); |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 719 | StringRef fieldName = DT.getName(); |
| 720 | if (fieldName == "__forwarding") |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 721 | forwardingField = Element; |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 722 | else if (fieldName == varName) |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 723 | varField = Element; |
| 724 | } |
Daniel Dunbar | f612ff6 | 2009-09-19 20:40:05 +0000 | [diff] [blame] | 725 | |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 726 | // Get the offsets for the forwarding field and the variable field. |
Chris Lattner | 1d65ba7 | 2010-03-31 06:06:37 +0000 | [diff] [blame] | 727 | unsigned forwardingFieldOffset = |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 728 | DIDerivedType(forwardingField.getNode()).getOffsetInBits() >> 3; |
Chris Lattner | 1d65ba7 | 2010-03-31 06:06:37 +0000 | [diff] [blame] | 729 | unsigned varFieldOffset = |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 730 | DIDerivedType(varField.getNode()).getOffsetInBits() >> 3; |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 731 | |
Mike Stump | 7e3720d | 2009-09-24 23:21:26 +0000 | [diff] [blame] | 732 | // Decode the original location, and use that as the start of the byref |
| 733 | // variable's location. |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 734 | const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 735 | unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false); |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 736 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
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 | if (Location.isReg()) { |
| 739 | if (Reg < 32) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 740 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg); |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 741 | else { |
| 742 | Reg = Reg - dwarf::DW_OP_reg0; |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 743 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); |
| 744 | addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 745 | } |
| 746 | } else { |
| 747 | if (Reg < 32) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 748 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 749 | else { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 750 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); |
| 751 | addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 752 | } |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 753 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 754 | addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset()); |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 755 | } |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 756 | |
Mike Stump | 7e3720d | 2009-09-24 23:21:26 +0000 | [diff] [blame] | 757 | // If we started with a pointer to the __Block_byref... struct, then |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 758 | // the first thing we need to do is dereference the pointer (DW_OP_deref). |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 759 | if (isPointer) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 760 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 761 | |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 762 | // Next add the offset for the '__forwarding' field: |
| 763 | // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in |
| 764 | // adding the offset if it's 0. |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 765 | if (forwardingFieldOffset > 0) { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 766 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 767 | addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset); |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 768 | } |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 769 | |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 770 | // Now dereference the __forwarding field to get to the real __Block_byref |
| 771 | // struct: DW_OP_deref. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 772 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 773 | |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 774 | // Now that we've got the real __Block_byref... struct, add the offset |
| 775 | // for the variable's field to get to the location of the actual variable: |
| 776 | // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0. |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 777 | if (varFieldOffset > 0) { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 778 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 779 | addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset); |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 780 | } |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 781 | |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 782 | // Now attach the location information to the DIE. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 783 | addBlock(Die, Attribute, 0, Block); |
Caroline Tice | dc8f604 | 2009-08-31 21:19:37 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 786 | /// addAddress - Add an address attribute to a die based on the location |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 787 | /// provided. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 788 | void DwarfDebug::addAddress(DIE *Die, unsigned Attribute, |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 789 | const MachineLocation &Location) { |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 790 | const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 791 | unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false); |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 792 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 793 | |
| 794 | if (Location.isReg()) { |
| 795 | if (Reg < 32) { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 796 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 797 | } else { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 798 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); |
| 799 | addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 800 | } |
| 801 | } else { |
| 802 | if (Reg < 32) { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 803 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 804 | } else { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 805 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); |
| 806 | addUInt(Block, 0, dwarf::DW_FORM_udata, Reg); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 809 | addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 812 | addBlock(Die, Attribute, 0, Block); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Devang Patel | a43098d | 2010-04-28 01:03:09 +0000 | [diff] [blame] | 815 | /// addRegisterAddress - Add register location entry in variable DIE. |
| 816 | bool DwarfDebug::addRegisterAddress(DIE *Die, DbgVariable *DV, |
| 817 | const MachineOperand &MO) { |
| 818 | assert (MO.isReg() && "Invalid machine operand!"); |
| 819 | if (!MO.getReg()) |
| 820 | return false; |
| 821 | MachineLocation Location; |
| 822 | Location.set(MO.getReg()); |
| 823 | addAddress(Die, dwarf::DW_AT_location, Location); |
| 824 | if (MCSymbol *VS = DV->getDbgValueLabel()) |
| 825 | addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS); |
| 826 | return true; |
| 827 | } |
| 828 | |
| 829 | /// addConstantValue - Add constant value entry in variable DIE. |
| 830 | bool DwarfDebug::addConstantValue(DIE *Die, DbgVariable *DV, |
| 831 | const MachineOperand &MO) { |
| 832 | assert (MO.isImm() && "Invalid machine operand!"); |
| 833 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
| 834 | unsigned Imm = MO.getImm(); |
| 835 | addUInt(Block, 0, dwarf::DW_FORM_udata, Imm); |
| 836 | addBlock(Die, dwarf::DW_AT_const_value, 0, Block); |
| 837 | if (MCSymbol *VS = DV->getDbgValueLabel()) |
| 838 | addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, VS); |
| 839 | return true; |
| 840 | } |
| 841 | |
| 842 | /// addConstantFPValue - Add constant value entry in variable DIE. |
| 843 | bool DwarfDebug::addConstantFPValue(DIE *Die, DbgVariable *DV, |
| 844 | const MachineOperand &MO) { |
| 845 | assert (MO.isFPImm() && "Invalid machine operand!"); |
| 846 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
| 847 | APFloat FPImm = MO.getFPImm()->getValueAPF(); |
| 848 | |
| 849 | // Get the raw data form of the floating point. |
| 850 | const APInt FltVal = FPImm.bitcastToAPInt(); |
| 851 | const char *FltPtr = (const char*)FltVal.getRawData(); |
| 852 | |
| 853 | int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. |
| 854 | bool LittleEndian = Asm->getTargetData().isLittleEndian(); |
| 855 | int Incr = (LittleEndian ? 1 : -1); |
| 856 | int Start = (LittleEndian ? 0 : NumBytes - 1); |
| 857 | int Stop = (LittleEndian ? NumBytes : -1); |
| 858 | |
| 859 | // Output the constant to DWARF one byte at a time. |
| 860 | for (; Start != Stop; Start += Incr) |
| 861 | addUInt(Block, 0, dwarf::DW_FORM_data1, |
| 862 | (unsigned char)0xFF & FltPtr[Start]); |
| 863 | |
| 864 | addBlock(Die, dwarf::DW_AT_const_value, 0, Block); |
| 865 | |
| 866 | if (MCSymbol *VS = DV->getDbgValueLabel()) |
| 867 | addLabel(Die, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, |
| 868 | VS); |
| 869 | return true; |
| 870 | } |
| 871 | |
| 872 | |
Devang Patel | c366f83 | 2009-12-10 19:14:49 +0000 | [diff] [blame] | 873 | /// addToContextOwner - Add Die into the list of its context owner's children. |
| 874 | void DwarfDebug::addToContextOwner(DIE *Die, DIDescriptor Context) { |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 875 | if (Context.isType()) { |
Devang Patel | c366f83 | 2009-12-10 19:14:49 +0000 | [diff] [blame] | 876 | DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context.getNode())); |
| 877 | ContextDIE->addChild(Die); |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 878 | } else if (Context.isNameSpace()) { |
| 879 | DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context.getNode())); |
| 880 | ContextDIE->addChild(Die); |
Devang Patel | c366f83 | 2009-12-10 19:14:49 +0000 | [diff] [blame] | 881 | } else if (DIE *ContextDIE = ModuleCU->getDIE(Context.getNode())) |
| 882 | ContextDIE->addChild(Die); |
| 883 | else |
| 884 | ModuleCU->addDie(Die); |
| 885 | } |
| 886 | |
Devang Patel | 16ced73 | 2009-12-10 18:05:33 +0000 | [diff] [blame] | 887 | /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the |
| 888 | /// given DIType. |
| 889 | DIE *DwarfDebug::getOrCreateTypeDIE(DIType Ty) { |
| 890 | DIE *TyDIE = ModuleCU->getDIE(Ty.getNode()); |
| 891 | if (TyDIE) |
| 892 | return TyDIE; |
| 893 | |
| 894 | // Create new type. |
| 895 | TyDIE = new DIE(dwarf::DW_TAG_base_type); |
| 896 | ModuleCU->insertDIE(Ty.getNode(), TyDIE); |
| 897 | if (Ty.isBasicType()) |
| 898 | constructTypeDIE(*TyDIE, DIBasicType(Ty.getNode())); |
| 899 | else if (Ty.isCompositeType()) |
| 900 | constructTypeDIE(*TyDIE, DICompositeType(Ty.getNode())); |
| 901 | else { |
| 902 | assert(Ty.isDerivedType() && "Unknown kind of DIType"); |
| 903 | constructTypeDIE(*TyDIE, DIDerivedType(Ty.getNode())); |
| 904 | } |
| 905 | |
Devang Patel | c366f83 | 2009-12-10 19:14:49 +0000 | [diff] [blame] | 906 | addToContextOwner(TyDIE, Ty.getContext()); |
Devang Patel | 16ced73 | 2009-12-10 18:05:33 +0000 | [diff] [blame] | 907 | return TyDIE; |
| 908 | } |
| 909 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 910 | /// addType - Add a new type attribute to the specified entity. |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 911 | void DwarfDebug::addType(DIE *Entity, DIType Ty) { |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 912 | if (!Ty.isValid()) |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 913 | return; |
| 914 | |
| 915 | // Check for pre-existence. |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 916 | DIEEntry *Entry = ModuleCU->getDIEEntry(Ty.getNode()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 917 | // If it exists then use the existing value. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 918 | if (Entry) { |
| 919 | Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 920 | return; |
| 921 | } |
| 922 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 923 | // Construct type. |
Devang Patel | 16ced73 | 2009-12-10 18:05:33 +0000 | [diff] [blame] | 924 | DIE *Buffer = getOrCreateTypeDIE(Ty); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 925 | |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 926 | // Set up proxy. |
| 927 | Entry = createDIEEntry(Buffer); |
| 928 | ModuleCU->insertDIEEntry(Ty.getNode(), Entry); |
| 929 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 930 | Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 933 | /// constructTypeDIE - Construct basic type die from DIBasicType. |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 934 | void DwarfDebug::constructTypeDIE(DIE &Buffer, DIBasicType BTy) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 935 | // Get core information. |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 936 | StringRef Name = BTy.getName(); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 937 | Buffer.setTag(dwarf::DW_TAG_base_type); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 938 | addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 939 | BTy.getEncoding()); |
| 940 | |
| 941 | // Add name if not anonymous or intermediate type. |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 942 | if (!Name.empty()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 943 | addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 944 | uint64_t Size = BTy.getSizeInBits() >> 3; |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 945 | addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 948 | /// constructTypeDIE - Construct derived type die from DIDerivedType. |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 949 | void DwarfDebug::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 950 | // Get core information. |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 951 | StringRef Name = DTy.getName(); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 952 | uint64_t Size = DTy.getSizeInBits() >> 3; |
| 953 | unsigned Tag = DTy.getTag(); |
| 954 | |
| 955 | // FIXME - Workaround for templates. |
| 956 | if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type; |
| 957 | |
| 958 | Buffer.setTag(Tag); |
| 959 | |
| 960 | // Map to main type, void will not have a type. |
| 961 | DIType FromTy = DTy.getTypeDerivedFrom(); |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 962 | addType(&Buffer, FromTy); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 963 | |
| 964 | // Add name if not anonymous or intermediate type. |
Devang Patel | deea564 | 2009-11-30 23:56:56 +0000 | [diff] [blame] | 965 | if (!Name.empty()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 966 | addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 967 | |
| 968 | // Add size if non-zero (derived types might be zero-sized.) |
| 969 | if (Size) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 970 | addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 971 | |
| 972 | // Add source line info if available and TyDesc is not a forward declaration. |
Devang Patel | 05f6fa8 | 2009-11-23 18:43:37 +0000 | [diff] [blame] | 973 | if (!DTy.isForwardDecl()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 974 | addSourceLine(&Buffer, &DTy); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 977 | /// constructTypeDIE - Construct type DIE from DICompositeType. |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 978 | void DwarfDebug::constructTypeDIE(DIE &Buffer, DICompositeType CTy) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 979 | // Get core information. |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 980 | StringRef Name = CTy.getName(); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 981 | |
| 982 | uint64_t Size = CTy.getSizeInBits() >> 3; |
| 983 | unsigned Tag = CTy.getTag(); |
| 984 | Buffer.setTag(Tag); |
| 985 | |
| 986 | switch (Tag) { |
| 987 | case dwarf::DW_TAG_vector_type: |
| 988 | case dwarf::DW_TAG_array_type: |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 989 | constructArrayTypeDIE(Buffer, &CTy); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 990 | break; |
| 991 | case dwarf::DW_TAG_enumeration_type: { |
| 992 | DIArray Elements = CTy.getTypeArray(); |
| 993 | |
| 994 | // Add enumerators to enumeration type. |
| 995 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
| 996 | DIE *ElemDie = NULL; |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 997 | DIDescriptor Enum(Elements.getElement(i).getNode()); |
| 998 | if (Enum.isEnumerator()) { |
| 999 | ElemDie = constructEnumTypeDIE(DIEnumerator(Enum.getNode())); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1000 | Buffer.addChild(ElemDie); |
Devang Patel | c525472 | 2009-10-09 17:51:49 +0000 | [diff] [blame] | 1001 | } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | break; |
| 1005 | case dwarf::DW_TAG_subroutine_type: { |
| 1006 | // Add return type. |
| 1007 | DIArray Elements = CTy.getTypeArray(); |
| 1008 | DIDescriptor RTy = Elements.getElement(0); |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1009 | addType(&Buffer, DIType(RTy.getNode())); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1010 | |
| 1011 | // Add prototype flag. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1012 | addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1013 | |
| 1014 | // Add arguments. |
| 1015 | for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) { |
| 1016 | DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); |
| 1017 | DIDescriptor Ty = Elements.getElement(i); |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1018 | addType(Arg, DIType(Ty.getNode())); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1019 | Buffer.addChild(Arg); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | break; |
| 1023 | case dwarf::DW_TAG_structure_type: |
| 1024 | case dwarf::DW_TAG_union_type: |
| 1025 | case dwarf::DW_TAG_class_type: { |
| 1026 | // Add elements to structure type. |
| 1027 | DIArray Elements = CTy.getTypeArray(); |
| 1028 | |
| 1029 | // A forward struct declared type may not have elements available. |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1030 | unsigned N = Elements.getNumElements(); |
| 1031 | if (N == 0) |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1032 | break; |
| 1033 | |
| 1034 | // Add elements to structure type. |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1035 | for (unsigned i = 0; i < N; ++i) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1036 | DIDescriptor Element = Elements.getElement(i); |
| 1037 | DIE *ElemDie = NULL; |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1038 | if (Element.isSubprogram()) |
Devang Patel | ffe966c | 2009-12-14 16:18:45 +0000 | [diff] [blame] | 1039 | ElemDie = createSubprogramDIE(DISubprogram(Element.getNode())); |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1040 | else if (Element.isVariable()) { |
Devang Patel | 1ee0cb9 | 2010-01-30 01:08:30 +0000 | [diff] [blame] | 1041 | DIVariable DV(Element.getNode()); |
| 1042 | ElemDie = new DIE(dwarf::DW_TAG_variable); |
| 1043 | addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, |
| 1044 | DV.getName()); |
| 1045 | addType(ElemDie, DV.getType()); |
| 1046 | addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); |
| 1047 | addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1); |
| 1048 | addSourceLine(ElemDie, &DV); |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1049 | } else if (Element.isDerivedType()) |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1050 | ElemDie = createMemberDIE(DIDerivedType(Element.getNode())); |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1051 | else |
| 1052 | continue; |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1053 | Buffer.addChild(ElemDie); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Devang Patel | a1ba269 | 2009-08-27 23:51:51 +0000 | [diff] [blame] | 1056 | if (CTy.isAppleBlockExtension()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1057 | addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1058 | |
| 1059 | unsigned RLang = CTy.getRunTimeLang(); |
| 1060 | if (RLang) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1061 | addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1062 | dwarf::DW_FORM_data1, RLang); |
Devang Patel | b554499 | 2010-01-26 21:16:06 +0000 | [diff] [blame] | 1063 | |
| 1064 | DICompositeType ContainingType = CTy.getContainingType(); |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1065 | if (DIDescriptor(ContainingType.getNode()).isCompositeType()) |
Devang Patel | b554499 | 2010-01-26 21:16:06 +0000 | [diff] [blame] | 1066 | addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, |
| 1067 | getOrCreateTypeDIE(DIType(ContainingType.getNode()))); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1068 | break; |
| 1069 | } |
| 1070 | default: |
| 1071 | break; |
| 1072 | } |
| 1073 | |
| 1074 | // Add name if not anonymous or intermediate type. |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1075 | if (!Name.empty()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1076 | addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1077 | |
Devang Patel | 8cbb612 | 2010-01-29 18:34:58 +0000 | [diff] [blame] | 1078 | if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type || |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1079 | Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) { |
| 1080 | // Add size if non-zero (derived types might be zero-sized.) |
| 1081 | if (Size) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1082 | addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1083 | else { |
| 1084 | // Add zero size if it is not a forward declaration. |
| 1085 | if (CTy.isForwardDecl()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1086 | addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1087 | else |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1088 | addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | // Add source line info if available. |
| 1092 | if (!CTy.isForwardDecl()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1093 | addSourceLine(&Buffer, &CTy); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1097 | /// constructSubrangeDIE - Construct subrange DIE from DISubrange. |
| 1098 | void DwarfDebug::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){ |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1099 | int64_t L = SR.getLo(); |
| 1100 | int64_t H = SR.getHi(); |
| 1101 | DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type); |
| 1102 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1103 | addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy); |
Devang Patel | 6325a53 | 2009-08-14 20:59:16 +0000 | [diff] [blame] | 1104 | if (L) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1105 | addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L); |
Devang Patel | d55224c | 2009-12-04 23:10:24 +0000 | [diff] [blame] | 1106 | addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1107 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1108 | Buffer.addChild(DW_Subrange); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1111 | /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1112 | void DwarfDebug::constructArrayTypeDIE(DIE &Buffer, |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1113 | DICompositeType *CTy) { |
| 1114 | Buffer.setTag(dwarf::DW_TAG_array_type); |
| 1115 | if (CTy->getTag() == dwarf::DW_TAG_vector_type) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1116 | addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1117 | |
| 1118 | // Emit derived type. |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1119 | addType(&Buffer, CTy->getTypeDerivedFrom()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1120 | DIArray Elements = CTy->getTypeArray(); |
| 1121 | |
Devang Patel | 6f01d9c | 2009-11-21 00:31:03 +0000 | [diff] [blame] | 1122 | // Get an anonymous type for index type. |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1123 | DIE *IdxTy = ModuleCU->getIndexTyDie(); |
Devang Patel | 6f01d9c | 2009-11-21 00:31:03 +0000 | [diff] [blame] | 1124 | if (!IdxTy) { |
| 1125 | // Construct an anonymous type for index type. |
| 1126 | IdxTy = new DIE(dwarf::DW_TAG_base_type); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1127 | addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t)); |
| 1128 | addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, |
Devang Patel | 6f01d9c | 2009-11-21 00:31:03 +0000 | [diff] [blame] | 1129 | dwarf::DW_ATE_signed); |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1130 | ModuleCU->addDie(IdxTy); |
| 1131 | ModuleCU->setIndexTyDie(IdxTy); |
Devang Patel | 6f01d9c | 2009-11-21 00:31:03 +0000 | [diff] [blame] | 1132 | } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1133 | |
| 1134 | // Add subranges to array type. |
| 1135 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
| 1136 | DIDescriptor Element = Elements.getElement(i); |
| 1137 | if (Element.getTag() == dwarf::DW_TAG_subrange_type) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1138 | constructSubrangeDIE(Buffer, DISubrange(Element.getNode()), IdxTy); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1142 | /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator. |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1143 | DIE *DwarfDebug::constructEnumTypeDIE(DIEnumerator ETy) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1144 | DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator); |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1145 | StringRef Name = ETy.getName(); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1146 | addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1147 | int64_t Value = ETy.getEnumValue(); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1148 | addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1149 | return Enumerator; |
| 1150 | } |
| 1151 | |
Devang Patel | 351ca33 | 2010-01-05 01:46:14 +0000 | [diff] [blame] | 1152 | /// getRealLinkageName - If special LLVM prefix that is used to inform the asm |
| 1153 | /// printer to not emit usual symbol prefix before the symbol name is used then |
| 1154 | /// return linkage name after skipping this special LLVM prefix. |
| 1155 | static StringRef getRealLinkageName(StringRef LinkageName) { |
| 1156 | char One = '\1'; |
| 1157 | if (LinkageName.startswith(StringRef(&One, 1))) |
| 1158 | return LinkageName.substr(1); |
| 1159 | return LinkageName; |
| 1160 | } |
| 1161 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1162 | /// createGlobalVariableDIE - Create new DIE using GV. |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1163 | DIE *DwarfDebug::createGlobalVariableDIE(const DIGlobalVariable &GV) { |
Jim Grosbach | 7ab38df | 2009-11-22 19:20:36 +0000 | [diff] [blame] | 1164 | // If the global variable was optmized out then no need to create debug info |
| 1165 | // entry. |
Devang Patel | 84c73e9 | 2009-11-06 17:58:12 +0000 | [diff] [blame] | 1166 | if (!GV.getGlobal()) return NULL; |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1167 | if (GV.getDisplayName().empty()) return NULL; |
Devang Patel | 465c3be | 2009-11-06 01:30:04 +0000 | [diff] [blame] | 1168 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1169 | DIE *GVDie = new DIE(dwarf::DW_TAG_variable); |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 1170 | addString(GVDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, |
Devang Patel | 5ccdd10 | 2009-09-29 18:40:58 +0000 | [diff] [blame] | 1171 | GV.getDisplayName()); |
| 1172 | |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1173 | StringRef LinkageName = GV.getLinkageName(); |
Devang Patel | 351ca33 | 2010-01-05 01:46:14 +0000 | [diff] [blame] | 1174 | if (!LinkageName.empty()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1175 | addString(GVDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string, |
Devang Patel | 351ca33 | 2010-01-05 01:46:14 +0000 | [diff] [blame] | 1176 | getRealLinkageName(LinkageName)); |
| 1177 | |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1178 | addType(GVDie, GV.getType()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1179 | if (!GV.isLocalToUnit()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1180 | addUInt(GVDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1); |
| 1181 | addSourceLine(GVDie, &GV); |
Devang Patel | b71a16d | 2009-10-05 23:22:08 +0000 | [diff] [blame] | 1182 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1183 | return GVDie; |
| 1184 | } |
| 1185 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1186 | /// createMemberDIE - Create new member DIE. |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1187 | DIE *DwarfDebug::createMemberDIE(const DIDerivedType &DT) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1188 | DIE *MemberDie = new DIE(DT.getTag()); |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1189 | StringRef Name = DT.getName(); |
| 1190 | if (!Name.empty()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1191 | addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1192 | |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1193 | addType(MemberDie, DT.getTypeDerivedFrom()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1194 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1195 | addSourceLine(MemberDie, &DT); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1196 | |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 1197 | DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock(); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1198 | addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
Devang Patel | 33db508 | 2009-11-04 22:06:12 +0000 | [diff] [blame] | 1199 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1200 | uint64_t Size = DT.getSizeInBits(); |
Devang Patel | 61ecbd1 | 2009-11-04 23:48:00 +0000 | [diff] [blame] | 1201 | uint64_t FieldSize = DT.getOriginalTypeSize(); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1202 | |
| 1203 | if (Size != FieldSize) { |
| 1204 | // Handle bitfield. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1205 | addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3); |
| 1206 | addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1207 | |
| 1208 | uint64_t Offset = DT.getOffsetInBits(); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1209 | uint64_t AlignMask = ~(DT.getAlignInBits() - 1); |
| 1210 | uint64_t HiMark = (Offset + FieldSize) & AlignMask; |
Benjamin Kramer | 3d594fd | 2010-01-07 17:50:57 +0000 | [diff] [blame] | 1211 | uint64_t FieldOffset = (HiMark - FieldSize); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1212 | Offset -= FieldOffset; |
| 1213 | |
| 1214 | // Maybe we need to work from the other end. |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1215 | if (Asm->getTargetData().isLittleEndian()) |
| 1216 | Offset = FieldSize - (Offset + Size); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1217 | addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1218 | |
Devang Patel | 33db508 | 2009-11-04 22:06:12 +0000 | [diff] [blame] | 1219 | // Here WD_AT_data_member_location points to the anonymous |
| 1220 | // field that includes this bit field. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1221 | addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3); |
Devang Patel | 33db508 | 2009-11-04 22:06:12 +0000 | [diff] [blame] | 1222 | |
| 1223 | } else |
| 1224 | // This is not a bitfield. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1225 | addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3); |
Devang Patel | 33db508 | 2009-11-04 22:06:12 +0000 | [diff] [blame] | 1226 | |
Devang Patel | c1dc8ff | 2010-02-03 20:08:48 +0000 | [diff] [blame] | 1227 | if (DT.getTag() == dwarf::DW_TAG_inheritance |
| 1228 | && DT.isVirtual()) { |
| 1229 | |
| 1230 | // For C++, virtual base classes are not at fixed offset. Use following |
| 1231 | // expression to extract appropriate offset from vtable. |
| 1232 | // BaseAddr = ObAddr + *((*ObAddr) - Offset) |
| 1233 | |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 1234 | DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock(); |
Devang Patel | c1dc8ff | 2010-02-03 20:08:48 +0000 | [diff] [blame] | 1235 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); |
| 1236 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
| 1237 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); |
| 1238 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits()); |
| 1239 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); |
| 1240 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
| 1241 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); |
| 1242 | |
| 1243 | addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, |
| 1244 | VBaseLocationDie); |
| 1245 | } else |
| 1246 | addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1247 | |
| 1248 | if (DT.isProtected()) |
Devang Patel | 5d11eb0 | 2009-12-03 19:11:07 +0000 | [diff] [blame] | 1249 | addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag, |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1250 | dwarf::DW_ACCESS_protected); |
| 1251 | else if (DT.isPrivate()) |
Devang Patel | 5d11eb0 | 2009-12-03 19:11:07 +0000 | [diff] [blame] | 1252 | addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag, |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1253 | dwarf::DW_ACCESS_private); |
Devang Patel | 5d11eb0 | 2009-12-03 19:11:07 +0000 | [diff] [blame] | 1254 | else if (DT.getTag() == dwarf::DW_TAG_inheritance) |
| 1255 | addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag, |
| 1256 | dwarf::DW_ACCESS_public); |
| 1257 | if (DT.isVirtual()) |
| 1258 | addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, |
| 1259 | dwarf::DW_VIRTUALITY_virtual); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1260 | return MemberDie; |
| 1261 | } |
| 1262 | |
Devang Patel | ffe966c | 2009-12-14 16:18:45 +0000 | [diff] [blame] | 1263 | /// createSubprogramDIE - Create new DIE using SP. |
| 1264 | DIE *DwarfDebug::createSubprogramDIE(const DISubprogram &SP, bool MakeDecl) { |
| 1265 | DIE *SPDie = ModuleCU->getDIE(SP.getNode()); |
| 1266 | if (SPDie) |
| 1267 | return SPDie; |
| 1268 | |
| 1269 | SPDie = new DIE(dwarf::DW_TAG_subprogram); |
Devang Patel | 1eac3e7 | 2010-03-02 17:58:15 +0000 | [diff] [blame] | 1270 | // Constructors and operators for anonymous aggregates do not have names. |
Devang Patel | 6b506cb | 2010-03-02 01:26:20 +0000 | [diff] [blame] | 1271 | if (!SP.getName().empty()) |
| 1272 | addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, SP.getName()); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1273 | |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1274 | StringRef LinkageName = SP.getLinkageName(); |
Devang Patel | 351ca33 | 2010-01-05 01:46:14 +0000 | [diff] [blame] | 1275 | if (!LinkageName.empty()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1276 | addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string, |
Devang Patel | 351ca33 | 2010-01-05 01:46:14 +0000 | [diff] [blame] | 1277 | getRealLinkageName(LinkageName)); |
| 1278 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1279 | addSourceLine(SPDie, &SP); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1280 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1281 | // Add prototyped tag, if C or ObjC. |
| 1282 | unsigned Lang = SP.getCompileUnit().getLanguage(); |
| 1283 | if (Lang == dwarf::DW_LANG_C99 || Lang == dwarf::DW_LANG_C89 || |
| 1284 | Lang == dwarf::DW_LANG_ObjC) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1285 | addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1286 | |
| 1287 | // Add Return Type. |
Devang Patel | 1d5cc1d | 2009-12-03 01:25:38 +0000 | [diff] [blame] | 1288 | DICompositeType SPTy = SP.getType(); |
| 1289 | DIArray Args = SPTy.getTypeArray(); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1290 | unsigned SPTag = SPTy.getTag(); |
Devang Patel | 5d11eb0 | 2009-12-03 19:11:07 +0000 | [diff] [blame] | 1291 | |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1292 | if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type) |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1293 | addType(SPDie, SPTy); |
Devang Patel | 1d5cc1d | 2009-12-03 01:25:38 +0000 | [diff] [blame] | 1294 | else |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1295 | addType(SPDie, DIType(Args.getElement(0).getNode())); |
Devang Patel | 1d5cc1d | 2009-12-03 01:25:38 +0000 | [diff] [blame] | 1296 | |
Devang Patel | 5d11eb0 | 2009-12-03 19:11:07 +0000 | [diff] [blame] | 1297 | unsigned VK = SP.getVirtuality(); |
| 1298 | if (VK) { |
| 1299 | addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK); |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 1300 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
Devang Patel | 5d11eb0 | 2009-12-03 19:11:07 +0000 | [diff] [blame] | 1301 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); |
| 1302 | addUInt(Block, 0, dwarf::DW_FORM_data1, SP.getVirtualIndex()); |
| 1303 | addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block); |
Devang Patel | 622b026 | 2010-01-19 06:19:05 +0000 | [diff] [blame] | 1304 | ContainingTypeMap.insert(std::make_pair(SPDie, |
| 1305 | SP.getContainingType().getNode())); |
Devang Patel | 5d11eb0 | 2009-12-03 19:11:07 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Devang Patel | ffe966c | 2009-12-14 16:18:45 +0000 | [diff] [blame] | 1308 | if (MakeDecl || !SP.isDefinition()) { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1309 | addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Add arguments. Do not add arguments for subprogram definition. They will |
Devang Patel | 1d5cc1d | 2009-12-03 01:25:38 +0000 | [diff] [blame] | 1312 | // be handled while processing variables. |
| 1313 | DICompositeType SPTy = SP.getType(); |
| 1314 | DIArray Args = SPTy.getTypeArray(); |
| 1315 | unsigned SPTag = SPTy.getTag(); |
| 1316 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1317 | if (SPTag == dwarf::DW_TAG_subroutine_type) |
| 1318 | for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { |
| 1319 | DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); |
Devang Patel | b464564 | 2010-02-06 01:02:37 +0000 | [diff] [blame] | 1320 | DIType ATy = DIType(DIType(Args.getElement(i).getNode())); |
| 1321 | addType(Arg, ATy); |
| 1322 | if (ATy.isArtificial()) |
| 1323 | addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1324 | SPDie->addChild(Arg); |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
Devang Patel | 4e0d19d | 2010-02-03 19:57:19 +0000 | [diff] [blame] | 1328 | if (SP.isArtificial()) |
| 1329 | addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); |
| 1330 | |
Devang Patel | ccff812 | 2010-04-30 19:38:23 +0000 | [diff] [blame] | 1331 | if (!SP.isLocalToUnit()) |
| 1332 | addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1); |
Devang Patel | 2a4a3b7 | 2010-04-19 19:14:02 +0000 | [diff] [blame] | 1333 | |
Devang Patel | ccff812 | 2010-04-30 19:38:23 +0000 | [diff] [blame] | 1334 | if (SP.isOptimized()) |
| 1335 | addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1); |
| 1336 | |
Devang Patel | ccff812 | 2010-04-30 19:38:23 +0000 | [diff] [blame] | 1337 | // DW_TAG_inlined_subroutine may refer to this DIE. |
| 1338 | ModuleCU->insertDIE(SP.getNode(), SPDie); |
| 1339 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 1340 | return SPDie; |
| 1341 | } |
| 1342 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1343 | DbgScope *DwarfDebug::getOrCreateAbstractScope(MDNode *N) { |
Chris Lattner | ed7a77b | 2010-03-31 05:36:29 +0000 | [diff] [blame] | 1344 | assert(N && "Invalid Scope encoding!"); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1345 | |
| 1346 | DbgScope *AScope = AbstractScopes.lookup(N); |
| 1347 | if (AScope) |
| 1348 | return AScope; |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 1349 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1350 | DbgScope *Parent = NULL; |
| 1351 | |
| 1352 | DIDescriptor Scope(N); |
| 1353 | if (Scope.isLexicalBlock()) { |
| 1354 | DILexicalBlock DB(N); |
| 1355 | DIDescriptor ParentDesc = DB.getContext(); |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1356 | Parent = getOrCreateAbstractScope(ParentDesc.getNode()); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | AScope = new DbgScope(Parent, DIDescriptor(N), NULL); |
| 1360 | |
| 1361 | if (Parent) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1362 | Parent->addScope(AScope); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1363 | AScope->setAbstractScope(); |
| 1364 | AbstractScopes[N] = AScope; |
| 1365 | if (DIDescriptor(N).isSubprogram()) |
| 1366 | AbstractScopesList.push_back(AScope); |
| 1367 | return AScope; |
| 1368 | } |
Devang Patel | af9e847 | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 1369 | |
Devang Patel | 5f09400 | 2010-04-06 23:53:48 +0000 | [diff] [blame] | 1370 | /// isSubprogramContext - Return true if Context is either a subprogram |
| 1371 | /// or another context nested inside a subprogram. |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 1372 | static bool isSubprogramContext(MDNode *Context) { |
Devang Patel | 5f09400 | 2010-04-06 23:53:48 +0000 | [diff] [blame] | 1373 | if (!Context) |
| 1374 | return false; |
| 1375 | DIDescriptor D(Context); |
| 1376 | if (D.isSubprogram()) |
| 1377 | return true; |
| 1378 | if (D.isType()) |
| 1379 | return isSubprogramContext(DIType(Context).getContext().getNode()); |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 1383 | /// updateSubprogramScopeDIE - Find DIE for the given subprogram and |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1384 | /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes. |
| 1385 | /// If there are global variables in this scope then create and insert |
| 1386 | /// DIEs for these variables. |
| 1387 | DIE *DwarfDebug::updateSubprogramScopeDIE(MDNode *SPNode) { |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1388 | DIE *SPDie = ModuleCU->getDIE(SPNode); |
| 1389 | assert(SPDie && "Unable to find subprogram DIE!"); |
| 1390 | DISubprogram SP(SPNode); |
Chris Lattner | 206d61e | 2010-03-13 07:26:18 +0000 | [diff] [blame] | 1391 | |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1392 | // There is not any need to generate specification DIE for a function |
| 1393 | // defined at compile unit level. If a function is defined inside another |
| 1394 | // function then gdb prefers the definition at top level and but does not |
| 1395 | // expect specification DIE in parent function. So avoid creating |
| 1396 | // specification DIE for a function defined inside a function. |
| 1397 | if (SP.isDefinition() && !SP.getContext().isCompileUnit() && |
Devang Patel | 5f09400 | 2010-04-06 23:53:48 +0000 | [diff] [blame] | 1398 | !SP.getContext().isFile() && |
| 1399 | !isSubprogramContext(SP.getContext().getNode())) { |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1400 | addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); |
| 1401 | |
| 1402 | // Add arguments. |
| 1403 | DICompositeType SPTy = SP.getType(); |
| 1404 | DIArray Args = SPTy.getTypeArray(); |
| 1405 | unsigned SPTag = SPTy.getTag(); |
| 1406 | if (SPTag == dwarf::DW_TAG_subroutine_type) |
| 1407 | for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { |
| 1408 | DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); |
| 1409 | DIType ATy = DIType(DIType(Args.getElement(i).getNode())); |
| 1410 | addType(Arg, ATy); |
| 1411 | if (ATy.isArtificial()) |
| 1412 | addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); |
| 1413 | SPDie->addChild(Arg); |
| 1414 | } |
| 1415 | DIE *SPDeclDie = SPDie; |
| 1416 | SPDie = new DIE(dwarf::DW_TAG_subprogram); |
| 1417 | addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4, |
| 1418 | SPDeclDie); |
| 1419 | ModuleCU->addDie(SPDie); |
| 1420 | } |
| 1421 | |
| 1422 | addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, |
| 1423 | Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber())); |
| 1424 | addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, |
| 1425 | Asm->GetTempSymbol("func_end", Asm->getFunctionNumber())); |
| 1426 | const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); |
| 1427 | MachineLocation Location(RI->getFrameRegister(*Asm->MF)); |
| 1428 | addAddress(SPDie, dwarf::DW_AT_frame_base, Location); |
Devang Patel | b464564 | 2010-02-06 01:02:37 +0000 | [diff] [blame] | 1429 | |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1430 | return SPDie; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 1433 | /// constructLexicalScope - Construct new DW_TAG_lexical_block |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1434 | /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels. |
| 1435 | DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) { |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1436 | |
| 1437 | DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block); |
| 1438 | if (Scope->isAbstractScope()) |
| 1439 | return ScopeDIE; |
| 1440 | |
| 1441 | const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges(); |
| 1442 | if (Ranges.empty()) |
| 1443 | return 0; |
| 1444 | |
| 1445 | SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(); |
| 1446 | if (Ranges.size() > 1) { |
| 1447 | // .debug_range section has not been laid out yet. Emit offset in |
| 1448 | // .debug_range as a uint, size 4, for now. emitDIE will handle |
| 1449 | // DW_AT_ranges appropriately. |
| 1450 | addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4, |
| 1451 | DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize()); |
| 1452 | for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(), |
| 1453 | RE = Ranges.end(); RI != RE; ++RI) { |
| 1454 | DebugRangeSymbols.push_back(LabelsBeforeInsn.lookup(RI->first)); |
| 1455 | DebugRangeSymbols.push_back(LabelsAfterInsn.lookup(RI->second)); |
| 1456 | } |
| 1457 | DebugRangeSymbols.push_back(NULL); |
| 1458 | DebugRangeSymbols.push_back(NULL); |
| 1459 | return ScopeDIE; |
| 1460 | } |
| 1461 | |
| 1462 | MCSymbol *Start = LabelsBeforeInsn.lookup(RI->first); |
| 1463 | MCSymbol *End = LabelsAfterInsn.lookup(RI->second); |
| 1464 | |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 1465 | if (Start == 0 || End == 0) return 0; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1466 | |
Chris Lattner | b7db733 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 1467 | assert(Start->isDefined() && "Invalid starting label for an inlined scope!"); |
| 1468 | assert(End->isDefined() && "Invalid end label for an inlined scope!"); |
Chris Lattner | a34ec229 | 2010-03-09 01:51:43 +0000 | [diff] [blame] | 1469 | |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1470 | addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start); |
| 1471 | addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1472 | |
| 1473 | return ScopeDIE; |
| 1474 | } |
| 1475 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1476 | /// constructInlinedScopeDIE - This scope represents inlined body of |
| 1477 | /// a function. Construct DIE to represent this concrete inlined copy |
| 1478 | /// of the function. |
| 1479 | DIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) { |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1480 | |
| 1481 | const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges(); |
| 1482 | assert (Ranges.empty() == false |
| 1483 | && "DbgScope does not have instruction markers!"); |
| 1484 | |
| 1485 | // FIXME : .debug_inlined section specification does not clearly state how |
| 1486 | // to emit inlined scope that is split into multiple instruction ranges. |
| 1487 | // For now, use first instruction range and emit low_pc/high_pc pair and |
| 1488 | // corresponding .debug_inlined section entry for this pair. |
| 1489 | SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(); |
| 1490 | MCSymbol *StartLabel = LabelsBeforeInsn.lookup(RI->first); |
| 1491 | MCSymbol *EndLabel = LabelsAfterInsn.lookup(RI->second); |
| 1492 | |
| 1493 | if (StartLabel == 0 || EndLabel == 0) { |
| 1494 | assert (0 && "Unexpected Start and End labels for a inlined scope!"); |
| 1495 | return 0; |
| 1496 | } |
Chris Lattner | b7db733 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 1497 | assert(StartLabel->isDefined() && |
Chris Lattner | a34ec229 | 2010-03-09 01:51:43 +0000 | [diff] [blame] | 1498 | "Invalid starting label for an inlined scope!"); |
Chris Lattner | b7db733 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 1499 | assert(EndLabel->isDefined() && |
Chris Lattner | a34ec229 | 2010-03-09 01:51:43 +0000 | [diff] [blame] | 1500 | "Invalid end label for an inlined scope!"); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1501 | |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1502 | if (!Scope->getScopeNode()) |
Devang Patel | 0ef3fa6 | 2010-03-08 19:20:38 +0000 | [diff] [blame] | 1503 | return NULL; |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1504 | DIScope DS(Scope->getScopeNode()); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1505 | DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine); |
| 1506 | |
| 1507 | DISubprogram InlinedSP = getDISubprogram(DS.getNode()); |
Devang Patel | 017d121 | 2009-11-20 21:37:22 +0000 | [diff] [blame] | 1508 | DIE *OriginDIE = ModuleCU->getDIE(InlinedSP.getNode()); |
Chris Lattner | ed7a77b | 2010-03-31 05:36:29 +0000 | [diff] [blame] | 1509 | assert(OriginDIE && "Unable to find Origin DIE!"); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1510 | addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1511 | dwarf::DW_FORM_ref4, OriginDIE); |
| 1512 | |
Chris Lattner | 6ed0f90 | 2010-03-09 00:31:02 +0000 | [diff] [blame] | 1513 | addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel); |
Chris Lattner | b7db733 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 1514 | addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1515 | |
| 1516 | InlinedSubprogramDIEs.insert(OriginDIE); |
| 1517 | |
| 1518 | // Track the start label for this inlined function. |
Devang Patel | 622b026 | 2010-01-19 06:19:05 +0000 | [diff] [blame] | 1519 | DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1520 | I = InlineInfo.find(InlinedSP.getNode()); |
| 1521 | |
| 1522 | if (I == InlineInfo.end()) { |
Chris Lattner | 6ed0f90 | 2010-03-09 00:31:02 +0000 | [diff] [blame] | 1523 | InlineInfo[InlinedSP.getNode()].push_back(std::make_pair(StartLabel, |
Jim Grosbach | 7ab38df | 2009-11-22 19:20:36 +0000 | [diff] [blame] | 1524 | ScopeDIE)); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1525 | InlinedSPNodes.push_back(InlinedSP.getNode()); |
| 1526 | } else |
Chris Lattner | 6ed0f90 | 2010-03-09 00:31:02 +0000 | [diff] [blame] | 1527 | I->second.push_back(std::make_pair(StartLabel, ScopeDIE)); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1528 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1529 | DILocation DL(Scope->getInlinedAt()); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1530 | addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, ModuleCU->getID()); |
| 1531 | addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber()); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1532 | |
| 1533 | return ScopeDIE; |
| 1534 | } |
| 1535 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1536 | |
| 1537 | /// constructVariableDIE - Construct a DIE for the given DbgVariable. |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1538 | DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) { |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1539 | // Get the descriptor. |
| 1540 | const DIVariable &VD = DV->getVariable(); |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1541 | StringRef Name = VD.getName(); |
| 1542 | if (Name.empty()) |
Devang Patel | 3fb6bd6 | 2009-11-13 02:25:26 +0000 | [diff] [blame] | 1543 | return NULL; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1544 | |
| 1545 | // Translate tag to proper Dwarf tag. The result variable is dropped for |
| 1546 | // now. |
| 1547 | unsigned Tag; |
| 1548 | switch (VD.getTag()) { |
| 1549 | case dwarf::DW_TAG_return_variable: |
| 1550 | return NULL; |
| 1551 | case dwarf::DW_TAG_arg_variable: |
| 1552 | Tag = dwarf::DW_TAG_formal_parameter; |
| 1553 | break; |
| 1554 | case dwarf::DW_TAG_auto_variable: // fall thru |
| 1555 | default: |
| 1556 | Tag = dwarf::DW_TAG_variable; |
| 1557 | break; |
| 1558 | } |
| 1559 | |
| 1560 | // Define variable debug information entry. |
| 1561 | DIE *VariableDie = new DIE(Tag); |
| 1562 | |
| 1563 | |
| 1564 | DIE *AbsDIE = NULL; |
| 1565 | if (DbgVariable *AV = DV->getAbstractVariable()) |
| 1566 | AbsDIE = AV->getDIE(); |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 1567 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1568 | if (AbsDIE) { |
| 1569 | DIScope DS(Scope->getScopeNode()); |
| 1570 | DISubprogram InlinedSP = getDISubprogram(DS.getNode()); |
Devang Patel | 017d121 | 2009-11-20 21:37:22 +0000 | [diff] [blame] | 1571 | DIE *OriginSPDIE = ModuleCU->getDIE(InlinedSP.getNode()); |
Daniel Dunbar | c032679 | 2009-11-11 03:09:50 +0000 | [diff] [blame] | 1572 | (void) OriginSPDIE; |
Chris Lattner | ed7a77b | 2010-03-31 05:36:29 +0000 | [diff] [blame] | 1573 | assert(OriginSPDIE && "Unable to find Origin DIE for the SP!"); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1574 | DIE *AbsDIE = DV->getAbstractVariable()->getDIE(); |
Chris Lattner | ed7a77b | 2010-03-31 05:36:29 +0000 | [diff] [blame] | 1575 | assert(AbsDIE && "Unable to find Origin DIE for the Variable!"); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1576 | addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1577 | dwarf::DW_FORM_ref4, AbsDIE); |
| 1578 | } |
| 1579 | else { |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1580 | addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name); |
| 1581 | addSourceLine(VariableDie, &VD); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1582 | |
| 1583 | // Add variable type. |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 1584 | // FIXME: isBlockByrefVariable should be reformulated in terms of complex |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1585 | // addresses instead. |
| 1586 | if (VD.isBlockByrefVariable()) |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1587 | addType(VariableDie, getBlockByrefType(VD.getType(), Name)); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1588 | else |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1589 | addType(VariableDie, VD.getType()); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | // Add variable address. |
| 1593 | if (!Scope->isAbstractScope()) { |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 1594 | // Check if variable is described by DBG_VALUE instruction. |
Devang Patel | a43098d | 2010-04-28 01:03:09 +0000 | [diff] [blame] | 1595 | if (const MachineInstr *DVInsn = DV->getDbgValue()) { |
| 1596 | bool updated = false; |
| 1597 | // FIXME : Handle getNumOperands != 3 |
| 1598 | if (DVInsn->getNumOperands() == 3) { |
| 1599 | if (DVInsn->getOperand(0).isReg()) |
| 1600 | updated = addRegisterAddress(VariableDie, DV, DVInsn->getOperand(0)); |
| 1601 | else if (DVInsn->getOperand(0).isImm()) |
| 1602 | updated = addConstantValue(VariableDie, DV, DVInsn->getOperand(0)); |
| 1603 | else if (DVInsn->getOperand(0).isFPImm()) |
| 1604 | updated = addConstantFPValue(VariableDie, DV, DVInsn->getOperand(0)); |
Devang Patel | 28ff35d | 2010-04-28 01:39:28 +0000 | [diff] [blame] | 1605 | } else { |
| 1606 | MachineLocation Location = Asm->getDebugValueLocation(DVInsn); |
| 1607 | if (Location.getReg()) { |
| 1608 | addAddress(VariableDie, dwarf::DW_AT_location, Location); |
| 1609 | if (MCSymbol *VS = DV->getDbgValueLabel()) |
| 1610 | addLabel(VariableDie, dwarf::DW_AT_start_scope, dwarf::DW_FORM_addr, |
| 1611 | VS); |
| 1612 | updated = true; |
| 1613 | } |
Devang Patel | a43098d | 2010-04-28 01:03:09 +0000 | [diff] [blame] | 1614 | } |
| 1615 | if (!updated) { |
| 1616 | // If variableDie is not updated then DBG_VALUE instruction does not |
| 1617 | // have valid variable info. |
| 1618 | delete VariableDie; |
| 1619 | return NULL; |
| 1620 | } |
| 1621 | } |
| 1622 | else { |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 1623 | MachineLocation Location; |
| 1624 | unsigned FrameReg; |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1625 | const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); |
| 1626 | int Offset = RI->getFrameIndexReference(*Asm->MF, DV->getFrameIndex(), |
Chris Lattner | 1d65ba7 | 2010-03-31 06:06:37 +0000 | [diff] [blame] | 1627 | FrameReg); |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 1628 | Location.set(FrameReg, Offset); |
| 1629 | |
| 1630 | if (VD.hasComplexAddress()) |
| 1631 | addComplexAddress(DV, VariableDie, dwarf::DW_AT_location, Location); |
| 1632 | else if (VD.isBlockByrefVariable()) |
| 1633 | addBlockByrefAddress(DV, VariableDie, dwarf::DW_AT_location, Location); |
| 1634 | else |
| 1635 | addAddress(VariableDie, dwarf::DW_AT_location, Location); |
| 1636 | } |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1637 | } |
Devang Patel | b464564 | 2010-02-06 01:02:37 +0000 | [diff] [blame] | 1638 | |
| 1639 | if (Tag == dwarf::DW_TAG_formal_parameter && VD.getType().isArtificial()) |
| 1640 | addUInt(VariableDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1641 | DV->setDIE(VariableDie); |
| 1642 | return VariableDie; |
| 1643 | |
| 1644 | } |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1645 | |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1646 | void DwarfDebug::addPubTypes(DISubprogram SP) { |
| 1647 | DICompositeType SPTy = SP.getType(); |
| 1648 | unsigned SPTag = SPTy.getTag(); |
| 1649 | if (SPTag != dwarf::DW_TAG_subroutine_type) |
| 1650 | return; |
| 1651 | |
| 1652 | DIArray Args = SPTy.getTypeArray(); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1653 | for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) { |
| 1654 | DIType ATy(Args.getElement(i).getNode()); |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1655 | if (!ATy.isValid()) |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1656 | continue; |
| 1657 | DICompositeType CATy = getDICompositeType(ATy); |
Devang Patel | 50d80e3 | 2010-04-13 20:35:04 +0000 | [diff] [blame] | 1658 | if (DIDescriptor(CATy.getNode()).Verify() && !CATy.getName().empty() |
| 1659 | && !CATy.isForwardDecl()) { |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1660 | if (DIEEntry *Entry = ModuleCU->getDIEEntry(CATy.getNode())) |
| 1661 | ModuleCU->addGlobalType(CATy.getName(), Entry->getEntry()); |
| 1662 | } |
| 1663 | } |
| 1664 | } |
| 1665 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1666 | /// constructScopeDIE - Construct a DIE for this scope. |
| 1667 | DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) { |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1668 | if (!Scope || !Scope->getScopeNode()) |
| 1669 | return NULL; |
| 1670 | |
| 1671 | DIScope DS(Scope->getScopeNode()); |
| 1672 | DIE *ScopeDIE = NULL; |
| 1673 | if (Scope->getInlinedAt()) |
| 1674 | ScopeDIE = constructInlinedScopeDIE(Scope); |
| 1675 | else if (DS.isSubprogram()) { |
| 1676 | if (Scope->isAbstractScope()) |
| 1677 | ScopeDIE = ModuleCU->getDIE(DS.getNode()); |
| 1678 | else |
| 1679 | ScopeDIE = updateSubprogramScopeDIE(DS.getNode()); |
| 1680 | } |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 1681 | else |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1682 | ScopeDIE = constructLexicalScopeDIE(Scope); |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 1683 | if (!ScopeDIE) return NULL; |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 1684 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1685 | // Add variables to scope. |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 1686 | const SmallVector<DbgVariable *, 8> &Variables = Scope->getVariables(); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1687 | for (unsigned i = 0, N = Variables.size(); i < N; ++i) { |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1688 | DIE *VariableDIE = constructVariableDIE(Variables[i], Scope); |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 1689 | if (VariableDIE) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1690 | ScopeDIE->addChild(VariableDIE); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | // Add nested scopes. |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 1694 | const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes(); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1695 | for (unsigned j = 0, M = Scopes.size(); j < M; ++j) { |
| 1696 | // Define the Scope debug information entry. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1697 | DIE *NestedDIE = constructScopeDIE(Scopes[j]); |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 1698 | if (NestedDIE) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1699 | ScopeDIE->addChild(NestedDIE); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1700 | } |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1701 | |
| 1702 | if (DS.isSubprogram()) |
| 1703 | addPubTypes(DISubprogram(DS.getNode())); |
| 1704 | |
| 1705 | return ScopeDIE; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1708 | /// GetOrCreateSourceID - Look up the source id with the given directory and |
| 1709 | /// source file names. If none currently exists, create a new id and insert it |
| 1710 | /// in the SourceIds map. This can update DirectoryNames and SourceFileNames |
| 1711 | /// maps as well. |
Chris Lattner | 1d65ba7 | 2010-03-31 06:06:37 +0000 | [diff] [blame] | 1712 | unsigned DwarfDebug::GetOrCreateSourceID(StringRef DirName, StringRef FileName){ |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1713 | unsigned DId; |
| 1714 | StringMap<unsigned>::iterator DI = DirectoryIdMap.find(DirName); |
| 1715 | if (DI != DirectoryIdMap.end()) { |
| 1716 | DId = DI->getValue(); |
| 1717 | } else { |
| 1718 | DId = DirectoryNames.size() + 1; |
| 1719 | DirectoryIdMap[DirName] = DId; |
| 1720 | DirectoryNames.push_back(DirName); |
| 1721 | } |
| 1722 | |
| 1723 | unsigned FId; |
| 1724 | StringMap<unsigned>::iterator FI = SourceFileIdMap.find(FileName); |
| 1725 | if (FI != SourceFileIdMap.end()) { |
| 1726 | FId = FI->getValue(); |
| 1727 | } else { |
| 1728 | FId = SourceFileNames.size() + 1; |
| 1729 | SourceFileIdMap[FileName] = FId; |
| 1730 | SourceFileNames.push_back(FileName); |
| 1731 | } |
| 1732 | |
| 1733 | DenseMap<std::pair<unsigned, unsigned>, unsigned>::iterator SI = |
| 1734 | SourceIdMap.find(std::make_pair(DId, FId)); |
| 1735 | if (SI != SourceIdMap.end()) |
| 1736 | return SI->second; |
| 1737 | |
| 1738 | unsigned SrcId = SourceIds.size() + 1; // DW_AT_decl_file cannot be 0. |
| 1739 | SourceIdMap[std::make_pair(DId, FId)] = SrcId; |
| 1740 | SourceIds.push_back(std::make_pair(DId, FId)); |
| 1741 | |
| 1742 | return SrcId; |
| 1743 | } |
| 1744 | |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 1745 | /// getOrCreateNameSpace - Create a DIE for DINameSpace. |
| 1746 | DIE *DwarfDebug::getOrCreateNameSpace(DINameSpace NS) { |
| 1747 | DIE *NDie = ModuleCU->getDIE(NS.getNode()); |
| 1748 | if (NDie) |
| 1749 | return NDie; |
| 1750 | NDie = new DIE(dwarf::DW_TAG_namespace); |
| 1751 | ModuleCU->insertDIE(NS.getNode(), NDie); |
| 1752 | if (!NS.getName().empty()) |
| 1753 | addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName()); |
| 1754 | addSourceLine(NDie, &NS); |
| 1755 | addToContextOwner(NDie, NS.getContext()); |
| 1756 | return NDie; |
| 1757 | } |
| 1758 | |
Jeffrey Yasskin | d0f393d | 2010-03-11 18:29:55 +0000 | [diff] [blame] | 1759 | void DwarfDebug::constructCompileUnit(MDNode *N) { |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 1760 | DICompileUnit DIUnit(N); |
Jeffrey Yasskin | d0f393d | 2010-03-11 18:29:55 +0000 | [diff] [blame] | 1761 | // Use first compile unit marked as isMain as the compile unit for this |
| 1762 | // module. |
| 1763 | if (ModuleCU || !DIUnit.isMain()) |
| 1764 | return; |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1765 | StringRef FN = DIUnit.getFilename(); |
| 1766 | StringRef Dir = DIUnit.getDirectory(); |
Devang Patel | 5ccdd10 | 2009-09-29 18:40:58 +0000 | [diff] [blame] | 1767 | unsigned ID = GetOrCreateSourceID(Dir, FN); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1768 | |
| 1769 | DIE *Die = new DIE(dwarf::DW_TAG_compile_unit); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1770 | addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string, |
Devang Patel | 5ccdd10 | 2009-09-29 18:40:58 +0000 | [diff] [blame] | 1771 | DIUnit.getProducer()); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1772 | addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data1, |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1773 | DIUnit.getLanguage()); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1774 | addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN); |
Devang Patel | 5098da0 | 2010-04-26 22:54:28 +0000 | [diff] [blame] | 1775 | // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This |
| 1776 | // simplifies debug range entries. |
| 1777 | addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_data4, 0); |
Devang Patel | 4a602ca | 2010-03-22 23:11:36 +0000 | [diff] [blame] | 1778 | // DW_AT_stmt_list is a offset of line number information for this |
| 1779 | // compile unit in debug_line section. It is always zero when only one |
| 1780 | // compile unit is emitted in one object file. |
| 1781 | addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1782 | |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1783 | if (!Dir.empty()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1784 | addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1785 | if (DIUnit.isOptimized()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1786 | addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1787 | |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1788 | StringRef Flags = DIUnit.getFlags(); |
| 1789 | if (!Flags.empty()) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1790 | addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1791 | |
| 1792 | unsigned RVer = DIUnit.getRunTimeVersion(); |
| 1793 | if (RVer) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1794 | addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1795 | dwarf::DW_FORM_data1, RVer); |
| 1796 | |
Jeffrey Yasskin | d0f393d | 2010-03-11 18:29:55 +0000 | [diff] [blame] | 1797 | assert(!ModuleCU && |
| 1798 | "ModuleCU assigned since the top of constructCompileUnit"); |
| 1799 | ModuleCU = new CompileUnit(ID, Die); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1802 | void DwarfDebug::constructGlobalVariableDIE(MDNode *N) { |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 1803 | DIGlobalVariable DI_GV(N); |
Daniel Dunbar | f612ff6 | 2009-09-19 20:40:05 +0000 | [diff] [blame] | 1804 | |
Devang Patel | 905cf5e | 2009-09-04 23:59:07 +0000 | [diff] [blame] | 1805 | // If debug information is malformed then ignore it. |
| 1806 | if (DI_GV.Verify() == false) |
| 1807 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1808 | |
| 1809 | // Check for pre-existence. |
Devang Patel | 017d121 | 2009-11-20 21:37:22 +0000 | [diff] [blame] | 1810 | if (ModuleCU->getDIE(DI_GV.getNode())) |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1811 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1812 | |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1813 | DIE *VariableDie = createGlobalVariableDIE(DI_GV); |
Devang Patel | edb4563 | 2009-12-10 23:25:41 +0000 | [diff] [blame] | 1814 | if (!VariableDie) |
| 1815 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1816 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1817 | // Add to map. |
Devang Patel | 017d121 | 2009-11-20 21:37:22 +0000 | [diff] [blame] | 1818 | ModuleCU->insertDIE(N, VariableDie); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1819 | |
| 1820 | // Add to context owner. |
Devang Patel | c9b16cc | 2010-01-15 01:12:22 +0000 | [diff] [blame] | 1821 | DIDescriptor GVContext = DI_GV.getContext(); |
| 1822 | // Do not create specification DIE if context is either compile unit |
| 1823 | // or a subprogram. |
Chris Lattner | 1d65ba7 | 2010-03-31 06:06:37 +0000 | [diff] [blame] | 1824 | if (DI_GV.isDefinition() && !GVContext.isCompileUnit() && |
Devang Patel | 5f09400 | 2010-04-06 23:53:48 +0000 | [diff] [blame] | 1825 | !GVContext.isFile() && |
| 1826 | !isSubprogramContext(GVContext.getNode())) { |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 1827 | // Create specification DIE. |
| 1828 | DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable); |
| 1829 | addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, |
| 1830 | dwarf::DW_FORM_ref4, VariableDie); |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 1831 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 1832 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); |
Chris Lattner | 4faf59a | 2010-03-08 22:31:46 +0000 | [diff] [blame] | 1833 | addLabel(Block, 0, dwarf::DW_FORM_udata, |
Chris Lattner | deb0cba | 2010-03-12 21:09:07 +0000 | [diff] [blame] | 1834 | Asm->Mang->getSymbol(DI_GV.getGlobal())); |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 1835 | addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block); |
Devang Patel | 8581e01 | 2010-02-09 01:58:33 +0000 | [diff] [blame] | 1836 | addUInt(VariableDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 1837 | ModuleCU->addDie(VariableSpecDIE); |
| 1838 | } else { |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 1839 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 1840 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); |
Chris Lattner | 4faf59a | 2010-03-08 22:31:46 +0000 | [diff] [blame] | 1841 | addLabel(Block, 0, dwarf::DW_FORM_udata, |
Chris Lattner | deb0cba | 2010-03-12 21:09:07 +0000 | [diff] [blame] | 1842 | Asm->Mang->getSymbol(DI_GV.getGlobal())); |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 1843 | addBlock(VariableDie, dwarf::DW_AT_location, 0, Block); |
| 1844 | } |
Devang Patel | c9b16cc | 2010-01-15 01:12:22 +0000 | [diff] [blame] | 1845 | addToContextOwner(VariableDie, GVContext); |
Devang Patel | c366f83 | 2009-12-10 19:14:49 +0000 | [diff] [blame] | 1846 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1847 | // Expose as global. FIXME - need to check external flag. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1848 | ModuleCU->addGlobal(DI_GV.getName(), VariableDie); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1849 | |
| 1850 | DIType GTy = DI_GV.getType(); |
Devang Patel | 50d80e3 | 2010-04-13 20:35:04 +0000 | [diff] [blame] | 1851 | if (GTy.isCompositeType() && !GTy.getName().empty() |
| 1852 | && !GTy.isForwardDecl()) { |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1853 | DIEEntry *Entry = ModuleCU->getDIEEntry(GTy.getNode()); |
Chris Lattner | ed7a77b | 2010-03-31 05:36:29 +0000 | [diff] [blame] | 1854 | assert(Entry && "Missing global type!"); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1855 | ModuleCU->addGlobalType(GTy.getName(), Entry->getEntry()); |
| 1856 | } |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1857 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1860 | void DwarfDebug::constructSubprogramDIE(MDNode *N) { |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 1861 | DISubprogram SP(N); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1862 | |
Stuart Hastings | 639336e | 2010-04-06 21:38:29 +0000 | [diff] [blame] | 1863 | // Check for pre-existence. |
| 1864 | if (ModuleCU->getDIE(N)) |
| 1865 | return; |
| 1866 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1867 | if (!SP.isDefinition()) |
| 1868 | // This is a method declaration which will be handled while constructing |
| 1869 | // class type. |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1870 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1871 | |
Stuart Hastings | 639336e | 2010-04-06 21:38:29 +0000 | [diff] [blame] | 1872 | DIE *SubprogramDie = createSubprogramDIE(SP); |
| 1873 | |
| 1874 | // Add to map. |
| 1875 | ModuleCU->insertDIE(N, SubprogramDie); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Add to context owner. |
Devang Patel | 6404e4e | 2009-12-15 19:16:48 +0000 | [diff] [blame] | 1878 | addToContextOwner(SubprogramDie, SP.getContext()); |
Devang Patel | 0000fad | 2009-12-08 23:21:45 +0000 | [diff] [blame] | 1879 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1880 | // Expose as global. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1881 | ModuleCU->addGlobal(SP.getName(), SubprogramDie); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1882 | |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1883 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1886 | /// beginModule - Emit all Dwarf sections that should come prior to the |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 1887 | /// content. Create global DIEs and emit initial debug info sections. |
| 1888 | /// This is inovked by the target AsmPrinter. |
Chris Lattner | 75f5072 | 2010-04-04 07:48:20 +0000 | [diff] [blame] | 1889 | void DwarfDebug::beginModule(Module *M) { |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1890 | if (DisableDebugInfoPrinting) |
| 1891 | return; |
| 1892 | |
Devang Patel | 78ab9e2 | 2009-07-30 18:56:46 +0000 | [diff] [blame] | 1893 | DebugInfoFinder DbgFinder; |
| 1894 | DbgFinder.processModule(*M); |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1895 | |
Chris Lattner | d850ac7 | 2010-04-05 02:19:28 +0000 | [diff] [blame] | 1896 | bool HasDebugInfo = false; |
| 1897 | |
| 1898 | // Scan all the compile-units to see if there are any marked as the main unit. |
| 1899 | // if not, we do not generate debug info. |
| 1900 | for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(), |
| 1901 | E = DbgFinder.compile_unit_end(); I != E; ++I) { |
| 1902 | if (DICompileUnit(*I).isMain()) { |
| 1903 | HasDebugInfo = true; |
| 1904 | break; |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | if (!HasDebugInfo) return; |
| 1909 | |
| 1910 | // Tell MMI that we have debug info. |
| 1911 | MMI->setDebugInfoAvailability(true); |
| 1912 | |
Chris Lattner | be15beb | 2010-04-04 23:17:54 +0000 | [diff] [blame] | 1913 | // Emit initial sections. |
Chris Lattner | d850ac7 | 2010-04-05 02:19:28 +0000 | [diff] [blame] | 1914 | EmitSectionLabels(); |
Chris Lattner | be15beb | 2010-04-04 23:17:54 +0000 | [diff] [blame] | 1915 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1916 | // Create all the compile unit DIEs. |
Devang Patel | 78ab9e2 | 2009-07-30 18:56:46 +0000 | [diff] [blame] | 1917 | for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(), |
| 1918 | E = DbgFinder.compile_unit_end(); I != E; ++I) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1919 | constructCompileUnit(*I); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1920 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1921 | // Create DIEs for each subprogram. |
Devang Patel | 78ab9e2 | 2009-07-30 18:56:46 +0000 | [diff] [blame] | 1922 | for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(), |
| 1923 | E = DbgFinder.subprogram_end(); I != E; ++I) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1924 | constructSubprogramDIE(*I); |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 1925 | |
Devang Patel | c366f83 | 2009-12-10 19:14:49 +0000 | [diff] [blame] | 1926 | // Create DIEs for each global variable. |
| 1927 | for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(), |
| 1928 | E = DbgFinder.global_variable_end(); I != E; ++I) |
| 1929 | constructGlobalVariableDIE(*I); |
| 1930 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1931 | // Prime section data. |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 1932 | SectionMap.insert(Asm->getObjFileLowering().getTextSection()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1933 | |
| 1934 | // Print out .file directives to specify files for .loc directives. These are |
| 1935 | // printed out early so that they precede any .loc directives. |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1936 | if (Asm->MAI->hasDotLocAndDotFile()) { |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1937 | for (unsigned i = 1, e = getNumSourceIds()+1; i != e; ++i) { |
| 1938 | // Remember source id starts at 1. |
| 1939 | std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(i); |
Chris Lattner | 0ad9c91 | 2010-01-22 22:09:00 +0000 | [diff] [blame] | 1940 | // FIXME: don't use sys::path for this! This should not depend on the |
| 1941 | // host. |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1942 | sys::Path FullPath(getSourceDirectoryName(Id.first)); |
| 1943 | bool AppendOk = |
| 1944 | FullPath.appendComponent(getSourceFileName(Id.second)); |
| 1945 | assert(AppendOk && "Could not append filename to directory!"); |
| 1946 | AppendOk = false; |
Chris Lattner | a6594fc | 2010-01-25 18:58:59 +0000 | [diff] [blame] | 1947 | Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1948 | } |
| 1949 | } |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1952 | /// endModule - Emit all Dwarf sections that should come after the content. |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1953 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1954 | void DwarfDebug::endModule() { |
Bill Wendling | 5f017e8 | 2010-04-07 09:28:04 +0000 | [diff] [blame] | 1955 | if (!ModuleCU) return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1956 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1957 | // Attach DW_AT_inline attribute with inlined subprogram DIEs. |
| 1958 | for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(), |
| 1959 | AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) { |
| 1960 | DIE *ISP = *AI; |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1961 | addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Devang Patel | 622b026 | 2010-01-19 06:19:05 +0000 | [diff] [blame] | 1964 | for (DenseMap<DIE *, MDNode *>::iterator CI = ContainingTypeMap.begin(), |
Devang Patel | 5d11eb0 | 2009-12-03 19:11:07 +0000 | [diff] [blame] | 1965 | CE = ContainingTypeMap.end(); CI != CE; ++CI) { |
| 1966 | DIE *SPDie = CI->first; |
| 1967 | MDNode *N = dyn_cast_or_null<MDNode>(CI->second); |
| 1968 | if (!N) continue; |
| 1969 | DIE *NDie = ModuleCU->getDIE(N); |
| 1970 | if (!NDie) continue; |
| 1971 | addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie); |
Devang Patel | 5d11eb0 | 2009-12-03 19:11:07 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1974 | // Standard sections final addresses. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1975 | Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection()); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 1976 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end")); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1977 | Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection()); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 1978 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end")); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1979 | |
| 1980 | // End text sections. |
| 1981 | for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1982 | Asm->OutStreamer.SwitchSection(SectionMap[i]); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 1983 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i)); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | // Emit common frame information. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1987 | emitCommonDebugFrame(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1988 | |
| 1989 | // Emit function debug frame information |
| 1990 | for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(), |
| 1991 | E = DebugFrames.end(); I != E; ++I) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1992 | emitFunctionDebugFrame(*I); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1993 | |
| 1994 | // Compute DIE offsets and sizes. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1995 | computeSizeAndOffsets(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1996 | |
| 1997 | // Emit all the DIEs into a debug info section |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1998 | emitDebugInfo(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1999 | |
| 2000 | // Corresponding abbreviations into a abbrev section. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2001 | emitAbbreviations(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2002 | |
| 2003 | // Emit source line correspondence into a debug line section. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2004 | emitDebugLines(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2005 | |
| 2006 | // Emit info into a debug pubnames section. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2007 | emitDebugPubNames(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2008 | |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 2009 | // Emit info into a debug pubtypes section. |
| 2010 | emitDebugPubTypes(); |
| 2011 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2012 | // Emit info into a debug loc section. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2013 | emitDebugLoc(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2014 | |
| 2015 | // Emit info into a debug aranges section. |
| 2016 | EmitDebugARanges(); |
| 2017 | |
| 2018 | // Emit info into a debug ranges section. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2019 | emitDebugRanges(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2020 | |
| 2021 | // Emit info into a debug macinfo section. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2022 | emitDebugMacInfo(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2023 | |
| 2024 | // Emit inline info. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2025 | emitDebugInlineInfo(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2026 | |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 2027 | // Emit info into a debug str section. |
| 2028 | emitDebugStr(); |
| 2029 | |
Jeffrey Yasskin | d0f393d | 2010-03-11 18:29:55 +0000 | [diff] [blame] | 2030 | delete ModuleCU; |
| 2031 | ModuleCU = NULL; // Reset for the next Module, if any. |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2034 | /// findAbstractVariable - Find abstract variable, if any, associated with Var. |
Jim Grosbach | 7ab38df | 2009-11-22 19:20:36 +0000 | [diff] [blame] | 2035 | DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var, |
| 2036 | unsigned FrameIdx, |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2037 | DebugLoc ScopeLoc) { |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2038 | |
| 2039 | DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode()); |
| 2040 | if (AbsDbgVariable) |
| 2041 | return AbsDbgVariable; |
| 2042 | |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2043 | LLVMContext &Ctx = Var.getNode()->getContext(); |
| 2044 | DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx)); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2045 | if (!Scope) |
| 2046 | return NULL; |
| 2047 | |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 2048 | AbsDbgVariable = new DbgVariable(Var, FrameIdx, |
| 2049 | NULL /* No more-abstract variable*/); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2050 | Scope->addVariable(AbsDbgVariable); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2051 | AbstractVariables[Var.getNode()] = AbsDbgVariable; |
| 2052 | return AbsDbgVariable; |
| 2053 | } |
| 2054 | |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2055 | /// findAbstractVariable - Find abstract variable, if any, associated with Var. |
| 2056 | /// FIXME : Refactor findAbstractVariable. |
| 2057 | DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var, |
| 2058 | const MachineInstr *MI, |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2059 | DebugLoc ScopeLoc) { |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2060 | |
| 2061 | DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var.getNode()); |
| 2062 | if (AbsDbgVariable) |
| 2063 | return AbsDbgVariable; |
| 2064 | |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2065 | LLVMContext &Ctx = Var.getNode()->getContext(); |
| 2066 | DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx)); |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2067 | if (!Scope) |
| 2068 | return NULL; |
| 2069 | |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 2070 | AbsDbgVariable = new DbgVariable(Var, MI, |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2071 | NULL /* No more-abstract variable*/); |
| 2072 | Scope->addVariable(AbsDbgVariable); |
| 2073 | AbstractVariables[Var.getNode()] = AbsDbgVariable; |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 2074 | DbgValueStartMap[MI] = AbsDbgVariable; |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2075 | return AbsDbgVariable; |
| 2076 | } |
| 2077 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2078 | /// collectVariableInfo - Populate DbgScope entries with variables' info. |
| 2079 | void DwarfDebug::collectVariableInfo() { |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2080 | const LLVMContext &Ctx = Asm->MF->getFunction()->getContext(); |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2081 | |
Devang Patel | e717faa | 2009-10-06 01:26:37 +0000 | [diff] [blame] | 2082 | MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo(); |
| 2083 | for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(), |
| 2084 | VE = VMap.end(); VI != VE; ++VI) { |
Devang Patel | bc5201f | 2010-01-22 22:52:10 +0000 | [diff] [blame] | 2085 | MDNode *Var = VI->first; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2086 | if (!Var) continue; |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2087 | DIVariable DV(Var); |
| 2088 | const std::pair<unsigned, DebugLoc> &VP = VI->second; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2089 | |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2090 | DbgScope *Scope = 0; |
| 2091 | if (MDNode *IA = VP.second.getInlinedAt(Ctx)) |
| 2092 | Scope = ConcreteScopes.lookup(IA); |
| 2093 | if (Scope == 0) |
| 2094 | Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx)); |
| 2095 | |
Devang Patel | fb0ee43 | 2009-11-10 23:20:04 +0000 | [diff] [blame] | 2096 | // If variable scope is not found then skip this variable. |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2097 | if (Scope == 0) |
Devang Patel | fb0ee43 | 2009-11-10 23:20:04 +0000 | [diff] [blame] | 2098 | continue; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2099 | |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2100 | DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.first, VP.second); |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 2101 | DbgVariable *RegVar = new DbgVariable(DV, VP.first, AbsDbgVariable); |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2102 | Scope->addVariable(RegVar); |
Devang Patel | e717faa | 2009-10-06 01:26:37 +0000 | [diff] [blame] | 2103 | } |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2104 | |
| 2105 | // Collect variable information from DBG_VALUE machine instructions; |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2106 | for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end(); |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2107 | I != E; ++I) { |
| 2108 | for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 2109 | II != IE; ++II) { |
| 2110 | const MachineInstr *MInsn = II; |
Chris Lattner | 14d750d | 2010-03-31 05:39:57 +0000 | [diff] [blame] | 2111 | if (!MInsn->isDebugValue()) |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2112 | continue; |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 2113 | |
Devang Patel | 0f9d952 | 2010-04-27 20:54:45 +0000 | [diff] [blame] | 2114 | // Ignore Undef values. |
Devang Patel | 97303ee | 2010-04-27 22:04:41 +0000 | [diff] [blame] | 2115 | if (MInsn->getOperand(0).isReg() && !MInsn->getOperand(0).getReg()) |
Devang Patel | 0f9d952 | 2010-04-27 20:54:45 +0000 | [diff] [blame] | 2116 | continue; |
| 2117 | |
Dan Gohman | 82d5eaf | 2010-04-17 16:43:55 +0000 | [diff] [blame] | 2118 | DIVariable DV( |
| 2119 | const_cast<MDNode *>(MInsn->getOperand(MInsn->getNumOperands() - 1) |
| 2120 | .getMetadata())); |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2121 | if (DV.getTag() == dwarf::DW_TAG_arg_variable) { |
| 2122 | // FIXME Handle inlined subroutine arguments. |
| 2123 | DbgVariable *ArgVar = new DbgVariable(DV, MInsn, NULL); |
| 2124 | CurrentFnDbgScope->addVariable(ArgVar); |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 2125 | DbgValueStartMap[MInsn] = ArgVar; |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2126 | continue; |
| 2127 | } |
| 2128 | |
| 2129 | DebugLoc DL = MInsn->getDebugLoc(); |
| 2130 | if (DL.isUnknown()) continue; |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2131 | DbgScope *Scope = 0; |
| 2132 | if (MDNode *IA = DL.getInlinedAt(Ctx)) |
| 2133 | Scope = ConcreteScopes.lookup(IA); |
| 2134 | if (Scope == 0) |
| 2135 | Scope = DbgScopeMap.lookup(DL.getScope(Ctx)); |
| 2136 | |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2137 | // If variable scope is not found then skip this variable. |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2138 | if (Scope == 0) |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2139 | continue; |
| 2140 | |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2141 | DbgVariable *AbsDbgVariable = findAbstractVariable(DV, MInsn, DL); |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2142 | DbgVariable *RegVar = new DbgVariable(DV, MInsn, AbsDbgVariable); |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 2143 | DbgValueStartMap[MInsn] = RegVar; |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 2144 | Scope->addVariable(RegVar); |
| 2145 | } |
| 2146 | } |
Devang Patel | e717faa | 2009-10-06 01:26:37 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Devang Patel | 553881b | 2010-03-29 17:20:31 +0000 | [diff] [blame] | 2149 | /// beginScope - Process beginning of a scope. |
| 2150 | void DwarfDebug::beginScope(const MachineInstr *MI) { |
Devang Patel | 553881b | 2010-03-29 17:20:31 +0000 | [diff] [blame] | 2151 | // Check location. |
| 2152 | DebugLoc DL = MI->getDebugLoc(); |
| 2153 | if (DL.isUnknown()) |
| 2154 | return; |
Devang Patel | 553881b | 2010-03-29 17:20:31 +0000 | [diff] [blame] | 2155 | |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2156 | MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext()); |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2157 | |
| 2158 | // FIXME: Should only verify each scope once! |
| 2159 | if (!DIScope(Scope).Verify()) |
Devang Patel | 553881b | 2010-03-29 17:20:31 +0000 | [diff] [blame] | 2160 | return; |
Devang Patel | 553881b | 2010-03-29 17:20:31 +0000 | [diff] [blame] | 2161 | |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 2162 | // DBG_VALUE instruction establishes new value. |
Chris Lattner | 14d750d | 2010-03-31 05:39:57 +0000 | [diff] [blame] | 2163 | if (MI->isDebugValue()) { |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 2164 | DenseMap<const MachineInstr *, DbgVariable *>::iterator DI |
| 2165 | = DbgValueStartMap.find(MI); |
| 2166 | if (DI != DbgValueStartMap.end()) { |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2167 | MCSymbol *Label = NULL; |
| 2168 | if (DL == PrevInstLoc) |
| 2169 | Label = PrevLabel; |
| 2170 | else { |
| 2171 | Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope); |
| 2172 | PrevInstLoc = DL; |
| 2173 | PrevLabel = Label; |
| 2174 | } |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 2175 | DI->second->setDbgValueLabel(Label); |
| 2176 | } |
Devang Patel | 7ed6311 | 2010-03-30 18:07:00 +0000 | [diff] [blame] | 2177 | return; |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Devang Patel | 553881b | 2010-03-29 17:20:31 +0000 | [diff] [blame] | 2180 | // Emit a label to indicate location change. This is used for line |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2181 | // table even if this instruction does not start a new scope. |
| 2182 | MCSymbol *Label = NULL; |
| 2183 | if (DL == PrevInstLoc) |
| 2184 | Label = PrevLabel; |
| 2185 | else { |
| 2186 | Label = recordSourceLine(DL.getLine(), DL.getCol(), Scope); |
| 2187 | PrevInstLoc = DL; |
| 2188 | PrevLabel = Label; |
| 2189 | } |
Devang Patel | 553881b | 2010-03-29 17:20:31 +0000 | [diff] [blame] | 2190 | |
Devang Patel | 1c24635 | 2010-04-08 16:50:29 +0000 | [diff] [blame] | 2191 | // If this instruction begins a scope then note down corresponding label. |
| 2192 | if (InsnsBeginScopeSet.count(MI) != 0) |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2193 | LabelsBeforeInsn[MI] = Label; |
Devang Patel | 0d20ac8 | 2009-10-06 01:50:42 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2196 | /// endScope - Process end of a scope. |
| 2197 | void DwarfDebug::endScope(const MachineInstr *MI) { |
Devang Patel | 1c24635 | 2010-04-08 16:50:29 +0000 | [diff] [blame] | 2198 | if (InsnsEndScopeSet.count(MI) != 0) { |
| 2199 | // Emit a label if this instruction ends a scope. |
| 2200 | MCSymbol *Label = MMI->getContext().CreateTempSymbol(); |
| 2201 | Asm->OutStreamer.EmitLabel(Label); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2202 | LabelsAfterInsn[MI] = Label; |
Devang Patel | 1c24635 | 2010-04-08 16:50:29 +0000 | [diff] [blame] | 2203 | } |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2206 | /// getOrCreateDbgScope - Create DbgScope for the scope. |
| 2207 | DbgScope *DwarfDebug::getOrCreateDbgScope(MDNode *Scope, MDNode *InlinedAt) { |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2208 | if (!InlinedAt) { |
| 2209 | DbgScope *WScope = DbgScopeMap.lookup(Scope); |
| 2210 | if (WScope) |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2211 | return WScope; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2212 | WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL); |
| 2213 | DbgScopeMap.insert(std::make_pair(Scope, WScope)); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2214 | if (DIDescriptor(Scope).isLexicalBlock()) { |
| 2215 | DbgScope *Parent = |
| 2216 | getOrCreateDbgScope(DILexicalBlock(Scope).getContext().getNode(), NULL); |
| 2217 | WScope->setParent(Parent); |
| 2218 | Parent->addScope(WScope); |
| 2219 | } |
| 2220 | |
| 2221 | if (!WScope->getParent()) { |
| 2222 | StringRef SPName = DISubprogram(Scope).getLinkageName(); |
| 2223 | if (SPName == Asm->MF->getFunction()->getName()) |
| 2224 | CurrentFnDbgScope = WScope; |
| 2225 | } |
| 2226 | |
| 2227 | return WScope; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | DbgScope *WScope = DbgScopeMap.lookup(InlinedAt); |
| 2231 | if (WScope) |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2232 | return WScope; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2233 | |
| 2234 | WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt); |
| 2235 | DbgScopeMap.insert(std::make_pair(InlinedAt, WScope)); |
| 2236 | DILocation DL(InlinedAt); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2237 | DbgScope *Parent = |
| 2238 | getOrCreateDbgScope(DL.getScope().getNode(), DL.getOrigLocation().getNode()); |
| 2239 | WScope->setParent(Parent); |
| 2240 | Parent->addScope(WScope); |
| 2241 | |
| 2242 | ConcreteScopes[InlinedAt] = WScope; |
| 2243 | getOrCreateAbstractScope(Scope); |
| 2244 | |
| 2245 | return WScope; |
Devang Patel | 0d20ac8 | 2009-10-06 01:50:42 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2248 | /// hasValidLocation - Return true if debug location entry attached with |
| 2249 | /// machine instruction encodes valid location info. |
| 2250 | static bool hasValidLocation(LLVMContext &Ctx, |
| 2251 | const MachineInstr *MInsn, |
| 2252 | MDNode *&Scope, MDNode *&InlinedAt) { |
| 2253 | if (MInsn->isDebugValue()) |
| 2254 | return false; |
| 2255 | DebugLoc DL = MInsn->getDebugLoc(); |
| 2256 | if (DL.isUnknown()) return false; |
| 2257 | |
| 2258 | MDNode *S = DL.getScope(Ctx); |
| 2259 | |
| 2260 | // There is no need to create another DIE for compile unit. For all |
| 2261 | // other scopes, create one DbgScope now. This will be translated |
| 2262 | // into a scope DIE at the end. |
| 2263 | if (DIScope(S).isCompileUnit()) return false; |
| 2264 | |
| 2265 | Scope = S; |
| 2266 | InlinedAt = DL.getInlinedAt(Ctx); |
| 2267 | return true; |
| 2268 | } |
| 2269 | |
| 2270 | /// calculateDominanceGraph - Calculate dominance graph for DbgScope |
| 2271 | /// hierarchy. |
| 2272 | static void calculateDominanceGraph(DbgScope *Scope) { |
| 2273 | assert (Scope && "Unable to calculate scop edominance graph!"); |
| 2274 | SmallVector<DbgScope *, 4> WorkStack; |
| 2275 | WorkStack.push_back(Scope); |
| 2276 | unsigned Counter = 0; |
| 2277 | while (!WorkStack.empty()) { |
| 2278 | DbgScope *WS = WorkStack.back(); |
| 2279 | const SmallVector<DbgScope *, 4> &Children = WS->getScopes(); |
| 2280 | bool visitedChildren = false; |
| 2281 | for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(), |
| 2282 | SE = Children.end(); SI != SE; ++SI) { |
| 2283 | DbgScope *ChildScope = *SI; |
| 2284 | if (!ChildScope->getDFSOut()) { |
| 2285 | WorkStack.push_back(ChildScope); |
| 2286 | visitedChildren = true; |
| 2287 | ChildScope->setDFSIn(++Counter); |
| 2288 | break; |
| 2289 | } |
| 2290 | } |
| 2291 | if (!visitedChildren) { |
| 2292 | WorkStack.pop_back(); |
| 2293 | WS->setDFSOut(++Counter); |
| 2294 | } |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | /// printDbgScopeInfo - Print DbgScope info for each machine instruction. |
| 2299 | static |
| 2300 | void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF, |
| 2301 | DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap) |
| 2302 | { |
| 2303 | #ifndef NDEBUG |
| 2304 | unsigned PrevDFSIn = 0; |
| 2305 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); |
| 2306 | I != E; ++I) { |
| 2307 | for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 2308 | II != IE; ++II) { |
| 2309 | const MachineInstr *MInsn = II; |
| 2310 | MDNode *Scope = NULL; |
| 2311 | MDNode *InlinedAt = NULL; |
| 2312 | |
| 2313 | // Check if instruction has valid location information. |
| 2314 | if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) { |
| 2315 | dbgs() << " [ "; |
| 2316 | if (InlinedAt) |
| 2317 | dbgs() << "*"; |
| 2318 | DenseMap<const MachineInstr *, DbgScope *>::iterator DI = |
| 2319 | MI2ScopeMap.find(MInsn); |
| 2320 | if (DI != MI2ScopeMap.end()) { |
| 2321 | DbgScope *S = DI->second; |
| 2322 | dbgs() << S->getDFSIn(); |
| 2323 | PrevDFSIn = S->getDFSIn(); |
| 2324 | } else |
| 2325 | dbgs() << PrevDFSIn; |
| 2326 | } else |
| 2327 | dbgs() << " [ x" << PrevDFSIn; |
| 2328 | dbgs() << " ]"; |
| 2329 | MInsn->dump(); |
| 2330 | } |
| 2331 | dbgs() << "\n"; |
| 2332 | } |
| 2333 | #endif |
| 2334 | } |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2335 | /// extractScopeInformation - Scan machine instructions in this function |
Chris Lattner | 14d750d | 2010-03-31 05:39:57 +0000 | [diff] [blame] | 2336 | /// and collect DbgScopes. Return true, if at least one scope was found. |
Chris Lattner | eec791a | 2010-01-26 23:18:02 +0000 | [diff] [blame] | 2337 | bool DwarfDebug::extractScopeInformation() { |
Devang Patel | af9e847 | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 2338 | // If scope information was extracted using .dbg intrinsics then there is not |
| 2339 | // any need to extract these information by scanning each instruction. |
| 2340 | if (!DbgScopeMap.empty()) |
| 2341 | return false; |
| 2342 | |
Dan Gohman | 314bf7c | 2010-04-23 01:18:53 +0000 | [diff] [blame] | 2343 | // Scan each instruction and create scopes. First build working set of scopes. |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2344 | LLVMContext &Ctx = Asm->MF->getFunction()->getContext(); |
| 2345 | SmallVector<DbgRange, 4> MIRanges; |
| 2346 | DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap; |
| 2347 | MDNode *PrevScope = NULL; |
| 2348 | MDNode *PrevInlinedAt = NULL; |
| 2349 | const MachineInstr *RangeBeginMI = NULL; |
| 2350 | const MachineInstr *PrevMI = NULL; |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2351 | for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end(); |
Devang Patel | af9e847 | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 2352 | I != E; ++I) { |
| 2353 | for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 2354 | II != IE; ++II) { |
| 2355 | const MachineInstr *MInsn = II; |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2356 | MDNode *Scope = NULL; |
| 2357 | MDNode *InlinedAt = NULL; |
| 2358 | |
| 2359 | // Check if instruction has valid location information. |
| 2360 | if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) { |
| 2361 | PrevMI = MInsn; |
| 2362 | continue; |
| 2363 | } |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2364 | |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2365 | // If scope has not changed then skip this instruction. |
| 2366 | if (Scope == PrevScope && PrevInlinedAt == InlinedAt) { |
| 2367 | PrevMI = MInsn; |
| 2368 | continue; |
| 2369 | } |
| 2370 | |
| 2371 | if (RangeBeginMI) { |
| 2372 | // If we have alread seen a beginning of a instruction range and |
| 2373 | // current instruction scope does not match scope of first instruction |
| 2374 | // in this range then create a new instruction range. |
| 2375 | DbgRange R(RangeBeginMI, PrevMI); |
| 2376 | MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt); |
| 2377 | MIRanges.push_back(R); |
| 2378 | } |
| 2379 | |
| 2380 | // This is a beginning of a new instruction range. |
| 2381 | RangeBeginMI = MInsn; |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2382 | |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2383 | // Reset previous markers. |
| 2384 | PrevMI = MInsn; |
| 2385 | PrevScope = Scope; |
| 2386 | PrevInlinedAt = InlinedAt; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2387 | } |
| 2388 | } |
| 2389 | |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2390 | // Create last instruction range. |
| 2391 | if (RangeBeginMI && PrevMI && PrevScope) { |
| 2392 | DbgRange R(RangeBeginMI, PrevMI); |
| 2393 | MIRanges.push_back(R); |
| 2394 | MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt); |
Devang Patel | af9e847 | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 2395 | } |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2396 | |
Devang Patel | 344130e | 2010-01-04 20:44:00 +0000 | [diff] [blame] | 2397 | if (!CurrentFnDbgScope) |
| 2398 | return false; |
| 2399 | |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2400 | calculateDominanceGraph(CurrentFnDbgScope); |
| 2401 | if (PrintDbgScope) |
| 2402 | printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap); |
| 2403 | |
| 2404 | // Find ranges of instructions covered by each DbgScope; |
| 2405 | DbgScope *PrevDbgScope = NULL; |
| 2406 | for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(), |
| 2407 | RE = MIRanges.end(); RI != RE; ++RI) { |
| 2408 | const DbgRange &R = *RI; |
| 2409 | DbgScope *S = MI2ScopeMap.lookup(R.first); |
| 2410 | assert (S && "Lost DbgScope for a machine instruction!"); |
| 2411 | if (PrevDbgScope && !PrevDbgScope->dominates(S)) |
| 2412 | PrevDbgScope->closeInsnRange(S); |
| 2413 | S->openInsnRange(R.first); |
| 2414 | S->extendInsnRange(R.second); |
| 2415 | PrevDbgScope = S; |
| 2416 | } |
| 2417 | |
| 2418 | if (PrevDbgScope) |
| 2419 | PrevDbgScope->closeInsnRange(); |
Devang Patel | af9e847 | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 2420 | |
Devang Patel | e37b0c6 | 2010-04-08 18:43:56 +0000 | [diff] [blame] | 2421 | identifyScopeMarkers(); |
Devang Patel | 6122a4d | 2010-04-08 15:37:09 +0000 | [diff] [blame] | 2422 | |
| 2423 | return !DbgScopeMap.empty(); |
| 2424 | } |
| 2425 | |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2426 | /// identifyScopeMarkers() - |
| 2427 | /// Each DbgScope has first instruction and last instruction to mark beginning |
| 2428 | /// and end of a scope respectively. Create an inverse map that list scopes |
| 2429 | /// starts (and ends) with an instruction. One instruction may start (or end) |
| 2430 | /// multiple scopes. Ignore scopes that are not reachable. |
Devang Patel | e37b0c6 | 2010-04-08 18:43:56 +0000 | [diff] [blame] | 2431 | void DwarfDebug::identifyScopeMarkers() { |
Devang Patel | 42aafd7 | 2010-01-20 02:05:23 +0000 | [diff] [blame] | 2432 | SmallVector<DbgScope *, 4> WorkList; |
| 2433 | WorkList.push_back(CurrentFnDbgScope); |
| 2434 | while (!WorkList.empty()) { |
Chris Lattner | 14d750d | 2010-03-31 05:39:57 +0000 | [diff] [blame] | 2435 | DbgScope *S = WorkList.pop_back_val(); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2436 | |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 2437 | const SmallVector<DbgScope *, 4> &Children = S->getScopes(); |
Devang Patel | 42aafd7 | 2010-01-20 02:05:23 +0000 | [diff] [blame] | 2438 | if (!Children.empty()) |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 2439 | for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(), |
Devang Patel | 42aafd7 | 2010-01-20 02:05:23 +0000 | [diff] [blame] | 2440 | SE = Children.end(); SI != SE; ++SI) |
| 2441 | WorkList.push_back(*SI); |
| 2442 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2443 | if (S->isAbstractScope()) |
| 2444 | continue; |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2445 | |
| 2446 | const SmallVector<DbgRange, 4> &Ranges = S->getRanges(); |
| 2447 | if (Ranges.empty()) |
| 2448 | continue; |
| 2449 | for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(), |
| 2450 | RE = Ranges.end(); RI != RE; ++RI) { |
| 2451 | assert(RI->first && "DbgRange does not have first instruction!"); |
| 2452 | assert(RI->second && "DbgRange does not have second instruction!"); |
| 2453 | InsnsBeginScopeSet.insert(RI->first); |
| 2454 | InsnsEndScopeSet.insert(RI->second); |
| 2455 | } |
Devang Patel | af9e847 | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 2456 | } |
Devang Patel | af9e847 | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Dan Gohman | 084751c | 2010-04-20 00:37:27 +0000 | [diff] [blame] | 2459 | /// FindFirstDebugLoc - Find the first debug location in the function. This |
| 2460 | /// is intended to be an approximation for the source position of the |
| 2461 | /// beginning of the function. |
| 2462 | static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) { |
| 2463 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); |
| 2464 | I != E; ++I) |
| 2465 | for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end(); |
| 2466 | MBBI != MBBE; ++MBBI) { |
| 2467 | DebugLoc DL = MBBI->getDebugLoc(); |
| 2468 | if (!DL.isUnknown()) |
| 2469 | return DL; |
| 2470 | } |
| 2471 | return DebugLoc(); |
| 2472 | } |
| 2473 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2474 | /// beginFunction - Gather pre-function debug information. Assumes being |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2475 | /// emitted immediately after the function entry point. |
Chris Lattner | eec791a | 2010-01-26 23:18:02 +0000 | [diff] [blame] | 2476 | void DwarfDebug::beginFunction(const MachineFunction *MF) { |
Chris Lattner | 994cb12 | 2010-04-05 03:52:55 +0000 | [diff] [blame] | 2477 | if (!MMI->hasDebugInfo()) return; |
Bill Wendling | 5f017e8 | 2010-04-07 09:28:04 +0000 | [diff] [blame] | 2478 | if (!extractScopeInformation()) return; |
Chris Lattner | a909d66 | 2010-03-29 20:38:20 +0000 | [diff] [blame] | 2479 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2480 | collectVariableInfo(); |
Devang Patel | 60b35bd | 2009-10-06 18:37:31 +0000 | [diff] [blame] | 2481 | |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2482 | FunctionBeginSym = Asm->GetTempSymbol("func_begin", |
| 2483 | Asm->getFunctionNumber()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2484 | // Assumes in correct section after the entry point. |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2485 | Asm->OutStreamer.EmitLabel(FunctionBeginSym); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2486 | |
| 2487 | // Emit label for the implicitly defined dbg.stoppoint at the start of the |
| 2488 | // function. |
Dan Gohman | 084751c | 2010-04-20 00:37:27 +0000 | [diff] [blame] | 2489 | DebugLoc FDL = FindFirstDebugLoc(MF); |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2490 | if (FDL.isUnknown()) return; |
| 2491 | |
| 2492 | MDNode *Scope = FDL.getScope(MF->getFunction()->getContext()); |
| 2493 | |
| 2494 | DISubprogram SP = getDISubprogram(Scope); |
| 2495 | unsigned Line, Col; |
| 2496 | if (SP.Verify()) { |
| 2497 | Line = SP.getLineNumber(); |
| 2498 | Col = 0; |
| 2499 | } else { |
| 2500 | Line = FDL.getLine(); |
| 2501 | Col = FDL.getCol(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2502 | } |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 2503 | |
| 2504 | recordSourceLine(Line, Col, Scope); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2507 | /// endFunction - Gather and emit post-function debug information. |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2508 | /// |
Chris Lattner | eec791a | 2010-01-26 23:18:02 +0000 | [diff] [blame] | 2509 | void DwarfDebug::endFunction(const MachineFunction *MF) { |
Bill Wendling | 5f017e8 | 2010-04-07 09:28:04 +0000 | [diff] [blame] | 2510 | if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return; |
Devang Patel | 70d75ca | 2009-11-12 19:02:56 +0000 | [diff] [blame] | 2511 | |
Devang Patel | 344130e | 2010-01-04 20:44:00 +0000 | [diff] [blame] | 2512 | if (CurrentFnDbgScope) { |
| 2513 | // Define end label for subprogram. |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2514 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("func_end", |
| 2515 | Asm->getFunctionNumber())); |
Devang Patel | 344130e | 2010-01-04 20:44:00 +0000 | [diff] [blame] | 2516 | |
| 2517 | // Get function line info. |
| 2518 | if (!Lines.empty()) { |
| 2519 | // Get section line info. |
| 2520 | unsigned ID = SectionMap.insert(Asm->getCurrentSection()); |
| 2521 | if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID); |
| 2522 | std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1]; |
| 2523 | // Append the function info to section info. |
| 2524 | SectionLineInfos.insert(SectionLineInfos.end(), |
| 2525 | Lines.begin(), Lines.end()); |
| 2526 | } |
| 2527 | |
| 2528 | // Construct abstract scopes. |
| 2529 | for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(), |
| 2530 | AE = AbstractScopesList.end(); AI != AE; ++AI) |
| 2531 | constructScopeDIE(*AI); |
| 2532 | |
Devang Patel | 9c48837 | 2010-05-04 06:15:30 +0000 | [diff] [blame^] | 2533 | DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope); |
Devang Patel | 344130e | 2010-01-04 20:44:00 +0000 | [diff] [blame] | 2534 | |
Devang Patel | 9c48837 | 2010-05-04 06:15:30 +0000 | [diff] [blame^] | 2535 | if (!DisableFramePointerElim(*MF)) |
| 2536 | addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr, |
| 2537 | dwarf::DW_FORM_flag, 1); |
| 2538 | |
| 2539 | |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2540 | DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(), |
Devang Patel | 344130e | 2010-01-04 20:44:00 +0000 | [diff] [blame] | 2541 | MMI->getFrameMoves())); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2544 | // Clear debug info |
Devang Patel | f54b852 | 2010-01-19 01:26:02 +0000 | [diff] [blame] | 2545 | CurrentFnDbgScope = NULL; |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 2546 | DeleteContainerSeconds(DbgScopeMap); |
Devang Patel | 5142471 | 2010-04-09 16:04:20 +0000 | [diff] [blame] | 2547 | InsnsBeginScopeSet.clear(); |
| 2548 | InsnsEndScopeSet.clear(); |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 2549 | DbgValueStartMap.clear(); |
Devang Patel | f54b852 | 2010-01-19 01:26:02 +0000 | [diff] [blame] | 2550 | ConcreteScopes.clear(); |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 2551 | DeleteContainerSeconds(AbstractScopes); |
Devang Patel | f54b852 | 2010-01-19 01:26:02 +0000 | [diff] [blame] | 2552 | AbstractScopesList.clear(); |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 2553 | AbstractVariables.clear(); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 2554 | LabelsBeforeInsn.clear(); |
| 2555 | LabelsAfterInsn.clear(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2556 | Lines.clear(); |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 2557 | PrevLabel = NULL; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
Chris Lattner | c608784 | 2010-03-09 04:54:43 +0000 | [diff] [blame] | 2560 | /// recordSourceLine - Register a source line with debug info. Returns the |
| 2561 | /// unique label that was emitted and which provides correspondence to |
| 2562 | /// the source line list. |
| 2563 | MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, MDNode *S) { |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 2564 | StringRef Dir; |
| 2565 | StringRef Fn; |
Devang Patel | f84548d | 2009-10-05 18:03:19 +0000 | [diff] [blame] | 2566 | |
| 2567 | DIDescriptor Scope(S); |
| 2568 | if (Scope.isCompileUnit()) { |
| 2569 | DICompileUnit CU(S); |
| 2570 | Dir = CU.getDirectory(); |
| 2571 | Fn = CU.getFilename(); |
| 2572 | } else if (Scope.isSubprogram()) { |
| 2573 | DISubprogram SP(S); |
| 2574 | Dir = SP.getDirectory(); |
| 2575 | Fn = SP.getFilename(); |
| 2576 | } else if (Scope.isLexicalBlock()) { |
| 2577 | DILexicalBlock DB(S); |
| 2578 | Dir = DB.getDirectory(); |
| 2579 | Fn = DB.getFilename(); |
| 2580 | } else |
Chris Lattner | 206d61e | 2010-03-13 07:26:18 +0000 | [diff] [blame] | 2581 | assert(0 && "Unexpected scope info"); |
Devang Patel | f84548d | 2009-10-05 18:03:19 +0000 | [diff] [blame] | 2582 | |
| 2583 | unsigned Src = GetOrCreateSourceID(Dir, Fn); |
Chris Lattner | 63d7836 | 2010-03-14 08:36:50 +0000 | [diff] [blame] | 2584 | MCSymbol *Label = MMI->getContext().CreateTempSymbol(); |
Chris Lattner | 25b68c6 | 2010-03-14 08:15:55 +0000 | [diff] [blame] | 2585 | Lines.push_back(SrcLineInfo(Line, Col, Src, Label)); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2586 | |
Chris Lattner | c608784 | 2010-03-09 04:54:43 +0000 | [diff] [blame] | 2587 | Asm->OutStreamer.EmitLabel(Label); |
| 2588 | return Label; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
Bill Wendling | 829e67b | 2009-05-20 23:22:40 +0000 | [diff] [blame] | 2591 | //===----------------------------------------------------------------------===// |
| 2592 | // Emit Methods |
| 2593 | //===----------------------------------------------------------------------===// |
| 2594 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2595 | /// computeSizeAndOffset - Compute the size and offset of a DIE. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2596 | /// |
Jim Grosbach | 7ab38df | 2009-11-22 19:20:36 +0000 | [diff] [blame] | 2597 | unsigned |
| 2598 | DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2599 | // Get the children. |
| 2600 | const std::vector<DIE *> &Children = Die->getChildren(); |
| 2601 | |
| 2602 | // If not last sibling and has children then add sibling offset attribute. |
Jeffrey Yasskin | 638fe8d | 2010-03-22 18:47:14 +0000 | [diff] [blame] | 2603 | if (!Last && !Children.empty()) |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 2604 | Die->addSiblingOffset(DIEValueAllocator); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2605 | |
| 2606 | // Record the abbreviation. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2607 | assignAbbrevNumber(Die->getAbbrev()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2608 | |
| 2609 | // Get the abbreviation for this DIE. |
| 2610 | unsigned AbbrevNumber = Die->getAbbrevNumber(); |
| 2611 | const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1]; |
| 2612 | |
| 2613 | // Set DIE offset |
| 2614 | Die->setOffset(Offset); |
| 2615 | |
| 2616 | // Start the size with the size of abbreviation code. |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 2617 | Offset += MCAsmInfo::getULEB128Size(AbbrevNumber); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2618 | |
| 2619 | const SmallVector<DIEValue*, 32> &Values = Die->getValues(); |
| 2620 | const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData(); |
| 2621 | |
| 2622 | // Size the DIE attribute values. |
| 2623 | for (unsigned i = 0, N = Values.size(); i < N; ++i) |
| 2624 | // Size attribute value. |
Chris Lattner | a37d538 | 2010-04-05 00:18:22 +0000 | [diff] [blame] | 2625 | Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2626 | |
| 2627 | // Size the DIE children if any. |
| 2628 | if (!Children.empty()) { |
| 2629 | assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes && |
| 2630 | "Children flag not set"); |
| 2631 | |
| 2632 | for (unsigned j = 0, M = Children.size(); j < M; ++j) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2633 | Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2634 | |
| 2635 | // End of children marker. |
| 2636 | Offset += sizeof(int8_t); |
| 2637 | } |
| 2638 | |
| 2639 | Die->setSize(Offset - Die->getOffset()); |
| 2640 | return Offset; |
| 2641 | } |
| 2642 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2643 | /// computeSizeAndOffsets - Compute the size and offset of all the DIEs. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2644 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2645 | void DwarfDebug::computeSizeAndOffsets() { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2646 | // Compute size of compile unit header. |
| 2647 | static unsigned Offset = |
| 2648 | sizeof(int32_t) + // Length of Compilation Unit Info |
| 2649 | sizeof(int16_t) + // DWARF version number |
| 2650 | sizeof(int32_t) + // Offset Into Abbrev. Section |
| 2651 | sizeof(int8_t); // Pointer Size (in bytes) |
| 2652 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2653 | computeSizeAndOffset(ModuleCU->getCUDie(), Offset, true); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 2656 | /// EmitSectionSym - Switch to the specified MCSection and emit an assembler |
| 2657 | /// temporary label to it if SymbolStem is specified. |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 2658 | static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section, |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 2659 | const char *SymbolStem = 0) { |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 2660 | Asm->OutStreamer.SwitchSection(Section); |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 2661 | if (!SymbolStem) return 0; |
| 2662 | |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 2663 | MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem); |
| 2664 | Asm->OutStreamer.EmitLabel(TmpSym); |
| 2665 | return TmpSym; |
| 2666 | } |
| 2667 | |
| 2668 | /// EmitSectionLabels - Emit initial Dwarf sections with a label at |
| 2669 | /// the start of each one. |
Chris Lattner | fa070b0 | 2010-04-04 22:33:59 +0000 | [diff] [blame] | 2670 | void DwarfDebug::EmitSectionLabels() { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2671 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
Daniel Dunbar | f612ff6 | 2009-09-19 20:40:05 +0000 | [diff] [blame] | 2672 | |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2673 | // Dwarf sections base addresses. |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2674 | if (Asm->MAI->doesDwarfRequireFrameSection()) { |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 2675 | DwarfFrameSectionSym = |
| 2676 | EmitSectionSym(Asm, TLOF.getDwarfFrameSection(), "section_debug_frame"); |
| 2677 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2678 | |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 2679 | DwarfInfoSectionSym = |
| 2680 | EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info"); |
| 2681 | DwarfAbbrevSectionSym = |
| 2682 | EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev"); |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 2683 | EmitSectionSym(Asm, TLOF.getDwarfARangesSection()); |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 2684 | |
| 2685 | if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection()) |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 2686 | EmitSectionSym(Asm, MacroInfo); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2687 | |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 2688 | EmitSectionSym(Asm, TLOF.getDwarfLineSection()); |
| 2689 | EmitSectionSym(Asm, TLOF.getDwarfLocSection()); |
| 2690 | EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection()); |
| 2691 | EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection()); |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 2692 | DwarfStrSectionSym = |
| 2693 | EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str"); |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 2694 | DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(), |
| 2695 | "debug_range"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2696 | |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 2697 | TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin"); |
Chris Lattner | 4ad1efe | 2010-04-04 23:10:38 +0000 | [diff] [blame] | 2698 | EmitSectionSym(Asm, TLOF.getDataSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2701 | /// emitDIE - Recusively Emits a debug information entry. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2702 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2703 | void DwarfDebug::emitDIE(DIE *Die) { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2704 | // Get the abbreviation for this DIE. |
| 2705 | unsigned AbbrevNumber = Die->getAbbrevNumber(); |
| 2706 | const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1]; |
| 2707 | |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2708 | // Emit the code (index) for the abbreviation. |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 2709 | if (Asm->isVerbose()) |
Chris Lattner | 894d75a | 2010-01-22 23:18:42 +0000 | [diff] [blame] | 2710 | Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" + |
| 2711 | Twine::utohexstr(Die->getOffset()) + ":0x" + |
| 2712 | Twine::utohexstr(Die->getSize()) + " " + |
| 2713 | dwarf::TagString(Abbrev->getTag())); |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 2714 | Asm->EmitULEB128(AbbrevNumber); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2715 | |
Jeffrey Yasskin | 638fe8d | 2010-03-22 18:47:14 +0000 | [diff] [blame] | 2716 | const SmallVector<DIEValue*, 32> &Values = Die->getValues(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2717 | const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData(); |
| 2718 | |
| 2719 | // Emit the DIE attribute values. |
| 2720 | for (unsigned i = 0, N = Values.size(); i < N; ++i) { |
| 2721 | unsigned Attr = AbbrevData[i].getAttribute(); |
| 2722 | unsigned Form = AbbrevData[i].getForm(); |
| 2723 | assert(Form && "Too many attributes for DIE (check abbreviation)"); |
| 2724 | |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 2725 | if (Asm->isVerbose()) |
Chris Lattner | a801362 | 2010-01-24 18:54:17 +0000 | [diff] [blame] | 2726 | Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr)); |
| 2727 | |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2728 | switch (Attr) { |
| 2729 | case dwarf::DW_AT_sibling: |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2730 | Asm->EmitInt32(Die->getSiblingOffset()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2731 | break; |
| 2732 | case dwarf::DW_AT_abstract_origin: { |
| 2733 | DIEEntry *E = cast<DIEEntry>(Values[i]); |
| 2734 | DIE *Origin = E->getEntry(); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2735 | unsigned Addr = Origin->getOffset(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2736 | Asm->EmitInt32(Addr); |
| 2737 | break; |
| 2738 | } |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 2739 | case dwarf::DW_AT_ranges: { |
| 2740 | // DW_AT_range Value encodes offset in debug_range section. |
| 2741 | DIEInteger *V = cast<DIEInteger>(Values[i]); |
| 2742 | Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym, |
| 2743 | V->getValue(), |
| 2744 | DwarfDebugRangeSectionSym, |
| 2745 | 4); |
| 2746 | break; |
| 2747 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2748 | default: |
| 2749 | // Emit an attribute using the defined form. |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2750 | Values[i]->EmitValue(Asm, Form); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2751 | break; |
| 2752 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | // Emit the DIE children if any. |
| 2756 | if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) { |
| 2757 | const std::vector<DIE *> &Children = Die->getChildren(); |
| 2758 | |
| 2759 | for (unsigned j = 0, M = Children.size(); j < M; ++j) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2760 | emitDIE(Children[j]); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2761 | |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 2762 | if (Asm->isVerbose()) |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2763 | Asm->OutStreamer.AddComment("End Of Children Mark"); |
| 2764 | Asm->EmitInt8(0); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 2768 | /// emitDebugInfo - Emit the debug info section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2769 | /// |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 2770 | void DwarfDebug::emitDebugInfo() { |
| 2771 | // Start debug info section. |
| 2772 | Asm->OutStreamer.SwitchSection( |
| 2773 | Asm->getObjFileLowering().getDwarfInfoSection()); |
| 2774 | DIE *Die = ModuleCU->getCUDie(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2775 | |
| 2776 | // Emit the compile units header. |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 2777 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin", |
| 2778 | ModuleCU->getID())); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2779 | |
| 2780 | // Emit size of content not including length itself |
| 2781 | unsigned ContentSize = Die->getSize() + |
| 2782 | sizeof(int16_t) + // DWARF version number |
| 2783 | sizeof(int32_t) + // Offset Into Abbrev. Section |
| 2784 | sizeof(int8_t) + // Pointer Size (in bytes) |
| 2785 | sizeof(int32_t); // FIXME - extra pad for gdb bug. |
| 2786 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2787 | Asm->OutStreamer.AddComment("Length of Compilation Unit Info"); |
| 2788 | Asm->EmitInt32(ContentSize); |
| 2789 | Asm->OutStreamer.AddComment("DWARF version number"); |
| 2790 | Asm->EmitInt16(dwarf::DWARF_VERSION); |
| 2791 | Asm->OutStreamer.AddComment("Offset Into Abbrev. Section"); |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 2792 | Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"), |
| 2793 | DwarfAbbrevSectionSym); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2794 | Asm->OutStreamer.AddComment("Address Size (in bytes)"); |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2795 | Asm->EmitInt8(Asm->getTargetData().getPointerSize()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2796 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2797 | emitDIE(Die); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2798 | // FIXME - extra padding for gdb bug. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2799 | Asm->OutStreamer.AddComment("4 extra padding bytes for GDB"); |
| 2800 | Asm->EmitInt8(0); |
| 2801 | Asm->EmitInt8(0); |
| 2802 | Asm->EmitInt8(0); |
| 2803 | Asm->EmitInt8(0); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 2804 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", ModuleCU->getID())); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2807 | /// emitAbbreviations - Emit the abbreviation section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2808 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2809 | void DwarfDebug::emitAbbreviations() const { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2810 | // Check to see if it is worth the effort. |
| 2811 | if (!Abbreviations.empty()) { |
| 2812 | // Start the debug abbrev section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2813 | Asm->OutStreamer.SwitchSection( |
| 2814 | Asm->getObjFileLowering().getDwarfAbbrevSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2815 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 2816 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin")); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2817 | |
| 2818 | // For each abbrevation. |
| 2819 | for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) { |
| 2820 | // Get abbreviation data |
| 2821 | const DIEAbbrev *Abbrev = Abbreviations[i]; |
| 2822 | |
| 2823 | // Emit the abbrevations code (base 1 index.) |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 2824 | Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2825 | |
| 2826 | // Emit the abbreviations data. |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2827 | Abbrev->Emit(Asm); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | // Mark end of abbreviations. |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 2831 | Asm->EmitULEB128(0, "EOM(3)"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2832 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 2833 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end")); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2834 | } |
| 2835 | } |
| 2836 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2837 | /// emitEndOfLineMatrix - Emit the last address of the section and the end of |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2838 | /// the line matrix. |
| 2839 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2840 | void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2841 | // Define last address of section. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2842 | Asm->OutStreamer.AddComment("Extended Op"); |
| 2843 | Asm->EmitInt8(0); |
| 2844 | |
| 2845 | Asm->OutStreamer.AddComment("Op size"); |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2846 | Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2847 | Asm->OutStreamer.AddComment("DW_LNE_set_address"); |
| 2848 | Asm->EmitInt8(dwarf::DW_LNE_set_address); |
| 2849 | |
| 2850 | Asm->OutStreamer.AddComment("Section end label"); |
Chris Lattner | d85fc6e | 2010-03-10 01:17:49 +0000 | [diff] [blame] | 2851 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 2852 | Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd), |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2853 | Asm->getTargetData().getPointerSize(), |
| 2854 | 0/*AddrSpace*/); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2855 | |
| 2856 | // Mark end of matrix. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2857 | Asm->OutStreamer.AddComment("DW_LNE_end_sequence"); |
| 2858 | Asm->EmitInt8(0); |
Chris Lattner | 0ad9c91 | 2010-01-22 22:09:00 +0000 | [diff] [blame] | 2859 | Asm->EmitInt8(1); |
Chris Lattner | 894d75a | 2010-01-22 23:18:42 +0000 | [diff] [blame] | 2860 | Asm->EmitInt8(1); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2863 | /// emitDebugLines - Emit source line information. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2864 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 2865 | void DwarfDebug::emitDebugLines() { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2866 | // If the target is using .loc/.file, the assembler will be emitting the |
| 2867 | // .debug_line table automatically. |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2868 | if (Asm->MAI->hasDotLocAndDotFile()) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2869 | return; |
| 2870 | |
| 2871 | // Minimum line delta, thus ranging from -10..(255-10). |
| 2872 | const int MinLineDelta = -(dwarf::DW_LNS_fixed_advance_pc + 1); |
| 2873 | // Maximum line delta, thus ranging from -10..(255-10). |
| 2874 | const int MaxLineDelta = 255 + MinLineDelta; |
| 2875 | |
| 2876 | // Start the dwarf line section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 2877 | Asm->OutStreamer.SwitchSection( |
| 2878 | Asm->getObjFileLowering().getDwarfLineSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2879 | |
| 2880 | // Construct the section header. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2881 | Asm->OutStreamer.AddComment("Length of Source Line Info"); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 2882 | Asm->EmitLabelDifference(Asm->GetTempSymbol("line_end"), |
| 2883 | Asm->GetTempSymbol("line_begin"), 4); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 2884 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_begin")); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2885 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2886 | Asm->OutStreamer.AddComment("DWARF version number"); |
| 2887 | Asm->EmitInt16(dwarf::DWARF_VERSION); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2888 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2889 | Asm->OutStreamer.AddComment("Prolog Length"); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 2890 | Asm->EmitLabelDifference(Asm->GetTempSymbol("line_prolog_end"), |
| 2891 | Asm->GetTempSymbol("line_prolog_begin"), 4); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 2892 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_begin")); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2893 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2894 | Asm->OutStreamer.AddComment("Minimum Instruction Length"); |
| 2895 | Asm->EmitInt8(1); |
| 2896 | Asm->OutStreamer.AddComment("Default is_stmt_start flag"); |
| 2897 | Asm->EmitInt8(1); |
| 2898 | Asm->OutStreamer.AddComment("Line Base Value (Special Opcodes)"); |
| 2899 | Asm->EmitInt8(MinLineDelta); |
| 2900 | Asm->OutStreamer.AddComment("Line Range Value (Special Opcodes)"); |
| 2901 | Asm->EmitInt8(MaxLineDelta); |
| 2902 | Asm->OutStreamer.AddComment("Special Opcode Base"); |
| 2903 | Asm->EmitInt8(-MinLineDelta); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2904 | |
| 2905 | // Line number standard opcode encodings argument count |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2906 | Asm->OutStreamer.AddComment("DW_LNS_copy arg count"); |
| 2907 | Asm->EmitInt8(0); |
| 2908 | Asm->OutStreamer.AddComment("DW_LNS_advance_pc arg count"); |
| 2909 | Asm->EmitInt8(1); |
| 2910 | Asm->OutStreamer.AddComment("DW_LNS_advance_line arg count"); |
| 2911 | Asm->EmitInt8(1); |
| 2912 | Asm->OutStreamer.AddComment("DW_LNS_set_file arg count"); |
| 2913 | Asm->EmitInt8(1); |
| 2914 | Asm->OutStreamer.AddComment("DW_LNS_set_column arg count"); |
| 2915 | Asm->EmitInt8(1); |
| 2916 | Asm->OutStreamer.AddComment("DW_LNS_negate_stmt arg count"); |
| 2917 | Asm->EmitInt8(0); |
| 2918 | Asm->OutStreamer.AddComment("DW_LNS_set_basic_block arg count"); |
| 2919 | Asm->EmitInt8(0); |
| 2920 | Asm->OutStreamer.AddComment("DW_LNS_const_add_pc arg count"); |
| 2921 | Asm->EmitInt8(0); |
| 2922 | Asm->OutStreamer.AddComment("DW_LNS_fixed_advance_pc arg count"); |
| 2923 | Asm->EmitInt8(1); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2924 | |
| 2925 | // Emit directories. |
| 2926 | for (unsigned DI = 1, DE = getNumSourceDirectories()+1; DI != DE; ++DI) { |
Chris Lattner | 4cf202b | 2010-01-23 03:11:46 +0000 | [diff] [blame] | 2927 | const std::string &Dir = getSourceDirectoryName(DI); |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 2928 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Directory"); |
Chris Lattner | 4cf202b | 2010-01-23 03:11:46 +0000 | [diff] [blame] | 2929 | Asm->OutStreamer.EmitBytes(StringRef(Dir.c_str(), Dir.size()+1), 0); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2932 | Asm->OutStreamer.AddComment("End of directories"); |
| 2933 | Asm->EmitInt8(0); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2934 | |
| 2935 | // Emit files. |
| 2936 | for (unsigned SI = 1, SE = getNumSourceIds()+1; SI != SE; ++SI) { |
| 2937 | // Remember source id starts at 1. |
| 2938 | std::pair<unsigned, unsigned> Id = getSourceDirectoryAndFileIds(SI); |
Chris Lattner | 4cf202b | 2010-01-23 03:11:46 +0000 | [diff] [blame] | 2939 | const std::string &FN = getSourceFileName(Id.second); |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 2940 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("Source"); |
Chris Lattner | 4cf202b | 2010-01-23 03:11:46 +0000 | [diff] [blame] | 2941 | Asm->OutStreamer.EmitBytes(StringRef(FN.c_str(), FN.size()+1), 0); |
| 2942 | |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 2943 | Asm->EmitULEB128(Id.first, "Directory #"); |
| 2944 | Asm->EmitULEB128(0, "Mod date"); |
| 2945 | Asm->EmitULEB128(0, "File size"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2948 | Asm->OutStreamer.AddComment("End of files"); |
| 2949 | Asm->EmitInt8(0); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2950 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 2951 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_prolog_end")); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2952 | |
| 2953 | // A sequence for each text section. |
| 2954 | unsigned SecSrcLinesSize = SectionSourceLines.size(); |
| 2955 | |
| 2956 | for (unsigned j = 0; j < SecSrcLinesSize; ++j) { |
| 2957 | // Isolate current sections line info. |
| 2958 | const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j]; |
| 2959 | |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2960 | // Dwarf assumes we start with first line of first source file. |
| 2961 | unsigned Source = 1; |
| 2962 | unsigned Line = 1; |
| 2963 | |
| 2964 | // Construct rows of the address, source, line, column matrix. |
| 2965 | for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) { |
| 2966 | const SrcLineInfo &LineInfo = LineInfos[i]; |
Chris Lattner | 25b68c6 | 2010-03-14 08:15:55 +0000 | [diff] [blame] | 2967 | MCSymbol *Label = LineInfo.getLabel(); |
Chris Lattner | b913060 | 2010-03-14 02:20:58 +0000 | [diff] [blame] | 2968 | if (!Label->isDefined()) continue; // Not emitted, in dead code. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2969 | |
Caroline Tice | c6f9d62 | 2009-09-11 18:25:54 +0000 | [diff] [blame] | 2970 | if (LineInfo.getLine() == 0) continue; |
| 2971 | |
Chris Lattner | 0d9d70f | 2010-03-09 23:38:23 +0000 | [diff] [blame] | 2972 | if (Asm->isVerbose()) { |
Chris Lattner | 188a87d | 2010-03-10 01:04:13 +0000 | [diff] [blame] | 2973 | std::pair<unsigned, unsigned> SrcID = |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2974 | getSourceDirectoryAndFileIds(LineInfo.getSourceID()); |
Chris Lattner | 188a87d | 2010-03-10 01:04:13 +0000 | [diff] [blame] | 2975 | Asm->OutStreamer.AddComment(Twine(getSourceDirectoryName(SrcID.first)) + |
Chris Lattner | 49f618a | 2010-03-10 02:29:31 +0000 | [diff] [blame] | 2976 | "/" + |
| 2977 | Twine(getSourceFileName(SrcID.second)) + |
Chris Lattner | 188a87d | 2010-03-10 01:04:13 +0000 | [diff] [blame] | 2978 | ":" + Twine(LineInfo.getLine())); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | // Define the line address. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2982 | Asm->OutStreamer.AddComment("Extended Op"); |
| 2983 | Asm->EmitInt8(0); |
| 2984 | Asm->OutStreamer.AddComment("Op size"); |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2985 | Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2986 | |
| 2987 | Asm->OutStreamer.AddComment("DW_LNE_set_address"); |
| 2988 | Asm->EmitInt8(dwarf::DW_LNE_set_address); |
| 2989 | |
| 2990 | Asm->OutStreamer.AddComment("Location label"); |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2991 | Asm->OutStreamer.EmitSymbolValue(Label, |
| 2992 | Asm->getTargetData().getPointerSize(), |
Chris Lattner | b913060 | 2010-03-14 02:20:58 +0000 | [diff] [blame] | 2993 | 0/*AddrSpace*/); |
Chris Lattner | d85fc6e | 2010-03-10 01:17:49 +0000 | [diff] [blame] | 2994 | |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2995 | // If change of source, then switch to the new source. |
| 2996 | if (Source != LineInfo.getSourceID()) { |
| 2997 | Source = LineInfo.getSourceID(); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2998 | Asm->OutStreamer.AddComment("DW_LNS_set_file"); |
| 2999 | Asm->EmitInt8(dwarf::DW_LNS_set_file); |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 3000 | Asm->EmitULEB128(Source, "New Source"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | // If change of line. |
| 3004 | if (Line != LineInfo.getLine()) { |
| 3005 | // Determine offset. |
| 3006 | int Offset = LineInfo.getLine() - Line; |
| 3007 | int Delta = Offset - MinLineDelta; |
| 3008 | |
| 3009 | // Update line. |
| 3010 | Line = LineInfo.getLine(); |
| 3011 | |
| 3012 | // If delta is small enough and in range... |
| 3013 | if (Delta >= 0 && Delta < (MaxLineDelta - 1)) { |
| 3014 | // ... then use fast opcode. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3015 | Asm->OutStreamer.AddComment("Line Delta"); |
| 3016 | Asm->EmitInt8(Delta - MinLineDelta); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3017 | } else { |
| 3018 | // ... otherwise use long hand. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3019 | Asm->OutStreamer.AddComment("DW_LNS_advance_line"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3020 | Asm->EmitInt8(dwarf::DW_LNS_advance_line); |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 3021 | Asm->EmitSLEB128(Offset, "Line Offset"); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3022 | Asm->OutStreamer.AddComment("DW_LNS_copy"); |
| 3023 | Asm->EmitInt8(dwarf::DW_LNS_copy); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3024 | } |
| 3025 | } else { |
| 3026 | // Copy the previous row (different address or source) |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3027 | Asm->OutStreamer.AddComment("DW_LNS_copy"); |
| 3028 | Asm->EmitInt8(dwarf::DW_LNS_copy); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3029 | } |
| 3030 | } |
| 3031 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3032 | emitEndOfLineMatrix(j + 1); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | if (SecSrcLinesSize == 0) |
| 3036 | // Because we're emitting a debug_line section, we still need a line |
| 3037 | // table. The linker and friends expect it to exist. If there's nothing to |
| 3038 | // put into it, emit an empty table. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3039 | emitEndOfLineMatrix(1); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3040 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3041 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("line_end")); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3044 | /// emitCommonDebugFrame - Emit common frame info into a debug frame section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3045 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3046 | void DwarfDebug::emitCommonDebugFrame() { |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 3047 | if (!Asm->MAI->doesDwarfRequireFrameSection()) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3048 | return; |
| 3049 | |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 3050 | int stackGrowth = Asm->getTargetData().getPointerSize(); |
| 3051 | if (Asm->TM.getFrameInfo()->getStackGrowthDirection() == |
| 3052 | TargetFrameInfo::StackGrowsDown) |
| 3053 | stackGrowth *= -1; |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3054 | |
| 3055 | // Start the dwarf frame section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 3056 | Asm->OutStreamer.SwitchSection( |
| 3057 | Asm->getObjFileLowering().getDwarfFrameSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3058 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3059 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common")); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3060 | Asm->OutStreamer.AddComment("Length of Common Information Entry"); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 3061 | Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_frame_common_end"), |
| 3062 | Asm->GetTempSymbol("debug_frame_common_begin"), 4); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3063 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3064 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_begin")); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3065 | Asm->OutStreamer.AddComment("CIE Identifier Tag"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3066 | Asm->EmitInt32((int)dwarf::DW_CIE_ID); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3067 | Asm->OutStreamer.AddComment("CIE Version"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3068 | Asm->EmitInt8(dwarf::DW_CIE_VERSION); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3069 | Asm->OutStreamer.AddComment("CIE Augmentation"); |
Chris Lattner | 4cf202b | 2010-01-23 03:11:46 +0000 | [diff] [blame] | 3070 | Asm->OutStreamer.EmitIntValue(0, 1, /*addrspace*/0); // nul terminator. |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 3071 | Asm->EmitULEB128(1, "CIE Code Alignment Factor"); |
| 3072 | Asm->EmitSLEB128(stackGrowth, "CIE Data Alignment Factor"); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3073 | Asm->OutStreamer.AddComment("CIE RA Column"); |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 3074 | const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3075 | Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false)); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3076 | |
| 3077 | std::vector<MachineMove> Moves; |
| 3078 | RI->getInitialFrameState(Moves); |
| 3079 | |
Chris Lattner | 02b86b9 | 2010-04-04 23:41:46 +0000 | [diff] [blame] | 3080 | Asm->EmitFrameMoves(Moves, 0, false); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3081 | |
Chris Lattner | 059ea13 | 2010-04-28 01:05:45 +0000 | [diff] [blame] | 3082 | Asm->EmitAlignment(2); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3083 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_frame_common_end")); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3086 | /// emitFunctionDebugFrame - Emit per function frame info into a debug frame |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3087 | /// section. |
Chris Lattner | 206d61e | 2010-03-13 07:26:18 +0000 | [diff] [blame] | 3088 | void DwarfDebug:: |
| 3089 | emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) { |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 3090 | if (!Asm->MAI->doesDwarfRequireFrameSection()) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3091 | return; |
| 3092 | |
| 3093 | // Start the dwarf frame section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 3094 | Asm->OutStreamer.SwitchSection( |
| 3095 | Asm->getObjFileLowering().getDwarfFrameSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3096 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3097 | Asm->OutStreamer.AddComment("Length of Frame Information Entry"); |
Chris Lattner | 206d61e | 2010-03-13 07:26:18 +0000 | [diff] [blame] | 3098 | MCSymbol *DebugFrameBegin = |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3099 | Asm->GetTempSymbol("debug_frame_begin", DebugFrameInfo.Number); |
Chris Lattner | 206d61e | 2010-03-13 07:26:18 +0000 | [diff] [blame] | 3100 | MCSymbol *DebugFrameEnd = |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3101 | Asm->GetTempSymbol("debug_frame_end", DebugFrameInfo.Number); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 3102 | Asm->EmitLabelDifference(DebugFrameEnd, DebugFrameBegin, 4); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3103 | |
Chris Lattner | 206d61e | 2010-03-13 07:26:18 +0000 | [diff] [blame] | 3104 | Asm->OutStreamer.EmitLabel(DebugFrameBegin); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3105 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3106 | Asm->OutStreamer.AddComment("FDE CIE offset"); |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 3107 | Asm->EmitSectionOffset(Asm->GetTempSymbol("debug_frame_common"), |
| 3108 | DwarfFrameSectionSym); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3109 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3110 | Asm->OutStreamer.AddComment("FDE initial location"); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3111 | MCSymbol *FuncBeginSym = |
| 3112 | Asm->GetTempSymbol("func_begin", DebugFrameInfo.Number); |
Chris Lattner | fb65807 | 2010-03-13 07:40:56 +0000 | [diff] [blame] | 3113 | Asm->OutStreamer.EmitSymbolValue(FuncBeginSym, |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 3114 | Asm->getTargetData().getPointerSize(), |
| 3115 | 0/*AddrSpace*/); |
Chris Lattner | d85fc6e | 2010-03-10 01:17:49 +0000 | [diff] [blame] | 3116 | |
| 3117 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3118 | Asm->OutStreamer.AddComment("FDE address range"); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 3119 | Asm->EmitLabelDifference(Asm->GetTempSymbol("func_end",DebugFrameInfo.Number), |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 3120 | FuncBeginSym, Asm->getTargetData().getPointerSize()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3121 | |
Chris Lattner | 02b86b9 | 2010-04-04 23:41:46 +0000 | [diff] [blame] | 3122 | Asm->EmitFrameMoves(DebugFrameInfo.Moves, FuncBeginSym, false); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3123 | |
Chris Lattner | 059ea13 | 2010-04-28 01:05:45 +0000 | [diff] [blame] | 3124 | Asm->EmitAlignment(2); |
Chris Lattner | 206d61e | 2010-03-13 07:26:18 +0000 | [diff] [blame] | 3125 | Asm->OutStreamer.EmitLabel(DebugFrameEnd); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 3128 | /// emitDebugPubNames - Emit visible names into a debug pubnames section. |
| 3129 | /// |
| 3130 | void DwarfDebug::emitDebugPubNames() { |
| 3131 | // Start the dwarf pubnames section. |
| 3132 | Asm->OutStreamer.SwitchSection( |
| 3133 | Asm->getObjFileLowering().getDwarfPubNamesSection()); |
| 3134 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3135 | Asm->OutStreamer.AddComment("Length of Public Names Info"); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 3136 | Asm->EmitLabelDifference( |
| 3137 | Asm->GetTempSymbol("pubnames_end", ModuleCU->getID()), |
| 3138 | Asm->GetTempSymbol("pubnames_begin", ModuleCU->getID()), 4); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3139 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3140 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin", |
| 3141 | ModuleCU->getID())); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3142 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3143 | Asm->OutStreamer.AddComment("DWARF Version"); |
| 3144 | Asm->EmitInt16(dwarf::DWARF_VERSION); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3145 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3146 | Asm->OutStreamer.AddComment("Offset of Compilation Unit Info"); |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 3147 | Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()), |
| 3148 | DwarfInfoSectionSym); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3149 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3150 | Asm->OutStreamer.AddComment("Compilation Unit Length"); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 3151 | Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()), |
| 3152 | Asm->GetTempSymbol("info_begin", ModuleCU->getID()), |
| 3153 | 4); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3154 | |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 3155 | const StringMap<DIE*> &Globals = ModuleCU->getGlobals(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3156 | for (StringMap<DIE*>::const_iterator |
| 3157 | GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) { |
| 3158 | const char *Name = GI->getKeyData(); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3159 | DIE *Entity = GI->second; |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3160 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3161 | Asm->OutStreamer.AddComment("DIE offset"); |
| 3162 | Asm->EmitInt32(Entity->getOffset()); |
Chris Lattner | 4cf202b | 2010-01-23 03:11:46 +0000 | [diff] [blame] | 3163 | |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 3164 | if (Asm->isVerbose()) |
Chris Lattner | 4cf202b | 2010-01-23 03:11:46 +0000 | [diff] [blame] | 3165 | Asm->OutStreamer.AddComment("External Name"); |
| 3166 | Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3169 | Asm->OutStreamer.AddComment("End Mark"); |
| 3170 | Asm->EmitInt32(0); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3171 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end", |
| 3172 | ModuleCU->getID())); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 3175 | void DwarfDebug::emitDebugPubTypes() { |
Devang Patel | f3a0376 | 2009-11-24 19:18:41 +0000 | [diff] [blame] | 3176 | // Start the dwarf pubnames section. |
| 3177 | Asm->OutStreamer.SwitchSection( |
| 3178 | Asm->getObjFileLowering().getDwarfPubTypesSection()); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3179 | Asm->OutStreamer.AddComment("Length of Public Types Info"); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 3180 | Asm->EmitLabelDifference( |
| 3181 | Asm->GetTempSymbol("pubtypes_end", ModuleCU->getID()), |
| 3182 | Asm->GetTempSymbol("pubtypes_begin", ModuleCU->getID()), 4); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 3183 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3184 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin", |
| 3185 | ModuleCU->getID())); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 3186 | |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 3187 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version"); |
Devang Patel | e3d6d22 | 2010-02-02 03:47:27 +0000 | [diff] [blame] | 3188 | Asm->EmitInt16(dwarf::DWARF_VERSION); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 3189 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3190 | Asm->OutStreamer.AddComment("Offset of Compilation ModuleCU Info"); |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 3191 | Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", ModuleCU->getID()), |
| 3192 | DwarfInfoSectionSym); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 3193 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3194 | Asm->OutStreamer.AddComment("Compilation ModuleCU Length"); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 3195 | Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", ModuleCU->getID()), |
| 3196 | Asm->GetTempSymbol("info_begin", ModuleCU->getID()), |
| 3197 | 4); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 3198 | |
| 3199 | const StringMap<DIE*> &Globals = ModuleCU->getGlobalTypes(); |
| 3200 | for (StringMap<DIE*>::const_iterator |
| 3201 | GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) { |
| 3202 | const char *Name = GI->getKeyData(); |
| 3203 | DIE * Entity = GI->second; |
| 3204 | |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 3205 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset"); |
Devang Patel | e3d6d22 | 2010-02-02 03:47:27 +0000 | [diff] [blame] | 3206 | Asm->EmitInt32(Entity->getOffset()); |
Chris Lattner | 4cf202b | 2010-01-23 03:11:46 +0000 | [diff] [blame] | 3207 | |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 3208 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name"); |
Devang Patel | 31acb89 | 2010-02-02 03:37:03 +0000 | [diff] [blame] | 3209 | Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3212 | Asm->OutStreamer.AddComment("End Mark"); |
| 3213 | Asm->EmitInt32(0); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3214 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end", |
| 3215 | ModuleCU->getID())); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3218 | /// emitDebugStr - Emit visible names into a debug str section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3219 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3220 | void DwarfDebug::emitDebugStr() { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3221 | // Check to see if it is worth the effort. |
Chris Lattner | 0d9d70f | 2010-03-09 23:38:23 +0000 | [diff] [blame] | 3222 | if (StringPool.empty()) return; |
| 3223 | |
| 3224 | // Start the dwarf str section. |
| 3225 | Asm->OutStreamer.SwitchSection( |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 3226 | Asm->getObjFileLowering().getDwarfStrSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3227 | |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 3228 | // Get all of the string pool entries and put them in an array by their ID so |
| 3229 | // we can sort them. |
| 3230 | SmallVector<std::pair<unsigned, |
| 3231 | StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries; |
| 3232 | |
| 3233 | for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator |
| 3234 | I = StringPool.begin(), E = StringPool.end(); I != E; ++I) |
| 3235 | Entries.push_back(std::make_pair(I->second.second, &*I)); |
| 3236 | |
| 3237 | array_pod_sort(Entries.begin(), Entries.end()); |
| 3238 | |
| 3239 | for (unsigned i = 0, e = Entries.size(); i != e; ++i) { |
Chris Lattner | 0d9d70f | 2010-03-09 23:38:23 +0000 | [diff] [blame] | 3240 | // Emit a label for reference from debug information entries. |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 3241 | Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first); |
Chris Lattner | 0d9d70f | 2010-03-09 23:38:23 +0000 | [diff] [blame] | 3242 | |
| 3243 | // Emit the string itself. |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 3244 | Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3245 | } |
| 3246 | } |
| 3247 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3248 | /// emitDebugLoc - Emit visible names into a debug loc section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3249 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3250 | void DwarfDebug::emitDebugLoc() { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3251 | // Start the dwarf loc section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 3252 | Asm->OutStreamer.SwitchSection( |
| 3253 | Asm->getObjFileLowering().getDwarfLocSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | /// EmitDebugARanges - Emit visible names into a debug aranges section. |
| 3257 | /// |
| 3258 | void DwarfDebug::EmitDebugARanges() { |
| 3259 | // Start the dwarf aranges section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 3260 | Asm->OutStreamer.SwitchSection( |
| 3261 | Asm->getObjFileLowering().getDwarfARangesSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3264 | /// emitDebugRanges - Emit visible names into a debug ranges section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3265 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3266 | void DwarfDebug::emitDebugRanges() { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3267 | // Start the dwarf ranges section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 3268 | Asm->OutStreamer.SwitchSection( |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 3269 | Asm->getObjFileLowering().getDwarfRangesSection()); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 3270 | unsigned char Size = Asm->getTargetData().getPointerSize(); |
| 3271 | for (SmallVector<const MCSymbol *, 8>::iterator |
| 3272 | I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end(); |
| 3273 | I != E; ++I) { |
| 3274 | if (*I) |
| 3275 | Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0); |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 3276 | else |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 3277 | Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0); |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 3278 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3281 | /// emitDebugMacInfo - Emit visible names into a debug macinfo section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3282 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3283 | void DwarfDebug::emitDebugMacInfo() { |
Daniel Dunbar | f612ff6 | 2009-09-19 20:40:05 +0000 | [diff] [blame] | 3284 | if (const MCSection *LineInfo = |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 3285 | Asm->getObjFileLowering().getDwarfMacroInfoSection()) { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3286 | // Start the dwarf macinfo section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 3287 | Asm->OutStreamer.SwitchSection(LineInfo); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3288 | } |
| 3289 | } |
| 3290 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3291 | /// emitDebugInlineInfo - Emit inline info using following format. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3292 | /// Section Header: |
| 3293 | /// 1. length of section |
| 3294 | /// 2. Dwarf version number |
| 3295 | /// 3. address size. |
| 3296 | /// |
| 3297 | /// Entries (one "entry" for each function that was inlined): |
| 3298 | /// |
| 3299 | /// 1. offset into __debug_str section for MIPS linkage name, if exists; |
| 3300 | /// otherwise offset into __debug_str for regular function name. |
| 3301 | /// 2. offset into __debug_str section for regular function name. |
| 3302 | /// 3. an unsigned LEB128 number indicating the number of distinct inlining |
| 3303 | /// instances for the function. |
| 3304 | /// |
| 3305 | /// The rest of the entry consists of a {die_offset, low_pc} pair for each |
| 3306 | /// inlined instance; the die_offset points to the inlined_subroutine die in the |
| 3307 | /// __debug_info section, and the low_pc is the starting address for the |
| 3308 | /// inlining instance. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 3309 | void DwarfDebug::emitDebugInlineInfo() { |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 3310 | if (!Asm->MAI->doesDwarfUsesInlineInfoSection()) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3311 | return; |
| 3312 | |
Devang Patel | 1dbc771 | 2009-06-29 20:45:18 +0000 | [diff] [blame] | 3313 | if (!ModuleCU) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3314 | return; |
| 3315 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 3316 | Asm->OutStreamer.SwitchSection( |
| 3317 | Asm->getObjFileLowering().getDwarfDebugInlineSection()); |
Chris Lattner | 0ad9c91 | 2010-01-22 22:09:00 +0000 | [diff] [blame] | 3318 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3319 | Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry"); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 3320 | Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1), |
| 3321 | Asm->GetTempSymbol("debug_inlined_begin", 1), 4); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3322 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3323 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1)); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3324 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3325 | Asm->OutStreamer.AddComment("Dwarf Version"); |
| 3326 | Asm->EmitInt16(dwarf::DWARF_VERSION); |
| 3327 | Asm->OutStreamer.AddComment("Address Size (in bytes)"); |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 3328 | Asm->EmitInt8(Asm->getTargetData().getPointerSize()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3329 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 3330 | for (SmallVector<MDNode *, 4>::iterator I = InlinedSPNodes.begin(), |
| 3331 | E = InlinedSPNodes.end(); I != E; ++I) { |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 3332 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 3333 | MDNode *Node = *I; |
Devang Patel | 622b026 | 2010-01-19 06:19:05 +0000 | [diff] [blame] | 3334 | DenseMap<MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II |
Jim Grosbach | 7ab38df | 2009-11-22 19:20:36 +0000 | [diff] [blame] | 3335 | = InlineInfo.find(Node); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 3336 | SmallVector<InlineInfoLabels, 4> &Labels = II->second; |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 3337 | DISubprogram SP(Node); |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 3338 | StringRef LName = SP.getLinkageName(); |
| 3339 | StringRef Name = SP.getName(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3340 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3341 | Asm->OutStreamer.AddComment("MIPS linkage name"); |
Chris Lattner | 4cf202b | 2010-01-23 03:11:46 +0000 | [diff] [blame] | 3342 | if (LName.empty()) { |
| 3343 | Asm->OutStreamer.EmitBytes(Name, 0); |
| 3344 | Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator. |
| 3345 | } else |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 3346 | Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)), |
| 3347 | DwarfStrSectionSym); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 3348 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 3349 | Asm->OutStreamer.AddComment("Function name"); |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 3350 | Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym); |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 3351 | Asm->EmitULEB128(Labels.size(), "Inline count"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3352 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 3353 | for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(), |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3354 | LE = Labels.end(); LI != LE; ++LI) { |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 3355 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset"); |
Chris Lattner | 6ed0f90 | 2010-03-09 00:31:02 +0000 | [diff] [blame] | 3356 | Asm->EmitInt32(LI->second->getOffset()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3357 | |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 3358 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc"); |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 3359 | Asm->OutStreamer.EmitSymbolValue(LI->first, |
| 3360 | Asm->getTargetData().getPointerSize(),0); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 3364 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1)); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 3365 | } |