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