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