Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===// |
| 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 | // |
Eric Christopher | 443c9ed | 2012-08-14 05:13:29 +0000 | [diff] [blame] | 10 | // This file contains support for constructing a dwarf compile unit. |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "dwarfdebug" |
| 15 | |
| 16 | #include "DwarfCompileUnit.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "DwarfAccelTable.h" |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 18 | #include "DwarfDebug.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/APFloat.h" |
Bill Wendling | 16eeb6f | 2012-06-29 08:32:07 +0000 | [diff] [blame] | 20 | #include "llvm/DIBuilder.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/DataLayout.h" |
| 23 | #include "llvm/IR/GlobalVariable.h" |
| 24 | #include "llvm/IR/Instructions.h" |
Eric Christopher | d61c34b | 2011-11-11 03:16:32 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 27 | #include "llvm/Target/Mangler.h" |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetFrameLowering.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
| 30 | #include "llvm/Target/TargetRegisterInfo.h" |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
| 34 | /// CompileUnit - Compile unit constructor. |
Eric Christopher | 9c57ad2 | 2013-05-08 00:58:51 +0000 | [diff] [blame] | 35 | CompileUnit::CompileUnit(unsigned UID, unsigned L, DIE *D, const MDNode *N, |
| 36 | AsmPrinter *A, DwarfDebug *DW, DwarfUnits *DWU) |
Eric Christopher | 2e5d870 | 2012-12-20 21:58:36 +0000 | [diff] [blame] | 37 | : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), DU(DWU), |
Manman Ren | bc3e96f | 2013-03-12 18:27:15 +0000 | [diff] [blame] | 38 | IndexTyDie(0), DebugInfoOffset(0) { |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 39 | DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1); |
Eric Christopher | 9c57ad2 | 2013-05-08 00:58:51 +0000 | [diff] [blame] | 40 | insertDIE(N, D); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /// ~CompileUnit - Destructor for compile unit. |
| 44 | CompileUnit::~CompileUnit() { |
| 45 | for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j) |
| 46 | DIEBlocks[j]->~DIEBlock(); |
| 47 | } |
| 48 | |
| 49 | /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug |
| 50 | /// information entry. |
| 51 | DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) { |
| 52 | DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry); |
| 53 | return Value; |
| 54 | } |
| 55 | |
Bill Wendling | 6afe478 | 2012-12-06 07:55:19 +0000 | [diff] [blame] | 56 | /// getDefaultLowerBound - Return the default lower bound for an array. If the |
Bill Wendling | 222c2fd | 2012-12-06 07:38:10 +0000 | [diff] [blame] | 57 | /// DWARF version doesn't handle the language, return -1. |
Bill Wendling | 6afe478 | 2012-12-06 07:55:19 +0000 | [diff] [blame] | 58 | int64_t CompileUnit::getDefaultLowerBound() const { |
Bill Wendling | 222c2fd | 2012-12-06 07:38:10 +0000 | [diff] [blame] | 59 | switch (Language) { |
| 60 | default: |
| 61 | break; |
| 62 | |
| 63 | case dwarf::DW_LANG_C89: |
| 64 | case dwarf::DW_LANG_C99: |
| 65 | case dwarf::DW_LANG_C: |
| 66 | case dwarf::DW_LANG_C_plus_plus: |
| 67 | case dwarf::DW_LANG_ObjC: |
| 68 | case dwarf::DW_LANG_ObjC_plus_plus: |
| 69 | return 0; |
| 70 | |
| 71 | case dwarf::DW_LANG_Fortran77: |
| 72 | case dwarf::DW_LANG_Fortran90: |
| 73 | case dwarf::DW_LANG_Fortran95: |
| 74 | return 1; |
| 75 | |
| 76 | // The languages below have valid values only if the DWARF version >= 4. |
| 77 | case dwarf::DW_LANG_Java: |
| 78 | case dwarf::DW_LANG_Python: |
| 79 | case dwarf::DW_LANG_UPC: |
| 80 | case dwarf::DW_LANG_D: |
| 81 | if (dwarf::DWARF_VERSION >= 4) |
| 82 | return 0; |
| 83 | break; |
| 84 | |
| 85 | case dwarf::DW_LANG_Ada83: |
| 86 | case dwarf::DW_LANG_Ada95: |
| 87 | case dwarf::DW_LANG_Cobol74: |
| 88 | case dwarf::DW_LANG_Cobol85: |
| 89 | case dwarf::DW_LANG_Modula2: |
| 90 | case dwarf::DW_LANG_Pascal83: |
| 91 | case dwarf::DW_LANG_PLI: |
| 92 | if (dwarf::DWARF_VERSION >= 4) |
| 93 | return 1; |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | return -1; |
| 98 | } |
| 99 | |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 100 | /// addFlag - Add a flag that is true. |
| 101 | void CompileUnit::addFlag(DIE *Die, unsigned Attribute) { |
| 102 | if (!DD->useDarwinGDBCompat()) |
| 103 | Die->addValue(Attribute, dwarf::DW_FORM_flag_present, |
| 104 | DIEIntegerOne); |
| 105 | else |
| 106 | addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1); |
| 107 | } |
| 108 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 109 | /// addUInt - Add an unsigned integer attribute data and value. |
| 110 | /// |
| 111 | void CompileUnit::addUInt(DIE *Die, unsigned Attribute, |
| 112 | unsigned Form, uint64_t Integer) { |
| 113 | if (!Form) Form = DIEInteger::BestForm(false, Integer); |
| 114 | DIEValue *Value = Integer == 1 ? |
| 115 | DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer); |
| 116 | Die->addValue(Attribute, Form, Value); |
| 117 | } |
| 118 | |
| 119 | /// addSInt - Add an signed integer attribute data and value. |
| 120 | /// |
| 121 | void CompileUnit::addSInt(DIE *Die, unsigned Attribute, |
| 122 | unsigned Form, int64_t Integer) { |
| 123 | if (!Form) Form = DIEInteger::BestForm(true, Integer); |
| 124 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer); |
| 125 | Die->addValue(Attribute, Form, Value); |
| 126 | } |
| 127 | |
Nick Lewycky | 6a7efcf | 2011-10-28 05:29:47 +0000 | [diff] [blame] | 128 | /// addString - Add a string attribute data and value. We always emit a |
| 129 | /// reference to the string pool instead of immediate strings so that DIEs have |
Eric Christopher | 3cc4220 | 2013-01-07 19:32:45 +0000 | [diff] [blame] | 130 | /// more predictable sizes. In the case of split dwarf we emit an index |
| 131 | /// into another table which gets us the static offset into the string |
| 132 | /// table. |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 133 | void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) { |
Eric Christopher | dd8e9f3 | 2013-01-07 19:32:41 +0000 | [diff] [blame] | 134 | if (!DD->useSplitDwarf()) { |
| 135 | MCSymbol *Symb = DU->getStringPoolEntry(String); |
| 136 | DIEValue *Value; |
| 137 | if (Asm->needsRelocationsForDwarfStringPool()) |
| 138 | Value = new (DIEValueAllocator) DIELabel(Symb); |
| 139 | else { |
| 140 | MCSymbol *StringPool = DU->getStringPoolSym(); |
| 141 | Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool); |
| 142 | } |
| 143 | Die->addValue(Attribute, dwarf::DW_FORM_strp, Value); |
| 144 | } else { |
| 145 | unsigned idx = DU->getStringPoolIndex(String); |
| 146 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); |
| 147 | Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Value); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /// addLocalString - Add a string attribute data and value. This is guaranteed |
| 152 | /// to be in the local string pool instead of indirected. |
| 153 | void CompileUnit::addLocalString(DIE *Die, unsigned Attribute, |
| 154 | StringRef String) { |
Eric Christopher | 2e5d870 | 2012-12-20 21:58:36 +0000 | [diff] [blame] | 155 | MCSymbol *Symb = DU->getStringPoolEntry(String); |
Nick Lewycky | 6a7efcf | 2011-10-28 05:29:47 +0000 | [diff] [blame] | 156 | DIEValue *Value; |
| 157 | if (Asm->needsRelocationsForDwarfStringPool()) |
| 158 | Value = new (DIEValueAllocator) DIELabel(Symb); |
| 159 | else { |
Eric Christopher | 2e5d870 | 2012-12-20 21:58:36 +0000 | [diff] [blame] | 160 | MCSymbol *StringPool = DU->getStringPoolSym(); |
Nick Lewycky | 6a7efcf | 2011-10-28 05:29:47 +0000 | [diff] [blame] | 161 | Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool); |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 162 | } |
Nick Lewycky | 6a7efcf | 2011-10-28 05:29:47 +0000 | [diff] [blame] | 163 | Die->addValue(Attribute, dwarf::DW_FORM_strp, Value); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /// addLabel - Add a Dwarf label attribute data and value. |
| 167 | /// |
| 168 | void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form, |
| 169 | const MCSymbol *Label) { |
| 170 | DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); |
| 171 | Die->addValue(Attribute, Form, Value); |
| 172 | } |
| 173 | |
Eric Christopher | 72f7bfb | 2013-01-15 23:56:56 +0000 | [diff] [blame] | 174 | /// addLabelAddress - Add a dwarf label attribute data and value using |
| 175 | /// DW_FORM_addr or DW_FORM_GNU_addr_index. |
| 176 | /// |
| 177 | void CompileUnit::addLabelAddress(DIE *Die, unsigned Attribute, |
| 178 | MCSymbol *Label) { |
| 179 | if (!DD->useSplitDwarf()) { |
| 180 | if (Label != NULL) { |
| 181 | DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); |
| 182 | Die->addValue(Attribute, dwarf::DW_FORM_addr, Value); |
| 183 | } else { |
| 184 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(0); |
| 185 | Die->addValue(Attribute, dwarf::DW_FORM_addr, Value); |
| 186 | } |
| 187 | } else { |
| 188 | unsigned idx = DU->getAddrPoolIndex(Label); |
| 189 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); |
| 190 | Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value); |
| 191 | } |
| 192 | } |
| 193 | |
Eric Christopher | 0969ddf | 2013-01-18 22:11:33 +0000 | [diff] [blame] | 194 | /// addOpAddress - Add a dwarf op address data and value using the |
| 195 | /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index. |
| 196 | /// |
| 197 | void CompileUnit::addOpAddress(DIE *Die, MCSymbol *Sym) { |
| 198 | |
| 199 | if (!DD->useSplitDwarf()) { |
| 200 | addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); |
| 201 | addLabel(Die, 0, dwarf::DW_FORM_udata, Sym); |
| 202 | } else { |
| 203 | unsigned idx = DU->getAddrPoolIndex(Sym); |
| 204 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); |
| 205 | addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index); |
| 206 | Die->addValue(0, dwarf::DW_FORM_GNU_addr_index, Value); |
| 207 | } |
| 208 | } |
| 209 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 210 | /// addDelta - Add a label delta attribute data and value. |
| 211 | /// |
| 212 | void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form, |
| 213 | const MCSymbol *Hi, const MCSymbol *Lo) { |
| 214 | DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); |
| 215 | Die->addValue(Attribute, Form, Value); |
| 216 | } |
| 217 | |
| 218 | /// addDIEEntry - Add a DIE attribute data and value. |
| 219 | /// |
| 220 | void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, |
| 221 | DIE *Entry) { |
| 222 | Die->addValue(Attribute, Form, createDIEEntry(Entry)); |
| 223 | } |
| 224 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 225 | /// addBlock - Add block data. |
| 226 | /// |
| 227 | void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form, |
| 228 | DIEBlock *Block) { |
| 229 | Block->ComputeSize(Asm); |
| 230 | DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on. |
| 231 | Die->addValue(Attribute, Block->BestForm(), Block); |
| 232 | } |
| 233 | |
| 234 | /// addSourceLine - Add location information to specified debug information |
| 235 | /// entry. |
| 236 | void CompileUnit::addSourceLine(DIE *Die, DIVariable V) { |
| 237 | // Verify variable. |
| 238 | if (!V.Verify()) |
| 239 | return; |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 240 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 241 | unsigned Line = V.getLineNumber(); |
| 242 | if (Line == 0) |
| 243 | return; |
Eric Christopher | 7ee5f5d | 2012-11-21 00:34:35 +0000 | [diff] [blame] | 244 | unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(), |
Manman Ren | 3de61b4 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 245 | V.getContext().getDirectory(), |
| 246 | getUniqueID()); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 247 | assert(FileID && "Invalid file id"); |
| 248 | addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 249 | addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
| 250 | } |
| 251 | |
| 252 | /// addSourceLine - Add location information to specified debug information |
| 253 | /// entry. |
| 254 | void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) { |
| 255 | // Verify global variable. |
| 256 | if (!G.Verify()) |
| 257 | return; |
| 258 | |
| 259 | unsigned Line = G.getLineNumber(); |
| 260 | if (Line == 0) |
| 261 | return; |
Manman Ren | 3de61b4 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 262 | unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), |
| 263 | getUniqueID()); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 264 | assert(FileID && "Invalid file id"); |
| 265 | addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 266 | addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
| 267 | } |
| 268 | |
| 269 | /// addSourceLine - Add location information to specified debug information |
| 270 | /// entry. |
| 271 | void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) { |
| 272 | // Verify subprogram. |
| 273 | if (!SP.Verify()) |
| 274 | return; |
Eric Christopher | 2125d5a | 2012-03-15 23:55:40 +0000 | [diff] [blame] | 275 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 276 | // If the line number is 0, don't add it. |
Eric Christopher | 2125d5a | 2012-03-15 23:55:40 +0000 | [diff] [blame] | 277 | unsigned Line = SP.getLineNumber(); |
| 278 | if (Line == 0) |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 279 | return; |
| 280 | |
Eric Christopher | 7ee5f5d | 2012-11-21 00:34:35 +0000 | [diff] [blame] | 281 | unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), |
Manman Ren | 3de61b4 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 282 | SP.getDirectory(), getUniqueID()); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 283 | assert(FileID && "Invalid file id"); |
| 284 | addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 285 | addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
| 286 | } |
| 287 | |
| 288 | /// addSourceLine - Add location information to specified debug information |
| 289 | /// entry. |
| 290 | void CompileUnit::addSourceLine(DIE *Die, DIType Ty) { |
| 291 | // Verify type. |
| 292 | if (!Ty.Verify()) |
| 293 | return; |
| 294 | |
| 295 | unsigned Line = Ty.getLineNumber(); |
Eric Christopher | 2125d5a | 2012-03-15 23:55:40 +0000 | [diff] [blame] | 296 | if (Line == 0) |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 297 | return; |
Eric Christopher | 7ee5f5d | 2012-11-21 00:34:35 +0000 | [diff] [blame] | 298 | unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), |
Manman Ren | 3de61b4 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 299 | Ty.getDirectory(), getUniqueID()); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 300 | assert(FileID && "Invalid file id"); |
| 301 | addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 302 | addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
| 303 | } |
| 304 | |
| 305 | /// addSourceLine - Add location information to specified debug information |
| 306 | /// entry. |
Eric Christopher | b8ca988 | 2012-03-29 08:42:56 +0000 | [diff] [blame] | 307 | void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) { |
| 308 | // Verify type. |
| 309 | if (!Ty.Verify()) |
| 310 | return; |
| 311 | |
| 312 | unsigned Line = Ty.getLineNumber(); |
| 313 | if (Line == 0) |
| 314 | return; |
| 315 | DIFile File = Ty.getFile(); |
Eric Christopher | 7ee5f5d | 2012-11-21 00:34:35 +0000 | [diff] [blame] | 316 | unsigned FileID = DD->getOrCreateSourceID(File.getFilename(), |
Manman Ren | 3de61b4 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 317 | File.getDirectory(), getUniqueID()); |
Eric Christopher | b8ca988 | 2012-03-29 08:42:56 +0000 | [diff] [blame] | 318 | assert(FileID && "Invalid file id"); |
| 319 | addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 320 | addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
| 321 | } |
| 322 | |
| 323 | /// addSourceLine - Add location information to specified debug information |
| 324 | /// entry. |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 325 | void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) { |
| 326 | // Verify namespace. |
| 327 | if (!NS.Verify()) |
| 328 | return; |
| 329 | |
| 330 | unsigned Line = NS.getLineNumber(); |
| 331 | if (Line == 0) |
| 332 | return; |
| 333 | StringRef FN = NS.getFilename(); |
| 334 | |
Manman Ren | 3de61b4 | 2013-03-07 01:42:00 +0000 | [diff] [blame] | 335 | unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory(), |
| 336 | getUniqueID()); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 337 | assert(FileID && "Invalid file id"); |
| 338 | addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); |
| 339 | addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); |
| 340 | } |
| 341 | |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 342 | /// addVariableAddress - Add DW_AT_location attribute for a |
Devang Patel | e1cdf84 | 2011-04-27 22:45:24 +0000 | [diff] [blame] | 343 | /// DbgVariable based on provided MachineLocation. |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 344 | void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die, |
Devang Patel | e1cdf84 | 2011-04-27 22:45:24 +0000 | [diff] [blame] | 345 | MachineLocation Location) { |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 346 | if (DV->variableHasComplexAddress()) |
| 347 | addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); |
| 348 | else if (DV->isBlockByrefVariable()) |
| 349 | addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); |
| 350 | else |
| 351 | addAddress(Die, dwarf::DW_AT_location, Location); |
| 352 | } |
| 353 | |
Devang Patel | 116da2f | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 354 | /// addRegisterOp - Add register operand. |
| 355 | void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) { |
| 356 | const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); |
| 357 | unsigned DWReg = RI->getDwarfRegNum(Reg, false); |
| 358 | if (DWReg < 32) |
| 359 | addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg); |
| 360 | else { |
| 361 | addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); |
| 362 | addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /// addRegisterOffset - Add register offset. |
| 367 | void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg, |
| 368 | int64_t Offset) { |
| 369 | const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); |
| 370 | unsigned DWReg = RI->getDwarfRegNum(Reg, false); |
| 371 | const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo(); |
| 372 | if (Reg == TRI->getFrameRegister(*Asm->MF)) |
| 373 | // If variable offset is based in frame register then use fbreg. |
| 374 | addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg); |
| 375 | else if (DWReg < 32) |
| 376 | addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg); |
| 377 | else { |
| 378 | addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); |
| 379 | addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg); |
| 380 | } |
| 381 | addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset); |
| 382 | } |
| 383 | |
| 384 | /// addAddress - Add an address attribute to a die based on the location |
| 385 | /// provided. |
| 386 | void CompileUnit::addAddress(DIE *Die, unsigned Attribute, |
| 387 | const MachineLocation &Location) { |
| 388 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
| 389 | |
| 390 | if (Location.isReg()) |
| 391 | addRegisterOp(Block, Location.getReg()); |
| 392 | else |
| 393 | addRegisterOffset(Block, Location.getReg(), Location.getOffset()); |
| 394 | |
| 395 | // Now attach the location information to the DIE. |
| 396 | addBlock(Die, Attribute, 0, Block); |
| 397 | } |
| 398 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 399 | /// addComplexAddress - Start with the address based on the location provided, |
| 400 | /// and generate the DWARF information necessary to find the actual variable |
| 401 | /// given the extra address information encoded in the DIVariable, starting from |
| 402 | /// the starting location. Add the DWARF information to the die. |
| 403 | /// |
| 404 | void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die, |
| 405 | unsigned Attribute, |
| 406 | const MachineLocation &Location) { |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 407 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
Devang Patel | c26f544 | 2011-04-28 02:22:40 +0000 | [diff] [blame] | 408 | unsigned N = DV->getNumAddrElements(); |
| 409 | unsigned i = 0; |
| 410 | if (Location.isReg()) { |
| 411 | if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) { |
| 412 | // If first address element is OpPlus then emit |
| 413 | // DW_OP_breg + Offset instead of DW_OP_reg + Offset. |
| 414 | addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1)); |
| 415 | i = 2; |
| 416 | } else |
| 417 | addRegisterOp(Block, Location.getReg()); |
| 418 | } |
Devang Patel | 116da2f | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 419 | else |
| 420 | addRegisterOffset(Block, Location.getReg(), Location.getOffset()); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 421 | |
Devang Patel | c26f544 | 2011-04-28 02:22:40 +0000 | [diff] [blame] | 422 | for (;i < N; ++i) { |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 423 | uint64_t Element = DV->getAddrElement(i); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 424 | if (Element == DIBuilder::OpPlus) { |
| 425 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 426 | addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i)); |
| 427 | } else if (Element == DIBuilder::OpDeref) { |
Eric Christopher | 5012076 | 2012-05-08 18:56:00 +0000 | [diff] [blame] | 428 | if (!Location.isReg()) |
| 429 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 430 | } else llvm_unreachable("unknown DIBuilder Opcode"); |
| 431 | } |
| 432 | |
| 433 | // Now attach the location information to the DIE. |
| 434 | addBlock(Die, Attribute, 0, Block); |
| 435 | } |
| 436 | |
| 437 | /* Byref variables, in Blocks, are declared by the programmer as "SomeType |
| 438 | VarName;", but the compiler creates a __Block_byref_x_VarName struct, and |
| 439 | gives the variable VarName either the struct, or a pointer to the struct, as |
| 440 | its type. This is necessary for various behind-the-scenes things the |
| 441 | compiler needs to do with by-reference variables in Blocks. |
| 442 | |
| 443 | However, as far as the original *programmer* is concerned, the variable |
| 444 | should still have type 'SomeType', as originally declared. |
| 445 | |
| 446 | The function getBlockByrefType dives into the __Block_byref_x_VarName |
| 447 | struct to find the original type of the variable, which is then assigned to |
| 448 | the variable's Debug Information Entry as its real type. So far, so good. |
| 449 | However now the debugger will expect the variable VarName to have the type |
| 450 | SomeType. So we need the location attribute for the variable to be an |
| 451 | expression that explains to the debugger how to navigate through the |
| 452 | pointers and struct to find the actual variable of type SomeType. |
| 453 | |
| 454 | The following function does just that. We start by getting |
| 455 | the "normal" location for the variable. This will be the location |
| 456 | of either the struct __Block_byref_x_VarName or the pointer to the |
| 457 | struct __Block_byref_x_VarName. |
| 458 | |
| 459 | The struct will look something like: |
| 460 | |
| 461 | struct __Block_byref_x_VarName { |
| 462 | ... <various fields> |
| 463 | struct __Block_byref_x_VarName *forwarding; |
| 464 | ... <various other fields> |
| 465 | SomeType VarName; |
| 466 | ... <maybe more fields> |
| 467 | }; |
| 468 | |
| 469 | If we are given the struct directly (as our starting point) we |
| 470 | need to tell the debugger to: |
| 471 | |
| 472 | 1). Add the offset of the forwarding field. |
| 473 | |
| 474 | 2). Follow that pointer to get the real __Block_byref_x_VarName |
| 475 | struct to use (the real one may have been copied onto the heap). |
| 476 | |
| 477 | 3). Add the offset for the field VarName, to find the actual variable. |
| 478 | |
| 479 | If we started with a pointer to the struct, then we need to |
| 480 | dereference that pointer first, before the other steps. |
| 481 | Translating this into DWARF ops, we will need to append the following |
| 482 | to the current location description for the variable: |
| 483 | |
| 484 | DW_OP_deref -- optional, if we start with a pointer |
| 485 | DW_OP_plus_uconst <forward_fld_offset> |
| 486 | DW_OP_deref |
| 487 | DW_OP_plus_uconst <varName_fld_offset> |
| 488 | |
| 489 | That is what this function does. */ |
| 490 | |
| 491 | /// addBlockByrefAddress - Start with the address based on the location |
| 492 | /// provided, and generate the DWARF information necessary to find the |
| 493 | /// actual Block variable (navigating the Block struct) based on the |
| 494 | /// starting location. Add the DWARF information to the die. For |
| 495 | /// more information, read large comment just above here. |
| 496 | /// |
| 497 | void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die, |
| 498 | unsigned Attribute, |
| 499 | const MachineLocation &Location) { |
| 500 | DIType Ty = DV->getType(); |
| 501 | DIType TmpTy = Ty; |
| 502 | unsigned Tag = Ty.getTag(); |
| 503 | bool isPointer = false; |
| 504 | |
| 505 | StringRef varName = DV->getName(); |
| 506 | |
| 507 | if (Tag == dwarf::DW_TAG_pointer_type) { |
| 508 | DIDerivedType DTy = DIDerivedType(Ty); |
| 509 | TmpTy = DTy.getTypeDerivedFrom(); |
| 510 | isPointer = true; |
| 511 | } |
| 512 | |
| 513 | DICompositeType blockStruct = DICompositeType(TmpTy); |
| 514 | |
| 515 | // Find the __forwarding field and the variable field in the __Block_byref |
| 516 | // struct. |
| 517 | DIArray Fields = blockStruct.getTypeArray(); |
| 518 | DIDescriptor varField = DIDescriptor(); |
| 519 | DIDescriptor forwardingField = DIDescriptor(); |
| 520 | |
| 521 | for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) { |
| 522 | DIDescriptor Element = Fields.getElement(i); |
| 523 | DIDerivedType DT = DIDerivedType(Element); |
| 524 | StringRef fieldName = DT.getName(); |
| 525 | if (fieldName == "__forwarding") |
| 526 | forwardingField = Element; |
| 527 | else if (fieldName == varName) |
| 528 | varField = Element; |
| 529 | } |
| 530 | |
| 531 | // Get the offsets for the forwarding field and the variable field. |
| 532 | unsigned forwardingFieldOffset = |
| 533 | DIDerivedType(forwardingField).getOffsetInBits() >> 3; |
| 534 | unsigned varFieldOffset = |
| 535 | DIDerivedType(varField).getOffsetInBits() >> 3; |
| 536 | |
| 537 | // Decode the original location, and use that as the start of the byref |
| 538 | // variable's location. |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 539 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
| 540 | |
Eric Christopher | caba263 | 2012-07-04 02:02:18 +0000 | [diff] [blame] | 541 | if (Location.isReg()) |
| 542 | addRegisterOp(Block, Location.getReg()); |
| 543 | else |
| 544 | addRegisterOffset(Block, Location.getReg(), Location.getOffset()); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 545 | |
| 546 | // If we started with a pointer to the __Block_byref... struct, then |
| 547 | // the first thing we need to do is dereference the pointer (DW_OP_deref). |
| 548 | if (isPointer) |
| 549 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
| 550 | |
| 551 | // Next add the offset for the '__forwarding' field: |
| 552 | // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in |
| 553 | // adding the offset if it's 0. |
| 554 | if (forwardingFieldOffset > 0) { |
| 555 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 556 | addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset); |
| 557 | } |
| 558 | |
| 559 | // Now dereference the __forwarding field to get to the real __Block_byref |
| 560 | // struct: DW_OP_deref. |
| 561 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
| 562 | |
| 563 | // Now that we've got the real __Block_byref... struct, add the offset |
| 564 | // for the variable's field to get to the location of the actual variable: |
| 565 | // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0. |
| 566 | if (varFieldOffset > 0) { |
| 567 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 568 | addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset); |
| 569 | } |
| 570 | |
| 571 | // Now attach the location information to the DIE. |
| 572 | addBlock(Die, Attribute, 0, Block); |
| 573 | } |
| 574 | |
Devang Patel | 4ec14b0 | 2011-07-20 21:57:04 +0000 | [diff] [blame] | 575 | /// isTypeSigned - Return true if the type is signed. |
| 576 | static bool isTypeSigned(DIType Ty, int *SizeInBits) { |
| 577 | if (Ty.isDerivedType()) |
| 578 | return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits); |
| 579 | if (Ty.isBasicType()) |
| 580 | if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed |
| 581 | || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) { |
| 582 | *SizeInBits = Ty.getSizeInBits(); |
| 583 | return true; |
| 584 | } |
| 585 | return false; |
| 586 | } |
| 587 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 588 | /// addConstantValue - Add constant value entry in variable DIE. |
Devang Patel | b58128e | 2011-05-27 16:45:18 +0000 | [diff] [blame] | 589 | bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO, |
| 590 | DIType Ty) { |
Nick Lewycky | 746cb67 | 2011-10-26 22:55:33 +0000 | [diff] [blame] | 591 | assert(MO.isImm() && "Invalid machine operand!"); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 592 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
Devang Patel | 4ec14b0 | 2011-07-20 21:57:04 +0000 | [diff] [blame] | 593 | int SizeInBits = -1; |
| 594 | bool SignedConstant = isTypeSigned(Ty, &SizeInBits); |
| 595 | unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata; |
| 596 | switch (SizeInBits) { |
| 597 | case 8: Form = dwarf::DW_FORM_data1; break; |
| 598 | case 16: Form = dwarf::DW_FORM_data2; break; |
| 599 | case 32: Form = dwarf::DW_FORM_data4; break; |
| 600 | case 64: Form = dwarf::DW_FORM_data8; break; |
Devang Patel | 045c1d4 | 2011-05-27 19:13:26 +0000 | [diff] [blame] | 601 | default: break; |
| 602 | } |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 603 | SignedConstant ? addSInt(Block, 0, Form, MO.getImm()) |
Devang Patel | 4ec14b0 | 2011-07-20 21:57:04 +0000 | [diff] [blame] | 604 | : addUInt(Block, 0, Form, MO.getImm()); |
Devang Patel | 72f0d9c | 2011-05-27 18:15:52 +0000 | [diff] [blame] | 605 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 606 | addBlock(Die, dwarf::DW_AT_const_value, 0, Block); |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | /// addConstantFPValue - Add constant value entry in variable DIE. |
| 611 | bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) { |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 612 | assert (MO.isFPImm() && "Invalid machine operand!"); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 613 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
| 614 | APFloat FPImm = MO.getFPImm()->getValueAPF(); |
| 615 | |
| 616 | // Get the raw data form of the floating point. |
| 617 | const APInt FltVal = FPImm.bitcastToAPInt(); |
| 618 | const char *FltPtr = (const char*)FltVal.getRawData(); |
| 619 | |
| 620 | int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 621 | bool LittleEndian = Asm->getDataLayout().isLittleEndian(); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 622 | int Incr = (LittleEndian ? 1 : -1); |
| 623 | int Start = (LittleEndian ? 0 : NumBytes - 1); |
| 624 | int Stop = (LittleEndian ? NumBytes : -1); |
| 625 | |
| 626 | // Output the constant to DWARF one byte at a time. |
| 627 | for (; Start != Stop; Start += Incr) |
| 628 | addUInt(Block, 0, dwarf::DW_FORM_data1, |
| 629 | (unsigned char)0xFF & FltPtr[Start]); |
| 630 | |
| 631 | addBlock(Die, dwarf::DW_AT_const_value, 0, Block); |
| 632 | return true; |
| 633 | } |
| 634 | |
David Blaikie | 1426841 | 2013-01-20 01:18:01 +0000 | [diff] [blame] | 635 | /// addConstantFPValue - Add constant value entry in variable DIE. |
| 636 | bool CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) { |
| 637 | return addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), false); |
| 638 | } |
| 639 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 640 | /// addConstantValue - Add constant value entry in variable DIE. |
Devang Patel | 8594d42 | 2011-06-24 20:46:11 +0000 | [diff] [blame] | 641 | bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI, |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 642 | bool Unsigned) { |
David Blaikie | 1426841 | 2013-01-20 01:18:01 +0000 | [diff] [blame] | 643 | return addConstantValue(Die, CI->getValue(), Unsigned); |
| 644 | } |
| 645 | |
| 646 | // addConstantValue - Add constant value entry in variable DIE. |
| 647 | bool CompileUnit::addConstantValue(DIE *Die, const APInt &Val, |
| 648 | bool Unsigned) { |
| 649 | unsigned CIBitWidth = Val.getBitWidth(); |
Devang Patel | d6a8136 | 2011-05-28 00:39:18 +0000 | [diff] [blame] | 650 | if (CIBitWidth <= 64) { |
| 651 | unsigned form = 0; |
| 652 | switch (CIBitWidth) { |
| 653 | case 8: form = dwarf::DW_FORM_data1; break; |
| 654 | case 16: form = dwarf::DW_FORM_data2; break; |
| 655 | case 32: form = dwarf::DW_FORM_data4; break; |
| 656 | case 64: form = dwarf::DW_FORM_data8; break; |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 657 | default: |
Devang Patel | d6a8136 | 2011-05-28 00:39:18 +0000 | [diff] [blame] | 658 | form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata; |
| 659 | } |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 660 | if (Unsigned) |
David Blaikie | 1426841 | 2013-01-20 01:18:01 +0000 | [diff] [blame] | 661 | addUInt(Die, dwarf::DW_AT_const_value, form, Val.getZExtValue()); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 662 | else |
David Blaikie | 1426841 | 2013-01-20 01:18:01 +0000 | [diff] [blame] | 663 | addSInt(Die, dwarf::DW_AT_const_value, form, Val.getSExtValue()); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 664 | return true; |
| 665 | } |
| 666 | |
| 667 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
| 668 | |
| 669 | // Get the raw data form of the large APInt. |
NAKAMURA Takumi | c3e48c3 | 2011-10-28 14:12:22 +0000 | [diff] [blame] | 670 | const uint64_t *Ptr64 = Val.getRawData(); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 671 | |
| 672 | int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 673 | bool LittleEndian = Asm->getDataLayout().isLittleEndian(); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 674 | |
| 675 | // Output the constant to DWARF one byte at a time. |
NAKAMURA Takumi | c3e48c3 | 2011-10-28 14:12:22 +0000 | [diff] [blame] | 676 | for (int i = 0; i < NumBytes; i++) { |
| 677 | uint8_t c; |
| 678 | if (LittleEndian) |
| 679 | c = Ptr64[i / 8] >> (8 * (i & 7)); |
| 680 | else |
| 681 | c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7)); |
| 682 | addUInt(Block, 0, dwarf::DW_FORM_data1, c); |
| 683 | } |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 684 | |
| 685 | addBlock(Die, dwarf::DW_AT_const_value, 0, Block); |
| 686 | return true; |
| 687 | } |
| 688 | |
Eric Christopher | 6c3bb94 | 2013-04-22 07:47:40 +0000 | [diff] [blame] | 689 | /// addTemplateParams - Add template parameters into buffer. |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 690 | void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) { |
| 691 | // Add template parameters. |
| 692 | for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) { |
| 693 | DIDescriptor Element = TParams.getElement(i); |
| 694 | if (Element.isTemplateTypeParameter()) |
| 695 | Buffer.addChild(getOrCreateTemplateTypeParameterDIE( |
| 696 | DITemplateTypeParameter(Element))); |
| 697 | else if (Element.isTemplateValueParameter()) |
| 698 | Buffer.addChild(getOrCreateTemplateValueParameterDIE( |
| 699 | DITemplateValueParameter(Element))); |
| 700 | } |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 701 | } |
Nick Lewycky | 746cb67 | 2011-10-26 22:55:33 +0000 | [diff] [blame] | 702 | |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 703 | /// getOrCreateContextDIE - Get context owner's DIE. |
| 704 | DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) { |
| 705 | if (Context.isType()) |
| 706 | return getOrCreateTypeDIE(DIType(Context)); |
| 707 | else if (Context.isNameSpace()) |
| 708 | return getOrCreateNameSpace(DINameSpace(Context)); |
| 709 | else if (Context.isSubprogram()) |
| 710 | return getOrCreateSubprogramDIE(DISubprogram(Context)); |
Eric Christopher | 6c3bb94 | 2013-04-22 07:47:40 +0000 | [diff] [blame] | 711 | else |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 712 | return getDIE(Context); |
| 713 | } |
| 714 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 715 | /// addToContextOwner - Add Die into the list of its context owner's children. |
| 716 | void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) { |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 717 | if (DIE *ContextDIE = getOrCreateContextDIE(Context)) |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 718 | ContextDIE->addChild(Die); |
| 719 | else |
| 720 | addDie(Die); |
| 721 | } |
| 722 | |
| 723 | /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the |
| 724 | /// given DIType. |
Devang Patel | 94c7ddb | 2011-08-16 22:09:43 +0000 | [diff] [blame] | 725 | DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) { |
| 726 | DIType Ty(TyNode); |
| 727 | if (!Ty.Verify()) |
| 728 | return NULL; |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 729 | DIE *TyDIE = getDIE(Ty); |
| 730 | if (TyDIE) |
| 731 | return TyDIE; |
| 732 | |
| 733 | // Create new type. |
| 734 | TyDIE = new DIE(dwarf::DW_TAG_base_type); |
| 735 | insertDIE(Ty, TyDIE); |
| 736 | if (Ty.isBasicType()) |
| 737 | constructTypeDIE(*TyDIE, DIBasicType(Ty)); |
| 738 | else if (Ty.isCompositeType()) |
| 739 | constructTypeDIE(*TyDIE, DICompositeType(Ty)); |
| 740 | else { |
| 741 | assert(Ty.isDerivedType() && "Unknown kind of DIType"); |
| 742 | constructTypeDIE(*TyDIE, DIDerivedType(Ty)); |
| 743 | } |
Eric Christopher | 1b3f919 | 2011-11-10 19:52:58 +0000 | [diff] [blame] | 744 | // If this is a named finished type then include it in the list of types |
| 745 | // for the accelerator tables. |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 746 | if (!Ty.getName().empty() && !Ty.isForwardDecl()) { |
| 747 | bool IsImplementation = 0; |
| 748 | if (Ty.isCompositeType()) { |
| 749 | DICompositeType CT(Ty); |
Eric Christopher | e016789 | 2012-01-06 23:03:37 +0000 | [diff] [blame] | 750 | // A runtime language of 0 actually means C/C++ and that any |
| 751 | // non-negative value is some version of Objective-C/C++. |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 752 | IsImplementation = (CT.getRunTimeLang() == 0) || |
Eric Christopher | e2dc933 | 2012-02-22 08:46:02 +0000 | [diff] [blame] | 753 | CT.isObjcClassComplete(); |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 754 | } |
Eric Christopher | e016789 | 2012-01-06 23:03:37 +0000 | [diff] [blame] | 755 | unsigned Flags = IsImplementation ? |
| 756 | DwarfAccelTable::eTypeFlagClassIsImplementation : 0; |
| 757 | addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags)); |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 758 | } |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 759 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 760 | addToContextOwner(TyDIE, Ty.getContext()); |
| 761 | return TyDIE; |
| 762 | } |
| 763 | |
| 764 | /// addType - Add a new type attribute to the specified entity. |
Eric Christopher | 4d069bf | 2012-05-22 18:45:24 +0000 | [diff] [blame] | 765 | void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) { |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 766 | if (!Ty.Verify()) |
| 767 | return; |
| 768 | |
| 769 | // Check for pre-existence. |
| 770 | DIEEntry *Entry = getDIEEntry(Ty); |
| 771 | // If it exists then use the existing value. |
| 772 | if (Entry) { |
Eric Christopher | 663e0cf | 2012-03-28 07:34:31 +0000 | [diff] [blame] | 773 | Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 774 | return; |
| 775 | } |
| 776 | |
| 777 | // Construct type. |
| 778 | DIE *Buffer = getOrCreateTypeDIE(Ty); |
| 779 | |
| 780 | // Set up proxy. |
| 781 | Entry = createDIEEntry(Buffer); |
| 782 | insertDIEEntry(Ty, Entry); |
Eric Christopher | 663e0cf | 2012-03-28 07:34:31 +0000 | [diff] [blame] | 783 | Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry); |
Devang Patel | e9ae06c | 2011-05-31 22:56:51 +0000 | [diff] [blame] | 784 | |
| 785 | // If this is a complete composite type then include it in the |
| 786 | // list of global types. |
Devang Patel | c20bdf1 | 2011-06-01 00:23:24 +0000 | [diff] [blame] | 787 | addGlobalType(Ty); |
Devang Patel | 66658e4 | 2011-05-31 23:30:30 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /// addGlobalType - Add a new global type to the compile unit. |
| 791 | /// |
Devang Patel | c20bdf1 | 2011-06-01 00:23:24 +0000 | [diff] [blame] | 792 | void CompileUnit::addGlobalType(DIType Ty) { |
Devang Patel | e9ae06c | 2011-05-31 22:56:51 +0000 | [diff] [blame] | 793 | DIDescriptor Context = Ty.getContext(); |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 794 | if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl() |
| 795 | && (!Context || Context.isCompileUnit() || Context.isFile() |
Devang Patel | 94c7ddb | 2011-08-16 22:09:43 +0000 | [diff] [blame] | 796 | || Context.isNameSpace())) |
Devang Patel | c20bdf1 | 2011-06-01 00:23:24 +0000 | [diff] [blame] | 797 | if (DIEEntry *Entry = getDIEEntry(Ty)) |
| 798 | GlobalTypes[Ty.getName()] = Entry->getEntry(); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Devang Patel | 31c5d05 | 2011-05-06 16:57:54 +0000 | [diff] [blame] | 801 | /// addPubTypes - Add type for pubtypes section. |
| 802 | void CompileUnit::addPubTypes(DISubprogram SP) { |
| 803 | DICompositeType SPTy = SP.getType(); |
| 804 | unsigned SPTag = SPTy.getTag(); |
| 805 | if (SPTag != dwarf::DW_TAG_subroutine_type) |
| 806 | return; |
| 807 | |
| 808 | DIArray Args = SPTy.getTypeArray(); |
| 809 | for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) { |
| 810 | DIType ATy(Args.getElement(i)); |
| 811 | if (!ATy.Verify()) |
| 812 | continue; |
Devang Patel | c20bdf1 | 2011-06-01 00:23:24 +0000 | [diff] [blame] | 813 | addGlobalType(ATy); |
Devang Patel | 31c5d05 | 2011-05-06 16:57:54 +0000 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 817 | /// constructTypeDIE - Construct basic type die from DIBasicType. |
| 818 | void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) { |
| 819 | // Get core information. |
| 820 | StringRef Name = BTy.getName(); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 821 | // Add name if not anonymous or intermediate type. |
| 822 | if (!Name.empty()) |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 823 | addString(&Buffer, dwarf::DW_AT_name, Name); |
Devang Patel | 734a67c | 2011-09-14 23:13:28 +0000 | [diff] [blame] | 824 | |
| 825 | if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) { |
| 826 | Buffer.setTag(dwarf::DW_TAG_unspecified_type); |
| 827 | // Unspecified types has only name, nothing else. |
| 828 | return; |
| 829 | } |
| 830 | |
| 831 | Buffer.setTag(dwarf::DW_TAG_base_type); |
Nick Lewycky | 746cb67 | 2011-10-26 22:55:33 +0000 | [diff] [blame] | 832 | addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, |
Devang Patel | 30d409c | 2012-02-07 23:33:58 +0000 | [diff] [blame] | 833 | BTy.getEncoding()); |
Devang Patel | 734a67c | 2011-09-14 23:13:28 +0000 | [diff] [blame] | 834 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 835 | uint64_t Size = BTy.getSizeInBits() >> 3; |
| 836 | addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); |
| 837 | } |
| 838 | |
| 839 | /// constructTypeDIE - Construct derived type die from DIDerivedType. |
| 840 | void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) { |
| 841 | // Get core information. |
| 842 | StringRef Name = DTy.getName(); |
| 843 | uint64_t Size = DTy.getSizeInBits() >> 3; |
| 844 | unsigned Tag = DTy.getTag(); |
| 845 | |
| 846 | // FIXME - Workaround for templates. |
| 847 | if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type; |
| 848 | |
| 849 | Buffer.setTag(Tag); |
| 850 | |
| 851 | // Map to main type, void will not have a type. |
| 852 | DIType FromTy = DTy.getTypeDerivedFrom(); |
| 853 | addType(&Buffer, FromTy); |
| 854 | |
| 855 | // Add name if not anonymous or intermediate type. |
| 856 | if (!Name.empty()) |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 857 | addString(&Buffer, dwarf::DW_AT_name, Name); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 858 | |
| 859 | // Add size if non-zero (derived types might be zero-sized.) |
Eric Christopher | 35f225a | 2012-02-21 22:25:53 +0000 | [diff] [blame] | 860 | if (Size && Tag != dwarf::DW_TAG_pointer_type) |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 861 | addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); |
| 862 | |
David Blaikie | 62fdfb5 | 2013-01-07 05:51:15 +0000 | [diff] [blame] | 863 | if (Tag == dwarf::DW_TAG_ptr_to_member_type) |
| 864 | addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, |
| 865 | getOrCreateTypeDIE(DTy.getClassType())); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 866 | // Add source line info if available and TyDesc is not a forward declaration. |
| 867 | if (!DTy.isForwardDecl()) |
| 868 | addSourceLine(&Buffer, DTy); |
| 869 | } |
| 870 | |
| 871 | /// constructTypeDIE - Construct type DIE from DICompositeType. |
| 872 | void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) { |
| 873 | // Get core information. |
| 874 | StringRef Name = CTy.getName(); |
| 875 | |
| 876 | uint64_t Size = CTy.getSizeInBits() >> 3; |
| 877 | unsigned Tag = CTy.getTag(); |
| 878 | Buffer.setTag(Tag); |
| 879 | |
| 880 | switch (Tag) { |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 881 | case dwarf::DW_TAG_array_type: |
| 882 | constructArrayTypeDIE(Buffer, &CTy); |
| 883 | break; |
| 884 | case dwarf::DW_TAG_enumeration_type: { |
| 885 | DIArray Elements = CTy.getTypeArray(); |
| 886 | |
| 887 | // Add enumerators to enumeration type. |
| 888 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
| 889 | DIE *ElemDie = NULL; |
| 890 | DIDescriptor Enum(Elements.getElement(i)); |
| 891 | if (Enum.isEnumerator()) { |
| 892 | ElemDie = constructEnumTypeDIE(DIEnumerator(Enum)); |
| 893 | Buffer.addChild(ElemDie); |
| 894 | } |
| 895 | } |
Eric Christopher | bb0f6ea | 2012-05-23 00:09:20 +0000 | [diff] [blame] | 896 | DIType DTy = CTy.getTypeDerivedFrom(); |
| 897 | if (DTy.Verify()) { |
| 898 | addType(&Buffer, DTy); |
| 899 | addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1); |
| 900 | } |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 901 | } |
| 902 | break; |
| 903 | case dwarf::DW_TAG_subroutine_type: { |
| 904 | // Add return type. |
| 905 | DIArray Elements = CTy.getTypeArray(); |
| 906 | DIDescriptor RTy = Elements.getElement(0); |
| 907 | addType(&Buffer, DIType(RTy)); |
| 908 | |
| 909 | bool isPrototyped = true; |
| 910 | // Add arguments. |
| 911 | for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) { |
| 912 | DIDescriptor Ty = Elements.getElement(i); |
| 913 | if (Ty.isUnspecifiedParameter()) { |
| 914 | DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters); |
| 915 | Buffer.addChild(Arg); |
| 916 | isPrototyped = false; |
| 917 | } else { |
| 918 | DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); |
| 919 | addType(Arg, DIType(Ty)); |
David Blaikie | aaf2e63 | 2013-01-29 19:35:24 +0000 | [diff] [blame] | 920 | if (DIType(Ty).isArtificial()) |
| 921 | addFlag(Arg, dwarf::DW_AT_artificial); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 922 | Buffer.addChild(Arg); |
| 923 | } |
| 924 | } |
Eric Christopher | 8b6fe6b | 2012-02-22 08:46:21 +0000 | [diff] [blame] | 925 | // Add prototype flag if we're dealing with a C language and the |
| 926 | // function has been prototyped. |
| 927 | if (isPrototyped && |
Eric Christopher | 4d069bf | 2012-05-22 18:45:24 +0000 | [diff] [blame] | 928 | (Language == dwarf::DW_LANG_C89 || |
| 929 | Language == dwarf::DW_LANG_C99 || |
| 930 | Language == dwarf::DW_LANG_ObjC)) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 931 | addFlag(&Buffer, dwarf::DW_AT_prototyped); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 932 | } |
| 933 | break; |
| 934 | case dwarf::DW_TAG_structure_type: |
| 935 | case dwarf::DW_TAG_union_type: |
| 936 | case dwarf::DW_TAG_class_type: { |
| 937 | // Add elements to structure type. |
| 938 | DIArray Elements = CTy.getTypeArray(); |
| 939 | |
| 940 | // A forward struct declared type may not have elements available. |
| 941 | unsigned N = Elements.getNumElements(); |
| 942 | if (N == 0) |
| 943 | break; |
| 944 | |
| 945 | // Add elements to structure type. |
| 946 | for (unsigned i = 0; i < N; ++i) { |
| 947 | DIDescriptor Element = Elements.getElement(i); |
| 948 | DIE *ElemDie = NULL; |
| 949 | if (Element.isSubprogram()) { |
| 950 | DISubprogram SP(Element); |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 951 | ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element)); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 952 | if (SP.isProtected()) |
Nick Lewycky | 13aaca5 | 2011-12-13 05:09:11 +0000 | [diff] [blame] | 953 | addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 954 | dwarf::DW_ACCESS_protected); |
| 955 | else if (SP.isPrivate()) |
Nick Lewycky | 13aaca5 | 2011-12-13 05:09:11 +0000 | [diff] [blame] | 956 | addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 957 | dwarf::DW_ACCESS_private); |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 958 | else |
Nick Lewycky | 13aaca5 | 2011-12-13 05:09:11 +0000 | [diff] [blame] | 959 | addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 960 | dwarf::DW_ACCESS_public); |
| 961 | if (SP.isExplicit()) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 962 | addFlag(ElemDie, dwarf::DW_AT_explicit); |
Eric Christopher | 663e0cf | 2012-03-28 07:34:31 +0000 | [diff] [blame] | 963 | } else if (Element.isDerivedType()) { |
Eric Christopher | 4d069bf | 2012-05-22 18:45:24 +0000 | [diff] [blame] | 964 | DIDerivedType DDTy(Element); |
| 965 | if (DDTy.getTag() == dwarf::DW_TAG_friend) { |
| 966 | ElemDie = new DIE(dwarf::DW_TAG_friend); |
| 967 | addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend); |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 968 | } else if (DDTy.isStaticMember()) |
| 969 | ElemDie = createStaticMemberDIE(DDTy); |
| 970 | else |
| 971 | ElemDie = createMemberDIE(DDTy); |
Eric Christopher | 663e0cf | 2012-03-28 07:34:31 +0000 | [diff] [blame] | 972 | } else if (Element.isObjCProperty()) { |
Devang Patel | 30d409c | 2012-02-07 23:33:58 +0000 | [diff] [blame] | 973 | DIObjCProperty Property(Element); |
| 974 | ElemDie = new DIE(Property.getTag()); |
| 975 | StringRef PropertyName = Property.getObjCPropertyName(); |
| 976 | addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); |
Eric Christopher | 4d069bf | 2012-05-22 18:45:24 +0000 | [diff] [blame] | 977 | addType(ElemDie, Property.getType()); |
| 978 | addSourceLine(ElemDie, Property); |
Devang Patel | 30d409c | 2012-02-07 23:33:58 +0000 | [diff] [blame] | 979 | StringRef GetterName = Property.getObjCPropertyGetterName(); |
| 980 | if (!GetterName.empty()) |
| 981 | addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName); |
| 982 | StringRef SetterName = Property.getObjCPropertySetterName(); |
| 983 | if (!SetterName.empty()) |
| 984 | addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName); |
| 985 | unsigned PropertyAttributes = 0; |
Devang Patel | 9e11eb1 | 2012-02-04 01:30:32 +0000 | [diff] [blame] | 986 | if (Property.isReadOnlyObjCProperty()) |
| 987 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly; |
| 988 | if (Property.isReadWriteObjCProperty()) |
| 989 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite; |
| 990 | if (Property.isAssignObjCProperty()) |
| 991 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign; |
| 992 | if (Property.isRetainObjCProperty()) |
| 993 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain; |
| 994 | if (Property.isCopyObjCProperty()) |
| 995 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy; |
| 996 | if (Property.isNonAtomicObjCProperty()) |
| 997 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic; |
| 998 | if (PropertyAttributes) |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 999 | addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0, |
Devang Patel | 9e11eb1 | 2012-02-04 01:30:32 +0000 | [diff] [blame] | 1000 | PropertyAttributes); |
Devang Patel | 6588abf | 2012-02-06 17:49:43 +0000 | [diff] [blame] | 1001 | |
Devang Patel | 30d409c | 2012-02-07 23:33:58 +0000 | [diff] [blame] | 1002 | DIEEntry *Entry = getDIEEntry(Element); |
| 1003 | if (!Entry) { |
| 1004 | Entry = createDIEEntry(ElemDie); |
| 1005 | insertDIEEntry(Element, Entry); |
| 1006 | } |
Devang Patel | 9e11eb1 | 2012-02-04 01:30:32 +0000 | [diff] [blame] | 1007 | } else |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1008 | continue; |
| 1009 | Buffer.addChild(ElemDie); |
| 1010 | } |
| 1011 | |
| 1012 | if (CTy.isAppleBlockExtension()) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1013 | addFlag(&Buffer, dwarf::DW_AT_APPLE_block); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1014 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1015 | DICompositeType ContainingType = CTy.getContainingType(); |
| 1016 | if (DIDescriptor(ContainingType).isCompositeType()) |
| 1017 | addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, |
| 1018 | getOrCreateTypeDIE(DIType(ContainingType))); |
| 1019 | else { |
| 1020 | DIDescriptor Context = CTy.getContext(); |
| 1021 | addToContextOwner(&Buffer, Context); |
| 1022 | } |
| 1023 | |
Devang Patel | 201e6cd | 2011-05-12 21:29:42 +0000 | [diff] [blame] | 1024 | if (CTy.isObjcClassComplete()) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1025 | addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type); |
Devang Patel | b11f80e | 2011-05-12 19:06:16 +0000 | [diff] [blame] | 1026 | |
Eric Christopher | 1a8e886 | 2011-12-16 23:42:42 +0000 | [diff] [blame] | 1027 | // Add template parameters to a class, structure or union types. |
| 1028 | // FIXME: The support isn't in the metadata for this yet. |
| 1029 | if (Tag == dwarf::DW_TAG_class_type || |
| 1030 | Tag == dwarf::DW_TAG_structure_type || |
| 1031 | Tag == dwarf::DW_TAG_union_type) |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1032 | addTemplateParams(Buffer, CTy.getTemplateParams()); |
| 1033 | |
| 1034 | break; |
| 1035 | } |
| 1036 | default: |
| 1037 | break; |
| 1038 | } |
| 1039 | |
| 1040 | // Add name if not anonymous or intermediate type. |
| 1041 | if (!Name.empty()) |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1042 | addString(&Buffer, dwarf::DW_AT_name, Name); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1043 | |
Eric Christopher | 4a5d839 | 2012-05-22 18:45:18 +0000 | [diff] [blame] | 1044 | if (Tag == dwarf::DW_TAG_enumeration_type || |
| 1045 | Tag == dwarf::DW_TAG_class_type || |
| 1046 | Tag == dwarf::DW_TAG_structure_type || |
| 1047 | Tag == dwarf::DW_TAG_union_type) { |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1048 | // Add size if non-zero (derived types might be zero-sized.) |
Eric Christopher | fc4199b | 2012-06-01 00:22:32 +0000 | [diff] [blame] | 1049 | // TODO: Do we care about size for enum forward declarations? |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1050 | if (Size) |
| 1051 | addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size); |
Eric Christopher | fc4199b | 2012-06-01 00:22:32 +0000 | [diff] [blame] | 1052 | else if (!CTy.isForwardDecl()) |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1053 | // Add zero size if it is not a forward declaration. |
Eric Christopher | fc4199b | 2012-06-01 00:22:32 +0000 | [diff] [blame] | 1054 | addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0); |
| 1055 | |
| 1056 | // If we're a forward decl, say so. |
| 1057 | if (CTy.isForwardDecl()) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1058 | addFlag(&Buffer, dwarf::DW_AT_declaration); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1059 | |
| 1060 | // Add source line info if available. |
| 1061 | if (!CTy.isForwardDecl()) |
| 1062 | addSourceLine(&Buffer, CTy); |
Eric Christopher | 8938895 | 2012-03-07 00:15:19 +0000 | [diff] [blame] | 1063 | |
| 1064 | // No harm in adding the runtime language to the declaration. |
| 1065 | unsigned RLang = CTy.getRunTimeLang(); |
| 1066 | if (RLang) |
| 1067 | addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, |
| 1068 | dwarf::DW_FORM_data1, RLang); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1072 | /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1073 | /// for the given DITemplateTypeParameter. |
| 1074 | DIE * |
| 1075 | CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) { |
| 1076 | DIE *ParamDIE = getDIE(TP); |
| 1077 | if (ParamDIE) |
| 1078 | return ParamDIE; |
| 1079 | |
| 1080 | ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter); |
| 1081 | addType(ParamDIE, TP.getType()); |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1082 | addString(ParamDIE, dwarf::DW_AT_name, TP.getName()); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1083 | return ParamDIE; |
| 1084 | } |
| 1085 | |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1086 | /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1087 | /// for the given DITemplateValueParameter. |
| 1088 | DIE * |
Eric Christopher | 4d069bf | 2012-05-22 18:45:24 +0000 | [diff] [blame] | 1089 | CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){ |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1090 | DIE *ParamDIE = getDIE(TPV); |
| 1091 | if (ParamDIE) |
| 1092 | return ParamDIE; |
| 1093 | |
| 1094 | ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter); |
| 1095 | addType(ParamDIE, TPV.getType()); |
| 1096 | if (!TPV.getName().empty()) |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1097 | addString(ParamDIE, dwarf::DW_AT_name, TPV.getName()); |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1098 | addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata, |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1099 | TPV.getValue()); |
| 1100 | return ParamDIE; |
| 1101 | } |
| 1102 | |
Devang Patel | 31c5d05 | 2011-05-06 16:57:54 +0000 | [diff] [blame] | 1103 | /// getOrCreateNameSpace - Create a DIE for DINameSpace. |
| 1104 | DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) { |
| 1105 | DIE *NDie = getDIE(NS); |
| 1106 | if (NDie) |
| 1107 | return NDie; |
| 1108 | NDie = new DIE(dwarf::DW_TAG_namespace); |
| 1109 | insertDIE(NS, NDie); |
Eric Christopher | 09ac3d8 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1110 | if (!NS.getName().empty()) { |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1111 | addString(NDie, dwarf::DW_AT_name, NS.getName()); |
Eric Christopher | 09ac3d8 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1112 | addAccelNamespace(NS.getName(), NDie); |
| 1113 | } else |
| 1114 | addAccelNamespace("(anonymous namespace)", NDie); |
Devang Patel | 31c5d05 | 2011-05-06 16:57:54 +0000 | [diff] [blame] | 1115 | addSourceLine(NDie, NS); |
| 1116 | addToContextOwner(NDie, NS.getContext()); |
| 1117 | return NDie; |
| 1118 | } |
| 1119 | |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1120 | /// getRealLinkageName - If special LLVM prefix that is used to inform the asm |
| 1121 | /// printer to not emit usual symbol prefix before the symbol name is used then |
| 1122 | /// return linkage name after skipping this special LLVM prefix. |
| 1123 | static StringRef getRealLinkageName(StringRef LinkageName) { |
| 1124 | char One = '\1'; |
| 1125 | if (LinkageName.startswith(StringRef(&One, 1))) |
| 1126 | return LinkageName.substr(1); |
| 1127 | return LinkageName; |
| 1128 | } |
| 1129 | |
| 1130 | /// getOrCreateSubprogramDIE - Create new DIE using SP. |
| 1131 | DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) { |
| 1132 | DIE *SPDie = getDIE(SP); |
| 1133 | if (SPDie) |
| 1134 | return SPDie; |
| 1135 | |
Peter Collingbourne | 27302f0 | 2012-05-27 18:36:44 +0000 | [diff] [blame] | 1136 | SPDie = new DIE(dwarf::DW_TAG_subprogram); |
| 1137 | |
| 1138 | // DW_TAG_inlined_subroutine may refer to this DIE. |
| 1139 | insertDIE(SP, SPDie); |
| 1140 | |
Rafael Espindola | 01b55b4 | 2011-11-10 22:34:29 +0000 | [diff] [blame] | 1141 | DISubprogram SPDecl = SP.getFunctionDeclaration(); |
| 1142 | DIE *DeclDie = NULL; |
| 1143 | if (SPDecl.isSubprogram()) { |
| 1144 | DeclDie = getOrCreateSubprogramDIE(SPDecl); |
| 1145 | } |
| 1146 | |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1147 | // Add to context owner. |
| 1148 | addToContextOwner(SPDie, SP.getContext()); |
| 1149 | |
| 1150 | // Add function template parameters. |
| 1151 | addTemplateParams(*SPDie, SP.getTemplateParams()); |
| 1152 | |
Eric Christopher | 8d101c3 | 2012-03-15 08:19:33 +0000 | [diff] [blame] | 1153 | StringRef LinkageName = SP.getLinkageName(); |
Eric Christopher | 8d101c3 | 2012-03-15 08:19:33 +0000 | [diff] [blame] | 1154 | |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1155 | // If this DIE is going to refer declaration info using AT_specification |
Eric Christopher | cc55d45 | 2013-05-08 21:23:22 +0000 | [diff] [blame^] | 1156 | // then there is no need to add other attributes, except |
| 1157 | // DW_AT_MIPS_linkage_name in two cases: |
| 1158 | // 1) The current DIE has a linkage name but the referred-to declaration |
| 1159 | // doesn't have one (e.g. constructor/destructor). |
| 1160 | // 2) To work around a bug in older gdbs that requires the linkage name to |
| 1161 | // resolve multiple template functions. TODO: Remove this case (the |
| 1162 | // DD->useDarwinGDBCompat() condition) when we get rid of the old gdb |
| 1163 | // compatibility. |
| 1164 | |
Rafael Espindola | 01b55b4 | 2011-11-10 22:34:29 +0000 | [diff] [blame] | 1165 | if (DeclDie) { |
Eric Christopher | cc55d45 | 2013-05-08 21:23:22 +0000 | [diff] [blame^] | 1166 | if (!LinkageName.empty() |
| 1167 | && (SPDecl.getLinkageName().empty() || DD->useDarwinGDBCompat())) |
| 1168 | addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, |
| 1169 | getRealLinkageName(LinkageName)); |
| 1170 | |
Rafael Espindola | 01b55b4 | 2011-11-10 22:34:29 +0000 | [diff] [blame] | 1171 | // Refer function declaration directly. |
| 1172 | addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4, |
| 1173 | DeclDie); |
| 1174 | |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1175 | return SPDie; |
Rafael Espindola | 01b55b4 | 2011-11-10 22:34:29 +0000 | [diff] [blame] | 1176 | } |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1177 | |
Eric Christopher | cbbd5b1 | 2012-08-23 22:52:55 +0000 | [diff] [blame] | 1178 | // Add the linkage name if we have one. |
Eric Christopher | cc55d45 | 2013-05-08 21:23:22 +0000 | [diff] [blame^] | 1179 | if (!LinkageName.empty()) |
Eric Christopher | cbbd5b1 | 2012-08-23 22:52:55 +0000 | [diff] [blame] | 1180 | addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, |
| 1181 | getRealLinkageName(LinkageName)); |
| 1182 | |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1183 | // Constructors and operators for anonymous aggregates do not have names. |
| 1184 | if (!SP.getName().empty()) |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1185 | addString(SPDie, dwarf::DW_AT_name, SP.getName()); |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1186 | |
| 1187 | addSourceLine(SPDie, SP); |
| 1188 | |
Eric Christopher | 8b6fe6b | 2012-02-22 08:46:21 +0000 | [diff] [blame] | 1189 | // Add the prototype if we have a prototype and we have a C like |
| 1190 | // language. |
| 1191 | if (SP.isPrototyped() && |
| 1192 | (Language == dwarf::DW_LANG_C89 || |
| 1193 | Language == dwarf::DW_LANG_C99 || |
| 1194 | Language == dwarf::DW_LANG_ObjC)) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1195 | addFlag(SPDie, dwarf::DW_AT_prototyped); |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1196 | |
| 1197 | // Add Return Type. |
| 1198 | DICompositeType SPTy = SP.getType(); |
| 1199 | DIArray Args = SPTy.getTypeArray(); |
| 1200 | unsigned SPTag = SPTy.getTag(); |
| 1201 | |
| 1202 | if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type) |
| 1203 | addType(SPDie, SPTy); |
| 1204 | else |
| 1205 | addType(SPDie, DIType(Args.getElement(0))); |
| 1206 | |
| 1207 | unsigned VK = SP.getVirtuality(); |
| 1208 | if (VK) { |
Nick Lewycky | 798313d | 2011-12-14 00:56:07 +0000 | [diff] [blame] | 1209 | addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK); |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1210 | DIEBlock *Block = getDIEBlock(); |
| 1211 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); |
| 1212 | addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex()); |
| 1213 | addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block); |
| 1214 | ContainingTypeMap.insert(std::make_pair(SPDie, |
| 1215 | SP.getContainingType())); |
| 1216 | } |
| 1217 | |
| 1218 | if (!SP.isDefinition()) { |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1219 | addFlag(SPDie, dwarf::DW_AT_declaration); |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1220 | |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1221 | // Add arguments. Do not add arguments for subprogram definition. They will |
| 1222 | // be handled while processing variables. |
| 1223 | DICompositeType SPTy = SP.getType(); |
| 1224 | DIArray Args = SPTy.getTypeArray(); |
| 1225 | unsigned SPTag = SPTy.getTag(); |
| 1226 | |
| 1227 | if (SPTag == dwarf::DW_TAG_subroutine_type) |
| 1228 | for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { |
| 1229 | DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter); |
Eric Christopher | 5c38de9 | 2012-09-10 23:33:57 +0000 | [diff] [blame] | 1230 | DIType ATy = DIType(Args.getElement(i)); |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1231 | addType(Arg, ATy); |
| 1232 | if (ATy.isArtificial()) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1233 | addFlag(Arg, dwarf::DW_AT_artificial); |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1234 | SPDie->addChild(Arg); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | if (SP.isArtificial()) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1239 | addFlag(SPDie, dwarf::DW_AT_artificial); |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1240 | |
| 1241 | if (!SP.isLocalToUnit()) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1242 | addFlag(SPDie, dwarf::DW_AT_external); |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1243 | |
| 1244 | if (SP.isOptimized()) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1245 | addFlag(SPDie, dwarf::DW_AT_APPLE_optimized); |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1246 | |
| 1247 | if (unsigned isa = Asm->getISAEncoding()) { |
| 1248 | addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa); |
| 1249 | } |
| 1250 | |
| 1251 | return SPDie; |
| 1252 | } |
| 1253 | |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1254 | // Return const expression if value is a GEP to access merged global |
| 1255 | // constant. e.g. |
| 1256 | // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) |
| 1257 | static const ConstantExpr *getMergedGlobalExpr(const Value *V) { |
| 1258 | const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V); |
| 1259 | if (!CE || CE->getNumOperands() != 3 || |
| 1260 | CE->getOpcode() != Instruction::GetElementPtr) |
| 1261 | return NULL; |
| 1262 | |
| 1263 | // First operand points to a global struct. |
| 1264 | Value *Ptr = CE->getOperand(0); |
| 1265 | if (!isa<GlobalValue>(Ptr) || |
| 1266 | !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType())) |
| 1267 | return NULL; |
| 1268 | |
| 1269 | // Second operand is zero. |
| 1270 | const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1)); |
| 1271 | if (!CI || !CI->isZero()) |
| 1272 | return NULL; |
| 1273 | |
| 1274 | // Third operand is offset. |
| 1275 | if (!isa<ConstantInt>(CE->getOperand(2))) |
| 1276 | return NULL; |
| 1277 | |
| 1278 | return CE; |
| 1279 | } |
| 1280 | |
| 1281 | /// createGlobalVariableDIE - create global variable DIE. |
| 1282 | void CompileUnit::createGlobalVariableDIE(const MDNode *N) { |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1283 | // Check for pre-existence. |
Devang Patel | 49e2f03 | 2011-08-18 22:21:50 +0000 | [diff] [blame] | 1284 | if (getDIE(N)) |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1285 | return; |
| 1286 | |
Devang Patel | 49e2f03 | 2011-08-18 22:21:50 +0000 | [diff] [blame] | 1287 | DIGlobalVariable GV(N); |
Devang Patel | 28bea08 | 2011-08-18 23:17:55 +0000 | [diff] [blame] | 1288 | if (!GV.Verify()) |
| 1289 | return; |
| 1290 | |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1291 | DIDescriptor GVContext = GV.getContext(); |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1292 | DIType GTy = GV.getType(); |
| 1293 | |
| 1294 | // If this is a static data member definition, some attributes belong |
| 1295 | // to the declaration DIE. |
| 1296 | DIE *VariableDIE = NULL; |
Manman Ren | 945e828 | 2013-02-01 23:54:37 +0000 | [diff] [blame] | 1297 | bool IsStaticMember = false; |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1298 | DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration(); |
| 1299 | if (SDMDecl.Verify()) { |
| 1300 | assert(SDMDecl.isStaticMember() && "Expected static member decl"); |
| 1301 | // We need the declaration DIE that is in the static member's class. |
| 1302 | // But that class might not exist in the DWARF yet. |
| 1303 | // Creating the class will create the static member decl DIE. |
| 1304 | getOrCreateContextDIE(SDMDecl.getContext()); |
| 1305 | VariableDIE = getDIE(SDMDecl); |
| 1306 | assert(VariableDIE && "Static member decl has no context?"); |
Manman Ren | 945e828 | 2013-02-01 23:54:37 +0000 | [diff] [blame] | 1307 | IsStaticMember = true; |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | // If this is not a static data member definition, create the variable |
| 1311 | // DIE and add the initial set of attributes to it. |
| 1312 | if (!VariableDIE) { |
| 1313 | VariableDIE = new DIE(GV.getTag()); |
| 1314 | // Add to map. |
| 1315 | insertDIE(N, VariableDIE); |
| 1316 | |
| 1317 | // Add name and type. |
| 1318 | addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName()); |
| 1319 | addType(VariableDIE, GTy); |
| 1320 | |
| 1321 | // Add scoping info. |
Krzysztof Parzyszek | c5ef7ee | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1322 | if (!GV.isLocalToUnit()) { |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1323 | addFlag(VariableDIE, dwarf::DW_AT_external); |
Krzysztof Parzyszek | c5ef7ee | 2013-02-12 18:00:14 +0000 | [diff] [blame] | 1324 | addGlobalName(GV.getName(), VariableDIE); |
| 1325 | } |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1326 | |
| 1327 | // Add line number info. |
| 1328 | addSourceLine(VariableDIE, GV); |
| 1329 | // Add to context owner. |
| 1330 | addToContextOwner(VariableDIE, GVContext); |
| 1331 | } |
| 1332 | |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1333 | // Add location. |
Eric Christopher | 09ac3d8 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1334 | bool addToAccelTable = false; |
Eric Christopher | d61c34b | 2011-11-11 03:16:32 +0000 | [diff] [blame] | 1335 | DIE *VariableSpecDIE = NULL; |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1336 | bool isGlobalVariable = GV.getGlobal() != NULL; |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1337 | if (isGlobalVariable) { |
Eric Christopher | 09ac3d8 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1338 | addToAccelTable = true; |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1339 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
Eric Christopher | 0969ddf | 2013-01-18 22:11:33 +0000 | [diff] [blame] | 1340 | addOpAddress(Block, Asm->Mang->getSymbol(GV.getGlobal())); |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1341 | // Do not create specification DIE if context is either compile unit |
| 1342 | // or a subprogram. |
Devang Patel | 1dd4e56 | 2011-09-21 23:41:11 +0000 | [diff] [blame] | 1343 | if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() && |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1344 | !GVContext.isFile() && !isSubprogramContext(GVContext)) { |
| 1345 | // Create specification DIE. |
Eric Christopher | d117fbb | 2011-11-11 01:55:22 +0000 | [diff] [blame] | 1346 | VariableSpecDIE = new DIE(dwarf::DW_TAG_variable); |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1347 | addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, |
| 1348 | dwarf::DW_FORM_ref4, VariableDIE); |
| 1349 | addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block); |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1350 | // A static member's declaration is already flagged as such. |
| 1351 | if (!SDMDecl.Verify()) |
| 1352 | addFlag(VariableDIE, dwarf::DW_AT_declaration); |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1353 | addDie(VariableSpecDIE); |
| 1354 | } else { |
| 1355 | addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block); |
Eric Christopher | 09ac3d8 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1356 | } |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1357 | // Add linkage name. |
| 1358 | StringRef LinkageName = GV.getLinkageName(); |
Eric Christopher | a8ada25 | 2013-02-27 23:49:50 +0000 | [diff] [blame] | 1359 | if (!LinkageName.empty()) { |
Eric Christopher | 8d45a98 | 2013-02-27 23:49:47 +0000 | [diff] [blame] | 1360 | // From DWARF4: DIEs to which DW_AT_linkage_name may apply include: |
| 1361 | // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and |
| 1362 | // TAG_variable. |
Manman Ren | 21a08a1 | 2013-02-27 23:21:02 +0000 | [diff] [blame] | 1363 | addString(IsStaticMember && VariableSpecDIE ? |
| 1364 | VariableSpecDIE : VariableDIE, dwarf::DW_AT_MIPS_linkage_name, |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1365 | getRealLinkageName(LinkageName)); |
Eric Christopher | 8d45a98 | 2013-02-27 23:49:47 +0000 | [diff] [blame] | 1366 | // In compatibility mode with older gdbs we put the linkage name on both |
| 1367 | // the TAG_variable DIE and on the TAG_member DIE. |
Manman Ren | 21a08a1 | 2013-02-27 23:21:02 +0000 | [diff] [blame] | 1368 | if (IsStaticMember && VariableSpecDIE && DD->useDarwinGDBCompat()) |
| 1369 | addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name, |
Manman Ren | 06df83c | 2013-02-27 00:02:32 +0000 | [diff] [blame] | 1370 | getRealLinkageName(LinkageName)); |
| 1371 | } |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1372 | } else if (const ConstantInt *CI = |
Manman Ren | 945e828 | 2013-02-01 23:54:37 +0000 | [diff] [blame] | 1373 | dyn_cast_or_null<ConstantInt>(GV.getConstant())) { |
Adrian Prantl | 2e892e4 | 2013-04-04 22:56:49 +0000 | [diff] [blame] | 1374 | // AT_const_value was added when the static member was created. To avoid |
Manman Ren | 945e828 | 2013-02-01 23:54:37 +0000 | [diff] [blame] | 1375 | // emitting AT_const_value multiple times, we only add AT_const_value when |
| 1376 | // it is not a static member. |
| 1377 | if (!IsStaticMember) |
| 1378 | addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType()); |
| 1379 | } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) { |
Eric Christopher | 09ac3d8 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1380 | addToAccelTable = true; |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1381 | // GV is a merged global. |
| 1382 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
| 1383 | Value *Ptr = CE->getOperand(0); |
Eric Christopher | 0969ddf | 2013-01-18 22:11:33 +0000 | [diff] [blame] | 1384 | addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr))); |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1385 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); |
| 1386 | SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end()); |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1387 | addUInt(Block, 0, dwarf::DW_FORM_udata, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1388 | Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1389 | addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); |
| 1390 | addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block); |
| 1391 | } |
| 1392 | |
Eric Christopher | d117fbb | 2011-11-11 01:55:22 +0000 | [diff] [blame] | 1393 | if (addToAccelTable) { |
| 1394 | DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE; |
| 1395 | addAccelName(GV.getName(), AddrDIE); |
Eric Christopher | 09ac3d8 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1396 | |
Eric Christopher | d117fbb | 2011-11-11 01:55:22 +0000 | [diff] [blame] | 1397 | // If the linkage name is different than the name, go ahead and output |
| 1398 | // that as well into the name table. |
| 1399 | if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName()) |
| 1400 | addAccelName(GV.getLinkageName(), AddrDIE); |
| 1401 | } |
Eric Christopher | 74d8a87 | 2011-11-08 21:56:23 +0000 | [diff] [blame] | 1402 | |
Devang Patel | 6f9d8ff | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1403 | return; |
| 1404 | } |
| 1405 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1406 | /// constructSubrangeDIE - Construct subrange DIE from DISubrange. |
Eric Christopher | 4d069bf | 2012-05-22 18:45:24 +0000 | [diff] [blame] | 1407 | void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, |
| 1408 | DIE *IndexTy) { |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1409 | DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type); |
| 1410 | addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1411 | |
Bill Wendling | 222c2fd | 2012-12-06 07:38:10 +0000 | [diff] [blame] | 1412 | // The LowerBound value defines the lower bounds which is typically zero for |
| 1413 | // C/C++. The Count value is the number of elements. Values are 64 bit. If |
| 1414 | // Count == -1 then the array is unbounded and we do not emit |
| 1415 | // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and |
| 1416 | // Count == 0, then the array has zero elements in which case we do not emit |
| 1417 | // an upper bound. |
| 1418 | int64_t LowerBound = SR.getLo(); |
Bill Wendling | 6afe478 | 2012-12-06 07:55:19 +0000 | [diff] [blame] | 1419 | int64_t DefaultLowerBound = getDefaultLowerBound(); |
Bill Wendling | 9493dae | 2012-12-04 21:34:03 +0000 | [diff] [blame] | 1420 | int64_t Count = SR.getCount(); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1421 | |
Bill Wendling | 6afe478 | 2012-12-06 07:55:19 +0000 | [diff] [blame] | 1422 | if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound) |
Bill Wendling | 222c2fd | 2012-12-06 07:38:10 +0000 | [diff] [blame] | 1423 | addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound); |
| 1424 | |
| 1425 | if (Count != -1 && Count != 0) |
Bill Wendling | 9493dae | 2012-12-04 21:34:03 +0000 | [diff] [blame] | 1426 | // FIXME: An unbounded array should reference the expression that defines |
| 1427 | // the array. |
Bill Wendling | 222c2fd | 2012-12-06 07:38:10 +0000 | [diff] [blame] | 1428 | addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1); |
Bill Wendling | 9493dae | 2012-12-04 21:34:03 +0000 | [diff] [blame] | 1429 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1430 | Buffer.addChild(DW_Subrange); |
| 1431 | } |
| 1432 | |
| 1433 | /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. |
| 1434 | void CompileUnit::constructArrayTypeDIE(DIE &Buffer, |
| 1435 | DICompositeType *CTy) { |
| 1436 | Buffer.setTag(dwarf::DW_TAG_array_type); |
Eric Christopher | 9a1e0e2 | 2013-01-08 01:53:52 +0000 | [diff] [blame] | 1437 | if (CTy->isVector()) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1438 | addFlag(&Buffer, dwarf::DW_AT_GNU_vector); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1439 | |
| 1440 | // Emit derived type. |
| 1441 | addType(&Buffer, CTy->getTypeDerivedFrom()); |
| 1442 | DIArray Elements = CTy->getTypeArray(); |
| 1443 | |
| 1444 | // Get an anonymous type for index type. |
Eric Christopher | 8cab6ed | 2013-01-04 21:51:53 +0000 | [diff] [blame] | 1445 | // FIXME: This type should be passed down from the front end |
| 1446 | // as different languages may have different sizes for indexes. |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1447 | DIE *IdxTy = getIndexTyDie(); |
| 1448 | if (!IdxTy) { |
| 1449 | // Construct an anonymous type for index type. |
| 1450 | IdxTy = new DIE(dwarf::DW_TAG_base_type); |
Eric Christopher | 8cab6ed | 2013-01-04 21:51:53 +0000 | [diff] [blame] | 1451 | addString(IdxTy, dwarf::DW_AT_name, "int"); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1452 | addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t)); |
| 1453 | addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, |
| 1454 | dwarf::DW_ATE_signed); |
| 1455 | addDie(IdxTy); |
| 1456 | setIndexTyDie(IdxTy); |
| 1457 | } |
| 1458 | |
| 1459 | // Add subranges to array type. |
| 1460 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
| 1461 | DIDescriptor Element = Elements.getElement(i); |
| 1462 | if (Element.getTag() == dwarf::DW_TAG_subrange_type) |
| 1463 | constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy); |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator. |
| 1468 | DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) { |
| 1469 | DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator); |
| 1470 | StringRef Name = ETy.getName(); |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1471 | addString(Enumerator, dwarf::DW_AT_name, Name); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1472 | int64_t Value = ETy.getEnumValue(); |
| 1473 | addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value); |
| 1474 | return Enumerator; |
| 1475 | } |
| 1476 | |
Devang Patel | dbc64af | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1477 | /// constructContainingTypeDIEs - Construct DIEs for types that contain |
| 1478 | /// vtables. |
| 1479 | void CompileUnit::constructContainingTypeDIEs() { |
| 1480 | for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(), |
| 1481 | CE = ContainingTypeMap.end(); CI != CE; ++CI) { |
| 1482 | DIE *SPDie = CI->first; |
| 1483 | const MDNode *N = CI->second; |
| 1484 | if (!N) continue; |
| 1485 | DIE *NDie = getDIE(N); |
| 1486 | if (!NDie) continue; |
| 1487 | addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie); |
| 1488 | } |
| 1489 | } |
| 1490 | |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1491 | /// constructVariableDIE - Construct a DIE for the given DbgVariable. |
| 1492 | DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) { |
| 1493 | StringRef Name = DV->getName(); |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1494 | |
| 1495 | // Translate tag to proper Dwarf tag. |
| 1496 | unsigned Tag = DV->getTag(); |
| 1497 | |
| 1498 | // Define variable debug information entry. |
| 1499 | DIE *VariableDie = new DIE(Tag); |
| 1500 | DbgVariable *AbsVar = DV->getAbstractVariable(); |
| 1501 | DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL; |
| 1502 | if (AbsDIE) |
| 1503 | addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, |
| 1504 | dwarf::DW_FORM_ref4, AbsDIE); |
| 1505 | else { |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1506 | addString(VariableDie, dwarf::DW_AT_name, Name); |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1507 | addSourceLine(VariableDie, DV->getVariable()); |
| 1508 | addType(VariableDie, DV->getType()); |
| 1509 | } |
| 1510 | |
| 1511 | if (DV->isArtificial()) |
Eric Christopher | 873cf0a | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1512 | addFlag(VariableDie, dwarf::DW_AT_artificial); |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1513 | |
| 1514 | if (isScopeAbstract) { |
| 1515 | DV->setDIE(VariableDie); |
| 1516 | return VariableDie; |
| 1517 | } |
| 1518 | |
| 1519 | // Add variable address. |
| 1520 | |
| 1521 | unsigned Offset = DV->getDotDebugLocOffset(); |
| 1522 | if (Offset != ~0U) { |
| 1523 | addLabel(VariableDie, dwarf::DW_AT_location, |
| 1524 | dwarf::DW_FORM_data4, |
| 1525 | Asm->GetTempSymbol("debug_loc", Offset)); |
| 1526 | DV->setDIE(VariableDie); |
| 1527 | return VariableDie; |
| 1528 | } |
| 1529 | |
Eric Christopher | 8cf5e74 | 2011-10-03 15:49:20 +0000 | [diff] [blame] | 1530 | // Check if variable is described by a DBG_VALUE instruction. |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1531 | if (const MachineInstr *DVInsn = DV->getMInsn()) { |
| 1532 | bool updated = false; |
| 1533 | if (DVInsn->getNumOperands() == 3) { |
| 1534 | if (DVInsn->getOperand(0).isReg()) { |
| 1535 | const MachineOperand RegOp = DVInsn->getOperand(0); |
| 1536 | const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo(); |
| 1537 | if (DVInsn->getOperand(1).isImm() && |
| 1538 | TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) { |
| 1539 | unsigned FrameReg = 0; |
| 1540 | const TargetFrameLowering *TFI = Asm->TM.getFrameLowering(); |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1541 | int Offset = |
| 1542 | TFI->getFrameIndexReference(*Asm->MF, |
| 1543 | DVInsn->getOperand(1).getImm(), |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1544 | FrameReg); |
| 1545 | MachineLocation Location(FrameReg, Offset); |
| 1546 | addVariableAddress(DV, VariableDie, Location); |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1547 | |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1548 | } else if (RegOp.getReg()) |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1549 | addVariableAddress(DV, VariableDie, |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1550 | MachineLocation(RegOp.getReg())); |
| 1551 | updated = true; |
| 1552 | } |
| 1553 | else if (DVInsn->getOperand(0).isImm()) |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1554 | updated = |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1555 | addConstantValue(VariableDie, DVInsn->getOperand(0), |
| 1556 | DV->getType()); |
| 1557 | else if (DVInsn->getOperand(0).isFPImm()) |
| 1558 | updated = |
| 1559 | addConstantFPValue(VariableDie, DVInsn->getOperand(0)); |
| 1560 | else if (DVInsn->getOperand(0).isCImm()) |
| 1561 | updated = |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1562 | addConstantValue(VariableDie, |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1563 | DVInsn->getOperand(0).getCImm(), |
| 1564 | DV->getType().isUnsignedDIType()); |
| 1565 | } else { |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1566 | addVariableAddress(DV, VariableDie, |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1567 | Asm->getDebugValueLocation(DVInsn)); |
| 1568 | updated = true; |
| 1569 | } |
| 1570 | if (!updated) { |
| 1571 | // If variableDie is not updated then DBG_VALUE instruction does not |
| 1572 | // have valid variable info. |
| 1573 | delete VariableDie; |
| 1574 | return NULL; |
| 1575 | } |
| 1576 | DV->setDIE(VariableDie); |
| 1577 | return VariableDie; |
| 1578 | } else { |
| 1579 | // .. else use frame index. |
| 1580 | int FI = DV->getFrameIndex(); |
| 1581 | if (FI != ~0) { |
| 1582 | unsigned FrameReg = 0; |
| 1583 | const TargetFrameLowering *TFI = Asm->TM.getFrameLowering(); |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1584 | int Offset = |
Devang Patel | d0b5a5e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1585 | TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); |
| 1586 | MachineLocation Location(FrameReg, Offset); |
| 1587 | addVariableAddress(DV, VariableDie, Location); |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | DV->setDIE(VariableDie); |
| 1592 | return VariableDie; |
| 1593 | } |
| 1594 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1595 | /// createMemberDIE - Create new member DIE. |
| 1596 | DIE *CompileUnit::createMemberDIE(DIDerivedType DT) { |
| 1597 | DIE *MemberDie = new DIE(DT.getTag()); |
| 1598 | StringRef Name = DT.getName(); |
| 1599 | if (!Name.empty()) |
Nick Lewycky | 390c40d | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1600 | addString(MemberDie, dwarf::DW_AT_name, Name); |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1601 | |
| 1602 | addType(MemberDie, DT.getTypeDerivedFrom()); |
| 1603 | |
| 1604 | addSourceLine(MemberDie, DT); |
| 1605 | |
| 1606 | DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock(); |
| 1607 | addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 1608 | |
| 1609 | uint64_t Size = DT.getSizeInBits(); |
| 1610 | uint64_t FieldSize = DT.getOriginalTypeSize(); |
| 1611 | |
| 1612 | if (Size != FieldSize) { |
| 1613 | // Handle bitfield. |
| 1614 | addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3); |
| 1615 | addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits()); |
| 1616 | |
| 1617 | uint64_t Offset = DT.getOffsetInBits(); |
| 1618 | uint64_t AlignMask = ~(DT.getAlignInBits() - 1); |
| 1619 | uint64_t HiMark = (Offset + FieldSize) & AlignMask; |
| 1620 | uint64_t FieldOffset = (HiMark - FieldSize); |
| 1621 | Offset -= FieldOffset; |
| 1622 | |
| 1623 | // Maybe we need to work from the other end. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1624 | if (Asm->getDataLayout().isLittleEndian()) |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1625 | Offset = FieldSize - (Offset + Size); |
| 1626 | addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset); |
| 1627 | |
| 1628 | // Here WD_AT_data_member_location points to the anonymous |
| 1629 | // field that includes this bit field. |
| 1630 | addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3); |
| 1631 | |
| 1632 | } else |
| 1633 | // This is not a bitfield. |
| 1634 | addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3); |
| 1635 | |
| 1636 | if (DT.getTag() == dwarf::DW_TAG_inheritance |
| 1637 | && DT.isVirtual()) { |
| 1638 | |
| 1639 | // For C++, virtual base classes are not at fixed offset. Use following |
| 1640 | // expression to extract appropriate offset from vtable. |
| 1641 | // BaseAddr = ObAddr + *((*ObAddr) - Offset) |
| 1642 | |
| 1643 | DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock(); |
| 1644 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); |
| 1645 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
| 1646 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); |
| 1647 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits()); |
| 1648 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); |
| 1649 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
| 1650 | addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); |
| 1651 | |
| 1652 | addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, |
| 1653 | VBaseLocationDie); |
| 1654 | } else |
| 1655 | addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie); |
| 1656 | |
| 1657 | if (DT.isProtected()) |
Nick Lewycky | 13aaca5 | 2011-12-13 05:09:11 +0000 | [diff] [blame] | 1658 | addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1659 | dwarf::DW_ACCESS_protected); |
| 1660 | else if (DT.isPrivate()) |
Nick Lewycky | 13aaca5 | 2011-12-13 05:09:11 +0000 | [diff] [blame] | 1661 | addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1662 | dwarf::DW_ACCESS_private); |
| 1663 | // Otherwise C++ member and base classes are considered public. |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1664 | else |
Nick Lewycky | 13aaca5 | 2011-12-13 05:09:11 +0000 | [diff] [blame] | 1665 | addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1666 | dwarf::DW_ACCESS_public); |
| 1667 | if (DT.isVirtual()) |
Nick Lewycky | 798313d | 2011-12-14 00:56:07 +0000 | [diff] [blame] | 1668 | addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1669 | dwarf::DW_VIRTUALITY_virtual); |
Devang Patel | e9db5e2 | 2011-04-16 00:11:51 +0000 | [diff] [blame] | 1670 | |
| 1671 | // Objective-C properties. |
Devang Patel | 6588abf | 2012-02-06 17:49:43 +0000 | [diff] [blame] | 1672 | if (MDNode *PNode = DT.getObjCProperty()) |
| 1673 | if (DIEEntry *PropertyDie = getDIEEntry(PNode)) |
Eric Christopher | 8b4310b | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1674 | MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4, |
Devang Patel | 6588abf | 2012-02-06 17:49:43 +0000 | [diff] [blame] | 1675 | PropertyDie); |
| 1676 | |
David Blaikie | 01bc2b3 | 2012-12-13 22:43:07 +0000 | [diff] [blame] | 1677 | if (DT.isArtificial()) |
| 1678 | addFlag(MemberDie, dwarf::DW_AT_artificial); |
| 1679 | |
Devang Patel | 161b2f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1680 | return MemberDie; |
| 1681 | } |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1682 | |
| 1683 | /// createStaticMemberDIE - Create new DIE for C++ static member. |
| 1684 | DIE *CompileUnit::createStaticMemberDIE(const DIDerivedType DT) { |
| 1685 | if (!DT.Verify()) |
| 1686 | return NULL; |
| 1687 | |
| 1688 | DIE *StaticMemberDIE = new DIE(DT.getTag()); |
| 1689 | DIType Ty = DT.getTypeDerivedFrom(); |
| 1690 | |
| 1691 | addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName()); |
| 1692 | addType(StaticMemberDIE, Ty); |
| 1693 | addSourceLine(StaticMemberDIE, DT); |
| 1694 | addFlag(StaticMemberDIE, dwarf::DW_AT_external); |
| 1695 | addFlag(StaticMemberDIE, dwarf::DW_AT_declaration); |
| 1696 | |
| 1697 | // FIXME: We could omit private if the parent is a class_type, and |
| 1698 | // public if the parent is something else. |
| 1699 | if (DT.isProtected()) |
| 1700 | addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
| 1701 | dwarf::DW_ACCESS_protected); |
| 1702 | else if (DT.isPrivate()) |
| 1703 | addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
| 1704 | dwarf::DW_ACCESS_private); |
| 1705 | else |
| 1706 | addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
| 1707 | dwarf::DW_ACCESS_public); |
| 1708 | |
| 1709 | if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant())) |
| 1710 | addConstantValue(StaticMemberDIE, CI, Ty.isUnsignedDIType()); |
David Blaikie | 1426841 | 2013-01-20 01:18:01 +0000 | [diff] [blame] | 1711 | if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant())) |
| 1712 | addConstantFPValue(StaticMemberDIE, CFP); |
Eric Christopher | 6b6061f | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1713 | |
| 1714 | insertDIE(DT, StaticMemberDIE); |
| 1715 | return StaticMemberDIE; |
| 1716 | } |