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" |
Devang Patel | 8b9df62 | 2011-04-12 17:40:32 +0000 | [diff] [blame] | 17 | #include "DwarfCompileUnit.h" |
Bill Wendling | 57fbba4 | 2010-04-05 22:59:21 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Devang Patel | e449d1f | 2011-01-20 00:02:16 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
David Greene | b2c66fc | 2009-08-19 21:52:55 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunction.h" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | b7db733 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSection.h" |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | b7db733 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSymbol.h" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetFrameLowering.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Chris Lattner | 9d1c1ad | 2010-04-04 18:06:11 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetRegisterInfo.h" |
Devang Patel | 2a4a3b7 | 2010-04-19 19:14:02 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 74e41f9 | 2010-04-05 05:24:55 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/DebugInfo.h" |
Devang Patel | 0eea95d | 2011-02-24 18:49:30 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/DIBuilder.h" |
Devang Patel | 9a31f0f | 2010-10-25 20:45:32 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 23132b1 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/StringExtras.h" |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 6e4bdfc | 2009-10-13 06:47:08 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
| 40 | #include "llvm/Support/ErrorHandling.h" |
Devang Patel | 3139fcf | 2010-01-26 21:39:14 +0000 | [diff] [blame] | 41 | #include "llvm/Support/ValueHandle.h" |
Chris Lattner | 0ad9c91 | 2010-01-22 22:09:00 +0000 | [diff] [blame] | 42 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | a87dea4 | 2009-07-31 18:48:30 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Timer.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Path.h" |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 47 | static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print", |
Devang Patel | 6140962 | 2010-07-07 20:12:52 +0000 | [diff] [blame] | 48 | cl::Hidden, |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 49 | cl::desc("Disable debug info printing")); |
| 50 | |
Dan Gohman | 281d65d | 2010-05-07 01:08:53 +0000 | [diff] [blame] | 51 | static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden, |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 52 | cl::desc("Make an absence of debug location information explicit."), |
Dan Gohman | 281d65d | 2010-05-07 01:08:53 +0000 | [diff] [blame] | 53 | cl::init(false)); |
| 54 | |
Bill Wendling | 5f017e8 | 2010-04-07 09:28:04 +0000 | [diff] [blame] | 55 | namespace { |
| 56 | const char *DWARFGroupName = "DWARF Emission"; |
| 57 | const char *DbgTimerName = "DWARF Debug Writer"; |
| 58 | } // end anonymous namespace |
| 59 | |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 60 | //===----------------------------------------------------------------------===// |
| 61 | |
| 62 | /// Configuration values for initial hash set sizes (log2). |
| 63 | /// |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 64 | static const unsigned InitAbbreviationsSetSize = 9; // log2(512) |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 65 | |
| 66 | namespace llvm { |
| 67 | |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 68 | DIType DbgVariable::getType() const { |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 69 | DIType Ty = Var.getType(); |
| 70 | // FIXME: isBlockByrefVariable should be reformulated in terms of complex |
| 71 | // addresses instead. |
| 72 | if (Var.isBlockByrefVariable()) { |
| 73 | /* Byref variables, in Blocks, are declared by the programmer as |
| 74 | "SomeType VarName;", but the compiler creates a |
| 75 | __Block_byref_x_VarName struct, and gives the variable VarName |
| 76 | either the struct, or a pointer to the struct, as its type. This |
| 77 | is necessary for various behind-the-scenes things the compiler |
| 78 | needs to do with by-reference variables in blocks. |
| 79 | |
| 80 | However, as far as the original *programmer* is concerned, the |
| 81 | variable should still have type 'SomeType', as originally declared. |
| 82 | |
| 83 | The following function dives into the __Block_byref_x_VarName |
| 84 | struct to find the original type of the variable. This will be |
| 85 | passed back to the code generating the type for the Debug |
| 86 | Information Entry for the variable 'VarName'. 'VarName' will then |
| 87 | have the original type 'SomeType' in its debug information. |
| 88 | |
| 89 | The original type 'SomeType' will be the type of the field named |
| 90 | 'VarName' inside the __Block_byref_x_VarName struct. |
| 91 | |
| 92 | NOTE: In order for this to not completely fail on the debugger |
| 93 | side, the Debug Information Entry for the variable VarName needs to |
| 94 | have a DW_AT_location that tells the debugger how to unwind through |
| 95 | the pointers and __Block_byref_x_VarName struct to find the actual |
| 96 | value of the variable. The function addBlockByrefType does this. */ |
| 97 | DIType subType = Ty; |
| 98 | unsigned tag = Ty.getTag(); |
| 99 | |
| 100 | if (tag == dwarf::DW_TAG_pointer_type) { |
| 101 | DIDerivedType DTy = DIDerivedType(Ty); |
| 102 | subType = DTy.getTypeDerivedFrom(); |
| 103 | } |
| 104 | |
| 105 | DICompositeType blockStruct = DICompositeType(subType); |
| 106 | DIArray Elements = blockStruct.getTypeArray(); |
| 107 | |
| 108 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
| 109 | DIDescriptor Element = Elements.getElement(i); |
| 110 | DIDerivedType DT = DIDerivedType(Element); |
| 111 | if (getName() == DT.getName()) |
| 112 | return (DT.getTypeDerivedFrom()); |
Devang Patel | 8bd11de | 2010-08-09 21:01:39 +0000 | [diff] [blame] | 113 | } |
| 114 | return Ty; |
| 115 | } |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 116 | return Ty; |
| 117 | } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 118 | |
Chris Lattner | ea76186 | 2010-04-05 04:09:20 +0000 | [diff] [blame] | 119 | } // end llvm namespace |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 49cd664 | 2010-04-05 05:11:15 +0000 | [diff] [blame] | 121 | DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M) |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 122 | : Asm(A), MMI(Asm->MMI), FirstCU(0), |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 123 | AbbreviationsSet(InitAbbreviationsSetSize), |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 124 | PrevLabel(NULL) { |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 125 | NextStringPoolNumber = 0; |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 126 | |
Rafael Espindola | f2b0423 | 2011-05-06 14:56:22 +0000 | [diff] [blame] | 127 | DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0; |
Chris Lattner | 4ad1efe | 2010-04-04 23:10:38 +0000 | [diff] [blame] | 128 | DwarfStrSectionSym = TextSectionSym = 0; |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 129 | DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0; |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 130 | FunctionBeginSym = FunctionEndSym = 0; |
Dan Gohman | 03c3dc7 | 2010-06-18 15:56:31 +0000 | [diff] [blame] | 131 | { |
| 132 | NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled); |
| 133 | beginModule(M); |
Torok Edwin | 9c42107 | 2010-04-07 10:44:46 +0000 | [diff] [blame] | 134 | } |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 135 | } |
| 136 | DwarfDebug::~DwarfDebug() { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 139 | MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) { |
| 140 | std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str]; |
| 141 | if (Entry.first) return Entry.first; |
| 142 | |
| 143 | Entry.second = NextStringPoolNumber++; |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 144 | return Entry.first = Asm->GetTempSymbol("string", Entry.second); |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 148 | /// assignAbbrevNumber - Define a unique number for the abbreviation. |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 149 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 150 | void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) { |
Bill Wendling | 0310d76 | 2009-05-15 09:23:25 +0000 | [diff] [blame] | 151 | // Profile the node so that we can make it unique. |
| 152 | FoldingSetNodeID ID; |
| 153 | Abbrev.Profile(ID); |
| 154 | |
| 155 | // Check the set for priors. |
| 156 | DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev); |
| 157 | |
| 158 | // If it's newly added. |
| 159 | if (InSet == &Abbrev) { |
| 160 | // Add to abbreviation list. |
| 161 | Abbreviations.push_back(&Abbrev); |
| 162 | |
| 163 | // Assign the vector position + 1 as its number. |
| 164 | Abbrev.setNumber(Abbreviations.size()); |
| 165 | } else { |
| 166 | // Assign existing abbreviation number. |
| 167 | Abbrev.setNumber(InSet->getNumber()); |
| 168 | } |
| 169 | } |
| 170 | |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 171 | /// getRealLinkageName - If special LLVM prefix that is used to inform the asm |
Devang Patel | 351ca33 | 2010-01-05 01:46:14 +0000 | [diff] [blame] | 172 | /// printer to not emit usual symbol prefix before the symbol name is used then |
| 173 | /// return linkage name after skipping this special LLVM prefix. |
| 174 | static StringRef getRealLinkageName(StringRef LinkageName) { |
| 175 | char One = '\1'; |
| 176 | if (LinkageName.startswith(StringRef(&One, 1))) |
| 177 | return LinkageName.substr(1); |
| 178 | return LinkageName; |
| 179 | } |
| 180 | |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 181 | /// updateSubprogramScopeDIE - Find DIE for the given subprogram and |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 182 | /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes. |
| 183 | /// If there are global variables in this scope then create and insert |
| 184 | /// DIEs for these variables. |
Devang Patel | e9f8f5e | 2010-05-07 20:54:48 +0000 | [diff] [blame] | 185 | DIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) { |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 186 | CompileUnit *SPCU = getCompileUnit(SPNode); |
| 187 | DIE *SPDie = SPCU->getDIE(SPNode); |
Devang Patel | 8aa6147 | 2010-07-07 22:20:57 +0000 | [diff] [blame] | 188 | |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 189 | assert(SPDie && "Unable to find subprogram DIE!"); |
| 190 | DISubprogram SP(SPNode); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 191 | |
Devang Patel | 5e06bb8 | 2011-04-22 23:10:17 +0000 | [diff] [blame] | 192 | DISubprogram SPDecl = SP.getFunctionDeclaration(); |
| 193 | if (SPDecl.isSubprogram()) |
| 194 | // Refer function declaration directly. |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 195 | SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4, |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 196 | SPCU->getOrCreateSubprogramDIE(SPDecl)); |
Devang Patel | 5e06bb8 | 2011-04-22 23:10:17 +0000 | [diff] [blame] | 197 | else { |
| 198 | // There is not any need to generate specification DIE for a function |
| 199 | // defined at compile unit level. If a function is defined inside another |
| 200 | // function then gdb prefers the definition at top level and but does not |
| 201 | // expect specification DIE in parent function. So avoid creating |
| 202 | // specification DIE for a function defined inside a function. |
| 203 | if (SP.isDefinition() && !SP.getContext().isCompileUnit() && |
| 204 | !SP.getContext().isFile() && |
| 205 | !isSubprogramContext(SP.getContext())) { |
| 206 | SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1); |
| 207 | |
| 208 | // Add arguments. |
| 209 | DICompositeType SPTy = SP.getType(); |
| 210 | DIArray Args = SPTy.getTypeArray(); |
| 211 | unsigned SPTag = SPTy.getTag(); |
| 212 | if (SPTag == dwarf::DW_TAG_subroutine_type) |
| 213 | for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { |
| 214 | DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); |
| 215 | DIType ATy = DIType(DIType(Args.getElement(i))); |
| 216 | SPCU->addType(Arg, ATy); |
| 217 | if (ATy.isArtificial()) |
| 218 | SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1); |
| 219 | SPDie->addChild(Arg); |
| 220 | } |
| 221 | DIE *SPDeclDie = SPDie; |
| 222 | SPDie = new DIE(dwarf::DW_TAG_subprogram); |
| 223 | SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4, |
| 224 | SPDeclDie); |
| 225 | SPCU->addDie(SPDie); |
| 226 | } |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 227 | } |
Devang Patel | 8aa6147 | 2010-07-07 22:20:57 +0000 | [diff] [blame] | 228 | // Pick up abstract subprogram DIE. |
| 229 | if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) { |
| 230 | SPDie = new DIE(dwarf::DW_TAG_subprogram); |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 231 | SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin, |
| 232 | dwarf::DW_FORM_ref4, AbsSPDIE); |
Devang Patel | 8aa6147 | 2010-07-07 22:20:57 +0000 | [diff] [blame] | 233 | SPCU->addDie(SPDie); |
| 234 | } |
| 235 | |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 236 | SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, |
| 237 | Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber())); |
| 238 | SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, |
| 239 | Asm->GetTempSymbol("func_end", Asm->getFunctionNumber())); |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 240 | const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); |
| 241 | MachineLocation Location(RI->getFrameRegister(*Asm->MF)); |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 242 | SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location); |
Devang Patel | b464564 | 2010-02-06 01:02:37 +0000 | [diff] [blame] | 243 | |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 244 | return SPDie; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 247 | /// constructLexicalScope - Construct new DW_TAG_lexical_block |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 248 | /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels. |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 249 | DIE *DwarfDebug::constructLexicalScopeDIE(LexicalScope *Scope) { |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 250 | |
| 251 | DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block); |
| 252 | if (Scope->isAbstractScope()) |
| 253 | return ScopeDIE; |
| 254 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 255 | const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges(); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 256 | if (Ranges.empty()) |
| 257 | return 0; |
| 258 | |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 259 | CompileUnit *TheCU = getCompileUnit(Scope->getScopeNode()); |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 260 | SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 261 | if (Ranges.size() > 1) { |
| 262 | // .debug_range section has not been laid out yet. Emit offset in |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 263 | // .debug_range as a uint, size 4, for now. emitDIE will handle |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 264 | // DW_AT_ranges appropriately. |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 265 | TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4, |
Devang Patel | 5bc942c | 2011-08-10 23:58:09 +0000 | [diff] [blame] | 266 | DebugRangeSymbols.size() |
| 267 | * Asm->getTargetData().getPointerSize()); |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 268 | for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(), |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 269 | RE = Ranges.end(); RI != RE; ++RI) { |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 270 | DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first)); |
| 271 | DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second)); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 272 | } |
| 273 | DebugRangeSymbols.push_back(NULL); |
| 274 | DebugRangeSymbols.push_back(NULL); |
| 275 | return ScopeDIE; |
| 276 | } |
| 277 | |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 278 | const MCSymbol *Start = getLabelBeforeInsn(RI->first); |
| 279 | const MCSymbol *End = getLabelAfterInsn(RI->second); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 280 | |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 281 | if (End == 0) return 0; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 282 | |
Chris Lattner | b7db733 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 283 | assert(Start->isDefined() && "Invalid starting label for an inlined scope!"); |
| 284 | assert(End->isDefined() && "Invalid end label for an inlined scope!"); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 285 | |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 286 | TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start); |
| 287 | TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 288 | |
| 289 | return ScopeDIE; |
| 290 | } |
| 291 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 292 | /// constructInlinedScopeDIE - This scope represents inlined body of |
| 293 | /// a function. Construct DIE to represent this concrete inlined copy |
| 294 | /// of the function. |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 295 | DIE *DwarfDebug::constructInlinedScopeDIE(LexicalScope *Scope) { |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 296 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 297 | const SmallVector<InsnRange, 4> &Ranges = Scope->getRanges(); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 298 | assert (Ranges.empty() == false |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 299 | && "LexicalScope does not have instruction markers!"); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 300 | |
Devang Patel | 26a9200 | 2011-07-27 00:34:13 +0000 | [diff] [blame] | 301 | if (!Scope->getScopeNode()) |
| 302 | return NULL; |
| 303 | DIScope DS(Scope->getScopeNode()); |
| 304 | DISubprogram InlinedSP = getDISubprogram(DS); |
| 305 | CompileUnit *TheCU = getCompileUnit(InlinedSP); |
| 306 | DIE *OriginDIE = TheCU->getDIE(InlinedSP); |
| 307 | if (!OriginDIE) { |
| 308 | DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram."); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 312 | SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(); |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 313 | const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first); |
| 314 | const MCSymbol *EndLabel = getLabelAfterInsn(RI->second); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 315 | |
Devang Patel | 0afbf23 | 2010-07-08 22:39:20 +0000 | [diff] [blame] | 316 | if (StartLabel == 0 || EndLabel == 0) { |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 317 | assert (0 && "Unexpected Start and End labels for a inlined scope!"); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 318 | return 0; |
| 319 | } |
Chris Lattner | b7db733 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 320 | assert(StartLabel->isDefined() && |
Chris Lattner | a34ec229 | 2010-03-09 01:51:43 +0000 | [diff] [blame] | 321 | "Invalid starting label for an inlined scope!"); |
Chris Lattner | b7db733 | 2010-03-09 01:58:53 +0000 | [diff] [blame] | 322 | assert(EndLabel->isDefined() && |
Chris Lattner | a34ec229 | 2010-03-09 01:51:43 +0000 | [diff] [blame] | 323 | "Invalid end label for an inlined scope!"); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 324 | |
Devang Patel | d96efb8 | 2011-05-05 17:54:26 +0000 | [diff] [blame] | 325 | DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine); |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 326 | TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, |
| 327 | dwarf::DW_FORM_ref4, OriginDIE); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 328 | |
Devang Patel | 26a9200 | 2011-07-27 00:34:13 +0000 | [diff] [blame] | 329 | if (Ranges.size() > 1) { |
| 330 | // .debug_range section has not been laid out yet. Emit offset in |
| 331 | // .debug_range as a uint, size 4, for now. emitDIE will handle |
| 332 | // DW_AT_ranges appropriately. |
| 333 | TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4, |
Devang Patel | 5bc942c | 2011-08-10 23:58:09 +0000 | [diff] [blame] | 334 | DebugRangeSymbols.size() |
| 335 | * Asm->getTargetData().getPointerSize()); |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 336 | for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(), |
Devang Patel | 26a9200 | 2011-07-27 00:34:13 +0000 | [diff] [blame] | 337 | RE = Ranges.end(); RI != RE; ++RI) { |
| 338 | DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first)); |
| 339 | DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second)); |
| 340 | } |
| 341 | DebugRangeSymbols.push_back(NULL); |
| 342 | DebugRangeSymbols.push_back(NULL); |
| 343 | } else { |
Devang Patel | 5bc942c | 2011-08-10 23:58:09 +0000 | [diff] [blame] | 344 | TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, |
| 345 | StartLabel); |
| 346 | TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, |
| 347 | EndLabel); |
Devang Patel | 26a9200 | 2011-07-27 00:34:13 +0000 | [diff] [blame] | 348 | } |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 349 | |
| 350 | InlinedSubprogramDIEs.insert(OriginDIE); |
| 351 | |
| 352 | // Track the start label for this inlined function. |
Devang Patel | 26a9200 | 2011-07-27 00:34:13 +0000 | [diff] [blame] | 353 | //.debug_inlined section specification does not clearly state how |
| 354 | // to emit inlined scope that is split into multiple instruction ranges. |
| 355 | // For now, use first instruction range and emit low_pc/high_pc pair and |
| 356 | // corresponding .debug_inlined section entry for this pair. |
Devang Patel | e9f8f5e | 2010-05-07 20:54:48 +0000 | [diff] [blame] | 357 | DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator |
Devang Patel | 2db49d7 | 2010-05-07 18:11:54 +0000 | [diff] [blame] | 358 | I = InlineInfo.find(InlinedSP); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 359 | |
| 360 | if (I == InlineInfo.end()) { |
Devang Patel | 2db49d7 | 2010-05-07 18:11:54 +0000 | [diff] [blame] | 361 | InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel, |
Jim Grosbach | 7ab38df | 2009-11-22 19:20:36 +0000 | [diff] [blame] | 362 | ScopeDIE)); |
Devang Patel | 2db49d7 | 2010-05-07 18:11:54 +0000 | [diff] [blame] | 363 | InlinedSPNodes.push_back(InlinedSP); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 364 | } else |
Chris Lattner | 6ed0f90 | 2010-03-09 00:31:02 +0000 | [diff] [blame] | 365 | I->second.push_back(std::make_pair(StartLabel, ScopeDIE)); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 366 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 367 | DILocation DL(Scope->getInlinedAt()); |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 368 | TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID()); |
| 369 | TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber()); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 370 | |
| 371 | return ScopeDIE; |
| 372 | } |
| 373 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 374 | /// constructScopeDIE - Construct a DIE for this scope. |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame^] | 375 | DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) { |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 376 | if (!Scope || !Scope->getScopeNode()) |
| 377 | return NULL; |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 378 | |
Devang Patel | 5bc9fec | 2011-02-19 01:31:27 +0000 | [diff] [blame] | 379 | SmallVector <DIE *, 8> Children; |
Devang Patel | 0478c15 | 2011-03-01 22:58:55 +0000 | [diff] [blame] | 380 | |
| 381 | // Collect arguments for current function. |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 382 | if (LScopes.isCurrentFunctionScope(Scope)) |
Devang Patel | 0478c15 | 2011-03-01 22:58:55 +0000 | [diff] [blame] | 383 | for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i) |
| 384 | if (DbgVariable *ArgDV = CurrentFnArguments[i]) |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame^] | 385 | if (DIE *Arg = |
| 386 | TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) |
Devang Patel | 0478c15 | 2011-03-01 22:58:55 +0000 | [diff] [blame] | 387 | Children.push_back(Arg); |
| 388 | |
Devang Patel | 5bc9fec | 2011-02-19 01:31:27 +0000 | [diff] [blame] | 389 | // Collect lexical scope childrens first. |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 390 | const SmallVector<DbgVariable *, 8> &Variables = ScopeVariables.lookup(Scope); |
Devang Patel | 5bc9fec | 2011-02-19 01:31:27 +0000 | [diff] [blame] | 391 | for (unsigned i = 0, N = Variables.size(); i < N; ++i) |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame^] | 392 | if (DIE *Variable = |
| 393 | TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope())) |
Devang Patel | 5bc9fec | 2011-02-19 01:31:27 +0000 | [diff] [blame] | 394 | Children.push_back(Variable); |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 395 | const SmallVector<LexicalScope *, 4> &Scopes = Scope->getChildren(); |
Devang Patel | 5bc9fec | 2011-02-19 01:31:27 +0000 | [diff] [blame] | 396 | for (unsigned j = 0, M = Scopes.size(); j < M; ++j) |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame^] | 397 | if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j])) |
Devang Patel | 5bc9fec | 2011-02-19 01:31:27 +0000 | [diff] [blame] | 398 | Children.push_back(Nested); |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 399 | DIScope DS(Scope->getScopeNode()); |
| 400 | DIE *ScopeDIE = NULL; |
| 401 | if (Scope->getInlinedAt()) |
| 402 | ScopeDIE = constructInlinedScopeDIE(Scope); |
| 403 | else if (DS.isSubprogram()) { |
Devang Patel | 0dd4558 | 2010-06-28 20:53:04 +0000 | [diff] [blame] | 404 | ProcessedSPNodes.insert(DS); |
Devang Patel | 8aa6147 | 2010-07-07 22:20:57 +0000 | [diff] [blame] | 405 | if (Scope->isAbstractScope()) { |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 406 | ScopeDIE = getCompileUnit(DS)->getDIE(DS); |
Devang Patel | 8aa6147 | 2010-07-07 22:20:57 +0000 | [diff] [blame] | 407 | // Note down abstract DIE. |
| 408 | if (ScopeDIE) |
| 409 | AbstractSPDies.insert(std::make_pair(DS, ScopeDIE)); |
| 410 | } |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 411 | else |
Devang Patel | 2db49d7 | 2010-05-07 18:11:54 +0000 | [diff] [blame] | 412 | ScopeDIE = updateSubprogramScopeDIE(DS); |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 413 | } |
Devang Patel | 5bc9fec | 2011-02-19 01:31:27 +0000 | [diff] [blame] | 414 | else { |
| 415 | // There is no need to emit empty lexical block DIE. |
| 416 | if (Children.empty()) |
| 417 | return NULL; |
Devang Patel | 3c91b05 | 2010-03-08 20:52:55 +0000 | [diff] [blame] | 418 | ScopeDIE = constructLexicalScopeDIE(Scope); |
Devang Patel | 5bc9fec | 2011-02-19 01:31:27 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 421 | if (!ScopeDIE) return NULL; |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 422 | |
Devang Patel | 5bc9fec | 2011-02-19 01:31:27 +0000 | [diff] [blame] | 423 | // Add children |
| 424 | for (SmallVector<DIE *, 8>::iterator I = Children.begin(), |
| 425 | E = Children.end(); I != E; ++I) |
| 426 | ScopeDIE->addChild(*I); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 427 | |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 428 | if (DS.isSubprogram()) |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 429 | getCompileUnit(DS)->addPubTypes(DISubprogram(DS)); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 430 | |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 431 | return ScopeDIE; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 434 | /// GetOrCreateSourceID - Look up the source id with the given directory and |
| 435 | /// source file names. If none currently exists, create a new id and insert it |
| 436 | /// in the SourceIds map. This can update DirectoryNames and SourceFileNames |
| 437 | /// maps as well. |
Devang Patel | 2f58485 | 2010-07-24 00:53:22 +0000 | [diff] [blame] | 438 | |
Devang Patel | 23670e5 | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 439 | unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName, |
| 440 | StringRef DirName) { |
Devang Patel | 1905a18 | 2010-09-16 20:57:49 +0000 | [diff] [blame] | 441 | // If FE did not provide a file name, then assume stdin. |
| 442 | if (FileName.empty()) |
Devang Patel | 23670e5 | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 443 | return GetOrCreateSourceID("<stdin>", StringRef()); |
| 444 | |
| 445 | // MCStream expects full path name as filename. |
Benjamin Kramer | ab5f788 | 2011-06-05 14:36:47 +0000 | [diff] [blame] | 446 | if (!DirName.empty() && !sys::path::is_absolute(FileName)) { |
| 447 | SmallString<128> FullPathName = DirName; |
| 448 | sys::path::append(FullPathName, FileName); |
Devang Patel | 23670e5 | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 449 | // Here FullPathName will be copied into StringMap by GetOrCreateSourceID. |
| 450 | return GetOrCreateSourceID(StringRef(FullPathName), StringRef()); |
| 451 | } |
Devang Patel | 1905a18 | 2010-09-16 20:57:49 +0000 | [diff] [blame] | 452 | |
Rafael Espindola | 5c05563 | 2010-11-18 02:04:25 +0000 | [diff] [blame] | 453 | StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName); |
| 454 | if (Entry.getValue()) |
| 455 | return Entry.getValue(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 456 | |
Rafael Espindola | 5c05563 | 2010-11-18 02:04:25 +0000 | [diff] [blame] | 457 | unsigned SrcId = SourceIdMap.size(); |
| 458 | Entry.setValue(SrcId); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 459 | |
Rafael Espindola | 5c05563 | 2010-11-18 02:04:25 +0000 | [diff] [blame] | 460 | // Print out a .file directive to specify files for .loc directives. |
Devang Patel | 23670e5 | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 461 | Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 462 | |
| 463 | return SrcId; |
| 464 | } |
| 465 | |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 466 | /// constructCompileUnit - Create new CompileUnit for the given |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 467 | /// metadata node with tag DW_TAG_compile_unit. |
Devang Patel | e9f8f5e | 2010-05-07 20:54:48 +0000 | [diff] [blame] | 468 | void DwarfDebug::constructCompileUnit(const MDNode *N) { |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 469 | DICompileUnit DIUnit(N); |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 470 | StringRef FN = DIUnit.getFilename(); |
| 471 | StringRef Dir = DIUnit.getDirectory(); |
Devang Patel | 23670e5 | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 472 | unsigned ID = GetOrCreateSourceID(FN, Dir); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 473 | |
| 474 | DIE *Die = new DIE(dwarf::DW_TAG_compile_unit); |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 475 | CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this); |
| 476 | NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string, |
| 477 | DIUnit.getProducer()); |
| 478 | NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2, |
| 479 | DIUnit.getLanguage()); |
| 480 | NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN); |
Devang Patel | 5098da0 | 2010-04-26 22:54:28 +0000 | [diff] [blame] | 481 | // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This |
| 482 | // simplifies debug range entries. |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 483 | NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0); |
Devang Patel | 4a602ca | 2010-03-22 23:11:36 +0000 | [diff] [blame] | 484 | // 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] | 485 | // compile unit in debug_line section. |
Rafael Espindola | dc52ecf | 2011-05-10 20:35:05 +0000 | [diff] [blame] | 486 | if(Asm->MAI->doesDwarfRequireRelocationForSectionOffset()) |
Rafael Espindola | 597a766 | 2011-05-04 17:44:06 +0000 | [diff] [blame] | 487 | NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 488 | Asm->GetTempSymbol("section_line")); |
Devang Patel | ae84d5b | 2010-08-31 23:50:19 +0000 | [diff] [blame] | 489 | else |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 490 | NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 491 | |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 492 | if (!Dir.empty()) |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 493 | NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 494 | if (DIUnit.isOptimized()) |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 495 | NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 496 | |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 497 | StringRef Flags = DIUnit.getFlags(); |
| 498 | if (!Flags.empty()) |
Devang Patel | 5bc942c | 2011-08-10 23:58:09 +0000 | [diff] [blame] | 499 | NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, |
| 500 | Flags); |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 501 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 502 | unsigned RVer = DIUnit.getRunTimeVersion(); |
| 503 | if (RVer) |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 504 | NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 505 | dwarf::DW_FORM_data1, RVer); |
| 506 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 507 | if (!FirstCU) |
| 508 | FirstCU = NewCU; |
| 509 | CUMap.insert(std::make_pair(N, NewCU)); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 512 | /// getCompileUnit - Get CompileUnit DIE. |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 513 | CompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const { |
| 514 | assert (N && "Invalid DwarfDebug::getCompileUnit argument!"); |
| 515 | DIDescriptor D(N); |
| 516 | const MDNode *CUNode = NULL; |
| 517 | if (D.isCompileUnit()) |
| 518 | CUNode = N; |
| 519 | else if (D.isSubprogram()) |
| 520 | CUNode = DISubprogram(N).getCompileUnit(); |
| 521 | else if (D.isType()) |
| 522 | CUNode = DIType(N).getCompileUnit(); |
| 523 | else if (D.isGlobalVariable()) |
| 524 | CUNode = DIGlobalVariable(N).getCompileUnit(); |
| 525 | else if (D.isVariable()) |
| 526 | CUNode = DIVariable(N).getCompileUnit(); |
| 527 | else if (D.isNameSpace()) |
| 528 | CUNode = DINameSpace(N).getCompileUnit(); |
| 529 | else if (D.isFile()) |
| 530 | CUNode = DIFile(N).getCompileUnit(); |
| 531 | else |
| 532 | return FirstCU; |
| 533 | |
| 534 | DenseMap<const MDNode *, CompileUnit *>::const_iterator I |
| 535 | = CUMap.find(CUNode); |
| 536 | if (I == CUMap.end()) |
| 537 | return FirstCU; |
| 538 | return I->second; |
| 539 | } |
| 540 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 541 | /// constructGlobalVariableDIE - Construct global variable DIE. |
Devang Patel | e9f8f5e | 2010-05-07 20:54:48 +0000 | [diff] [blame] | 542 | void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) { |
Devang Patel | 9fa539c | 2010-08-10 01:37:23 +0000 | [diff] [blame] | 543 | DIGlobalVariable GV(N); |
Daniel Dunbar | f612ff6 | 2009-09-19 20:40:05 +0000 | [diff] [blame] | 544 | |
Devang Patel | 905cf5e | 2009-09-04 23:59:07 +0000 | [diff] [blame] | 545 | // If debug information is malformed then ignore it. |
Devang Patel | 9fa539c | 2010-08-10 01:37:23 +0000 | [diff] [blame] | 546 | if (GV.Verify() == false) |
Devang Patel | 905cf5e | 2009-09-04 23:59:07 +0000 | [diff] [blame] | 547 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 548 | |
| 549 | // Check for pre-existence. |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 550 | CompileUnit *TheCU = getCompileUnit(N); |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 551 | TheCU->createGlobalVariableDIE(N); |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 552 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 555 | /// construct SubprogramDIE - Construct subprogram DIE. |
Devang Patel | e9f8f5e | 2010-05-07 20:54:48 +0000 | [diff] [blame] | 556 | void DwarfDebug::constructSubprogramDIE(const MDNode *N) { |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 557 | DISubprogram SP(N); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 558 | |
Stuart Hastings | 639336e | 2010-04-06 21:38:29 +0000 | [diff] [blame] | 559 | // Check for pre-existence. |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 560 | CompileUnit *TheCU = getCompileUnit(N); |
| 561 | if (TheCU->getDIE(N)) |
Stuart Hastings | 639336e | 2010-04-06 21:38:29 +0000 | [diff] [blame] | 562 | return; |
| 563 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 564 | if (!SP.isDefinition()) |
| 565 | // This is a method declaration which will be handled while constructing |
| 566 | // class type. |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 567 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 568 | |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 569 | DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP); |
Stuart Hastings | 639336e | 2010-04-06 21:38:29 +0000 | [diff] [blame] | 570 | |
| 571 | // Add to map. |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 572 | TheCU->insertDIE(N, SubprogramDie); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 573 | |
| 574 | // Add to context owner. |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 575 | TheCU->addToContextOwner(SubprogramDie, SP.getContext()); |
Devang Patel | 0000fad | 2009-12-08 23:21:45 +0000 | [diff] [blame] | 576 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 577 | // Expose as global. |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 578 | TheCU->addGlobal(SP.getName(), SubprogramDie); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 579 | |
Devang Patel | 13e16b6 | 2009-06-26 01:49:18 +0000 | [diff] [blame] | 580 | return; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 583 | /// beginModule - Emit all Dwarf sections that should come prior to the |
Daniel Dunbar | 0056499 | 2009-09-19 20:40:14 +0000 | [diff] [blame] | 584 | /// content. Create global DIEs and emit initial debug info sections. |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 585 | /// This is invoked by the target AsmPrinter. |
Chris Lattner | 75f5072 | 2010-04-04 07:48:20 +0000 | [diff] [blame] | 586 | void DwarfDebug::beginModule(Module *M) { |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 587 | if (DisableDebugInfoPrinting) |
| 588 | return; |
| 589 | |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 590 | // If module has named metadata anchors then use them, otherwise scan the |
| 591 | // module using debug info finder to collect debug info. |
Devang Patel | 30692ab | 2011-05-03 16:45:22 +0000 | [diff] [blame] | 592 | NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu"); |
| 593 | if (CU_Nodes) { |
Devang Patel | a938dd0 | 2011-05-04 16:34:02 +0000 | [diff] [blame] | 594 | |
| 595 | NamedMDNode *GV_Nodes = M->getNamedMetadata("llvm.dbg.gv"); |
| 596 | NamedMDNode *SP_Nodes = M->getNamedMetadata("llvm.dbg.sp"); |
| 597 | if (!GV_Nodes && !SP_Nodes) |
| 598 | // If there are not any global variables or any functions then |
| 599 | // there is not any debug info in this module. |
Devang Patel | 30692ab | 2011-05-03 16:45:22 +0000 | [diff] [blame] | 600 | return; |
| 601 | |
Devang Patel | a938dd0 | 2011-05-04 16:34:02 +0000 | [diff] [blame] | 602 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) |
| 603 | constructCompileUnit(CU_Nodes->getOperand(i)); |
Devang Patel | 30692ab | 2011-05-03 16:45:22 +0000 | [diff] [blame] | 604 | |
Devang Patel | a938dd0 | 2011-05-04 16:34:02 +0000 | [diff] [blame] | 605 | if (GV_Nodes) |
| 606 | for (unsigned i = 0, e = GV_Nodes->getNumOperands(); i != e; ++i) |
| 607 | constructGlobalVariableDIE(GV_Nodes->getOperand(i)); |
| 608 | |
| 609 | if (SP_Nodes) |
| 610 | for (unsigned i = 0, e = SP_Nodes->getNumOperands(); i != e; ++i) |
| 611 | constructSubprogramDIE(SP_Nodes->getOperand(i)); |
Devang Patel | 30692ab | 2011-05-03 16:45:22 +0000 | [diff] [blame] | 612 | |
| 613 | } else { |
| 614 | |
| 615 | DebugInfoFinder DbgFinder; |
| 616 | DbgFinder.processModule(*M); |
| 617 | |
Devang Patel | a938dd0 | 2011-05-04 16:34:02 +0000 | [diff] [blame] | 618 | bool HasDebugInfo = false; |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 619 | // Scan all the compile-units to see if there are any marked as the main |
| 620 | // unit. If not, we do not generate debug info. |
Devang Patel | 30692ab | 2011-05-03 16:45:22 +0000 | [diff] [blame] | 621 | for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(), |
| 622 | E = DbgFinder.compile_unit_end(); I != E; ++I) { |
| 623 | if (DICompileUnit(*I).isMain()) { |
| 624 | HasDebugInfo = true; |
| 625 | break; |
| 626 | } |
| 627 | } |
Devang Patel | a938dd0 | 2011-05-04 16:34:02 +0000 | [diff] [blame] | 628 | if (!HasDebugInfo) return; |
Devang Patel | 30692ab | 2011-05-03 16:45:22 +0000 | [diff] [blame] | 629 | |
| 630 | // Create all the compile unit DIEs. |
| 631 | for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(), |
| 632 | E = DbgFinder.compile_unit_end(); I != E; ++I) |
| 633 | constructCompileUnit(*I); |
| 634 | |
| 635 | // Create DIEs for each global variable. |
| 636 | for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(), |
| 637 | E = DbgFinder.global_variable_end(); I != E; ++I) |
| 638 | constructGlobalVariableDIE(*I); |
| 639 | |
| 640 | // Create DIEs for each subprogram. |
| 641 | for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(), |
| 642 | E = DbgFinder.subprogram_end(); I != E; ++I) |
| 643 | constructSubprogramDIE(*I); |
Chris Lattner | d850ac7 | 2010-04-05 02:19:28 +0000 | [diff] [blame] | 644 | } |
Devang Patel | 30692ab | 2011-05-03 16:45:22 +0000 | [diff] [blame] | 645 | |
Chris Lattner | d850ac7 | 2010-04-05 02:19:28 +0000 | [diff] [blame] | 646 | // Tell MMI that we have debug info. |
| 647 | MMI->setDebugInfoAvailability(true); |
Devang Patel | 30692ab | 2011-05-03 16:45:22 +0000 | [diff] [blame] | 648 | |
Chris Lattner | be15beb | 2010-04-04 23:17:54 +0000 | [diff] [blame] | 649 | // Emit initial sections. |
Chris Lattner | d850ac7 | 2010-04-05 02:19:28 +0000 | [diff] [blame] | 650 | EmitSectionLabels(); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 651 | |
Devang Patel | e7e5a0f | 2010-08-10 20:01:20 +0000 | [diff] [blame] | 652 | //getOrCreateTypeDIE |
| 653 | if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum")) |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 654 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { |
| 655 | DIType Ty(NMD->getOperand(i)); |
| 656 | getCompileUnit(Ty)->getOrCreateTypeDIE(Ty); |
| 657 | } |
Devang Patel | e7e5a0f | 2010-08-10 20:01:20 +0000 | [diff] [blame] | 658 | |
Devang Patel | 1a7ca03 | 2010-09-28 18:08:20 +0000 | [diff] [blame] | 659 | if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty")) |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 660 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { |
| 661 | DIType Ty(NMD->getOperand(i)); |
| 662 | getCompileUnit(Ty)->getOrCreateTypeDIE(Ty); |
| 663 | } |
Devang Patel | 1a7ca03 | 2010-09-28 18:08:20 +0000 | [diff] [blame] | 664 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 665 | // Prime section data. |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 666 | SectionMap.insert(Asm->getObjFileLowering().getTextSection()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 669 | /// endModule - Emit all Dwarf sections that should come after the content. |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 670 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 671 | void DwarfDebug::endModule() { |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 672 | if (!FirstCU) return; |
Devang Patel | 4a1cad6 | 2010-06-28 18:25:03 +0000 | [diff] [blame] | 673 | const Module *M = MMI->getModule(); |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 674 | DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap; |
Devang Patel | 4a1cad6 | 2010-06-28 18:25:03 +0000 | [diff] [blame] | 675 | if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) { |
| 676 | for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) { |
| 677 | if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue; |
| 678 | DISubprogram SP(AllSPs->getOperand(SI)); |
| 679 | if (!SP.Verify()) continue; |
| 680 | |
| 681 | // Collect info for variables that were optimized out. |
Devang Patel | 8b3a6b6 | 2010-07-19 17:53:55 +0000 | [diff] [blame] | 682 | if (!SP.isDefinition()) continue; |
Devang Patel | 4a1cad6 | 2010-06-28 18:25:03 +0000 | [diff] [blame] | 683 | StringRef FName = SP.getLinkageName(); |
| 684 | if (FName.empty()) |
| 685 | FName = SP.getName(); |
Devang Patel | 6236704 | 2010-11-10 22:19:21 +0000 | [diff] [blame] | 686 | NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName); |
Devang Patel | 4a1cad6 | 2010-06-28 18:25:03 +0000 | [diff] [blame] | 687 | if (!NMD) continue; |
| 688 | unsigned E = NMD->getNumOperands(); |
| 689 | if (!E) continue; |
Devang Patel | 5bc942c | 2011-08-10 23:58:09 +0000 | [diff] [blame] | 690 | LexicalScope *Scope = new LexicalScope(NULL, DIDescriptor(SP), NULL, |
| 691 | false); |
Devang Patel | e9a1cca | 2010-08-02 17:32:15 +0000 | [diff] [blame] | 692 | DeadFnScopeMap[SP] = Scope; |
Benjamin Kramer | 0fa5e05 | 2011-08-11 18:39:28 +0000 | [diff] [blame] | 693 | SmallVector<DbgVariable, 8> Variables; |
Devang Patel | 4a1cad6 | 2010-06-28 18:25:03 +0000 | [diff] [blame] | 694 | for (unsigned I = 0; I != E; ++I) { |
| 695 | DIVariable DV(NMD->getOperand(I)); |
| 696 | if (!DV.Verify()) continue; |
Devang Patel | 5a1a67c | 2011-08-15 19:01:20 +0000 | [diff] [blame] | 697 | Variables.push_back(DbgVariable(DV, NULL)); |
Devang Patel | 4a1cad6 | 2010-06-28 18:25:03 +0000 | [diff] [blame] | 698 | } |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 699 | |
Devang Patel | 4a1cad6 | 2010-06-28 18:25:03 +0000 | [diff] [blame] | 700 | // Construct subprogram DIE and add variables DIEs. |
| 701 | constructSubprogramDIE(SP); |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame^] | 702 | CompileUnit *SPCU = getCompileUnit(SP); |
| 703 | DIE *ScopeDIE = SPCU->getDIE(SP); |
Devang Patel | 4a1cad6 | 2010-06-28 18:25:03 +0000 | [diff] [blame] | 704 | for (unsigned i = 0, N = Variables.size(); i < N; ++i) { |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame^] | 705 | if (DIE *VariableDIE = |
| 706 | SPCU->constructVariableDIE(&Variables[i], Scope->isAbstractScope())) |
Devang Patel | 4a1cad6 | 2010-06-28 18:25:03 +0000 | [diff] [blame] | 707 | ScopeDIE->addChild(VariableDIE); |
| 708 | } |
| 709 | } |
| 710 | } |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 711 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 712 | // Attach DW_AT_inline attribute with inlined subprogram DIEs. |
| 713 | for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(), |
| 714 | AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) { |
| 715 | DIE *ISP = *AI; |
Devang Patel | 3cbee30 | 2011-04-12 22:53:02 +0000 | [diff] [blame] | 716 | FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 719 | // Emit DW_AT_containing_type attribute to connect types with their |
| 720 | // vtable holding type. |
| 721 | for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(), |
| 722 | CUE = CUMap.end(); CUI != CUE; ++CUI) { |
| 723 | CompileUnit *TheCU = CUI->second; |
| 724 | TheCU->constructContainingTypeDIEs(); |
Devang Patel | 5d11eb0 | 2009-12-03 19:11:07 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 727 | // Standard sections final addresses. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 728 | Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection()); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 729 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end")); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 730 | Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection()); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 731 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end")); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 732 | |
| 733 | // End text sections. |
| 734 | for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 735 | Asm->OutStreamer.SwitchSection(SectionMap[i]); |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 736 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i)); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 739 | // Compute DIE offsets and sizes. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 740 | computeSizeAndOffsets(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 741 | |
| 742 | // Emit all the DIEs into a debug info section |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 743 | emitDebugInfo(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 744 | |
| 745 | // Corresponding abbreviations into a abbrev section. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 746 | emitAbbreviations(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 747 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 748 | // Emit info into a debug pubnames section. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 749 | emitDebugPubNames(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 750 | |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 751 | // Emit info into a debug pubtypes section. |
| 752 | emitDebugPubTypes(); |
| 753 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 754 | // Emit info into a debug loc section. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 755 | emitDebugLoc(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 756 | |
| 757 | // Emit info into a debug aranges section. |
| 758 | EmitDebugARanges(); |
| 759 | |
| 760 | // Emit info into a debug ranges section. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 761 | emitDebugRanges(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 762 | |
| 763 | // Emit info into a debug macinfo section. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 764 | emitDebugMacInfo(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 765 | |
| 766 | // Emit inline info. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 767 | emitDebugInlineInfo(); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 768 | |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 769 | // Emit info into a debug str section. |
| 770 | emitDebugStr(); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 771 | |
Devang Patel | e9a1cca | 2010-08-02 17:32:15 +0000 | [diff] [blame] | 772 | // clean up. |
| 773 | DeleteContainerSeconds(DeadFnScopeMap); |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 774 | for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(), |
| 775 | E = CUMap.end(); I != E; ++I) |
| 776 | delete I->second; |
| 777 | FirstCU = NULL; // Reset for the next Module, if any. |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 780 | /// findAbstractVariable - Find abstract variable, if any, associated with Var. |
Devang Patel | b549bcf | 2011-08-10 21:50:54 +0000 | [diff] [blame] | 781 | DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV, |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 782 | DebugLoc ScopeLoc) { |
Devang Patel | b549bcf | 2011-08-10 21:50:54 +0000 | [diff] [blame] | 783 | LLVMContext &Ctx = DV->getContext(); |
| 784 | // More then one inlined variable corresponds to one abstract variable. |
| 785 | DIVariable Var = cleanseInlinedVariable(DV, Ctx); |
Devang Patel | 2db49d7 | 2010-05-07 18:11:54 +0000 | [diff] [blame] | 786 | DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 787 | if (AbsDbgVariable) |
| 788 | return AbsDbgVariable; |
| 789 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 790 | LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx)); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 791 | if (!Scope) |
| 792 | return NULL; |
| 793 | |
Devang Patel | 5a1a67c | 2011-08-15 19:01:20 +0000 | [diff] [blame] | 794 | AbsDbgVariable = new DbgVariable(Var, NULL); |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 795 | addScopeVariable(Scope, AbsDbgVariable); |
Devang Patel | 2db49d7 | 2010-05-07 18:11:54 +0000 | [diff] [blame] | 796 | AbstractVariables[Var] = AbsDbgVariable; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 797 | return AbsDbgVariable; |
| 798 | } |
| 799 | |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 800 | /// addCurrentFnArgument - If Var is a current function argument then add |
| 801 | /// it to CurrentFnArguments list. |
Devang Patel | 0478c15 | 2011-03-01 22:58:55 +0000 | [diff] [blame] | 802 | bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF, |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 803 | DbgVariable *Var, LexicalScope *Scope) { |
| 804 | if (!LScopes.isCurrentFunctionScope(Scope)) |
Devang Patel | 0478c15 | 2011-03-01 22:58:55 +0000 | [diff] [blame] | 805 | return false; |
| 806 | DIVariable DV = Var->getVariable(); |
| 807 | if (DV.getTag() != dwarf::DW_TAG_arg_variable) |
| 808 | return false; |
| 809 | unsigned ArgNo = DV.getArgNumber(); |
| 810 | if (ArgNo == 0) |
| 811 | return false; |
| 812 | |
Devang Patel | cb3a657 | 2011-03-03 20:02:02 +0000 | [diff] [blame] | 813 | size_t Size = CurrentFnArguments.size(); |
| 814 | if (Size == 0) |
Devang Patel | 0478c15 | 2011-03-01 22:58:55 +0000 | [diff] [blame] | 815 | CurrentFnArguments.resize(MF->getFunction()->arg_size()); |
Devang Patel | bbd0f45 | 2011-03-03 21:49:41 +0000 | [diff] [blame] | 816 | // llvm::Function argument size is not good indicator of how many |
Devang Patel | 6f676be | 2011-03-03 20:08:10 +0000 | [diff] [blame] | 817 | // arguments does the function have at source level. |
| 818 | if (ArgNo > Size) |
Devang Patel | cb3a657 | 2011-03-03 20:02:02 +0000 | [diff] [blame] | 819 | CurrentFnArguments.resize(ArgNo * 2); |
Devang Patel | 0478c15 | 2011-03-01 22:58:55 +0000 | [diff] [blame] | 820 | CurrentFnArguments[ArgNo - 1] = Var; |
| 821 | return true; |
| 822 | } |
| 823 | |
Devang Patel | ee43286 | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 824 | /// collectVariableInfoFromMMITable - Collect variable information from |
| 825 | /// side table maintained by MMI. |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 826 | void |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 827 | DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF, |
Devang Patel | ee43286 | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 828 | SmallPtrSet<const MDNode *, 16> &Processed) { |
Devang Patel | e717faa | 2009-10-06 01:26:37 +0000 | [diff] [blame] | 829 | MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo(); |
| 830 | for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(), |
| 831 | VE = VMap.end(); VI != VE; ++VI) { |
Devang Patel | e9f8f5e | 2010-05-07 20:54:48 +0000 | [diff] [blame] | 832 | const MDNode *Var = VI->first; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 833 | if (!Var) continue; |
Devang Patel | 98e1cac | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 834 | Processed.insert(Var); |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 835 | DIVariable DV(Var); |
| 836 | const std::pair<unsigned, DebugLoc> &VP = VI->second; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 837 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 838 | LexicalScope *Scope = LScopes.findLexicalScope(VP.second); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 839 | |
Devang Patel | fb0ee43 | 2009-11-10 23:20:04 +0000 | [diff] [blame] | 840 | // If variable scope is not found then skip this variable. |
Chris Lattner | de4845c | 2010-04-02 19:42:39 +0000 | [diff] [blame] | 841 | if (Scope == 0) |
Devang Patel | fb0ee43 | 2009-11-10 23:20:04 +0000 | [diff] [blame] | 842 | continue; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 843 | |
Devang Patel | 26c1e56 | 2010-05-20 16:36:41 +0000 | [diff] [blame] | 844 | DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second); |
Devang Patel | 5a1a67c | 2011-08-15 19:01:20 +0000 | [diff] [blame] | 845 | DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable); |
Devang Patel | ff9dd0a | 2011-08-15 21:24:36 +0000 | [diff] [blame] | 846 | RegVar->setFrameIndex(VP.first); |
Devang Patel | 0478c15 | 2011-03-01 22:58:55 +0000 | [diff] [blame] | 847 | if (!addCurrentFnArgument(MF, RegVar, Scope)) |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 848 | addScopeVariable(Scope, RegVar); |
Devang Patel | 5a1a67c | 2011-08-15 19:01:20 +0000 | [diff] [blame] | 849 | if (AbsDbgVariable) |
Devang Patel | ff9dd0a | 2011-08-15 21:24:36 +0000 | [diff] [blame] | 850 | AbsDbgVariable->setFrameIndex(VP.first); |
Devang Patel | e717faa | 2009-10-06 01:26:37 +0000 | [diff] [blame] | 851 | } |
Devang Patel | ee43286 | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 852 | } |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 853 | |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 854 | /// isDbgValueInDefinedReg - Return true if debug value, encoded by |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 855 | /// DBG_VALUE instruction, is in a defined reg. |
| 856 | static bool isDbgValueInDefinedReg(const MachineInstr *MI) { |
| 857 | assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!"); |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 858 | return MI->getNumOperands() == 3 && |
| 859 | MI->getOperand(0).isReg() && MI->getOperand(0).getReg() && |
| 860 | MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0; |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 863 | /// getDebugLocEntry - Get .debug_loc entry for the instruction range starting |
Devang Patel | 90b4041 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 864 | /// at MI. |
| 865 | static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm, |
| 866 | const MCSymbol *FLabel, |
| 867 | const MCSymbol *SLabel, |
| 868 | const MachineInstr *MI) { |
| 869 | const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata(); |
| 870 | |
| 871 | if (MI->getNumOperands() != 3) { |
| 872 | MachineLocation MLoc = Asm->getDebugValueLocation(MI); |
| 873 | return DotDebugLocEntry(FLabel, SLabel, MLoc, Var); |
| 874 | } |
| 875 | if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm()) { |
| 876 | MachineLocation MLoc; |
| 877 | MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm()); |
| 878 | return DotDebugLocEntry(FLabel, SLabel, MLoc, Var); |
| 879 | } |
| 880 | if (MI->getOperand(0).isImm()) |
| 881 | return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm()); |
| 882 | if (MI->getOperand(0).isFPImm()) |
| 883 | return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm()); |
| 884 | if (MI->getOperand(0).isCImm()) |
| 885 | return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm()); |
| 886 | |
| 887 | assert (0 && "Unexpected 3 operand DBG_VALUE instruction!"); |
| 888 | return DotDebugLocEntry(); |
| 889 | } |
| 890 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 891 | /// collectVariableInfo - Find variables for each lexical scope. |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 892 | void |
Devang Patel | 78e127d | 2010-06-25 22:07:34 +0000 | [diff] [blame] | 893 | DwarfDebug::collectVariableInfo(const MachineFunction *MF, |
| 894 | SmallPtrSet<const MDNode *, 16> &Processed) { |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 895 | |
Devang Patel | ee43286 | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 896 | /// collection info from MMI table. |
| 897 | collectVariableInfoFromMMITable(MF, Processed); |
| 898 | |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 899 | for (SmallVectorImpl<const MDNode*>::const_iterator |
| 900 | UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE; |
| 901 | ++UVI) { |
| 902 | const MDNode *Var = *UVI; |
| 903 | if (Processed.count(Var)) |
Devang Patel | ee43286 | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 904 | continue; |
| 905 | |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 906 | // History contains relevant DBG_VALUE instructions for Var and instructions |
| 907 | // clobbering it. |
| 908 | SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var]; |
| 909 | if (History.empty()) |
| 910 | continue; |
| 911 | const MachineInstr *MInsn = History.front(); |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 912 | |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 913 | DIVariable DV(Var); |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 914 | LexicalScope *Scope = NULL; |
Devang Patel | d8720f4 | 2010-05-27 20:25:04 +0000 | [diff] [blame] | 915 | if (DV.getTag() == dwarf::DW_TAG_arg_variable && |
| 916 | DISubprogram(DV.getContext()).describes(MF->getFunction())) |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 917 | Scope = LScopes.getCurrentFunctionScope(); |
Devang Patel | 40c7e41 | 2011-07-20 22:18:50 +0000 | [diff] [blame] | 918 | else { |
| 919 | if (DV.getVersion() <= LLVMDebugVersion9) |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 920 | Scope = LScopes.findLexicalScope(MInsn->getDebugLoc()); |
Devang Patel | 40c7e41 | 2011-07-20 22:18:50 +0000 | [diff] [blame] | 921 | else { |
| 922 | if (MDNode *IA = DV.getInlinedAt()) |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 923 | Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA)); |
Devang Patel | 40c7e41 | 2011-07-20 22:18:50 +0000 | [diff] [blame] | 924 | else |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 925 | Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1))); |
Devang Patel | 40c7e41 | 2011-07-20 22:18:50 +0000 | [diff] [blame] | 926 | } |
| 927 | } |
Devang Patel | ee43286 | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 928 | // If variable scope is not found then skip this variable. |
Devang Patel | c0c5a26 | 2010-05-21 00:10:20 +0000 | [diff] [blame] | 929 | if (!Scope) |
Devang Patel | ee43286 | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 930 | continue; |
| 931 | |
| 932 | Processed.insert(DV); |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 933 | assert(MInsn->isDebugValue() && "History must begin with debug value"); |
Devang Patel | 5a1a67c | 2011-08-15 19:01:20 +0000 | [diff] [blame] | 934 | DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc()); |
| 935 | DbgVariable *RegVar = new DbgVariable(DV, AbsVar); |
Devang Patel | 0478c15 | 2011-03-01 22:58:55 +0000 | [diff] [blame] | 936 | if (!addCurrentFnArgument(MF, RegVar, Scope)) |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 937 | addScopeVariable(Scope, RegVar); |
Devang Patel | 5a1a67c | 2011-08-15 19:01:20 +0000 | [diff] [blame] | 938 | if (AbsVar) |
Devang Patel | ff9dd0a | 2011-08-15 21:24:36 +0000 | [diff] [blame] | 939 | AbsVar->setMInsn(MInsn); |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 940 | |
| 941 | // Simple ranges that are fully coalesced. |
| 942 | if (History.size() <= 1 || (History.size() == 2 && |
| 943 | MInsn->isIdenticalTo(History.back()))) { |
Devang Patel | ff9dd0a | 2011-08-15 21:24:36 +0000 | [diff] [blame] | 944 | RegVar->setMInsn(MInsn); |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 945 | continue; |
| 946 | } |
| 947 | |
| 948 | // handle multiple DBG_VALUE instructions describing one variable. |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 949 | RegVar->setDotDebugLocOffset(DotDebugLocEntries.size()); |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 950 | |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 951 | for (SmallVectorImpl<const MachineInstr*>::const_iterator |
| 952 | HI = History.begin(), HE = History.end(); HI != HE; ++HI) { |
| 953 | const MachineInstr *Begin = *HI; |
| 954 | assert(Begin->isDebugValue() && "Invalid History entry"); |
Jakob Stoklund Olesen | e17232e | 2011-03-22 00:21:41 +0000 | [diff] [blame] | 955 | |
Devang Patel | 4ada1d7 | 2011-06-01 23:00:17 +0000 | [diff] [blame] | 956 | // Check if DBG_VALUE is truncating a range. |
| 957 | if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg() |
| 958 | && !Begin->getOperand(0).getReg()) |
| 959 | continue; |
| 960 | |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 961 | // Compute the range for a register location. |
| 962 | const MCSymbol *FLabel = getLabelBeforeInsn(Begin); |
| 963 | const MCSymbol *SLabel = 0; |
| 964 | |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 965 | if (HI + 1 == HE) |
| 966 | // If Begin is the last instruction in History then its value is valid |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 967 | // until the end of the function. |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 968 | SLabel = FunctionEndSym; |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 969 | else { |
| 970 | const MachineInstr *End = HI[1]; |
Devang Patel | 476df5f | 2011-07-07 21:44:42 +0000 | [diff] [blame] | 971 | DEBUG(dbgs() << "DotDebugLoc Pair:\n" |
| 972 | << "\t" << *Begin << "\t" << *End << "\n"); |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 973 | if (End->isDebugValue()) |
| 974 | SLabel = getLabelBeforeInsn(End); |
| 975 | else { |
| 976 | // End is a normal instruction clobbering the range. |
| 977 | SLabel = getLabelAfterInsn(End); |
| 978 | assert(SLabel && "Forgot label after clobber instruction"); |
| 979 | ++HI; |
| 980 | } |
| 981 | } |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 982 | |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 983 | // The value is valid until the next DBG_VALUE or clobber. |
Devang Patel | 90b4041 | 2011-07-08 17:09:57 +0000 | [diff] [blame] | 984 | DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel, Begin)); |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 985 | } |
| 986 | DotDebugLocEntries.push_back(DotDebugLocEntry()); |
Devang Patel | 90a48ad | 2010-03-15 18:33:46 +0000 | [diff] [blame] | 987 | } |
Devang Patel | 98e1cac | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 988 | |
| 989 | // Collect info for variables that were optimized out. |
Devang Patel | d1bbc6b | 2010-06-22 01:01:58 +0000 | [diff] [blame] | 990 | const Function *F = MF->getFunction(); |
Devang Patel | 6236704 | 2010-11-10 22:19:21 +0000 | [diff] [blame] | 991 | if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) { |
Devang Patel | 98e1cac | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 992 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { |
Dan Gohman | 872814a | 2010-07-21 18:54:18 +0000 | [diff] [blame] | 993 | DIVariable DV(cast<MDNode>(NMD->getOperand(i))); |
Devang Patel | ee43286 | 2010-05-20 19:57:06 +0000 | [diff] [blame] | 994 | if (!DV || !Processed.insert(DV)) |
Devang Patel | 98e1cac | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 995 | continue; |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 996 | if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext())) |
Devang Patel | 5a1a67c | 2011-08-15 19:01:20 +0000 | [diff] [blame] | 997 | addScopeVariable(Scope, new DbgVariable(DV, NULL)); |
Devang Patel | 98e1cac | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 998 | } |
| 999 | } |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1000 | } |
Devang Patel | 98e1cac | 2010-05-14 21:01:35 +0000 | [diff] [blame] | 1001 | |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1002 | /// getLabelBeforeInsn - Return Label preceding the instruction. |
| 1003 | const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) { |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1004 | MCSymbol *Label = LabelsBeforeInsn.lookup(MI); |
| 1005 | assert(Label && "Didn't insert label before instruction"); |
| 1006 | return Label; |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /// getLabelAfterInsn - Return Label immediately following the instruction. |
| 1010 | const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) { |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1011 | return LabelsAfterInsn.lookup(MI); |
Devang Patel | e717faa | 2009-10-06 01:26:37 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Devang Patel | cbbe287 | 2010-10-26 17:49:02 +0000 | [diff] [blame] | 1014 | /// beginInstruction - Process beginning of an instruction. |
| 1015 | void DwarfDebug::beginInstruction(const MachineInstr *MI) { |
Jakob Stoklund Olesen | 15a3ea0 | 2011-03-25 17:20:59 +0000 | [diff] [blame] | 1016 | // Check if source location changes, but ignore DBG_VALUE locations. |
| 1017 | if (!MI->isDebugValue()) { |
| 1018 | DebugLoc DL = MI->getDebugLoc(); |
| 1019 | if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) { |
Devang Patel | 4243e67 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1020 | unsigned Flags = DWARF2_FLAG_IS_STMT; |
Jakob Stoklund Olesen | 15a3ea0 | 2011-03-25 17:20:59 +0000 | [diff] [blame] | 1021 | PrevInstLoc = DL; |
Devang Patel | 4243e67 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1022 | if (DL == PrologEndLoc) { |
| 1023 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 1024 | PrologEndLoc = DebugLoc(); |
| 1025 | } |
Jakob Stoklund Olesen | 15a3ea0 | 2011-03-25 17:20:59 +0000 | [diff] [blame] | 1026 | if (!DL.isUnknown()) { |
| 1027 | const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext()); |
Devang Patel | 4243e67 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1028 | recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags); |
Jakob Stoklund Olesen | 15a3ea0 | 2011-03-25 17:20:59 +0000 | [diff] [blame] | 1029 | } else |
Devang Patel | 4243e67 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1030 | recordSourceLine(0, 0, 0, 0); |
Jakob Stoklund Olesen | 15a3ea0 | 2011-03-25 17:20:59 +0000 | [diff] [blame] | 1031 | } |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1032 | } |
Devang Patel | aead63c | 2010-03-29 22:59:58 +0000 | [diff] [blame] | 1033 | |
Jakob Stoklund Olesen | 15a3ea0 | 2011-03-25 17:20:59 +0000 | [diff] [blame] | 1034 | // Insert labels where requested. |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1035 | DenseMap<const MachineInstr*, MCSymbol*>::iterator I = |
| 1036 | LabelsBeforeInsn.find(MI); |
| 1037 | |
| 1038 | // No label needed. |
| 1039 | if (I == LabelsBeforeInsn.end()) |
| 1040 | return; |
| 1041 | |
| 1042 | // Label already assigned. |
| 1043 | if (I->second) |
Devang Patel | b2b31a6 | 2010-05-26 19:37:24 +0000 | [diff] [blame] | 1044 | return; |
Devang Patel | 553881b | 2010-03-29 17:20:31 +0000 | [diff] [blame] | 1045 | |
Jakob Stoklund Olesen | 15a3ea0 | 2011-03-25 17:20:59 +0000 | [diff] [blame] | 1046 | if (!PrevLabel) { |
Devang Patel | 77051f5 | 2010-05-26 21:23:46 +0000 | [diff] [blame] | 1047 | PrevLabel = MMI->getContext().CreateTempSymbol(); |
| 1048 | Asm->OutStreamer.EmitLabel(PrevLabel); |
Devang Patel | b2b31a6 | 2010-05-26 19:37:24 +0000 | [diff] [blame] | 1049 | } |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1050 | I->second = PrevLabel; |
Devang Patel | 0d20ac8 | 2009-10-06 01:50:42 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Devang Patel | cbbe287 | 2010-10-26 17:49:02 +0000 | [diff] [blame] | 1053 | /// endInstruction - Process end of an instruction. |
| 1054 | void DwarfDebug::endInstruction(const MachineInstr *MI) { |
Jakob Stoklund Olesen | 15a3ea0 | 2011-03-25 17:20:59 +0000 | [diff] [blame] | 1055 | // Don't create a new label after DBG_VALUE instructions. |
| 1056 | // They don't generate code. |
| 1057 | if (!MI->isDebugValue()) |
| 1058 | PrevLabel = 0; |
| 1059 | |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1060 | DenseMap<const MachineInstr*, MCSymbol*>::iterator I = |
| 1061 | LabelsAfterInsn.find(MI); |
| 1062 | |
| 1063 | // No label needed. |
| 1064 | if (I == LabelsAfterInsn.end()) |
| 1065 | return; |
| 1066 | |
| 1067 | // Label already assigned. |
| 1068 | if (I->second) |
Jakob Stoklund Olesen | 15a3ea0 | 2011-03-25 17:20:59 +0000 | [diff] [blame] | 1069 | return; |
| 1070 | |
| 1071 | // We need a label after this instruction. |
| 1072 | if (!PrevLabel) { |
| 1073 | PrevLabel = MMI->getContext().CreateTempSymbol(); |
| 1074 | Asm->OutStreamer.EmitLabel(PrevLabel); |
Devang Patel | 1c24635 | 2010-04-08 16:50:29 +0000 | [diff] [blame] | 1075 | } |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1076 | I->second = PrevLabel; |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1079 | /// identifyScopeMarkers() - |
Devang Patel | 5bc942c | 2011-08-10 23:58:09 +0000 | [diff] [blame] | 1080 | /// Each LexicalScope has first instruction and last instruction to mark |
| 1081 | /// beginning and end of a scope respectively. Create an inverse map that list |
| 1082 | /// scopes starts (and ends) with an instruction. One instruction may start (or |
| 1083 | /// end) multiple scopes. Ignore scopes that are not reachable. |
Devang Patel | e37b0c6 | 2010-04-08 18:43:56 +0000 | [diff] [blame] | 1084 | void DwarfDebug::identifyScopeMarkers() { |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1085 | SmallVector<LexicalScope *, 4> WorkList; |
| 1086 | WorkList.push_back(LScopes.getCurrentFunctionScope()); |
Devang Patel | 42aafd7 | 2010-01-20 02:05:23 +0000 | [diff] [blame] | 1087 | while (!WorkList.empty()) { |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1088 | LexicalScope *S = WorkList.pop_back_val(); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1089 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1090 | const SmallVector<LexicalScope *, 4> &Children = S->getChildren(); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1091 | if (!Children.empty()) |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1092 | for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(), |
Devang Patel | 42aafd7 | 2010-01-20 02:05:23 +0000 | [diff] [blame] | 1093 | SE = Children.end(); SI != SE; ++SI) |
| 1094 | WorkList.push_back(*SI); |
| 1095 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1096 | if (S->isAbstractScope()) |
| 1097 | continue; |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1098 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1099 | const SmallVector<InsnRange, 4> &Ranges = S->getRanges(); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1100 | if (Ranges.empty()) |
| 1101 | continue; |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1102 | for (SmallVector<InsnRange, 4>::const_iterator RI = Ranges.begin(), |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1103 | RE = Ranges.end(); RI != RE; ++RI) { |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1104 | assert(RI->first && "InsnRange does not have first instruction!"); |
| 1105 | assert(RI->second && "InsnRange does not have second instruction!"); |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1106 | requestLabelBeforeInsn(RI->first); |
| 1107 | requestLabelAfterInsn(RI->second); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1108 | } |
Devang Patel | af9e847 | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 1109 | } |
Devang Patel | af9e847 | 2009-10-01 20:31:14 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Devang Patel | a3f4867 | 2011-05-09 22:14:49 +0000 | [diff] [blame] | 1112 | /// getScopeNode - Get MDNode for DebugLoc's scope. |
| 1113 | static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) { |
| 1114 | if (MDNode *InlinedAt = DL.getInlinedAt(Ctx)) |
| 1115 | return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx); |
| 1116 | return DL.getScope(Ctx); |
| 1117 | } |
| 1118 | |
Devang Patel | 4243e67 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1119 | /// getFnDebugLoc - Walk up the scope chain of given debug loc and find |
| 1120 | /// line number info for the function. |
| 1121 | static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) { |
| 1122 | const MDNode *Scope = getScopeNode(DL, Ctx); |
| 1123 | DISubprogram SP = getDISubprogram(Scope); |
| 1124 | if (SP.Verify()) |
| 1125 | return DebugLoc::get(SP.getLineNumber(), 0, SP); |
| 1126 | return DebugLoc(); |
| 1127 | } |
| 1128 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1129 | /// beginFunction - Gather pre-function debug information. Assumes being |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1130 | /// emitted immediately after the function entry point. |
Chris Lattner | eec791a | 2010-01-26 23:18:02 +0000 | [diff] [blame] | 1131 | void DwarfDebug::beginFunction(const MachineFunction *MF) { |
Chris Lattner | 994cb12 | 2010-04-05 03:52:55 +0000 | [diff] [blame] | 1132 | if (!MMI->hasDebugInfo()) return; |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1133 | LScopes.initialize(*MF); |
| 1134 | if (LScopes.empty()) return; |
| 1135 | identifyScopeMarkers(); |
Devang Patel | 60b35bd | 2009-10-06 18:37:31 +0000 | [diff] [blame] | 1136 | |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1137 | FunctionBeginSym = Asm->GetTempSymbol("func_begin", |
| 1138 | Asm->getFunctionNumber()); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1139 | // Assumes in correct section after the entry point. |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1140 | Asm->OutStreamer.EmitLabel(FunctionBeginSym); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1141 | |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1142 | assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned"); |
| 1143 | |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 1144 | const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo(); |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 1145 | /// LiveUserVar - Map physreg numbers to the MDNode they contain. |
| 1146 | std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs()); |
| 1147 | |
Devang Patel | b2b31a6 | 2010-05-26 19:37:24 +0000 | [diff] [blame] | 1148 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1149 | I != E; ++I) { |
| 1150 | bool AtBlockEntry = true; |
Devang Patel | b2b31a6 | 2010-05-26 19:37:24 +0000 | [diff] [blame] | 1151 | for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); |
| 1152 | II != IE; ++II) { |
| 1153 | const MachineInstr *MI = II; |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1154 | |
Devang Patel | b2b31a6 | 2010-05-26 19:37:24 +0000 | [diff] [blame] | 1155 | if (MI->isDebugValue()) { |
Devang Patel | b2b31a6 | 2010-05-26 19:37:24 +0000 | [diff] [blame] | 1156 | assert (MI->getNumOperands() > 1 && "Invalid machine instruction!"); |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 1157 | |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1158 | // Keep track of user variables. |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 1159 | const MDNode *Var = |
| 1160 | MI->getOperand(MI->getNumOperands() - 1).getMetadata(); |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1161 | |
| 1162 | // Variable is in a register, we need to check for clobbers. |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 1163 | if (isDbgValueInDefinedReg(MI)) |
| 1164 | LiveUserVar[MI->getOperand(0).getReg()] = Var; |
| 1165 | |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1166 | // Check the history of this variable. |
| 1167 | SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var]; |
| 1168 | if (History.empty()) { |
| 1169 | UserVariables.push_back(Var); |
| 1170 | // The first mention of a function argument gets the FunctionBeginSym |
| 1171 | // label, so arguments are visible when breaking at function entry. |
| 1172 | DIVariable DV(Var); |
| 1173 | if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable && |
| 1174 | DISubprogram(getDISubprogram(DV.getContext())) |
| 1175 | .describes(MF->getFunction())) |
| 1176 | LabelsBeforeInsn[MI] = FunctionBeginSym; |
| 1177 | } else { |
| 1178 | // We have seen this variable before. Try to coalesce DBG_VALUEs. |
| 1179 | const MachineInstr *Prev = History.back(); |
| 1180 | if (Prev->isDebugValue()) { |
| 1181 | // Coalesce identical entries at the end of History. |
| 1182 | if (History.size() >= 2 && |
Devang Patel | 7986289 | 2011-07-07 00:14:27 +0000 | [diff] [blame] | 1183 | Prev->isIdenticalTo(History[History.size() - 2])) { |
| 1184 | DEBUG(dbgs() << "Coalesce identical DBG_VALUE entries:\n" |
| 1185 | << "\t" << *Prev |
| 1186 | << "\t" << *History[History.size() - 2] << "\n"); |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1187 | History.pop_back(); |
Devang Patel | 7986289 | 2011-07-07 00:14:27 +0000 | [diff] [blame] | 1188 | } |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1189 | |
| 1190 | // Terminate old register assignments that don't reach MI; |
| 1191 | MachineFunction::const_iterator PrevMBB = Prev->getParent(); |
| 1192 | if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) && |
| 1193 | isDbgValueInDefinedReg(Prev)) { |
| 1194 | // Previous register assignment needs to terminate at the end of |
| 1195 | // its basic block. |
| 1196 | MachineBasicBlock::const_iterator LastMI = |
| 1197 | PrevMBB->getLastNonDebugInstr(); |
Devang Patel | 7986289 | 2011-07-07 00:14:27 +0000 | [diff] [blame] | 1198 | if (LastMI == PrevMBB->end()) { |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1199 | // Drop DBG_VALUE for empty range. |
Devang Patel | 7986289 | 2011-07-07 00:14:27 +0000 | [diff] [blame] | 1200 | DEBUG(dbgs() << "Drop DBG_VALUE for empty range:\n" |
| 1201 | << "\t" << *Prev << "\n"); |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1202 | History.pop_back(); |
Devang Patel | 7986289 | 2011-07-07 00:14:27 +0000 | [diff] [blame] | 1203 | } |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1204 | else { |
| 1205 | // Terminate after LastMI. |
| 1206 | History.push_back(LastMI); |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | History.push_back(MI); |
Devang Patel | b2b31a6 | 2010-05-26 19:37:24 +0000 | [diff] [blame] | 1212 | } else { |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1213 | // Not a DBG_VALUE instruction. |
| 1214 | if (!MI->isLabel()) |
| 1215 | AtBlockEntry = false; |
| 1216 | |
Devang Patel | 4243e67 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1217 | // First known non DBG_VALUE location marks beginning of function |
| 1218 | // body. |
| 1219 | if (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()) |
| 1220 | PrologEndLoc = MI->getDebugLoc(); |
| 1221 | |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 1222 | // Check if the instruction clobbers any registers with debug vars. |
| 1223 | for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(), |
| 1224 | MOE = MI->operands_end(); MOI != MOE; ++MOI) { |
| 1225 | if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg()) |
| 1226 | continue; |
| 1227 | for (const unsigned *AI = TRI->getOverlaps(MOI->getReg()); |
| 1228 | unsigned Reg = *AI; ++AI) { |
| 1229 | const MDNode *Var = LiveUserVar[Reg]; |
| 1230 | if (!Var) |
| 1231 | continue; |
| 1232 | // Reg is now clobbered. |
| 1233 | LiveUserVar[Reg] = 0; |
| 1234 | |
| 1235 | // Was MD last defined by a DBG_VALUE referring to Reg? |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1236 | DbgValueHistoryMap::iterator HistI = DbgValues.find(Var); |
| 1237 | if (HistI == DbgValues.end()) |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 1238 | continue; |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1239 | SmallVectorImpl<const MachineInstr*> &History = HistI->second; |
| 1240 | if (History.empty()) |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 1241 | continue; |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1242 | const MachineInstr *Prev = History.back(); |
| 1243 | // Sanity-check: Register assignments are terminated at the end of |
| 1244 | // their block. |
| 1245 | if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent()) |
| 1246 | continue; |
| 1247 | // Is the variable still in Reg? |
| 1248 | if (!isDbgValueInDefinedReg(Prev) || |
| 1249 | Prev->getOperand(0).getReg() != Reg) |
| 1250 | continue; |
| 1251 | // Var is clobbered. Make sure the next instruction gets a label. |
| 1252 | History.push_back(MI); |
Jakob Stoklund Olesen | 28cf115 | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 1253 | } |
| 1254 | } |
Devang Patel | b2b31a6 | 2010-05-26 19:37:24 +0000 | [diff] [blame] | 1255 | } |
Devang Patel | b2b31a6 | 2010-05-26 19:37:24 +0000 | [diff] [blame] | 1256 | } |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end(); |
| 1260 | I != E; ++I) { |
| 1261 | SmallVectorImpl<const MachineInstr*> &History = I->second; |
| 1262 | if (History.empty()) |
| 1263 | continue; |
| 1264 | |
| 1265 | // Make sure the final register assignments are terminated. |
| 1266 | const MachineInstr *Prev = History.back(); |
| 1267 | if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) { |
| 1268 | const MachineBasicBlock *PrevMBB = Prev->getParent(); |
Devang Patel | 5bc942c | 2011-08-10 23:58:09 +0000 | [diff] [blame] | 1269 | MachineBasicBlock::const_iterator LastMI = |
| 1270 | PrevMBB->getLastNonDebugInstr(); |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1271 | if (LastMI == PrevMBB->end()) |
| 1272 | // Drop DBG_VALUE for empty range. |
| 1273 | History.pop_back(); |
| 1274 | else { |
| 1275 | // Terminate after LastMI. |
| 1276 | History.push_back(LastMI); |
| 1277 | } |
| 1278 | } |
| 1279 | // Request labels for the full history. |
| 1280 | for (unsigned i = 0, e = History.size(); i != e; ++i) { |
| 1281 | const MachineInstr *MI = History[i]; |
| 1282 | if (MI->isDebugValue()) |
| 1283 | requestLabelBeforeInsn(MI); |
| 1284 | else |
| 1285 | requestLabelAfterInsn(MI); |
| 1286 | } |
| 1287 | } |
Devang Patel | b2b31a6 | 2010-05-26 19:37:24 +0000 | [diff] [blame] | 1288 | |
Jakob Stoklund Olesen | 15a3ea0 | 2011-03-25 17:20:59 +0000 | [diff] [blame] | 1289 | PrevInstLoc = DebugLoc(); |
Devang Patel | b2b31a6 | 2010-05-26 19:37:24 +0000 | [diff] [blame] | 1290 | PrevLabel = FunctionBeginSym; |
Devang Patel | 4243e67 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Record beginning of function. |
| 1293 | if (!PrologEndLoc.isUnknown()) { |
| 1294 | DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc, |
| 1295 | MF->getFunction()->getContext()); |
| 1296 | recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(), |
| 1297 | FnStartDL.getScope(MF->getFunction()->getContext()), |
| 1298 | DWARF2_FLAG_IS_STMT); |
| 1299 | } |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1302 | void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) { |
| 1303 | // SmallVector<DbgVariable *, 8> &Vars = ScopeVariables.lookup(LS); |
| 1304 | ScopeVariables[LS].push_back(Var); |
| 1305 | // Vars.push_back(Var); |
| 1306 | } |
| 1307 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1308 | /// endFunction - Gather and emit post-function debug information. |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1309 | /// |
Chris Lattner | eec791a | 2010-01-26 23:18:02 +0000 | [diff] [blame] | 1310 | void DwarfDebug::endFunction(const MachineFunction *MF) { |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1311 | if (!MMI->hasDebugInfo() || LScopes.empty()) return; |
Devang Patel | 70d75ca | 2009-11-12 19:02:56 +0000 | [diff] [blame] | 1312 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1313 | // Define end label for subprogram. |
| 1314 | FunctionEndSym = Asm->GetTempSymbol("func_end", |
| 1315 | Asm->getFunctionNumber()); |
| 1316 | // Assumes in correct section after the entry point. |
| 1317 | Asm->OutStreamer.EmitLabel(FunctionEndSym); |
| 1318 | |
| 1319 | SmallPtrSet<const MDNode *, 16> ProcessedVars; |
| 1320 | collectVariableInfo(MF, ProcessedVars); |
| 1321 | |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame^] | 1322 | LexicalScope *FnScope = LScopes.getCurrentFunctionScope(); |
| 1323 | CompileUnit *TheCU = getCompileUnit(FnScope->getScopeNode()); |
| 1324 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1325 | // Construct abstract scopes. |
Devang Patel | cd9f6c5 | 2011-08-12 18:10:19 +0000 | [diff] [blame] | 1326 | ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList(); |
| 1327 | for (unsigned i = 0, e = AList.size(); i != e; ++i) { |
| 1328 | LexicalScope *AScope = AList[i]; |
| 1329 | DISubprogram SP(AScope->getScopeNode()); |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1330 | if (SP.Verify()) { |
| 1331 | // Collect info for variables that were optimized out. |
| 1332 | StringRef FName = SP.getLinkageName(); |
| 1333 | if (FName.empty()) |
| 1334 | FName = SP.getName(); |
| 1335 | if (NamedMDNode *NMD = |
| 1336 | getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) { |
| 1337 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { |
Dan Gohman | 872814a | 2010-07-21 18:54:18 +0000 | [diff] [blame] | 1338 | DIVariable DV(cast<MDNode>(NMD->getOperand(i))); |
Devang Patel | 78e127d | 2010-06-25 22:07:34 +0000 | [diff] [blame] | 1339 | if (!DV || !ProcessedVars.insert(DV)) |
| 1340 | continue; |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1341 | if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext())) |
Devang Patel | 5a1a67c | 2011-08-15 19:01:20 +0000 | [diff] [blame] | 1342 | addScopeVariable(Scope, new DbgVariable(DV, NULL)); |
Devang Patel | 78e127d | 2010-06-25 22:07:34 +0000 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | } |
Devang Patel | cd9f6c5 | 2011-08-12 18:10:19 +0000 | [diff] [blame] | 1346 | if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0) |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame^] | 1347 | constructScopeDIE(TheCU, AScope); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1348 | } |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1349 | |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame^] | 1350 | DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope); |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1351 | |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame^] | 1352 | if (!DisableFramePointerElim(*MF)) |
Devang Patel | 5bc942c | 2011-08-10 23:58:09 +0000 | [diff] [blame] | 1353 | TheCU->addUInt(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr, |
| 1354 | dwarf::DW_FORM_flag, 1); |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame^] | 1355 | |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1356 | DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(), |
| 1357 | MMI->getFrameMoves())); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1358 | |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1359 | // Clear debug info |
Devang Patel | bf47fdb | 2011-08-10 20:55:27 +0000 | [diff] [blame] | 1360 | for (DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >::iterator |
| 1361 | I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I) |
| 1362 | DeleteContainerPointers(I->second); |
| 1363 | ScopeVariables.clear(); |
Devang Patel | eac0c9d | 2011-04-22 18:09:57 +0000 | [diff] [blame] | 1364 | DeleteContainerPointers(CurrentFnArguments); |
Jakob Stoklund Olesen | adb877d | 2011-03-26 02:19:36 +0000 | [diff] [blame] | 1365 | UserVariables.clear(); |
| 1366 | DbgValues.clear(); |
Jeffrey Yasskin | 5c213dc | 2010-03-12 17:45:06 +0000 | [diff] [blame] | 1367 | AbstractVariables.clear(); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1368 | LabelsBeforeInsn.clear(); |
| 1369 | LabelsAfterInsn.clear(); |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 1370 | PrevLabel = NULL; |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Chris Lattner | c608784 | 2010-03-09 04:54:43 +0000 | [diff] [blame] | 1373 | /// recordSourceLine - Register a source line with debug info. Returns the |
| 1374 | /// unique label that was emitted and which provides correspondence to |
| 1375 | /// the source line list. |
Devang Patel | 4243e67 | 2011-05-11 19:22:19 +0000 | [diff] [blame] | 1376 | void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S, |
| 1377 | unsigned Flags) { |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 1378 | StringRef Fn; |
Devang Patel | 23670e5 | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 1379 | StringRef Dir; |
Dan Gohman | 1cc0d62 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1380 | unsigned Src = 1; |
| 1381 | if (S) { |
| 1382 | DIDescriptor Scope(S); |
Devang Patel | f84548d | 2009-10-05 18:03:19 +0000 | [diff] [blame] | 1383 | |
Dan Gohman | 1cc0d62 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1384 | if (Scope.isCompileUnit()) { |
| 1385 | DICompileUnit CU(S); |
Dan Gohman | 1cc0d62 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1386 | Fn = CU.getFilename(); |
Devang Patel | 23670e5 | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 1387 | Dir = CU.getDirectory(); |
Devang Patel | 3cabc9d | 2010-10-28 17:30:52 +0000 | [diff] [blame] | 1388 | } else if (Scope.isFile()) { |
| 1389 | DIFile F(S); |
Devang Patel | 3cabc9d | 2010-10-28 17:30:52 +0000 | [diff] [blame] | 1390 | Fn = F.getFilename(); |
Devang Patel | 23670e5 | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 1391 | Dir = F.getDirectory(); |
Dan Gohman | 1cc0d62 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1392 | } else if (Scope.isSubprogram()) { |
| 1393 | DISubprogram SP(S); |
Dan Gohman | 1cc0d62 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1394 | Fn = SP.getFilename(); |
Devang Patel | 23670e5 | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 1395 | Dir = SP.getDirectory(); |
Dan Gohman | 1cc0d62 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1396 | } else if (Scope.isLexicalBlock()) { |
| 1397 | DILexicalBlock DB(S); |
Dan Gohman | 1cc0d62 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1398 | Fn = DB.getFilename(); |
Devang Patel | 23670e5 | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 1399 | Dir = DB.getDirectory(); |
Dan Gohman | 1cc0d62 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1400 | } else |
| 1401 | assert(0 && "Unexpected scope info"); |
| 1402 | |
Devang Patel | 23670e5 | 2011-03-24 20:30:50 +0000 | [diff] [blame] | 1403 | Src = GetOrCreateSourceID(Fn, Dir); |
Dan Gohman | 1cc0d62 | 2010-05-05 23:41:32 +0000 | [diff] [blame] | 1404 | } |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 1405 | Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn); |
Bill Wendling | f0fb987 | 2009-05-20 23:19:06 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Bill Wendling | 829e67b | 2009-05-20 23:22:40 +0000 | [diff] [blame] | 1408 | //===----------------------------------------------------------------------===// |
| 1409 | // Emit Methods |
| 1410 | //===----------------------------------------------------------------------===// |
| 1411 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1412 | /// computeSizeAndOffset - Compute the size and offset of a DIE. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1413 | /// |
Jim Grosbach | 7ab38df | 2009-11-22 19:20:36 +0000 | [diff] [blame] | 1414 | unsigned |
| 1415 | DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1416 | // Get the children. |
| 1417 | const std::vector<DIE *> &Children = Die->getChildren(); |
| 1418 | |
| 1419 | // If not last sibling and has children then add sibling offset attribute. |
Jeffrey Yasskin | 638fe8d | 2010-03-22 18:47:14 +0000 | [diff] [blame] | 1420 | if (!Last && !Children.empty()) |
Benjamin Kramer | 345ef34 | 2010-03-31 19:34:01 +0000 | [diff] [blame] | 1421 | Die->addSiblingOffset(DIEValueAllocator); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1422 | |
| 1423 | // Record the abbreviation. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1424 | assignAbbrevNumber(Die->getAbbrev()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1425 | |
| 1426 | // Get the abbreviation for this DIE. |
| 1427 | unsigned AbbrevNumber = Die->getAbbrevNumber(); |
| 1428 | const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1]; |
| 1429 | |
| 1430 | // Set DIE offset |
| 1431 | Die->setOffset(Offset); |
| 1432 | |
| 1433 | // Start the size with the size of abbreviation code. |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 1434 | Offset += MCAsmInfo::getULEB128Size(AbbrevNumber); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1435 | |
| 1436 | const SmallVector<DIEValue*, 32> &Values = Die->getValues(); |
| 1437 | const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData(); |
| 1438 | |
| 1439 | // Size the DIE attribute values. |
| 1440 | for (unsigned i = 0, N = Values.size(); i < N; ++i) |
| 1441 | // Size attribute value. |
Chris Lattner | a37d538 | 2010-04-05 00:18:22 +0000 | [diff] [blame] | 1442 | Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1443 | |
| 1444 | // Size the DIE children if any. |
| 1445 | if (!Children.empty()) { |
| 1446 | assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes && |
| 1447 | "Children flag not set"); |
| 1448 | |
| 1449 | for (unsigned j = 0, M = Children.size(); j < M; ++j) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1450 | Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1451 | |
| 1452 | // End of children marker. |
| 1453 | Offset += sizeof(int8_t); |
| 1454 | } |
| 1455 | |
| 1456 | Die->setSize(Offset - Die->getOffset()); |
| 1457 | return Offset; |
| 1458 | } |
| 1459 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1460 | /// computeSizeAndOffsets - Compute the size and offset of all the DIEs. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1461 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1462 | void DwarfDebug::computeSizeAndOffsets() { |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1463 | for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(), |
| 1464 | E = CUMap.end(); I != E; ++I) { |
| 1465 | // Compute size of compile unit header. |
Devang Patel | 513edf6 | 2011-04-12 23:10:47 +0000 | [diff] [blame] | 1466 | unsigned Offset = |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1467 | sizeof(int32_t) + // Length of Compilation Unit Info |
| 1468 | sizeof(int16_t) + // DWARF version number |
| 1469 | sizeof(int32_t) + // Offset Into Abbrev. Section |
| 1470 | sizeof(int8_t); // Pointer Size (in bytes) |
| 1471 | computeSizeAndOffset(I->second->getCUDie(), Offset, true); |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1472 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 1475 | /// EmitSectionSym - Switch to the specified MCSection and emit an assembler |
| 1476 | /// temporary label to it if SymbolStem is specified. |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 1477 | static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section, |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 1478 | const char *SymbolStem = 0) { |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 1479 | Asm->OutStreamer.SwitchSection(Section); |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 1480 | if (!SymbolStem) return 0; |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1481 | |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 1482 | MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem); |
| 1483 | Asm->OutStreamer.EmitLabel(TmpSym); |
| 1484 | return TmpSym; |
| 1485 | } |
| 1486 | |
| 1487 | /// EmitSectionLabels - Emit initial Dwarf sections with a label at |
| 1488 | /// the start of each one. |
Chris Lattner | fa070b0 | 2010-04-04 22:33:59 +0000 | [diff] [blame] | 1489 | void DwarfDebug::EmitSectionLabels() { |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1490 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
Daniel Dunbar | f612ff6 | 2009-09-19 20:40:05 +0000 | [diff] [blame] | 1491 | |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1492 | // Dwarf sections base addresses. |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1493 | DwarfInfoSectionSym = |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 1494 | EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info"); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1495 | DwarfAbbrevSectionSym = |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 1496 | EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev"); |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 1497 | EmitSectionSym(Asm, TLOF.getDwarfARangesSection()); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1498 | |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 1499 | if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection()) |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 1500 | EmitSectionSym(Asm, MacroInfo); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1501 | |
Devang Patel | af608bd | 2010-08-24 00:06:12 +0000 | [diff] [blame] | 1502 | EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line"); |
Chris Lattner | 11b8f30 | 2010-04-04 23:02:02 +0000 | [diff] [blame] | 1503 | EmitSectionSym(Asm, TLOF.getDwarfLocSection()); |
| 1504 | EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection()); |
| 1505 | EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection()); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1506 | DwarfStrSectionSym = |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 1507 | EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str"); |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 1508 | DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(), |
| 1509 | "debug_range"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1510 | |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1511 | DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(), |
| 1512 | "section_debug_loc"); |
| 1513 | |
Chris Lattner | 9c69e28553 | 2010-04-04 22:59:04 +0000 | [diff] [blame] | 1514 | TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin"); |
Chris Lattner | 4ad1efe | 2010-04-04 23:10:38 +0000 | [diff] [blame] | 1515 | EmitSectionSym(Asm, TLOF.getDataSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 1518 | /// emitDIE - Recursively emits a debug information entry. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1519 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1520 | void DwarfDebug::emitDIE(DIE *Die) { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1521 | // Get the abbreviation for this DIE. |
| 1522 | unsigned AbbrevNumber = Die->getAbbrevNumber(); |
| 1523 | const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1]; |
| 1524 | |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1525 | // Emit the code (index) for the abbreviation. |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 1526 | if (Asm->isVerbose()) |
Chris Lattner | 894d75a | 2010-01-22 23:18:42 +0000 | [diff] [blame] | 1527 | Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" + |
| 1528 | Twine::utohexstr(Die->getOffset()) + ":0x" + |
| 1529 | Twine::utohexstr(Die->getSize()) + " " + |
| 1530 | dwarf::TagString(Abbrev->getTag())); |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 1531 | Asm->EmitULEB128(AbbrevNumber); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1532 | |
Jeffrey Yasskin | 638fe8d | 2010-03-22 18:47:14 +0000 | [diff] [blame] | 1533 | const SmallVector<DIEValue*, 32> &Values = Die->getValues(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1534 | const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData(); |
| 1535 | |
| 1536 | // Emit the DIE attribute values. |
| 1537 | for (unsigned i = 0, N = Values.size(); i < N; ++i) { |
| 1538 | unsigned Attr = AbbrevData[i].getAttribute(); |
| 1539 | unsigned Form = AbbrevData[i].getForm(); |
| 1540 | assert(Form && "Too many attributes for DIE (check abbreviation)"); |
| 1541 | |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 1542 | if (Asm->isVerbose()) |
Chris Lattner | a801362 | 2010-01-24 18:54:17 +0000 | [diff] [blame] | 1543 | Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr)); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1544 | |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1545 | switch (Attr) { |
| 1546 | case dwarf::DW_AT_sibling: |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1547 | Asm->EmitInt32(Die->getSiblingOffset()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1548 | break; |
| 1549 | case dwarf::DW_AT_abstract_origin: { |
| 1550 | DIEEntry *E = cast<DIEEntry>(Values[i]); |
| 1551 | DIE *Origin = E->getEntry(); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 1552 | unsigned Addr = Origin->getOffset(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1553 | Asm->EmitInt32(Addr); |
| 1554 | break; |
| 1555 | } |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 1556 | case dwarf::DW_AT_ranges: { |
| 1557 | // DW_AT_range Value encodes offset in debug_range section. |
| 1558 | DIEInteger *V = cast<DIEInteger>(Values[i]); |
Devang Patel | b1fcfbe | 2010-09-02 16:43:44 +0000 | [diff] [blame] | 1559 | |
| 1560 | if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) { |
| 1561 | Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym, |
| 1562 | V->getValue(), |
| 1563 | 4); |
| 1564 | } else { |
| 1565 | Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym, |
| 1566 | V->getValue(), |
| 1567 | DwarfDebugRangeSectionSym, |
| 1568 | 4); |
| 1569 | } |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 1570 | break; |
| 1571 | } |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1572 | case dwarf::DW_AT_location: { |
Devang Patel | 7a32827 | 2011-08-15 21:43:21 +0000 | [diff] [blame] | 1573 | if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) |
Daniel Dunbar | 83320a0 | 2011-03-16 22:16:39 +0000 | [diff] [blame] | 1574 | Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4); |
Devang Patel | 7a32827 | 2011-08-15 21:43:21 +0000 | [diff] [blame] | 1575 | else |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1576 | Values[i]->EmitValue(Asm, Form); |
| 1577 | break; |
| 1578 | } |
Devang Patel | 2a36160 | 2010-09-29 19:08:08 +0000 | [diff] [blame] | 1579 | case dwarf::DW_AT_accessibility: { |
| 1580 | if (Asm->isVerbose()) { |
| 1581 | DIEInteger *V = cast<DIEInteger>(Values[i]); |
| 1582 | Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue())); |
| 1583 | } |
| 1584 | Values[i]->EmitValue(Asm, Form); |
| 1585 | break; |
| 1586 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1587 | default: |
| 1588 | // Emit an attribute using the defined form. |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1589 | Values[i]->EmitValue(Asm, Form); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1590 | break; |
| 1591 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | // Emit the DIE children if any. |
| 1595 | if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) { |
| 1596 | const std::vector<DIE *> &Children = Die->getChildren(); |
| 1597 | |
| 1598 | for (unsigned j = 0, M = Children.size(); j < M; ++j) |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1599 | emitDIE(Children[j]); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1600 | |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 1601 | if (Asm->isVerbose()) |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 1602 | Asm->OutStreamer.AddComment("End Of Children Mark"); |
| 1603 | Asm->EmitInt8(0); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1607 | /// emitDebugInfo - Emit the debug info section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1608 | /// |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1609 | void DwarfDebug::emitDebugInfo() { |
| 1610 | // Start debug info section. |
| 1611 | Asm->OutStreamer.SwitchSection( |
| 1612 | Asm->getObjFileLowering().getDwarfInfoSection()); |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1613 | for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(), |
| 1614 | E = CUMap.end(); I != E; ++I) { |
| 1615 | CompileUnit *TheCU = I->second; |
| 1616 | DIE *Die = TheCU->getCUDie(); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1617 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1618 | // Emit the compile units header. |
| 1619 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin", |
| 1620 | TheCU->getID())); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1621 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1622 | // Emit size of content not including length itself |
| 1623 | unsigned ContentSize = Die->getSize() + |
| 1624 | sizeof(int16_t) + // DWARF version number |
| 1625 | sizeof(int32_t) + // Offset Into Abbrev. Section |
Devang Patel | 65705d5 | 2011-04-13 19:41:17 +0000 | [diff] [blame] | 1626 | sizeof(int8_t); // Pointer Size (in bytes) |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1627 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1628 | Asm->OutStreamer.AddComment("Length of Compilation Unit Info"); |
| 1629 | Asm->EmitInt32(ContentSize); |
| 1630 | Asm->OutStreamer.AddComment("DWARF version number"); |
| 1631 | Asm->EmitInt16(dwarf::DWARF_VERSION); |
| 1632 | Asm->OutStreamer.AddComment("Offset Into Abbrev. Section"); |
| 1633 | Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"), |
| 1634 | DwarfAbbrevSectionSym); |
| 1635 | Asm->OutStreamer.AddComment("Address Size (in bytes)"); |
| 1636 | Asm->EmitInt8(Asm->getTargetData().getPointerSize()); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1637 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1638 | emitDIE(Die); |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1639 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID())); |
| 1640 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1643 | /// emitAbbreviations - Emit the abbreviation section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1644 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1645 | void DwarfDebug::emitAbbreviations() const { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1646 | // Check to see if it is worth the effort. |
| 1647 | if (!Abbreviations.empty()) { |
| 1648 | // Start the debug abbrev section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1649 | Asm->OutStreamer.SwitchSection( |
| 1650 | Asm->getObjFileLowering().getDwarfAbbrevSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1651 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 1652 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin")); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1653 | |
| 1654 | // For each abbrevation. |
| 1655 | for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) { |
| 1656 | // Get abbreviation data |
| 1657 | const DIEAbbrev *Abbrev = Abbreviations[i]; |
| 1658 | |
| 1659 | // Emit the abbrevations code (base 1 index.) |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 1660 | Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1661 | |
| 1662 | // Emit the abbreviations data. |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1663 | Abbrev->Emit(Asm); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | // Mark end of abbreviations. |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 1667 | Asm->EmitULEB128(0, "EOM(3)"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1668 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 1669 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end")); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1670 | } |
| 1671 | } |
| 1672 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1673 | /// emitEndOfLineMatrix - Emit the last address of the section and the end of |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1674 | /// the line matrix. |
| 1675 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1676 | void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1677 | // Define last address of section. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 1678 | Asm->OutStreamer.AddComment("Extended Op"); |
| 1679 | Asm->EmitInt8(0); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1680 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 1681 | Asm->OutStreamer.AddComment("Op size"); |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1682 | Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1); |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 1683 | Asm->OutStreamer.AddComment("DW_LNE_set_address"); |
| 1684 | Asm->EmitInt8(dwarf::DW_LNE_set_address); |
| 1685 | |
| 1686 | Asm->OutStreamer.AddComment("Section end label"); |
Chris Lattner | d85fc6e | 2010-03-10 01:17:49 +0000 | [diff] [blame] | 1687 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 1688 | Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd), |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1689 | Asm->getTargetData().getPointerSize(), |
| 1690 | 0/*AddrSpace*/); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1691 | |
| 1692 | // Mark end of matrix. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 1693 | Asm->OutStreamer.AddComment("DW_LNE_end_sequence"); |
| 1694 | Asm->EmitInt8(0); |
Chris Lattner | 0ad9c91 | 2010-01-22 22:09:00 +0000 | [diff] [blame] | 1695 | Asm->EmitInt8(1); |
Chris Lattner | 894d75a | 2010-01-22 23:18:42 +0000 | [diff] [blame] | 1696 | Asm->EmitInt8(1); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Devang Patel | 8a24114 | 2009-12-09 18:24:21 +0000 | [diff] [blame] | 1699 | /// emitDebugPubNames - Emit visible names into a debug pubnames section. |
| 1700 | /// |
| 1701 | void DwarfDebug::emitDebugPubNames() { |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1702 | for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(), |
| 1703 | E = CUMap.end(); I != E; ++I) { |
| 1704 | CompileUnit *TheCU = I->second; |
| 1705 | // Start the dwarf pubnames section. |
| 1706 | Asm->OutStreamer.SwitchSection( |
| 1707 | Asm->getObjFileLowering().getDwarfPubNamesSection()); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1708 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1709 | Asm->OutStreamer.AddComment("Length of Public Names Info"); |
| 1710 | Asm->EmitLabelDifference( |
| 1711 | Asm->GetTempSymbol("pubnames_end", TheCU->getID()), |
| 1712 | Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1713 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1714 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin", |
| 1715 | TheCU->getID())); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1716 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1717 | Asm->OutStreamer.AddComment("DWARF Version"); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1718 | Asm->EmitInt16(dwarf::DWARF_VERSION); |
| 1719 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1720 | Asm->OutStreamer.AddComment("Offset of Compilation Unit Info"); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1721 | Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()), |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1722 | DwarfInfoSectionSym); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1723 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1724 | Asm->OutStreamer.AddComment("Compilation Unit Length"); |
| 1725 | Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()), |
| 1726 | Asm->GetTempSymbol("info_begin", TheCU->getID()), |
| 1727 | 4); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1728 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1729 | const StringMap<DIE*> &Globals = TheCU->getGlobals(); |
| 1730 | for (StringMap<DIE*>::const_iterator |
| 1731 | GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) { |
| 1732 | const char *Name = GI->getKeyData(); |
| 1733 | DIE *Entity = GI->second; |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1734 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1735 | Asm->OutStreamer.AddComment("DIE offset"); |
| 1736 | Asm->EmitInt32(Entity->getOffset()); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1737 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1738 | if (Asm->isVerbose()) |
| 1739 | Asm->OutStreamer.AddComment("External Name"); |
| 1740 | Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0); |
| 1741 | } |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1742 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1743 | Asm->OutStreamer.AddComment("End Mark"); |
| 1744 | Asm->EmitInt32(0); |
| 1745 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end", |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 1746 | TheCU->getID())); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1747 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1750 | void DwarfDebug::emitDebugPubTypes() { |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1751 | for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(), |
| 1752 | E = CUMap.end(); I != E; ++I) { |
| 1753 | CompileUnit *TheCU = I->second; |
| 1754 | // Start the dwarf pubnames section. |
| 1755 | Asm->OutStreamer.SwitchSection( |
| 1756 | Asm->getObjFileLowering().getDwarfPubTypesSection()); |
| 1757 | Asm->OutStreamer.AddComment("Length of Public Types Info"); |
| 1758 | Asm->EmitLabelDifference( |
| 1759 | Asm->GetTempSymbol("pubtypes_end", TheCU->getID()), |
| 1760 | Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1761 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1762 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin", |
| 1763 | TheCU->getID())); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1764 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1765 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version"); |
| 1766 | Asm->EmitInt16(dwarf::DWARF_VERSION); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1767 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1768 | Asm->OutStreamer.AddComment("Offset of Compilation Unit Info"); |
| 1769 | Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()), |
| 1770 | DwarfInfoSectionSym); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1771 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1772 | Asm->OutStreamer.AddComment("Compilation Unit Length"); |
| 1773 | Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()), |
| 1774 | Asm->GetTempSymbol("info_begin", TheCU->getID()), |
| 1775 | 4); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1776 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1777 | const StringMap<DIE*> &Globals = TheCU->getGlobalTypes(); |
| 1778 | for (StringMap<DIE*>::const_iterator |
| 1779 | GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) { |
| 1780 | const char *Name = GI->getKeyData(); |
Nick Lewycky | 3bbb6f7 | 2011-07-29 03:49:23 +0000 | [diff] [blame] | 1781 | DIE *Entity = GI->second; |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1782 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1783 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset"); |
| 1784 | Asm->EmitInt32(Entity->getOffset()); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1785 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1786 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name"); |
| 1787 | Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0); |
| 1788 | } |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1789 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1790 | Asm->OutStreamer.AddComment("End Mark"); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1791 | Asm->EmitInt32(0); |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1792 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end", |
| 1793 | TheCU->getID())); |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1794 | } |
Devang Patel | 193f720 | 2009-11-24 01:14:22 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1797 | /// emitDebugStr - Emit visible names into a debug str section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1798 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1799 | void DwarfDebug::emitDebugStr() { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1800 | // Check to see if it is worth the effort. |
Chris Lattner | 0d9d70f | 2010-03-09 23:38:23 +0000 | [diff] [blame] | 1801 | if (StringPool.empty()) return; |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1802 | |
Chris Lattner | 0d9d70f | 2010-03-09 23:38:23 +0000 | [diff] [blame] | 1803 | // Start the dwarf str section. |
| 1804 | Asm->OutStreamer.SwitchSection( |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1805 | Asm->getObjFileLowering().getDwarfStrSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1806 | |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 1807 | // Get all of the string pool entries and put them in an array by their ID so |
| 1808 | // we can sort them. |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1809 | SmallVector<std::pair<unsigned, |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 1810 | StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries; |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1811 | |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 1812 | for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator |
| 1813 | I = StringPool.begin(), E = StringPool.end(); I != E; ++I) |
| 1814 | Entries.push_back(std::make_pair(I->second.second, &*I)); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1815 | |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 1816 | array_pod_sort(Entries.begin(), Entries.end()); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1817 | |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 1818 | for (unsigned i = 0, e = Entries.size(); i != e; ++i) { |
Chris Lattner | 0d9d70f | 2010-03-09 23:38:23 +0000 | [diff] [blame] | 1819 | // Emit a label for reference from debug information entries. |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 1820 | Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first); |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1821 | |
Chris Lattner | 0d9d70f | 2010-03-09 23:38:23 +0000 | [diff] [blame] | 1822 | // Emit the string itself. |
Chris Lattner | bc733f5 | 2010-03-13 02:17:42 +0000 | [diff] [blame] | 1823 | Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1827 | /// emitDebugLoc - Emit visible names into a debug loc section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1828 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1829 | void DwarfDebug::emitDebugLoc() { |
Devang Patel | 8025068 | 2010-05-26 23:55:23 +0000 | [diff] [blame] | 1830 | if (DotDebugLocEntries.empty()) |
| 1831 | return; |
| 1832 | |
Devang Patel | 6c3ea90 | 2011-02-04 22:57:18 +0000 | [diff] [blame] | 1833 | for (SmallVector<DotDebugLocEntry, 4>::iterator |
| 1834 | I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end(); |
| 1835 | I != E; ++I) { |
| 1836 | DotDebugLocEntry &Entry = *I; |
| 1837 | if (I + 1 != DotDebugLocEntries.end()) |
| 1838 | Entry.Merge(I+1); |
| 1839 | } |
| 1840 | |
Daniel Dunbar | 83320a0 | 2011-03-16 22:16:39 +0000 | [diff] [blame] | 1841 | // Start the dwarf loc section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1842 | Asm->OutStreamer.SwitchSection( |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1843 | Asm->getObjFileLowering().getDwarfLocSection()); |
| 1844 | unsigned char Size = Asm->getTargetData().getPointerSize(); |
Devang Patel | 8025068 | 2010-05-26 23:55:23 +0000 | [diff] [blame] | 1845 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0)); |
| 1846 | unsigned index = 1; |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1847 | for (SmallVector<DotDebugLocEntry, 4>::iterator |
| 1848 | I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end(); |
Devang Patel | 6140962 | 2010-07-07 20:12:52 +0000 | [diff] [blame] | 1849 | I != E; ++I, ++index) { |
Devang Patel | 6c3ea90 | 2011-02-04 22:57:18 +0000 | [diff] [blame] | 1850 | DotDebugLocEntry &Entry = *I; |
| 1851 | if (Entry.isMerged()) continue; |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1852 | if (Entry.isEmpty()) { |
| 1853 | Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0); |
| 1854 | Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0); |
Devang Patel | 8025068 | 2010-05-26 23:55:23 +0000 | [diff] [blame] | 1855 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index)); |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1856 | } else { |
| 1857 | Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0); |
| 1858 | Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0); |
Devang Patel | c26f544 | 2011-04-28 02:22:40 +0000 | [diff] [blame] | 1859 | DIVariable DV(Entry.Variable); |
Rafael Espindola | 5b23b7f | 2011-05-27 22:05:41 +0000 | [diff] [blame] | 1860 | Asm->OutStreamer.AddComment("Loc expr size"); |
| 1861 | MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol(); |
| 1862 | MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol(); |
| 1863 | Asm->EmitLabelDifference(end, begin, 2); |
| 1864 | Asm->OutStreamer.EmitLabel(begin); |
Devang Patel | 80efd4e | 2011-07-08 16:49:43 +0000 | [diff] [blame] | 1865 | if (Entry.isInt()) { |
Devang Patel | c432907 | 2011-06-01 22:03:25 +0000 | [diff] [blame] | 1866 | DIBasicType BTy(DV.getType()); |
| 1867 | if (BTy.Verify() && |
| 1868 | (BTy.getEncoding() == dwarf::DW_ATE_signed |
| 1869 | || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) { |
| 1870 | Asm->OutStreamer.AddComment("DW_OP_consts"); |
| 1871 | Asm->EmitInt8(dwarf::DW_OP_consts); |
Devang Patel | 80efd4e | 2011-07-08 16:49:43 +0000 | [diff] [blame] | 1872 | Asm->EmitSLEB128(Entry.getInt()); |
Devang Patel | c432907 | 2011-06-01 22:03:25 +0000 | [diff] [blame] | 1873 | } else { |
| 1874 | Asm->OutStreamer.AddComment("DW_OP_constu"); |
| 1875 | Asm->EmitInt8(dwarf::DW_OP_constu); |
Devang Patel | 80efd4e | 2011-07-08 16:49:43 +0000 | [diff] [blame] | 1876 | Asm->EmitULEB128(Entry.getInt()); |
Devang Patel | c432907 | 2011-06-01 22:03:25 +0000 | [diff] [blame] | 1877 | } |
Devang Patel | 80efd4e | 2011-07-08 16:49:43 +0000 | [diff] [blame] | 1878 | } else if (Entry.isLocation()) { |
| 1879 | if (!DV.hasComplexAddress()) |
| 1880 | // Regular entry. |
Devang Patel | c26f544 | 2011-04-28 02:22:40 +0000 | [diff] [blame] | 1881 | Asm->EmitDwarfRegOp(Entry.Loc); |
Devang Patel | 80efd4e | 2011-07-08 16:49:43 +0000 | [diff] [blame] | 1882 | else { |
| 1883 | // Complex address entry. |
| 1884 | unsigned N = DV.getNumAddrElements(); |
| 1885 | unsigned i = 0; |
| 1886 | if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) { |
| 1887 | if (Entry.Loc.getOffset()) { |
| 1888 | i = 2; |
| 1889 | Asm->EmitDwarfRegOp(Entry.Loc); |
| 1890 | Asm->OutStreamer.AddComment("DW_OP_deref"); |
| 1891 | Asm->EmitInt8(dwarf::DW_OP_deref); |
| 1892 | Asm->OutStreamer.AddComment("DW_OP_plus_uconst"); |
| 1893 | Asm->EmitInt8(dwarf::DW_OP_plus_uconst); |
| 1894 | Asm->EmitSLEB128(DV.getAddrElement(1)); |
| 1895 | } else { |
| 1896 | // If first address element is OpPlus then emit |
| 1897 | // DW_OP_breg + Offset instead of DW_OP_reg + Offset. |
| 1898 | MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1)); |
| 1899 | Asm->EmitDwarfRegOp(Loc); |
| 1900 | i = 2; |
| 1901 | } |
| 1902 | } else { |
| 1903 | Asm->EmitDwarfRegOp(Entry.Loc); |
| 1904 | } |
| 1905 | |
| 1906 | // Emit remaining complex address elements. |
| 1907 | for (; i < N; ++i) { |
| 1908 | uint64_t Element = DV.getAddrElement(i); |
| 1909 | if (Element == DIBuilder::OpPlus) { |
| 1910 | Asm->EmitInt8(dwarf::DW_OP_plus_uconst); |
| 1911 | Asm->EmitULEB128(DV.getAddrElement(++i)); |
| 1912 | } else if (Element == DIBuilder::OpDeref) |
| 1913 | Asm->EmitInt8(dwarf::DW_OP_deref); |
| 1914 | else llvm_unreachable("unknown Opcode found in complex address"); |
| 1915 | } |
Devang Patel | c26f544 | 2011-04-28 02:22:40 +0000 | [diff] [blame] | 1916 | } |
Devang Patel | c26f544 | 2011-04-28 02:22:40 +0000 | [diff] [blame] | 1917 | } |
Devang Patel | 80efd4e | 2011-07-08 16:49:43 +0000 | [diff] [blame] | 1918 | // else ... ignore constant fp. There is not any good way to |
| 1919 | // to represent them here in dwarf. |
Rafael Espindola | 5b23b7f | 2011-05-27 22:05:41 +0000 | [diff] [blame] | 1920 | Asm->OutStreamer.EmitLabel(end); |
Devang Patel | c3f5f78 | 2010-05-25 23:40:22 +0000 | [diff] [blame] | 1921 | } |
| 1922 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | /// EmitDebugARanges - Emit visible names into a debug aranges section. |
| 1926 | /// |
| 1927 | void DwarfDebug::EmitDebugARanges() { |
| 1928 | // Start the dwarf aranges section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1929 | Asm->OutStreamer.SwitchSection( |
| 1930 | Asm->getObjFileLowering().getDwarfARangesSection()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1933 | /// emitDebugRanges - Emit visible names into a debug ranges section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1934 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1935 | void DwarfDebug::emitDebugRanges() { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1936 | // Start the dwarf ranges section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1937 | Asm->OutStreamer.SwitchSection( |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 1938 | Asm->getObjFileLowering().getDwarfRangesSection()); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1939 | unsigned char Size = Asm->getTargetData().getPointerSize(); |
| 1940 | for (SmallVector<const MCSymbol *, 8>::iterator |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 1941 | I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end(); |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1942 | I != E; ++I) { |
| 1943 | if (*I) |
| 1944 | Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0); |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 1945 | else |
Devang Patel | eac9c07 | 2010-04-27 19:46:33 +0000 | [diff] [blame] | 1946 | Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0); |
Devang Patel | f2548ca | 2010-04-16 23:33:45 +0000 | [diff] [blame] | 1947 | } |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1950 | /// emitDebugMacInfo - Emit visible names into a debug macinfo section. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1951 | /// |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1952 | void DwarfDebug::emitDebugMacInfo() { |
Daniel Dunbar | f612ff6 | 2009-09-19 20:40:05 +0000 | [diff] [blame] | 1953 | if (const MCSection *LineInfo = |
Chris Lattner | 18a4c16 | 2009-08-02 07:24:22 +0000 | [diff] [blame] | 1954 | Asm->getObjFileLowering().getDwarfMacroInfoSection()) { |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1955 | // Start the dwarf macinfo section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1956 | Asm->OutStreamer.SwitchSection(LineInfo); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1957 | } |
| 1958 | } |
| 1959 | |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1960 | /// emitDebugInlineInfo - Emit inline info using following format. |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1961 | /// Section Header: |
| 1962 | /// 1. length of section |
| 1963 | /// 2. Dwarf version number |
| 1964 | /// 3. address size. |
| 1965 | /// |
| 1966 | /// Entries (one "entry" for each function that was inlined): |
| 1967 | /// |
| 1968 | /// 1. offset into __debug_str section for MIPS linkage name, if exists; |
| 1969 | /// otherwise offset into __debug_str for regular function name. |
| 1970 | /// 2. offset into __debug_str section for regular function name. |
| 1971 | /// 3. an unsigned LEB128 number indicating the number of distinct inlining |
| 1972 | /// instances for the function. |
| 1973 | /// |
| 1974 | /// The rest of the entry consists of a {die_offset, low_pc} pair for each |
| 1975 | /// inlined instance; the die_offset points to the inlined_subroutine die in the |
| 1976 | /// __debug_info section, and the low_pc is the starting address for the |
| 1977 | /// inlining instance. |
Devang Patel | 2c4ceb1 | 2009-11-21 02:48:08 +0000 | [diff] [blame] | 1978 | void DwarfDebug::emitDebugInlineInfo() { |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1979 | if (!Asm->MAI->doesDwarfUsesInlineInfoSection()) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1980 | return; |
| 1981 | |
Devang Patel | 163a9f7 | 2010-05-10 22:49:55 +0000 | [diff] [blame] | 1982 | if (!FirstCU) |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1983 | return; |
| 1984 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 1985 | Asm->OutStreamer.SwitchSection( |
| 1986 | Asm->getObjFileLowering().getDwarfDebugInlineSection()); |
Chris Lattner | 0ad9c91 | 2010-01-22 22:09:00 +0000 | [diff] [blame] | 1987 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 1988 | Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry"); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 1989 | Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1), |
| 1990 | Asm->GetTempSymbol("debug_inlined_begin", 1), 4); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1991 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 1992 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1)); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1993 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 1994 | Asm->OutStreamer.AddComment("Dwarf Version"); |
| 1995 | Asm->EmitInt16(dwarf::DWARF_VERSION); |
| 1996 | Asm->OutStreamer.AddComment("Address Size (in bytes)"); |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 1997 | Asm->EmitInt8(Asm->getTargetData().getPointerSize()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 1998 | |
Devang Patel | e9f8f5e | 2010-05-07 20:54:48 +0000 | [diff] [blame] | 1999 | for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(), |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2000 | E = InlinedSPNodes.end(); I != E; ++I) { |
Jim Grosbach | 31ef40e | 2009-11-21 23:12:12 +0000 | [diff] [blame] | 2001 | |
Devang Patel | e9f8f5e | 2010-05-07 20:54:48 +0000 | [diff] [blame] | 2002 | const MDNode *Node = *I; |
| 2003 | DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II |
Jim Grosbach | 7ab38df | 2009-11-22 19:20:36 +0000 | [diff] [blame] | 2004 | = InlineInfo.find(Node); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2005 | SmallVector<InlineInfoLabels, 4> &Labels = II->second; |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 2006 | DISubprogram SP(Node); |
Devang Patel | 65dbc90 | 2009-11-25 17:36:49 +0000 | [diff] [blame] | 2007 | StringRef LName = SP.getLinkageName(); |
| 2008 | StringRef Name = SP.getName(); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2009 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2010 | Asm->OutStreamer.AddComment("MIPS linkage name"); |
Chris Lattner | 4cf202b | 2010-01-23 03:11:46 +0000 | [diff] [blame] | 2011 | if (LName.empty()) { |
| 2012 | Asm->OutStreamer.EmitBytes(Name, 0); |
| 2013 | Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator. |
Jim Grosbach | 1e20b96 | 2010-07-21 21:21:52 +0000 | [diff] [blame] | 2014 | } else |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 2015 | Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)), |
| 2016 | DwarfStrSectionSym); |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2017 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 2018 | Asm->OutStreamer.AddComment("Function name"); |
Chris Lattner | 6189ed1 | 2010-04-04 23:25:33 +0000 | [diff] [blame] | 2019 | Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym); |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 2020 | Asm->EmitULEB128(Labels.size(), "Inline count"); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2021 | |
Devang Patel | 53bb5c9 | 2009-11-10 23:06:00 +0000 | [diff] [blame] | 2022 | for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(), |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2023 | LE = Labels.end(); LI != LE; ++LI) { |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 2024 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset"); |
Chris Lattner | 6ed0f90 | 2010-03-09 00:31:02 +0000 | [diff] [blame] | 2025 | Asm->EmitInt32(LI->second->getOffset()); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2026 | |
Chris Lattner | 3f53c83 | 2010-04-04 18:52:31 +0000 | [diff] [blame] | 2027 | if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc"); |
Chris Lattner | d38fee8 | 2010-04-05 00:13:49 +0000 | [diff] [blame] | 2028 | Asm->OutStreamer.EmitSymbolValue(LI->first, |
| 2029 | Asm->getTargetData().getPointerSize(),0); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2030 | } |
| 2031 | } |
| 2032 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 2033 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1)); |
Bill Wendling | 94d04b8 | 2009-05-20 23:21:38 +0000 | [diff] [blame] | 2034 | } |