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