David Blaikie | 319a05f | 2013-12-02 19:33:10 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===// |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 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 | 160522c | 2012-08-14 05:13:29 +0000 | [diff] [blame] | 10 | // This file contains support for constructing a dwarf compile unit. |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "dwarfdebug" |
| 15 | |
David Blaikie | 2c86a72 | 2013-12-02 19:33:15 +0000 | [diff] [blame] | 16 | #include "DwarfUnit.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "DwarfAccelTable.h" |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 18 | #include "DwarfDebug.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/APFloat.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 12664a0 | 2014-03-06 00:22:06 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DIBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
| 23 | #include "llvm/IR/GlobalVariable.h" |
| 24 | #include "llvm/IR/Instructions.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Mangler.h" |
Eric Christopher | 8458862 | 2013-12-28 01:39:17 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCAsmInfo.h" |
Adrian Prantl | cbcd578 | 2014-02-11 22:22:15 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCContext.h" |
David Blaikie | 6b288cf | 2013-10-30 20:42:41 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSection.h" |
| 29 | #include "llvm/MC/MCStreamer.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetFrameLowering.h" |
David Blaikie | f269497 | 2013-06-28 20:05:11 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetMachine.h" |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetRegisterInfo.h" |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace llvm; |
| 37 | |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 38 | static cl::opt<bool> |
| 39 | GenerateDwarfTypeUnits("generate-type-units", cl::Hidden, |
| 40 | cl::desc("Generate DWARF4 type units."), |
| 41 | cl::init(false)); |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 42 | |
David Blaikie | 2a80e44 | 2013-12-02 22:09:48 +0000 | [diff] [blame] | 43 | /// Unit - Unit constructor. |
David Blaikie | 7480ae6 | 2014-01-09 03:03:27 +0000 | [diff] [blame] | 44 | DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A, |
| 45 | DwarfDebug *DW, DwarfFile *DWU) |
David Blaikie | f645f96 | 2014-01-09 03:23:41 +0000 | [diff] [blame] | 46 | : UniqueID(UID), CUNode(Node), UnitDie(D), DebugInfoOffset(0), Asm(A), |
| 47 | DD(DW), DU(DWU), IndexTyDie(0), Section(0), Skeleton(0) { |
David Blaikie | 319a05f | 2013-12-02 19:33:10 +0000 | [diff] [blame] | 48 | DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1); |
| 49 | } |
| 50 | |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 51 | DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node, |
| 52 | AsmPrinter *A, DwarfDebug *DW, |
| 53 | DwarfFile *DWU) |
David Blaikie | 7480ae6 | 2014-01-09 03:03:27 +0000 | [diff] [blame] | 54 | : DwarfUnit(UID, D, Node, A, DW, DWU) { |
David Blaikie | 5a15240 | 2013-11-15 23:52:02 +0000 | [diff] [blame] | 55 | insertDIE(Node, D); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 56 | } |
| 57 | |
David Blaikie | 15632ae | 2014-02-12 00:31:30 +0000 | [diff] [blame] | 58 | DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU, |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 59 | AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU, |
David Blaikie | 8287aff | 2014-03-18 02:13:23 +0000 | [diff] [blame] | 60 | MCDwarfDwoLineTable *SplitLineTable) |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 61 | : DwarfUnit(UID, D, CU.getCUNode(), A, DW, DWU), CU(CU), |
| 62 | SplitLineTable(SplitLineTable) { |
| 63 | if (SplitLineTable) |
| 64 | addSectionOffset(UnitDie.get(), dwarf::DW_AT_stmt_list, 0); |
| 65 | } |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 66 | |
David Blaikie | 319a05f | 2013-12-02 19:33:10 +0000 | [diff] [blame] | 67 | /// ~Unit - Destructor for compile unit. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 68 | DwarfUnit::~DwarfUnit() { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 69 | for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j) |
| 70 | DIEBlocks[j]->~DIEBlock(); |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 71 | for (unsigned j = 0, M = DIELocs.size(); j < M; ++j) |
| 72 | DIELocs[j]->~DIELoc(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug |
| 76 | /// information entry. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 77 | DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 78 | DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry); |
| 79 | return Value; |
| 80 | } |
| 81 | |
Bill Wendling | 3495f9b | 2012-12-06 07:55:19 +0000 | [diff] [blame] | 82 | /// getDefaultLowerBound - Return the default lower bound for an array. If the |
Bill Wendling | 28fe9e7 | 2012-12-06 07:38:10 +0000 | [diff] [blame] | 83 | /// DWARF version doesn't handle the language, return -1. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 84 | int64_t DwarfUnit::getDefaultLowerBound() const { |
David Blaikie | cb8e435 | 2013-11-15 23:50:53 +0000 | [diff] [blame] | 85 | switch (getLanguage()) { |
Bill Wendling | 28fe9e7 | 2012-12-06 07:38:10 +0000 | [diff] [blame] | 86 | default: |
| 87 | break; |
| 88 | |
| 89 | case dwarf::DW_LANG_C89: |
| 90 | case dwarf::DW_LANG_C99: |
| 91 | case dwarf::DW_LANG_C: |
| 92 | case dwarf::DW_LANG_C_plus_plus: |
| 93 | case dwarf::DW_LANG_ObjC: |
| 94 | case dwarf::DW_LANG_ObjC_plus_plus: |
| 95 | return 0; |
| 96 | |
| 97 | case dwarf::DW_LANG_Fortran77: |
| 98 | case dwarf::DW_LANG_Fortran90: |
| 99 | case dwarf::DW_LANG_Fortran95: |
| 100 | return 1; |
| 101 | |
| 102 | // The languages below have valid values only if the DWARF version >= 4. |
| 103 | case dwarf::DW_LANG_Java: |
| 104 | case dwarf::DW_LANG_Python: |
| 105 | case dwarf::DW_LANG_UPC: |
| 106 | case dwarf::DW_LANG_D: |
| 107 | if (dwarf::DWARF_VERSION >= 4) |
| 108 | return 0; |
| 109 | break; |
| 110 | |
| 111 | case dwarf::DW_LANG_Ada83: |
| 112 | case dwarf::DW_LANG_Ada95: |
| 113 | case dwarf::DW_LANG_Cobol74: |
| 114 | case dwarf::DW_LANG_Cobol85: |
| 115 | case dwarf::DW_LANG_Modula2: |
| 116 | case dwarf::DW_LANG_Pascal83: |
| 117 | case dwarf::DW_LANG_PLI: |
| 118 | if (dwarf::DWARF_VERSION >= 4) |
| 119 | return 1; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | return -1; |
| 124 | } |
| 125 | |
Manman Ren | 4dbdc90 | 2013-10-31 17:54:35 +0000 | [diff] [blame] | 126 | /// Check whether the DIE for this MDNode can be shared across CUs. |
David Blaikie | 4201ddf | 2013-11-15 22:59:36 +0000 | [diff] [blame] | 127 | static bool isShareableAcrossCUs(DIDescriptor D) { |
David Blaikie | bcb418e | 2013-11-20 18:40:16 +0000 | [diff] [blame] | 128 | // When the MDNode can be part of the type system, the DIE can be shared |
| 129 | // across CUs. |
| 130 | // Combining type units and cross-CU DIE sharing is lower value (since |
| 131 | // cross-CU DIE sharing is used in LTO and removes type redundancy at that |
| 132 | // level already) but may be implementable for some value in projects |
| 133 | // building multiple independent libraries with LTO and then linking those |
| 134 | // together. |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 135 | return (D.isType() || |
| 136 | (D.isSubprogram() && !DISubprogram(D).isDefinition())) && |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 137 | !GenerateDwarfTypeUnits; |
Manman Ren | 4dbdc90 | 2013-10-31 17:54:35 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /// getDIE - Returns the debug information entry map slot for the |
| 141 | /// specified debug variable. We delegate the request to DwarfDebug |
| 142 | /// when the DIE for this MDNode can be shared across CUs. The mappings |
| 143 | /// will be kept in DwarfDebug for shareable DIEs. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 144 | DIE *DwarfUnit::getDIE(DIDescriptor D) const { |
David Blaikie | 2ad0016 | 2013-11-15 23:09:13 +0000 | [diff] [blame] | 145 | if (isShareableAcrossCUs(D)) |
| 146 | return DD->getDIE(D); |
| 147 | return MDNodeToDieMap.lookup(D); |
Manman Ren | 4dbdc90 | 2013-10-31 17:54:35 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug |
| 151 | /// when the DIE for this MDNode can be shared across CUs. The mappings |
| 152 | /// will be kept in DwarfDebug for shareable DIEs. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 153 | void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) { |
David Blaikie | 2ad0016 | 2013-11-15 23:09:13 +0000 | [diff] [blame] | 154 | if (isShareableAcrossCUs(Desc)) { |
| 155 | DD->insertDIE(Desc, D); |
Manman Ren | 4dbdc90 | 2013-10-31 17:54:35 +0000 | [diff] [blame] | 156 | return; |
| 157 | } |
David Blaikie | 2ad0016 | 2013-11-15 23:09:13 +0000 | [diff] [blame] | 158 | MDNodeToDieMap.insert(std::make_pair(Desc, D)); |
Manman Ren | 4dbdc90 | 2013-10-31 17:54:35 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 161 | /// addFlag - Add a flag that is true. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 162 | void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) { |
Michael Gottesman | c89466f | 2013-09-04 04:39:38 +0000 | [diff] [blame] | 163 | if (DD->getDwarfVersion() >= 4) |
Eric Christopher | dccd328 | 2013-10-04 22:40:05 +0000 | [diff] [blame] | 164 | Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne); |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 165 | else |
Eric Christopher | dccd328 | 2013-10-04 22:40:05 +0000 | [diff] [blame] | 166 | Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne); |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 169 | /// addUInt - Add an unsigned integer attribute data and value. |
| 170 | /// |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 171 | void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute, |
| 172 | Optional<dwarf::Form> Form, uint64_t Integer) { |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 173 | if (!Form) |
| 174 | Form = DIEInteger::BestForm(false, Integer); |
| 175 | DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator) |
| 176 | DIEInteger(Integer); |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 177 | Die->addValue(Attribute, *Form, Value); |
| 178 | } |
| 179 | |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 180 | void DwarfUnit::addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer) { |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 181 | addUInt(Block, (dwarf::Attribute)0, Form, Integer); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /// addSInt - Add an signed integer attribute data and value. |
| 185 | /// |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 186 | void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute, |
| 187 | Optional<dwarf::Form> Form, int64_t Integer) { |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 188 | if (!Form) |
| 189 | Form = DIEInteger::BestForm(true, Integer); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 190 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer); |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 191 | Die->addValue(Attribute, *Form, Value); |
| 192 | } |
| 193 | |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 194 | void DwarfUnit::addSInt(DIELoc *Die, Optional<dwarf::Form> Form, |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 195 | int64_t Integer) { |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 196 | addSInt(Die, (dwarf::Attribute)0, Form, Integer); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Nick Lewycky | cc64ae1 | 2011-10-28 05:29:47 +0000 | [diff] [blame] | 199 | /// addString - Add a string attribute data and value. We always emit a |
| 200 | /// reference to the string pool instead of immediate strings so that DIEs have |
Eric Christopher | fba2260 | 2013-01-07 19:32:45 +0000 | [diff] [blame] | 201 | /// more predictable sizes. In the case of split dwarf we emit an index |
| 202 | /// into another table which gets us the static offset into the string |
| 203 | /// table. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 204 | void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute, |
| 205 | StringRef String) { |
Eric Christopher | 05893f4 | 2013-12-30 18:32:31 +0000 | [diff] [blame] | 206 | |
| 207 | if (!DD->useSplitDwarf()) |
| 208 | return addLocalString(Die, Attribute, String); |
| 209 | |
| 210 | unsigned idx = DU->getStringPoolIndex(String); |
| 211 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); |
Eric Christopher | 6764643 | 2013-07-26 17:02:41 +0000 | [diff] [blame] | 212 | DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String); |
Eric Christopher | 05893f4 | 2013-12-30 18:32:31 +0000 | [diff] [blame] | 213 | Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str); |
Eric Christopher | 2cbd576 | 2013-01-07 19:32:41 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /// addLocalString - Add a string attribute data and value. This is guaranteed |
| 217 | /// to be in the local string pool instead of indirected. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 218 | void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute, |
| 219 | StringRef String) { |
Eric Christopher | e698f53 | 2012-12-20 21:58:36 +0000 | [diff] [blame] | 220 | MCSymbol *Symb = DU->getStringPoolEntry(String); |
Nick Lewycky | cc64ae1 | 2011-10-28 05:29:47 +0000 | [diff] [blame] | 221 | DIEValue *Value; |
Eric Christopher | 8458862 | 2013-12-28 01:39:17 +0000 | [diff] [blame] | 222 | if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) |
Ulrich Weigand | 396ba8b | 2013-07-02 18:46:26 +0000 | [diff] [blame] | 223 | Value = new (DIEValueAllocator) DIELabel(Symb); |
Nick Lewycky | cc64ae1 | 2011-10-28 05:29:47 +0000 | [diff] [blame] | 224 | else { |
Eric Christopher | e698f53 | 2012-12-20 21:58:36 +0000 | [diff] [blame] | 225 | MCSymbol *StringPool = DU->getStringPoolSym(); |
Nick Lewycky | cc64ae1 | 2011-10-28 05:29:47 +0000 | [diff] [blame] | 226 | Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool); |
Nick Lewycky | d59c0ca | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 227 | } |
Eric Christopher | 05893f4 | 2013-12-30 18:32:31 +0000 | [diff] [blame] | 228 | DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String); |
| 229 | Die->addValue(Attribute, dwarf::DW_FORM_strp, Str); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Ulrich Weigand | 396ba8b | 2013-07-02 18:46:26 +0000 | [diff] [blame] | 232 | /// addExpr - Add a Dwarf expression attribute data and value. |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 233 | /// |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 234 | void DwarfUnit::addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr) { |
Ulrich Weigand | 396ba8b | 2013-07-02 18:46:26 +0000 | [diff] [blame] | 235 | DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr); |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 236 | Die->addValue((dwarf::Attribute)0, Form, Value); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Eric Christopher | a27220f | 2014-03-05 22:41:20 +0000 | [diff] [blame] | 239 | /// addLocationList - Add a Dwarf loclistptr attribute data and value. |
| 240 | /// |
| 241 | void DwarfUnit::addLocationList(DIE *Die, dwarf::Attribute Attribute, |
| 242 | unsigned Index) { |
| 243 | DIEValue *Value = new (DIEValueAllocator) DIELocList(Index); |
| 244 | dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset |
| 245 | : dwarf::DW_FORM_data4; |
| 246 | Die->addValue(Attribute, Form, Value); |
| 247 | } |
| 248 | |
Ulrich Weigand | 396ba8b | 2013-07-02 18:46:26 +0000 | [diff] [blame] | 249 | /// addLabel - Add a Dwarf label attribute data and value. |
| 250 | /// |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 251 | void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form, |
| 252 | const MCSymbol *Label) { |
Ulrich Weigand | 396ba8b | 2013-07-02 18:46:26 +0000 | [diff] [blame] | 253 | DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); |
| 254 | Die->addValue(Attribute, Form, Value); |
David Blaikie | f3cd7c5 | 2013-06-28 20:05:04 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 257 | void DwarfUnit::addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label) { |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 258 | addLabel(Die, (dwarf::Attribute)0, Form, Label); |
| 259 | } |
| 260 | |
Eric Christopher | 33ff697 | 2013-11-21 23:46:41 +0000 | [diff] [blame] | 261 | /// addSectionLabel - Add a Dwarf section label attribute data and value. |
| 262 | /// |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 263 | void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute, |
| 264 | const MCSymbol *Label) { |
Eric Christopher | 33ff697 | 2013-11-21 23:46:41 +0000 | [diff] [blame] | 265 | if (DD->getDwarfVersion() >= 4) |
| 266 | addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label); |
| 267 | else |
| 268 | addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label); |
| 269 | } |
| 270 | |
| 271 | /// addSectionOffset - Add an offset into a section attribute data and value. |
| 272 | /// |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 273 | void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute, |
| 274 | uint64_t Integer) { |
Eric Christopher | 33ff697 | 2013-11-21 23:46:41 +0000 | [diff] [blame] | 275 | if (DD->getDwarfVersion() >= 4) |
| 276 | addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer); |
| 277 | else |
| 278 | addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer); |
| 279 | } |
| 280 | |
Eric Christopher | 962c908 | 2013-01-15 23:56:56 +0000 | [diff] [blame] | 281 | /// addLabelAddress - Add a dwarf label attribute data and value using |
| 282 | /// DW_FORM_addr or DW_FORM_GNU_addr_index. |
| 283 | /// |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 284 | void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute, |
| 285 | MCSymbol *Label) { |
Alexey Samsonov | 4436bf0 | 2013-10-03 08:54:43 +0000 | [diff] [blame] | 286 | if (Label) |
| 287 | DD->addArangeLabel(SymbolCU(this, Label)); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 288 | |
Eric Christopher | cf48ade | 2014-01-24 11:52:53 +0000 | [diff] [blame] | 289 | if (!DD->useSplitDwarf()) { |
| 290 | if (Label) { |
| 291 | DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); |
| 292 | Die->addValue(Attribute, dwarf::DW_FORM_addr, Value); |
| 293 | } else { |
| 294 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(0); |
| 295 | Die->addValue(Attribute, dwarf::DW_FORM_addr, Value); |
| 296 | } |
Eric Christopher | 962c908 | 2013-01-15 23:56:56 +0000 | [diff] [blame] | 297 | } else { |
Eric Christopher | cf48ade | 2014-01-24 11:52:53 +0000 | [diff] [blame] | 298 | unsigned idx = DU->getAddrPoolIndex(Label); |
| 299 | DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); |
| 300 | Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value); |
Eric Christopher | 962c908 | 2013-01-15 23:56:56 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
David Blaikie | 0e8d401 | 2014-03-17 23:53:25 +0000 | [diff] [blame] | 304 | unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) { |
| 305 | // If we print assembly, we can't separate .file entries according to |
| 306 | // compile units. Thus all files will belong to the default compile unit. |
| 307 | |
| 308 | // FIXME: add a better feature test than hasRawTextSupport. Even better, |
| 309 | // extend .file to support this. |
| 310 | return Asm->OutStreamer.EmitDwarfFileDirective( |
| 311 | 0, DirName, FileName, |
| 312 | Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID()); |
| 313 | } |
| 314 | |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 315 | unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) { |
| 316 | return SplitLineTable ? SplitLineTable->getFile(DirName, FileName) |
| 317 | : getCU().getOrCreateSourceID(FileName, DirName); |
| 318 | } |
| 319 | |
Eric Christopher | e9ec245 | 2013-01-18 22:11:33 +0000 | [diff] [blame] | 320 | /// addOpAddress - Add a dwarf op address data and value using the |
| 321 | /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index. |
| 322 | /// |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 323 | void DwarfUnit::addOpAddress(DIELoc *Die, const MCSymbol *Sym) { |
Eric Christopher | e9ec245 | 2013-01-18 22:11:33 +0000 | [diff] [blame] | 324 | if (!DD->useSplitDwarf()) { |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 325 | addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); |
| 326 | addLabel(Die, dwarf::DW_FORM_udata, Sym); |
Eric Christopher | e9ec245 | 2013-01-18 22:11:33 +0000 | [diff] [blame] | 327 | } else { |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 328 | addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index); |
| 329 | addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym)); |
Eric Christopher | e9ec245 | 2013-01-18 22:11:33 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Eric Christopher | 33ff697 | 2013-11-21 23:46:41 +0000 | [diff] [blame] | 333 | /// addSectionDelta - Add a section label delta attribute data and value. |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 334 | /// |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 335 | void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute, |
| 336 | const MCSymbol *Hi, const MCSymbol *Lo) { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 337 | DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); |
Eric Christopher | 33ff697 | 2013-11-21 23:46:41 +0000 | [diff] [blame] | 338 | if (DD->getDwarfVersion() >= 4) |
| 339 | Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value); |
| 340 | else |
| 341 | Die->addValue(Attribute, dwarf::DW_FORM_data4, Value); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 342 | } |
| 343 | |
David Blaikie | 48b1bdc | 2014-03-07 01:30:55 +0000 | [diff] [blame] | 344 | void DwarfUnit::addLabelDelta(DIE *Die, dwarf::Attribute Attribute, |
| 345 | const MCSymbol *Hi, const MCSymbol *Lo) { |
| 346 | DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); |
| 347 | Die->addValue(Attribute, dwarf::DW_FORM_data4, Value); |
| 348 | } |
| 349 | |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 350 | /// addDIEEntry - Add a DIE attribute data and value. |
| 351 | /// |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 352 | void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) { |
Manman Ren | 4dbdc90 | 2013-10-31 17:54:35 +0000 | [diff] [blame] | 353 | addDIEEntry(Die, Attribute, createDIEEntry(Entry)); |
| 354 | } |
| 355 | |
David Blaikie | 47f615e | 2013-12-17 23:32:35 +0000 | [diff] [blame] | 356 | void DwarfUnit::addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type) { |
| 357 | Die->addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8, |
| 358 | new (DIEValueAllocator) DIETypeSignature(Type)); |
| 359 | } |
| 360 | |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 361 | void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, |
| 362 | DIEEntry *Entry) { |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 363 | const DIE *DieCU = Die->getUnitOrNull(); |
| 364 | const DIE *EntryCU = Entry->getEntry()->getUnitOrNull(); |
Manman Ren | 4dbdc90 | 2013-10-31 17:54:35 +0000 | [diff] [blame] | 365 | if (!DieCU) |
| 366 | // We assume that Die belongs to this CU, if it is not linked to any CU yet. |
David Blaikie | 2a80e44 | 2013-12-02 22:09:48 +0000 | [diff] [blame] | 367 | DieCU = getUnitDie(); |
Manman Ren | 4dbdc90 | 2013-10-31 17:54:35 +0000 | [diff] [blame] | 368 | if (!EntryCU) |
David Blaikie | 2a80e44 | 2013-12-02 22:09:48 +0000 | [diff] [blame] | 369 | EntryCU = getUnitDie(); |
Manman Ren | 4dbdc90 | 2013-10-31 17:54:35 +0000 | [diff] [blame] | 370 | Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4 |
| 371 | : dwarf::DW_FORM_ref_addr, |
| 372 | Entry); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Manman Ren | b987e51 | 2013-10-29 00:53:03 +0000 | [diff] [blame] | 375 | /// Create a DIE with the given Tag, add the DIE to its parent, and |
| 376 | /// call insertDIE if MD is not null. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 377 | DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) { |
Manman Ren | b987e51 | 2013-10-29 00:53:03 +0000 | [diff] [blame] | 378 | DIE *Die = new DIE(Tag); |
| 379 | Parent.addChild(Die); |
David Blaikie | 52c5020 | 2013-11-16 00:29:01 +0000 | [diff] [blame] | 380 | if (N) |
| 381 | insertDIE(N, Die); |
Manman Ren | b987e51 | 2013-10-29 00:53:03 +0000 | [diff] [blame] | 382 | return Die; |
| 383 | } |
| 384 | |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 385 | /// addBlock - Add block data. |
| 386 | /// |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 387 | void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Loc) { |
Eric Christopher | 8bdab43 | 2014-02-27 18:36:10 +0000 | [diff] [blame] | 388 | Loc->ComputeSize(Asm); |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 389 | DIELocs.push_back(Loc); // Memoize so we can call the destructor later on. |
| 390 | Die->addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc); |
| 391 | } |
| 392 | |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 393 | void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute, |
| 394 | DIEBlock *Block) { |
Eric Christopher | 8bdab43 | 2014-02-27 18:36:10 +0000 | [diff] [blame] | 395 | Block->ComputeSize(Asm); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 396 | DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on. |
| 397 | Die->addValue(Attribute, Block->BestForm(), Block); |
| 398 | } |
| 399 | |
| 400 | /// addSourceLine - Add location information to specified debug information |
| 401 | /// entry. |
David Blaikie | 101613e | 2014-02-12 00:11:25 +0000 | [diff] [blame] | 402 | void DwarfUnit::addSourceLine(DIE *Die, unsigned Line, StringRef File, |
| 403 | StringRef Directory) { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 404 | if (Line == 0) |
| 405 | return; |
David Blaikie | 101613e | 2014-02-12 00:11:25 +0000 | [diff] [blame] | 406 | |
David Blaikie | 4a2f95f | 2014-03-18 01:17:26 +0000 | [diff] [blame] | 407 | unsigned FileID = getOrCreateSourceID(File, Directory); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 408 | assert(FileID && "Invalid file id"); |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 409 | addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); |
| 410 | addUInt(Die, dwarf::DW_AT_decl_line, None, Line); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /// addSourceLine - Add location information to specified debug information |
| 414 | /// entry. |
David Blaikie | 101613e | 2014-02-12 00:11:25 +0000 | [diff] [blame] | 415 | void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) { |
| 416 | assert(V.isVariable()); |
| 417 | |
Eric Christopher | 89a575c | 2014-02-12 22:38:04 +0000 | [diff] [blame] | 418 | addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(), |
| 419 | V.getContext().getDirectory()); |
David Blaikie | 101613e | 2014-02-12 00:11:25 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /// addSourceLine - Add location information to specified debug information |
| 423 | /// entry. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 424 | void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) { |
David Blaikie | 5201930 | 2014-02-11 23:57:03 +0000 | [diff] [blame] | 425 | assert(G.isGlobalVariable()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 426 | |
David Blaikie | 101613e | 2014-02-12 00:11:25 +0000 | [diff] [blame] | 427 | addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /// addSourceLine - Add location information to specified debug information |
| 431 | /// entry. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 432 | void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) { |
David Blaikie | 5201930 | 2014-02-11 23:57:03 +0000 | [diff] [blame] | 433 | assert(SP.isSubprogram()); |
Eric Christopher | 7734ca2 | 2012-03-15 23:55:40 +0000 | [diff] [blame] | 434 | |
David Blaikie | 101613e | 2014-02-12 00:11:25 +0000 | [diff] [blame] | 435 | addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /// addSourceLine - Add location information to specified debug information |
| 439 | /// entry. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 440 | void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) { |
David Blaikie | 5201930 | 2014-02-11 23:57:03 +0000 | [diff] [blame] | 441 | assert(Ty.isType()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 442 | |
David Blaikie | 101613e | 2014-02-12 00:11:25 +0000 | [diff] [blame] | 443 | addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /// addSourceLine - Add location information to specified debug information |
| 447 | /// entry. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 448 | void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) { |
David Blaikie | 5201930 | 2014-02-11 23:57:03 +0000 | [diff] [blame] | 449 | assert(Ty.isObjCProperty()); |
Eric Christopher | 70e1bd8 | 2012-03-29 08:42:56 +0000 | [diff] [blame] | 450 | |
Eric Christopher | 70e1bd8 | 2012-03-29 08:42:56 +0000 | [diff] [blame] | 451 | DIFile File = Ty.getFile(); |
David Blaikie | 101613e | 2014-02-12 00:11:25 +0000 | [diff] [blame] | 452 | addSourceLine(Die, Ty.getLineNumber(), File.getFilename(), |
| 453 | File.getDirectory()); |
Eric Christopher | 70e1bd8 | 2012-03-29 08:42:56 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /// addSourceLine - Add location information to specified debug information |
| 457 | /// entry. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 458 | void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) { |
David Blaikie | 5201930 | 2014-02-11 23:57:03 +0000 | [diff] [blame] | 459 | assert(NS.Verify()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 460 | |
David Blaikie | 101613e | 2014-02-12 00:11:25 +0000 | [diff] [blame] | 461 | addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Eric Christopher | 92331fd | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 464 | /// addVariableAddress - Add DW_AT_location attribute for a |
Devang Patel | 77dc541 | 2011-04-27 22:45:24 +0000 | [diff] [blame] | 465 | /// DbgVariable based on provided MachineLocation. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 466 | void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die, |
| 467 | MachineLocation Location) { |
Eric Christopher | bf2d23c | 2013-06-24 21:07:27 +0000 | [diff] [blame] | 468 | if (DV.variableHasComplexAddress()) |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 469 | addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); |
Eric Christopher | bf2d23c | 2013-06-24 21:07:27 +0000 | [diff] [blame] | 470 | else if (DV.isBlockByrefVariable()) |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 471 | addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); |
| 472 | else |
David Blaikie | ea2605d | 2013-06-20 00:25:24 +0000 | [diff] [blame] | 473 | addAddress(Die, dwarf::DW_AT_location, Location, |
Eric Christopher | bf2d23c | 2013-06-24 21:07:27 +0000 | [diff] [blame] | 474 | DV.getVariable().isIndirect()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 477 | /// addRegisterOp - Add register operand. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 478 | void DwarfUnit::addRegisterOp(DIELoc *TheDie, unsigned Reg) { |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 479 | const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); |
Adrian Prantl | cbcd578 | 2014-02-11 22:22:15 +0000 | [diff] [blame] | 480 | int DWReg = RI->getDwarfRegNum(Reg, false); |
| 481 | bool isSubRegister = DWReg < 0; |
| 482 | |
| 483 | unsigned Idx = 0; |
| 484 | |
| 485 | // Go up the super-register chain until we hit a valid dwarf register number. |
| 486 | for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) { |
| 487 | DWReg = RI->getDwarfRegNum(*SR, false); |
| 488 | if (DWReg >= 0) |
| 489 | Idx = RI->getSubRegIndex(*SR, Reg); |
| 490 | } |
| 491 | |
| 492 | if (DWReg < 0) { |
Eric Christopher | a13839f | 2014-02-26 23:27:16 +0000 | [diff] [blame] | 493 | DEBUG(dbgs() << "Invalid Dwarf register number.\n"); |
Adrian Prantl | cbcd578 | 2014-02-11 22:22:15 +0000 | [diff] [blame] | 494 | addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | // Emit register |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 499 | if (DWReg < 32) |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 500 | addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg); |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 501 | else { |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 502 | addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); |
| 503 | addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 504 | } |
Adrian Prantl | cbcd578 | 2014-02-11 22:22:15 +0000 | [diff] [blame] | 505 | |
| 506 | // Emit Mask |
| 507 | if (isSubRegister) { |
| 508 | unsigned Size = RI->getSubRegIdxSize(Idx); |
| 509 | unsigned Offset = RI->getSubRegIdxOffset(Idx); |
| 510 | if (Offset > 0) { |
| 511 | addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece); |
| 512 | addUInt(TheDie, dwarf::DW_FORM_data1, Size); |
| 513 | addUInt(TheDie, dwarf::DW_FORM_data1, Offset); |
| 514 | } else { |
Adrian Prantl | 7199fd5 | 2014-02-12 19:34:44 +0000 | [diff] [blame] | 515 | unsigned ByteSize = Size / 8; // Assuming 8 bits per byte. |
Adrian Prantl | cbcd578 | 2014-02-11 22:22:15 +0000 | [diff] [blame] | 516 | addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece); |
Adrian Prantl | 7199fd5 | 2014-02-12 19:34:44 +0000 | [diff] [blame] | 517 | addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize); |
Adrian Prantl | cbcd578 | 2014-02-11 22:22:15 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /// addRegisterOffset - Add register offset. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 523 | void DwarfUnit::addRegisterOffset(DIELoc *TheDie, unsigned Reg, |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 524 | int64_t Offset) { |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 525 | const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); |
| 526 | unsigned DWReg = RI->getDwarfRegNum(Reg, false); |
| 527 | const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo(); |
| 528 | if (Reg == TRI->getFrameRegister(*Asm->MF)) |
| 529 | // If variable offset is based in frame register then use fbreg. |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 530 | addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg); |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 531 | else if (DWReg < 32) |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 532 | addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg); |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 533 | else { |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 534 | addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); |
| 535 | addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 536 | } |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 537 | addSInt(TheDie, dwarf::DW_FORM_sdata, Offset); |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /// addAddress - Add an address attribute to a die based on the location |
| 541 | /// provided. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 542 | void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute, |
| 543 | const MachineLocation &Location, bool Indirect) { |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 544 | DIELoc *Loc = new (DIEValueAllocator) DIELoc(); |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 545 | |
David Blaikie | ea2605d | 2013-06-20 00:25:24 +0000 | [diff] [blame] | 546 | if (Location.isReg() && !Indirect) |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 547 | addRegisterOp(Loc, Location.getReg()); |
David Blaikie | ea2605d | 2013-06-20 00:25:24 +0000 | [diff] [blame] | 548 | else { |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 549 | addRegisterOffset(Loc, Location.getReg(), Location.getOffset()); |
David Blaikie | ea2605d | 2013-06-20 00:25:24 +0000 | [diff] [blame] | 550 | if (Indirect && !Location.isReg()) { |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 551 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
David Blaikie | ea2605d | 2013-06-20 00:25:24 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 554 | |
| 555 | // Now attach the location information to the DIE. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 556 | addBlock(Die, Attribute, Loc); |
Devang Patel | ba5fbf1 | 2011-04-26 19:06:18 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 559 | /// addComplexAddress - Start with the address based on the location provided, |
| 560 | /// and generate the DWARF information necessary to find the actual variable |
Eric Christopher | 1c70b67 | 2013-12-05 00:13:15 +0000 | [diff] [blame] | 561 | /// given the extra address information encoded in the DbgVariable, starting |
| 562 | /// from the starting location. Add the DWARF information to the die. |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 563 | /// |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 564 | void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die, |
| 565 | dwarf::Attribute Attribute, |
| 566 | const MachineLocation &Location) { |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 567 | DIELoc *Loc = new (DIEValueAllocator) DIELoc(); |
Eric Christopher | bf2d23c | 2013-06-24 21:07:27 +0000 | [diff] [blame] | 568 | unsigned N = DV.getNumAddrElements(); |
Devang Patel | 3e02153 | 2011-04-28 02:22:40 +0000 | [diff] [blame] | 569 | unsigned i = 0; |
| 570 | if (Location.isReg()) { |
Eric Christopher | bf2d23c | 2013-06-24 21:07:27 +0000 | [diff] [blame] | 571 | if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) { |
Devang Patel | 3e02153 | 2011-04-28 02:22:40 +0000 | [diff] [blame] | 572 | // If first address element is OpPlus then emit |
| 573 | // DW_OP_breg + Offset instead of DW_OP_reg + Offset. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 574 | addRegisterOffset(Loc, Location.getReg(), DV.getAddrElement(1)); |
Devang Patel | 3e02153 | 2011-04-28 02:22:40 +0000 | [diff] [blame] | 575 | i = 2; |
| 576 | } else |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 577 | addRegisterOp(Loc, Location.getReg()); |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 578 | } else |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 579 | addRegisterOffset(Loc, Location.getReg(), Location.getOffset()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 580 | |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 581 | for (; i < N; ++i) { |
Eric Christopher | bf2d23c | 2013-06-24 21:07:27 +0000 | [diff] [blame] | 582 | uint64_t Element = DV.getAddrElement(i); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 583 | if (Element == DIBuilder::OpPlus) { |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 584 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 585 | addUInt(Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i)); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 586 | } else if (Element == DIBuilder::OpDeref) { |
Eric Christopher | 4d25052 | 2012-05-08 18:56:00 +0000 | [diff] [blame] | 587 | if (!Location.isReg()) |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 588 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 589 | } else |
| 590 | llvm_unreachable("unknown DIBuilder Opcode"); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // Now attach the location information to the DIE. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 594 | addBlock(Die, Attribute, Loc); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* Byref variables, in Blocks, are declared by the programmer as "SomeType |
| 598 | VarName;", but the compiler creates a __Block_byref_x_VarName struct, and |
| 599 | gives the variable VarName either the struct, or a pointer to the struct, as |
| 600 | its type. This is necessary for various behind-the-scenes things the |
| 601 | compiler needs to do with by-reference variables in Blocks. |
| 602 | |
| 603 | However, as far as the original *programmer* is concerned, the variable |
| 604 | should still have type 'SomeType', as originally declared. |
| 605 | |
| 606 | The function getBlockByrefType dives into the __Block_byref_x_VarName |
| 607 | struct to find the original type of the variable, which is then assigned to |
| 608 | the variable's Debug Information Entry as its real type. So far, so good. |
| 609 | However now the debugger will expect the variable VarName to have the type |
| 610 | SomeType. So we need the location attribute for the variable to be an |
| 611 | expression that explains to the debugger how to navigate through the |
| 612 | pointers and struct to find the actual variable of type SomeType. |
| 613 | |
| 614 | The following function does just that. We start by getting |
| 615 | the "normal" location for the variable. This will be the location |
| 616 | of either the struct __Block_byref_x_VarName or the pointer to the |
| 617 | struct __Block_byref_x_VarName. |
| 618 | |
| 619 | The struct will look something like: |
| 620 | |
| 621 | struct __Block_byref_x_VarName { |
| 622 | ... <various fields> |
| 623 | struct __Block_byref_x_VarName *forwarding; |
| 624 | ... <various other fields> |
| 625 | SomeType VarName; |
| 626 | ... <maybe more fields> |
| 627 | }; |
| 628 | |
| 629 | If we are given the struct directly (as our starting point) we |
| 630 | need to tell the debugger to: |
| 631 | |
| 632 | 1). Add the offset of the forwarding field. |
| 633 | |
| 634 | 2). Follow that pointer to get the real __Block_byref_x_VarName |
| 635 | struct to use (the real one may have been copied onto the heap). |
| 636 | |
| 637 | 3). Add the offset for the field VarName, to find the actual variable. |
| 638 | |
| 639 | If we started with a pointer to the struct, then we need to |
| 640 | dereference that pointer first, before the other steps. |
| 641 | Translating this into DWARF ops, we will need to append the following |
| 642 | to the current location description for the variable: |
| 643 | |
| 644 | DW_OP_deref -- optional, if we start with a pointer |
| 645 | DW_OP_plus_uconst <forward_fld_offset> |
| 646 | DW_OP_deref |
| 647 | DW_OP_plus_uconst <varName_fld_offset> |
| 648 | |
| 649 | That is what this function does. */ |
| 650 | |
| 651 | /// addBlockByrefAddress - Start with the address based on the location |
| 652 | /// provided, and generate the DWARF information necessary to find the |
| 653 | /// actual Block variable (navigating the Block struct) based on the |
| 654 | /// starting location. Add the DWARF information to the die. For |
| 655 | /// more information, read large comment just above here. |
| 656 | /// |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 657 | void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die, |
| 658 | dwarf::Attribute Attribute, |
| 659 | const MachineLocation &Location) { |
Eric Christopher | bf2d23c | 2013-06-24 21:07:27 +0000 | [diff] [blame] | 660 | DIType Ty = DV.getType(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 661 | DIType TmpTy = Ty; |
Eric Christopher | 31b0576 | 2013-08-08 01:41:00 +0000 | [diff] [blame] | 662 | uint16_t Tag = Ty.getTag(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 663 | bool isPointer = false; |
| 664 | |
Eric Christopher | bf2d23c | 2013-06-24 21:07:27 +0000 | [diff] [blame] | 665 | StringRef varName = DV.getName(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 666 | |
| 667 | if (Tag == dwarf::DW_TAG_pointer_type) { |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 668 | DIDerivedType DTy(Ty); |
Manman Ren | 93b3090 | 2013-10-08 18:42:58 +0000 | [diff] [blame] | 669 | TmpTy = resolve(DTy.getTypeDerivedFrom()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 670 | isPointer = true; |
| 671 | } |
| 672 | |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 673 | DICompositeType blockStruct(TmpTy); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 674 | |
| 675 | // Find the __forwarding field and the variable field in the __Block_byref |
| 676 | // struct. |
| 677 | DIArray Fields = blockStruct.getTypeArray(); |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 678 | DIDerivedType varField; |
| 679 | DIDerivedType forwardingField; |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 680 | |
| 681 | for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) { |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 682 | DIDerivedType DT(Fields.getElement(i)); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 683 | StringRef fieldName = DT.getName(); |
| 684 | if (fieldName == "__forwarding") |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 685 | forwardingField = DT; |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 686 | else if (fieldName == varName) |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 687 | varField = DT; |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | // Get the offsets for the forwarding field and the variable field. |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 691 | unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3; |
| 692 | unsigned varFieldOffset = varField.getOffsetInBits() >> 2; |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 693 | |
| 694 | // Decode the original location, and use that as the start of the byref |
| 695 | // variable's location. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 696 | DIELoc *Loc = new (DIEValueAllocator) DIELoc(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 697 | |
Eric Christopher | ef9d710 | 2012-07-04 02:02:18 +0000 | [diff] [blame] | 698 | if (Location.isReg()) |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 699 | addRegisterOp(Loc, Location.getReg()); |
Eric Christopher | ef9d710 | 2012-07-04 02:02:18 +0000 | [diff] [blame] | 700 | else |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 701 | addRegisterOffset(Loc, Location.getReg(), Location.getOffset()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 702 | |
| 703 | // If we started with a pointer to the __Block_byref... struct, then |
| 704 | // the first thing we need to do is dereference the pointer (DW_OP_deref). |
| 705 | if (isPointer) |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 706 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 707 | |
| 708 | // Next add the offset for the '__forwarding' field: |
| 709 | // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in |
| 710 | // adding the offset if it's 0. |
| 711 | if (forwardingFieldOffset > 0) { |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 712 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 713 | addUInt(Loc, dwarf::DW_FORM_udata, forwardingFieldOffset); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | // Now dereference the __forwarding field to get to the real __Block_byref |
| 717 | // struct: DW_OP_deref. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 718 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 719 | |
| 720 | // Now that we've got the real __Block_byref... struct, add the offset |
| 721 | // for the variable's field to get to the location of the actual variable: |
| 722 | // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0. |
| 723 | if (varFieldOffset > 0) { |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 724 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 725 | addUInt(Loc, dwarf::DW_FORM_udata, varFieldOffset); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | // Now attach the location information to the DIE. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 729 | addBlock(Die, Attribute, Loc); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Devang Patel | bcd50a1 | 2011-07-20 21:57:04 +0000 | [diff] [blame] | 732 | /// isTypeSigned - Return true if the type is signed. |
Manman Ren | b338860 | 2013-10-05 01:43:03 +0000 | [diff] [blame] | 733 | static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) { |
Devang Patel | bcd50a1 | 2011-07-20 21:57:04 +0000 | [diff] [blame] | 734 | if (Ty.isDerivedType()) |
Manman Ren | b338860 | 2013-10-05 01:43:03 +0000 | [diff] [blame] | 735 | return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()), |
| 736 | SizeInBits); |
Devang Patel | bcd50a1 | 2011-07-20 21:57:04 +0000 | [diff] [blame] | 737 | if (Ty.isBasicType()) |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 738 | if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed || |
| 739 | DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) { |
Devang Patel | bcd50a1 | 2011-07-20 21:57:04 +0000 | [diff] [blame] | 740 | *SizeInBits = Ty.getSizeInBits(); |
| 741 | return true; |
| 742 | } |
| 743 | return false; |
| 744 | } |
| 745 | |
Manman Ren | b338860 | 2013-10-05 01:43:03 +0000 | [diff] [blame] | 746 | /// Return true if type encoding is unsigned. |
| 747 | static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) { |
| 748 | DIDerivedType DTy(Ty); |
| 749 | if (DTy.isDerivedType()) |
| 750 | return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom())); |
| 751 | |
| 752 | DIBasicType BTy(Ty); |
| 753 | if (BTy.isBasicType()) { |
| 754 | unsigned Encoding = BTy.getEncoding(); |
| 755 | if (Encoding == dwarf::DW_ATE_unsigned || |
| 756 | Encoding == dwarf::DW_ATE_unsigned_char || |
| 757 | Encoding == dwarf::DW_ATE_boolean) |
| 758 | return true; |
| 759 | } |
| 760 | return false; |
| 761 | } |
| 762 | |
| 763 | /// If this type is derived from a base type then return base type size. |
Manman Ren | bda410f | 2013-10-08 18:46:58 +0000 | [diff] [blame] | 764 | static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) { |
Manman Ren | b338860 | 2013-10-05 01:43:03 +0000 | [diff] [blame] | 765 | unsigned Tag = Ty.getTag(); |
| 766 | |
| 767 | if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && |
| 768 | Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && |
| 769 | Tag != dwarf::DW_TAG_restrict_type) |
| 770 | return Ty.getSizeInBits(); |
| 771 | |
| 772 | DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom()); |
| 773 | |
Eric Christopher | 8cc04fc | 2014-03-12 18:18:05 +0000 | [diff] [blame] | 774 | // If this type is not derived from any type or the type is a declaration then |
| 775 | // take conservative approach. |
| 776 | if (!BaseType.isValid() || BaseType.isForwardDecl()) |
Manman Ren | b338860 | 2013-10-05 01:43:03 +0000 | [diff] [blame] | 777 | return Ty.getSizeInBits(); |
| 778 | |
| 779 | // If this is a derived type, go ahead and get the base type, unless it's a |
| 780 | // reference then it's just the size of the field. Pointer types have no need |
| 781 | // of this since they're a different type of qualification on the type. |
| 782 | if (BaseType.getTag() == dwarf::DW_TAG_reference_type || |
| 783 | BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type) |
| 784 | return Ty.getSizeInBits(); |
| 785 | |
| 786 | if (BaseType.isDerivedType()) |
Manman Ren | bda410f | 2013-10-08 18:46:58 +0000 | [diff] [blame] | 787 | return getBaseTypeSize(DD, DIDerivedType(BaseType)); |
Manman Ren | b338860 | 2013-10-05 01:43:03 +0000 | [diff] [blame] | 788 | |
| 789 | return BaseType.getSizeInBits(); |
| 790 | } |
| 791 | |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 792 | /// addConstantValue - Add constant value entry in variable DIE. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 793 | void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO, |
| 794 | DIType Ty) { |
David Blaikie | a1e813d | 2013-05-10 21:52:07 +0000 | [diff] [blame] | 795 | // FIXME: This is a bit conservative/simple - it emits negative values at |
| 796 | // their maximum bit width which is a bit unfortunate (& doesn't prefer |
| 797 | // udata/sdata over dataN as suggested by the DWARF spec) |
Nick Lewycky | 654f5ce | 2011-10-26 22:55:33 +0000 | [diff] [blame] | 798 | assert(MO.isImm() && "Invalid machine operand!"); |
Devang Patel | bcd50a1 | 2011-07-20 21:57:04 +0000 | [diff] [blame] | 799 | int SizeInBits = -1; |
Manman Ren | b338860 | 2013-10-05 01:43:03 +0000 | [diff] [blame] | 800 | bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits); |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 801 | dwarf::Form Form; |
Devang Patel | f1d0470 | 2011-05-27 18:15:52 +0000 | [diff] [blame] | 802 | |
Eric Christopher | 9d1daa8 | 2013-08-27 23:49:04 +0000 | [diff] [blame] | 803 | // If we're a signed constant definitely use sdata. |
| 804 | if (SignedConstant) { |
| 805 | addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm()); |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | // Else use data for now unless it's larger than we can deal with. |
| 810 | switch (SizeInBits) { |
| 811 | case 8: |
| 812 | Form = dwarf::DW_FORM_data1; |
| 813 | break; |
| 814 | case 16: |
| 815 | Form = dwarf::DW_FORM_data2; |
| 816 | break; |
| 817 | case 32: |
| 818 | Form = dwarf::DW_FORM_data4; |
| 819 | break; |
| 820 | case 64: |
| 821 | Form = dwarf::DW_FORM_data8; |
| 822 | break; |
| 823 | default: |
| 824 | Form = dwarf::DW_FORM_udata; |
| 825 | addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm()); |
| 826 | return; |
| 827 | } |
| 828 | addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /// addConstantFPValue - Add constant value entry in variable DIE. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 832 | void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) { |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 833 | assert(MO.isFPImm() && "Invalid machine operand!"); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 834 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
| 835 | APFloat FPImm = MO.getFPImm()->getValueAPF(); |
| 836 | |
| 837 | // Get the raw data form of the floating point. |
| 838 | const APInt FltVal = FPImm.bitcastToAPInt(); |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 839 | const char *FltPtr = (const char *)FltVal.getRawData(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 840 | |
| 841 | int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 842 | bool LittleEndian = Asm->getDataLayout().isLittleEndian(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 843 | int Incr = (LittleEndian ? 1 : -1); |
| 844 | int Start = (LittleEndian ? 0 : NumBytes - 1); |
| 845 | int Stop = (LittleEndian ? NumBytes : -1); |
| 846 | |
| 847 | // Output the constant to DWARF one byte at a time. |
| 848 | for (; Start != Stop; Start += Incr) |
Eric Christopher | 98b7f17 | 2013-11-11 18:52:36 +0000 | [diff] [blame] | 849 | addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 850 | |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 851 | addBlock(Die, dwarf::DW_AT_const_value, Block); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 852 | } |
| 853 | |
David Blaikie | a39a76e | 2013-01-20 01:18:01 +0000 | [diff] [blame] | 854 | /// addConstantFPValue - Add constant value entry in variable DIE. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 855 | void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) { |
Eric Christopher | 9d1daa8 | 2013-08-27 23:49:04 +0000 | [diff] [blame] | 856 | // Pass this down to addConstantValue as an unsigned bag of bits. |
| 857 | addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true); |
David Blaikie | a39a76e | 2013-01-20 01:18:01 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 860 | /// addConstantValue - Add constant value entry in variable DIE. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 861 | void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI, |
| 862 | bool Unsigned) { |
Eric Christopher | 78fcf490 | 2013-07-03 01:08:30 +0000 | [diff] [blame] | 863 | addConstantValue(Die, CI->getValue(), Unsigned); |
David Blaikie | a39a76e | 2013-01-20 01:18:01 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | // addConstantValue - Add constant value entry in variable DIE. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 867 | void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) { |
David Blaikie | a39a76e | 2013-01-20 01:18:01 +0000 | [diff] [blame] | 868 | unsigned CIBitWidth = Val.getBitWidth(); |
Devang Patel | 8816bbc | 2011-05-28 00:39:18 +0000 | [diff] [blame] | 869 | if (CIBitWidth <= 64) { |
Eric Christopher | 9d1daa8 | 2013-08-27 23:49:04 +0000 | [diff] [blame] | 870 | // If we're a signed constant definitely use sdata. |
| 871 | if (!Unsigned) { |
| 872 | addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, |
| 873 | Val.getSExtValue()); |
| 874 | return; |
Devang Patel | 8816bbc | 2011-05-28 00:39:18 +0000 | [diff] [blame] | 875 | } |
Eric Christopher | 9d1daa8 | 2013-08-27 23:49:04 +0000 | [diff] [blame] | 876 | |
| 877 | // Else use data for now unless it's larger than we can deal with. |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 878 | dwarf::Form Form; |
Eric Christopher | 9d1daa8 | 2013-08-27 23:49:04 +0000 | [diff] [blame] | 879 | switch (CIBitWidth) { |
| 880 | case 8: |
| 881 | Form = dwarf::DW_FORM_data1; |
| 882 | break; |
| 883 | case 16: |
| 884 | Form = dwarf::DW_FORM_data2; |
| 885 | break; |
| 886 | case 32: |
| 887 | Form = dwarf::DW_FORM_data4; |
| 888 | break; |
| 889 | case 64: |
| 890 | Form = dwarf::DW_FORM_data8; |
| 891 | break; |
| 892 | default: |
| 893 | addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata, |
| 894 | Val.getZExtValue()); |
| 895 | return; |
| 896 | } |
| 897 | addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue()); |
Eric Christopher | 78fcf490 | 2013-07-03 01:08:30 +0000 | [diff] [blame] | 898 | return; |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); |
| 902 | |
| 903 | // Get the raw data form of the large APInt. |
NAKAMURA Takumi | 29ccdd8 | 2011-10-28 14:12:22 +0000 | [diff] [blame] | 904 | const uint64_t *Ptr64 = Val.getRawData(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 905 | |
| 906 | int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte. |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 907 | bool LittleEndian = Asm->getDataLayout().isLittleEndian(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 908 | |
| 909 | // Output the constant to DWARF one byte at a time. |
NAKAMURA Takumi | 29ccdd8 | 2011-10-28 14:12:22 +0000 | [diff] [blame] | 910 | for (int i = 0; i < NumBytes; i++) { |
| 911 | uint8_t c; |
| 912 | if (LittleEndian) |
| 913 | c = Ptr64[i / 8] >> (8 * (i & 7)); |
| 914 | else |
| 915 | c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7)); |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 916 | addUInt(Block, dwarf::DW_FORM_data1, c); |
NAKAMURA Takumi | 29ccdd8 | 2011-10-28 14:12:22 +0000 | [diff] [blame] | 917 | } |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 918 | |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 919 | addBlock(Die, dwarf::DW_AT_const_value, Block); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Eric Christopher | 25e3509 | 2013-04-22 07:47:40 +0000 | [diff] [blame] | 922 | /// addTemplateParams - Add template parameters into buffer. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 923 | void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 924 | // Add template parameters. |
| 925 | for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) { |
| 926 | DIDescriptor Element = TParams.getElement(i); |
| 927 | if (Element.isTemplateTypeParameter()) |
Manman Ren | ffc9a71 | 2013-10-23 23:05:28 +0000 | [diff] [blame] | 928 | constructTemplateTypeParameterDIE(Buffer, |
| 929 | DITemplateTypeParameter(Element)); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 930 | else if (Element.isTemplateValueParameter()) |
Manman Ren | ffc9a71 | 2013-10-23 23:05:28 +0000 | [diff] [blame] | 931 | constructTemplateValueParameterDIE(Buffer, |
| 932 | DITemplateValueParameter(Element)); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 933 | } |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 934 | } |
Nick Lewycky | 654f5ce | 2011-10-26 22:55:33 +0000 | [diff] [blame] | 935 | |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 936 | /// getOrCreateContextDIE - Get context owner's DIE. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 937 | DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) { |
David Blaikie | bd700e4 | 2013-11-14 21:24:34 +0000 | [diff] [blame] | 938 | if (!Context || Context.isFile()) |
David Blaikie | 2a80e44 | 2013-12-02 22:09:48 +0000 | [diff] [blame] | 939 | return getUnitDie(); |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 940 | if (Context.isType()) |
| 941 | return getOrCreateTypeDIE(DIType(Context)); |
David Blaikie | 1dbca70 | 2013-11-14 19:37:56 +0000 | [diff] [blame] | 942 | if (Context.isNameSpace()) |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 943 | return getOrCreateNameSpace(DINameSpace(Context)); |
David Blaikie | 1dbca70 | 2013-11-14 19:37:56 +0000 | [diff] [blame] | 944 | if (Context.isSubprogram()) |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 945 | return getOrCreateSubprogramDIE(DISubprogram(Context)); |
Adrian Prantl | 7d828bb | 2013-11-15 21:05:09 +0000 | [diff] [blame] | 946 | return getDIE(Context); |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 949 | DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) { |
David Blaikie | 9d861be | 2013-11-26 00:15:27 +0000 | [diff] [blame] | 950 | DIScope Context = resolve(Ty.getContext()); |
| 951 | DIE *ContextDIE = getOrCreateContextDIE(Context); |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 952 | |
| 953 | DIE *TyDIE = getDIE(Ty); |
| 954 | if (TyDIE) |
| 955 | return TyDIE; |
| 956 | |
| 957 | // Create new type. |
| 958 | TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty); |
| 959 | |
David Blaikie | fbd29eb | 2013-11-26 00:35:04 +0000 | [diff] [blame] | 960 | constructTypeDIE(*TyDIE, Ty); |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 961 | |
David Blaikie | 9d861be | 2013-11-26 00:15:27 +0000 | [diff] [blame] | 962 | updateAcceleratorTables(Context, Ty, TyDIE); |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 963 | return TyDIE; |
| 964 | } |
| 965 | |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 966 | /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the |
| 967 | /// given DIType. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 968 | DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) { |
David Blaikie | 3288755 | 2013-11-14 22:25:02 +0000 | [diff] [blame] | 969 | if (!TyNode) |
Devang Patel | eb1bb4e | 2011-08-16 22:09:43 +0000 | [diff] [blame] | 970 | return NULL; |
Manman Ren | f4c339e | 2013-10-29 22:49:29 +0000 | [diff] [blame] | 971 | |
David Blaikie | 3288755 | 2013-11-14 22:25:02 +0000 | [diff] [blame] | 972 | DIType Ty(TyNode); |
| 973 | assert(Ty.isType()); |
| 974 | |
Manman Ren | f4c339e | 2013-10-29 22:49:29 +0000 | [diff] [blame] | 975 | // Construct the context before querying for the existence of the DIE in case |
| 976 | // such construction creates the DIE. |
David Blaikie | 9d861be | 2013-11-26 00:15:27 +0000 | [diff] [blame] | 977 | DIScope Context = resolve(Ty.getContext()); |
| 978 | DIE *ContextDIE = getOrCreateContextDIE(Context); |
Adrian Prantl | 4583f7d | 2013-11-15 23:21:39 +0000 | [diff] [blame] | 979 | assert(ContextDIE); |
Manman Ren | f4c339e | 2013-10-29 22:49:29 +0000 | [diff] [blame] | 980 | |
Adrian Prantl | 2e4e62e | 2014-03-14 23:08:29 +0000 | [diff] [blame] | 981 | // Unique the type. This is a noop if the type has no unique identifier. |
| 982 | Ty = DIType(resolve(Ty.getRef())); |
| 983 | |
Eric Christopher | e595bae | 2013-10-04 17:08:38 +0000 | [diff] [blame] | 984 | DIE *TyDIE = getDIE(Ty); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 985 | if (TyDIE) |
| 986 | return TyDIE; |
| 987 | |
| 988 | // Create new type. |
Manman Ren | f4c339e | 2013-10-29 22:49:29 +0000 | [diff] [blame] | 989 | TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty); |
| 990 | |
David Blaikie | c3d9e9e | 2014-03-06 01:42:00 +0000 | [diff] [blame] | 991 | updateAcceleratorTables(Context, Ty, TyDIE); |
| 992 | |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 993 | if (Ty.isBasicType()) |
| 994 | constructTypeDIE(*TyDIE, DIBasicType(Ty)); |
David Blaikie | 8a263cb | 2013-11-26 00:22:37 +0000 | [diff] [blame] | 995 | else if (Ty.isCompositeType()) { |
| 996 | DICompositeType CTy(Ty); |
David Blaikie | cfb2115 | 2014-01-03 18:59:42 +0000 | [diff] [blame] | 997 | if (GenerateDwarfTypeUnits && !Ty.isForwardDecl()) |
| 998 | if (MDString *TypeId = CTy.getIdentifier()) { |
David Blaikie | 15632ae | 2014-02-12 00:31:30 +0000 | [diff] [blame] | 999 | DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy); |
Adrian Prantl | e2da17f | 2014-03-14 23:08:17 +0000 | [diff] [blame] | 1000 | // Skip updating the accelerator tables since this is not the full type. |
David Blaikie | cfb2115 | 2014-01-03 18:59:42 +0000 | [diff] [blame] | 1001 | return TyDIE; |
| 1002 | } |
David Blaikie | fbd29eb | 2013-11-26 00:35:04 +0000 | [diff] [blame] | 1003 | constructTypeDIE(*TyDIE, CTy); |
David Blaikie | 8a263cb | 2013-11-26 00:22:37 +0000 | [diff] [blame] | 1004 | } else { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1005 | assert(Ty.isDerivedType() && "Unknown kind of DIType"); |
| 1006 | constructTypeDIE(*TyDIE, DIDerivedType(Ty)); |
| 1007 | } |
David Blaikie | 2ea848b | 2013-11-19 22:51:04 +0000 | [diff] [blame] | 1008 | |
David Blaikie | 2ea848b | 2013-11-19 22:51:04 +0000 | [diff] [blame] | 1009 | return TyDIE; |
| 1010 | } |
| 1011 | |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1012 | void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty, |
| 1013 | const DIE *TyDIE) { |
Eric Christopher | 21bde87 | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 1014 | if (!Ty.getName().empty() && !Ty.isForwardDecl()) { |
| 1015 | bool IsImplementation = 0; |
| 1016 | if (Ty.isCompositeType()) { |
| 1017 | DICompositeType CT(Ty); |
Eric Christopher | 8ea8e4f | 2012-01-06 23:03:37 +0000 | [diff] [blame] | 1018 | // A runtime language of 0 actually means C/C++ and that any |
| 1019 | // non-negative value is some version of Objective-C/C++. |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1020 | IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete(); |
Eric Christopher | 21bde87 | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 1021 | } |
Eric Christopher | cf7289f | 2013-09-05 18:20:16 +0000 | [diff] [blame] | 1022 | unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0; |
Eric Christopher | 8ea8e4f | 2012-01-06 23:03:37 +0000 | [diff] [blame] | 1023 | addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags)); |
David Blaikie | 9d861be | 2013-11-26 00:15:27 +0000 | [diff] [blame] | 1024 | |
David Blaikie | adbea1e | 2014-03-12 03:34:38 +0000 | [diff] [blame] | 1025 | if ((!Context || Context.isCompileUnit() || Context.isFile() || |
| 1026 | Context.isNameSpace()) && |
| 1027 | getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) |
David Blaikie | 9d861be | 2013-11-26 00:15:27 +0000 | [diff] [blame] | 1028 | GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE; |
Eric Christopher | 21bde87 | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 1029 | } |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | /// addType - Add a new type attribute to the specified entity. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1033 | void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) { |
Eric Christopher | 0df08e2 | 2013-08-08 07:40:37 +0000 | [diff] [blame] | 1034 | assert(Ty && "Trying to add a type that doesn't exist?"); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1035 | |
| 1036 | // Check for pre-existence. |
| 1037 | DIEEntry *Entry = getDIEEntry(Ty); |
| 1038 | // If it exists then use the existing value. |
| 1039 | if (Entry) { |
Manman Ren | 4dbdc90 | 2013-10-31 17:54:35 +0000 | [diff] [blame] | 1040 | addDIEEntry(Entity, Attribute, Entry); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | // Construct type. |
| 1045 | DIE *Buffer = getOrCreateTypeDIE(Ty); |
| 1046 | |
| 1047 | // Set up proxy. |
| 1048 | Entry = createDIEEntry(Buffer); |
| 1049 | insertDIEEntry(Ty, Entry); |
Manman Ren | 4dbdc90 | 2013-10-31 17:54:35 +0000 | [diff] [blame] | 1050 | addDIEEntry(Entity, Attribute, Entry); |
Devang Patel | 1cb8ab4 | 2011-05-31 23:30:30 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 1053 | // Accelerator table mutators - add each name along with its companion |
| 1054 | // DIE to the proper table while ensuring that the name that we're going |
| 1055 | // to reference is in the string table. We do this since the names we |
| 1056 | // add may not only be identical to the names in the DIE. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1057 | void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) { |
Eric Christopher | d0d5bba | 2014-02-12 22:47:09 +0000 | [diff] [blame] | 1058 | if (!DD->useDwarfAccelTables()) |
| 1059 | return; |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 1060 | DU->getStringPoolEntry(Name); |
David Blaikie | 2ea848b | 2013-11-19 22:51:04 +0000 | [diff] [blame] | 1061 | std::vector<const DIE *> &DIEs = AccelNames[Name]; |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 1062 | DIEs.push_back(Die); |
| 1063 | } |
| 1064 | |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1065 | void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) { |
Eric Christopher | d0d5bba | 2014-02-12 22:47:09 +0000 | [diff] [blame] | 1066 | if (!DD->useDwarfAccelTables()) |
| 1067 | return; |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 1068 | DU->getStringPoolEntry(Name); |
David Blaikie | 2ea848b | 2013-11-19 22:51:04 +0000 | [diff] [blame] | 1069 | std::vector<const DIE *> &DIEs = AccelObjC[Name]; |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 1070 | DIEs.push_back(Die); |
| 1071 | } |
| 1072 | |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1073 | void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) { |
Eric Christopher | d0d5bba | 2014-02-12 22:47:09 +0000 | [diff] [blame] | 1074 | if (!DD->useDwarfAccelTables()) |
| 1075 | return; |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 1076 | DU->getStringPoolEntry(Name); |
David Blaikie | 2ea848b | 2013-11-19 22:51:04 +0000 | [diff] [blame] | 1077 | std::vector<const DIE *> &DIEs = AccelNamespace[Name]; |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 1078 | DIEs.push_back(Die); |
| 1079 | } |
| 1080 | |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1081 | void DwarfUnit::addAccelType(StringRef Name, |
| 1082 | std::pair<const DIE *, unsigned> Die) { |
Eric Christopher | d0d5bba | 2014-02-12 22:47:09 +0000 | [diff] [blame] | 1083 | if (!DD->useDwarfAccelTables()) |
| 1084 | return; |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 1085 | DU->getStringPoolEntry(Name); |
David Blaikie | 2ea848b | 2013-11-19 22:51:04 +0000 | [diff] [blame] | 1086 | std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name]; |
Eric Christopher | 9cd26af | 2013-09-20 23:22:52 +0000 | [diff] [blame] | 1087 | DIEs.push_back(Die); |
| 1088 | } |
| 1089 | |
Eric Christopher | 9c58f31 | 2013-09-20 22:20:55 +0000 | [diff] [blame] | 1090 | /// addGlobalName - Add a new global name to the compile unit. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1091 | void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) { |
David Blaikie | adbea1e | 2014-03-12 03:34:38 +0000 | [diff] [blame] | 1092 | if (getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly) |
| 1093 | return; |
Eric Christopher | 8dba0d5 | 2013-10-19 01:04:42 +0000 | [diff] [blame] | 1094 | std::string FullName = getParentContextString(Context) + Name.str(); |
Eric Christopher | 2c8b790 | 2013-10-17 02:06:06 +0000 | [diff] [blame] | 1095 | GlobalNames[FullName] = Die; |
Eric Christopher | 9c58f31 | 2013-09-20 22:20:55 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Eric Christopher | 2c8b790 | 2013-10-17 02:06:06 +0000 | [diff] [blame] | 1098 | /// getParentContextString - Walks the metadata parent chain in a language |
| 1099 | /// specific manner (using the compile unit language) and returns |
| 1100 | /// it as a string. This is done at the metadata level because DIEs may |
| 1101 | /// not currently have been added to the parent context and walking the |
| 1102 | /// DIEs looking for names is more expensive than walking the metadata. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1103 | std::string DwarfUnit::getParentContextString(DIScope Context) const { |
Eric Christopher | 2c8b790 | 2013-10-17 02:06:06 +0000 | [diff] [blame] | 1104 | if (!Context) |
| 1105 | return ""; |
| 1106 | |
| 1107 | // FIXME: Decide whether to implement this for non-C++ languages. |
| 1108 | if (getLanguage() != dwarf::DW_LANG_C_plus_plus) |
| 1109 | return ""; |
| 1110 | |
Eric Christopher | 8dba0d5 | 2013-10-19 01:04:42 +0000 | [diff] [blame] | 1111 | std::string CS; |
Eric Christopher | 2c8b790 | 2013-10-17 02:06:06 +0000 | [diff] [blame] | 1112 | SmallVector<DIScope, 1> Parents; |
| 1113 | while (!Context.isCompileUnit()) { |
| 1114 | Parents.push_back(Context); |
| 1115 | if (Context.getContext()) |
| 1116 | Context = resolve(Context.getContext()); |
| 1117 | else |
| 1118 | // Structure, etc types will have a NULL context if they're at the top |
| 1119 | // level. |
| 1120 | break; |
| 1121 | } |
| 1122 | |
| 1123 | // Reverse iterate over our list to go from the outermost construct to the |
| 1124 | // innermost. |
| 1125 | for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(), |
| 1126 | E = Parents.rend(); |
| 1127 | I != E; ++I) { |
| 1128 | DIScope Ctx = *I; |
| 1129 | StringRef Name = Ctx.getName(); |
Eric Christopher | 8dba0d5 | 2013-10-19 01:04:42 +0000 | [diff] [blame] | 1130 | if (!Name.empty()) { |
Eric Christopher | 2c8b790 | 2013-10-17 02:06:06 +0000 | [diff] [blame] | 1131 | CS += Name; |
| 1132 | CS += "::"; |
| 1133 | } |
| 1134 | } |
| 1135 | return CS; |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | /// constructTypeDIE - Construct basic type die from DIBasicType. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1139 | void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1140 | // Get core information. |
| 1141 | StringRef Name = BTy.getName(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1142 | // Add name if not anonymous or intermediate type. |
| 1143 | if (!Name.empty()) |
Nick Lewycky | d59c0ca | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1144 | addString(&Buffer, dwarf::DW_AT_name, Name); |
Devang Patel | 04d6d47 | 2011-09-14 23:13:28 +0000 | [diff] [blame] | 1145 | |
David Blaikie | fac5612 | 2013-10-04 23:21:16 +0000 | [diff] [blame] | 1146 | // An unspecified type only has a name attribute. |
| 1147 | if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) |
Devang Patel | 04d6d47 | 2011-09-14 23:13:28 +0000 | [diff] [blame] | 1148 | return; |
Devang Patel | 04d6d47 | 2011-09-14 23:13:28 +0000 | [diff] [blame] | 1149 | |
Nick Lewycky | 654f5ce | 2011-10-26 22:55:33 +0000 | [diff] [blame] | 1150 | addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, |
Devang Patel | d925d1a | 2012-02-07 23:33:58 +0000 | [diff] [blame] | 1151 | BTy.getEncoding()); |
Devang Patel | 04d6d47 | 2011-09-14 23:13:28 +0000 | [diff] [blame] | 1152 | |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1153 | uint64_t Size = BTy.getSizeInBits() >> 3; |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 1154 | addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | /// constructTypeDIE - Construct derived type die from DIDerivedType. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1158 | void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1159 | // Get core information. |
| 1160 | StringRef Name = DTy.getName(); |
| 1161 | uint64_t Size = DTy.getSizeInBits() >> 3; |
David Blaikie | fac5612 | 2013-10-04 23:21:16 +0000 | [diff] [blame] | 1162 | uint16_t Tag = Buffer.getTag(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1163 | |
| 1164 | // Map to main type, void will not have a type. |
Manman Ren | 93b3090 | 2013-10-08 18:42:58 +0000 | [diff] [blame] | 1165 | DIType FromTy = resolve(DTy.getTypeDerivedFrom()); |
Eric Christopher | 0df08e2 | 2013-08-08 07:40:37 +0000 | [diff] [blame] | 1166 | if (FromTy) |
| 1167 | addType(&Buffer, FromTy); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1168 | |
| 1169 | // Add name if not anonymous or intermediate type. |
| 1170 | if (!Name.empty()) |
Nick Lewycky | d59c0ca | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1171 | addString(&Buffer, dwarf::DW_AT_name, Name); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1172 | |
| 1173 | // Add size if non-zero (derived types might be zero-sized.) |
Eric Christopher | 8575790 | 2012-02-21 22:25:53 +0000 | [diff] [blame] | 1174 | if (Size && Tag != dwarf::DW_TAG_pointer_type) |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 1175 | addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1176 | |
David Blaikie | 5d3249b | 2013-01-07 05:51:15 +0000 | [diff] [blame] | 1177 | if (Tag == dwarf::DW_TAG_ptr_to_member_type) |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1178 | addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, |
| 1179 | getOrCreateTypeDIE(resolve(DTy.getClassType()))); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1180 | // Add source line info if available and TyDesc is not a forward declaration. |
| 1181 | if (!DTy.isForwardDecl()) |
| 1182 | addSourceLine(&Buffer, DTy); |
| 1183 | } |
| 1184 | |
Adrian Prantl | 3f49c89 | 2014-02-25 19:57:42 +0000 | [diff] [blame] | 1185 | /// constructSubprogramArguments - Construct function argument DIEs. |
| 1186 | void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DIArray Args) { |
Adrian Prantl | 69140d2 | 2014-02-25 22:27:14 +0000 | [diff] [blame] | 1187 | for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { |
| 1188 | DIDescriptor Ty = Args.getElement(i); |
| 1189 | if (Ty.isUnspecifiedParameter()) { |
| 1190 | assert(i == N-1 && "Unspecified parameter must be the last argument"); |
| 1191 | createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer); |
| 1192 | } else { |
| 1193 | DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer); |
| 1194 | addType(Arg, DIType(Ty)); |
| 1195 | if (DIType(Ty).isArtificial()) |
| 1196 | addFlag(Arg, dwarf::DW_AT_artificial); |
Adrian Prantl | 3f49c89 | 2014-02-25 19:57:42 +0000 | [diff] [blame] | 1197 | } |
Adrian Prantl | 69140d2 | 2014-02-25 22:27:14 +0000 | [diff] [blame] | 1198 | } |
Adrian Prantl | 3f49c89 | 2014-02-25 19:57:42 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1201 | /// constructTypeDIE - Construct type DIE from DICompositeType. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1202 | void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) { |
David Blaikie | 409dd9c | 2013-11-19 23:08:21 +0000 | [diff] [blame] | 1203 | // Add name if not anonymous or intermediate type. |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1204 | StringRef Name = CTy.getName(); |
| 1205 | |
| 1206 | uint64_t Size = CTy.getSizeInBits() >> 3; |
David Blaikie | fac5612 | 2013-10-04 23:21:16 +0000 | [diff] [blame] | 1207 | uint16_t Tag = Buffer.getTag(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1208 | |
| 1209 | switch (Tag) { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1210 | case dwarf::DW_TAG_array_type: |
Eric Christopher | df9955d | 2013-11-11 18:52:31 +0000 | [diff] [blame] | 1211 | constructArrayTypeDIE(Buffer, CTy); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1212 | break; |
Eric Christopher | aeb105f | 2013-11-11 18:52:39 +0000 | [diff] [blame] | 1213 | case dwarf::DW_TAG_enumeration_type: |
| 1214 | constructEnumTypeDIE(Buffer, CTy); |
| 1215 | break; |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1216 | case dwarf::DW_TAG_subroutine_type: { |
Eric Christopher | 0df08e2 | 2013-08-08 07:40:37 +0000 | [diff] [blame] | 1217 | // Add return type. A void return won't have a type. |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1218 | DIArray Elements = CTy.getTypeArray(); |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 1219 | DIType RTy(Elements.getElement(0)); |
Eric Christopher | 0df08e2 | 2013-08-08 07:40:37 +0000 | [diff] [blame] | 1220 | if (RTy) |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 1221 | addType(&Buffer, RTy); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1222 | |
| 1223 | bool isPrototyped = true; |
Adrian Prantl | 3f49c89 | 2014-02-25 19:57:42 +0000 | [diff] [blame] | 1224 | if (Elements.getNumElements() == 2 && |
| 1225 | Elements.getElement(1).isUnspecifiedParameter()) |
| 1226 | isPrototyped = false; |
| 1227 | |
| 1228 | constructSubprogramArguments(Buffer, Elements); |
| 1229 | |
Eric Christopher | 5cd2a9d | 2012-02-22 08:46:21 +0000 | [diff] [blame] | 1230 | // Add prototype flag if we're dealing with a C language and the |
| 1231 | // function has been prototyped. |
David Blaikie | cb8e435 | 2013-11-15 23:50:53 +0000 | [diff] [blame] | 1232 | uint16_t Language = getLanguage(); |
Eric Christopher | 5cd2a9d | 2012-02-22 08:46:21 +0000 | [diff] [blame] | 1233 | if (isPrototyped && |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1234 | (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || |
Eric Christopher | d42b92f | 2012-05-22 18:45:24 +0000 | [diff] [blame] | 1235 | Language == dwarf::DW_LANG_ObjC)) |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1236 | addFlag(&Buffer, dwarf::DW_AT_prototyped); |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 1237 | |
| 1238 | if (CTy.isLValueReference()) |
| 1239 | addFlag(&Buffer, dwarf::DW_AT_reference); |
| 1240 | |
| 1241 | if (CTy.isRValueReference()) |
| 1242 | addFlag(&Buffer, dwarf::DW_AT_rvalue_reference); |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1243 | } break; |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1244 | case dwarf::DW_TAG_structure_type: |
| 1245 | case dwarf::DW_TAG_union_type: |
| 1246 | case dwarf::DW_TAG_class_type: { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1247 | // Add elements to structure type. |
David Blaikie | a1ae0e6 | 2013-08-01 20:30:22 +0000 | [diff] [blame] | 1248 | DIArray Elements = CTy.getTypeArray(); |
| 1249 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1250 | DIDescriptor Element = Elements.getElement(i); |
| 1251 | DIE *ElemDie = NULL; |
Adrian Prantl | ef129fb | 2014-01-18 02:12:00 +0000 | [diff] [blame] | 1252 | if (Element.isSubprogram()) |
| 1253 | ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element)); |
| 1254 | else if (Element.isDerivedType()) { |
Eric Christopher | d42b92f | 2012-05-22 18:45:24 +0000 | [diff] [blame] | 1255 | DIDerivedType DDTy(Element); |
| 1256 | if (DDTy.getTag() == dwarf::DW_TAG_friend) { |
Manman Ren | b987e51 | 2013-10-29 00:53:03 +0000 | [diff] [blame] | 1257 | ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer); |
Manman Ren | 93b3090 | 2013-10-08 18:42:58 +0000 | [diff] [blame] | 1258 | addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()), |
Manman Ren | b338860 | 2013-10-05 01:43:03 +0000 | [diff] [blame] | 1259 | dwarf::DW_AT_friend); |
Manman Ren | c6b6392 | 2013-10-14 20:33:57 +0000 | [diff] [blame] | 1260 | } else if (DDTy.isStaticMember()) { |
Manman Ren | 57e6ff7 | 2013-10-23 22:57:12 +0000 | [diff] [blame] | 1261 | getOrCreateStaticMemberDIE(DDTy); |
Manman Ren | c6b6392 | 2013-10-14 20:33:57 +0000 | [diff] [blame] | 1262 | } else { |
Manman Ren | 230ec86 | 2013-10-23 23:00:44 +0000 | [diff] [blame] | 1263 | constructMemberDIE(Buffer, DDTy); |
Manman Ren | c6b6392 | 2013-10-14 20:33:57 +0000 | [diff] [blame] | 1264 | } |
Eric Christopher | 7285c7d | 2012-03-28 07:34:31 +0000 | [diff] [blame] | 1265 | } else if (Element.isObjCProperty()) { |
Devang Patel | d925d1a | 2012-02-07 23:33:58 +0000 | [diff] [blame] | 1266 | DIObjCProperty Property(Element); |
Manman Ren | b987e51 | 2013-10-29 00:53:03 +0000 | [diff] [blame] | 1267 | ElemDie = createAndAddDIE(Property.getTag(), Buffer); |
Devang Patel | d925d1a | 2012-02-07 23:33:58 +0000 | [diff] [blame] | 1268 | StringRef PropertyName = Property.getObjCPropertyName(); |
| 1269 | addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); |
Eric Christopher | 4c96056 | 2014-01-23 19:16:28 +0000 | [diff] [blame] | 1270 | if (Property.getType()) |
| 1271 | addType(ElemDie, Property.getType()); |
Eric Christopher | d42b92f | 2012-05-22 18:45:24 +0000 | [diff] [blame] | 1272 | addSourceLine(ElemDie, Property); |
Devang Patel | d925d1a | 2012-02-07 23:33:58 +0000 | [diff] [blame] | 1273 | StringRef GetterName = Property.getObjCPropertyGetterName(); |
| 1274 | if (!GetterName.empty()) |
| 1275 | addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName); |
| 1276 | StringRef SetterName = Property.getObjCPropertySetterName(); |
| 1277 | if (!SetterName.empty()) |
| 1278 | addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName); |
| 1279 | unsigned PropertyAttributes = 0; |
Devang Patel | 403e819 | 2012-02-04 01:30:32 +0000 | [diff] [blame] | 1280 | if (Property.isReadOnlyObjCProperty()) |
| 1281 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly; |
| 1282 | if (Property.isReadWriteObjCProperty()) |
| 1283 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite; |
| 1284 | if (Property.isAssignObjCProperty()) |
| 1285 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign; |
| 1286 | if (Property.isRetainObjCProperty()) |
| 1287 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain; |
| 1288 | if (Property.isCopyObjCProperty()) |
| 1289 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy; |
| 1290 | if (Property.isNonAtomicObjCProperty()) |
| 1291 | PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic; |
| 1292 | if (PropertyAttributes) |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 1293 | addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None, |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1294 | PropertyAttributes); |
Devang Patel | 4488217 | 2012-02-06 17:49:43 +0000 | [diff] [blame] | 1295 | |
Devang Patel | d925d1a | 2012-02-07 23:33:58 +0000 | [diff] [blame] | 1296 | DIEEntry *Entry = getDIEEntry(Element); |
| 1297 | if (!Entry) { |
| 1298 | Entry = createDIEEntry(ElemDie); |
| 1299 | insertDIEEntry(Element, Entry); |
| 1300 | } |
Devang Patel | 403e819 | 2012-02-04 01:30:32 +0000 | [diff] [blame] | 1301 | } else |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1302 | continue; |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | if (CTy.isAppleBlockExtension()) |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1306 | addFlag(&Buffer, dwarf::DW_AT_APPLE_block); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1307 | |
Eric Christopher | 9e429ae | 2013-10-05 00:27:02 +0000 | [diff] [blame] | 1308 | DICompositeType ContainingType(resolve(CTy.getContainingType())); |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 1309 | if (ContainingType) |
Manman Ren | 4c4b69c | 2013-10-11 23:58:05 +0000 | [diff] [blame] | 1310 | addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 1311 | getOrCreateTypeDIE(ContainingType)); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1312 | |
Devang Patel | 12419ae | 2011-05-12 21:29:42 +0000 | [diff] [blame] | 1313 | if (CTy.isObjcClassComplete()) |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1314 | addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type); |
Devang Patel | 2409e78 | 2011-05-12 19:06:16 +0000 | [diff] [blame] | 1315 | |
Eric Christopher | da011dd | 2011-12-16 23:42:42 +0000 | [diff] [blame] | 1316 | // Add template parameters to a class, structure or union types. |
| 1317 | // FIXME: The support isn't in the metadata for this yet. |
| 1318 | if (Tag == dwarf::DW_TAG_class_type || |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1319 | Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1320 | addTemplateParams(Buffer, CTy.getTemplateParams()); |
| 1321 | |
| 1322 | break; |
| 1323 | } |
| 1324 | default: |
| 1325 | break; |
| 1326 | } |
| 1327 | |
| 1328 | // Add name if not anonymous or intermediate type. |
| 1329 | if (!Name.empty()) |
Nick Lewycky | d59c0ca | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1330 | addString(&Buffer, dwarf::DW_AT_name, Name); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1331 | |
Eric Christopher | 775cbd2 | 2012-05-22 18:45:18 +0000 | [diff] [blame] | 1332 | if (Tag == dwarf::DW_TAG_enumeration_type || |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1333 | Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type || |
Eric Christopher | 775cbd2 | 2012-05-22 18:45:18 +0000 | [diff] [blame] | 1334 | Tag == dwarf::DW_TAG_union_type) { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1335 | // Add size if non-zero (derived types might be zero-sized.) |
Eric Christopher | 1cf3338 | 2012-06-01 00:22:32 +0000 | [diff] [blame] | 1336 | // TODO: Do we care about size for enum forward declarations? |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1337 | if (Size) |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 1338 | addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); |
Eric Christopher | 1cf3338 | 2012-06-01 00:22:32 +0000 | [diff] [blame] | 1339 | else if (!CTy.isForwardDecl()) |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1340 | // Add zero size if it is not a forward declaration. |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 1341 | addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0); |
Eric Christopher | 1cf3338 | 2012-06-01 00:22:32 +0000 | [diff] [blame] | 1342 | |
| 1343 | // If we're a forward decl, say so. |
| 1344 | if (CTy.isForwardDecl()) |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1345 | addFlag(&Buffer, dwarf::DW_AT_declaration); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1346 | |
| 1347 | // Add source line info if available. |
| 1348 | if (!CTy.isForwardDecl()) |
| 1349 | addSourceLine(&Buffer, CTy); |
Eric Christopher | 54cf8ff | 2012-03-07 00:15:19 +0000 | [diff] [blame] | 1350 | |
| 1351 | // No harm in adding the runtime language to the declaration. |
| 1352 | unsigned RLang = CTy.getRunTimeLang(); |
| 1353 | if (RLang) |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1354 | addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1, |
| 1355 | RLang); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | |
Manman Ren | ffc9a71 | 2013-10-23 23:05:28 +0000 | [diff] [blame] | 1359 | /// constructTemplateTypeParameterDIE - Construct new DIE for the given |
| 1360 | /// DITemplateTypeParameter. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1361 | void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer, |
| 1362 | DITemplateTypeParameter TP) { |
Manman Ren | b987e51 | 2013-10-29 00:53:03 +0000 | [diff] [blame] | 1363 | DIE *ParamDIE = |
| 1364 | createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer); |
Eric Christopher | 056b647 | 2013-08-08 08:09:43 +0000 | [diff] [blame] | 1365 | // Add the type if it exists, it could be void and therefore no type. |
| 1366 | if (TP.getType()) |
Manman Ren | 88b0f94 | 2013-10-09 19:46:28 +0000 | [diff] [blame] | 1367 | addType(ParamDIE, resolve(TP.getType())); |
David Blaikie | 2b38023 | 2013-06-22 18:59:11 +0000 | [diff] [blame] | 1368 | if (!TP.getName().empty()) |
| 1369 | addString(ParamDIE, dwarf::DW_AT_name, TP.getName()); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Manman Ren | ffc9a71 | 2013-10-23 23:05:28 +0000 | [diff] [blame] | 1372 | /// constructTemplateValueParameterDIE - Construct new DIE for the given |
| 1373 | /// DITemplateValueParameter. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1374 | void |
| 1375 | DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer, |
David Blaikie | 319a05f | 2013-12-02 19:33:10 +0000 | [diff] [blame] | 1376 | DITemplateValueParameter VP) { |
Manman Ren | b987e51 | 2013-10-29 00:53:03 +0000 | [diff] [blame] | 1377 | DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer); |
Eric Christopher | 0df08e2 | 2013-08-08 07:40:37 +0000 | [diff] [blame] | 1378 | |
| 1379 | // Add the type if there is one, template template and template parameter |
| 1380 | // packs will not have a type. |
Eric Christopher | 691281b | 2013-10-21 17:48:51 +0000 | [diff] [blame] | 1381 | if (VP.getTag() == dwarf::DW_TAG_template_value_parameter) |
Manman Ren | 88b0f94 | 2013-10-09 19:46:28 +0000 | [diff] [blame] | 1382 | addType(ParamDIE, resolve(VP.getType())); |
Eric Christopher | afb2c41 | 2013-08-08 07:40:31 +0000 | [diff] [blame] | 1383 | if (!VP.getName().empty()) |
| 1384 | addString(ParamDIE, dwarf::DW_AT_name, VP.getName()); |
| 1385 | if (Value *Val = VP.getValue()) { |
David Blaikie | a1e813d | 2013-05-10 21:52:07 +0000 | [diff] [blame] | 1386 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) |
Manman Ren | 88b0f94 | 2013-10-09 19:46:28 +0000 | [diff] [blame] | 1387 | addConstantValue(ParamDIE, CI, |
| 1388 | isUnsignedDIType(DD, resolve(VP.getType()))); |
David Blaikie | a1e813d | 2013-05-10 21:52:07 +0000 | [diff] [blame] | 1389 | else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) { |
| 1390 | // For declaration non-type template parameters (such as global values and |
| 1391 | // functions) |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1392 | DIELoc *Loc = new (DIEValueAllocator) DIELoc(); |
| 1393 | addOpAddress(Loc, Asm->getSymbol(GV)); |
David Blaikie | a1e813d | 2013-05-10 21:52:07 +0000 | [diff] [blame] | 1394 | // Emit DW_OP_stack_value to use the address as the immediate value of the |
| 1395 | // parameter, rather than a pointer to it. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1396 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value); |
| 1397 | addBlock(ParamDIE, dwarf::DW_AT_location, Loc); |
Eric Christopher | afb2c41 | 2013-08-08 07:40:31 +0000 | [diff] [blame] | 1398 | } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) { |
David Blaikie | 2b38023 | 2013-06-22 18:59:11 +0000 | [diff] [blame] | 1399 | assert(isa<MDString>(Val)); |
| 1400 | addString(ParamDIE, dwarf::DW_AT_GNU_template_name, |
| 1401 | cast<MDString>(Val)->getString()); |
Eric Christopher | afb2c41 | 2013-08-08 07:40:31 +0000 | [diff] [blame] | 1402 | } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) { |
David Blaikie | 2b38023 | 2013-06-22 18:59:11 +0000 | [diff] [blame] | 1403 | assert(isa<MDNode>(Val)); |
| 1404 | DIArray A(cast<MDNode>(Val)); |
| 1405 | addTemplateParams(*ParamDIE, A); |
David Blaikie | a1e813d | 2013-05-10 21:52:07 +0000 | [diff] [blame] | 1406 | } |
| 1407 | } |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Devang Patel | 17b5327 | 2011-05-06 16:57:54 +0000 | [diff] [blame] | 1410 | /// getOrCreateNameSpace - Create a DIE for DINameSpace. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1411 | DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) { |
Manman Ren | f6b936b | 2013-10-29 05:49:41 +0000 | [diff] [blame] | 1412 | // Construct the context before querying for the existence of the DIE in case |
| 1413 | // such construction creates the DIE. |
| 1414 | DIE *ContextDIE = getOrCreateContextDIE(NS.getContext()); |
Manman Ren | f6b936b | 2013-10-29 05:49:41 +0000 | [diff] [blame] | 1415 | |
Devang Patel | 17b5327 | 2011-05-06 16:57:54 +0000 | [diff] [blame] | 1416 | DIE *NDie = getDIE(NS); |
| 1417 | if (NDie) |
| 1418 | return NDie; |
Manman Ren | f6b936b | 2013-10-29 05:49:41 +0000 | [diff] [blame] | 1419 | NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS); |
| 1420 | |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1421 | if (!NS.getName().empty()) { |
Nick Lewycky | d59c0ca | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1422 | addString(NDie, dwarf::DW_AT_name, NS.getName()); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1423 | addAccelNamespace(NS.getName(), NDie); |
Eric Christopher | 2c8b790 | 2013-10-17 02:06:06 +0000 | [diff] [blame] | 1424 | addGlobalName(NS.getName(), NDie, NS.getContext()); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1425 | } else |
| 1426 | addAccelNamespace("(anonymous namespace)", NDie); |
Devang Patel | 17b5327 | 2011-05-06 16:57:54 +0000 | [diff] [blame] | 1427 | addSourceLine(NDie, NS); |
Devang Patel | 17b5327 | 2011-05-06 16:57:54 +0000 | [diff] [blame] | 1428 | return NDie; |
| 1429 | } |
| 1430 | |
Adrian Prantl | d1e6a4e | 2014-03-14 23:08:25 +0000 | [diff] [blame] | 1431 | /// Unique C++ member function declarations based on their |
| 1432 | /// context and mangled name. |
| 1433 | DISubprogram |
| 1434 | DwarfUnit::getOdrUniqueSubprogram(DIScope Context, DISubprogram SP) const { |
| 1435 | if (!hasODR() || |
| 1436 | !Context.isCompositeType() || |
| 1437 | SP.getLinkageName().empty() || |
| 1438 | SP.isDefinition()) |
| 1439 | return SP; |
| 1440 | // Create a key with the UID of the parent class and this SP's name. |
| 1441 | Twine Key = SP.getContext().getName() + SP.getLinkageName(); |
| 1442 | const MDNode *&Entry = DD->getOrCreateOdrMember(Key.str()); |
| 1443 | if (!Entry) |
| 1444 | Entry = &*SP; |
| 1445 | |
| 1446 | return DISubprogram(Entry); |
| 1447 | } |
| 1448 | |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1449 | /// getOrCreateSubprogramDIE - Create new DIE using SP. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1450 | DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) { |
David Blaikie | 309ffe4 | 2013-10-04 01:39:59 +0000 | [diff] [blame] | 1451 | // Construct the context before querying for the existence of the DIE in case |
| 1452 | // such construction creates the DIE (as is the case for member function |
| 1453 | // declarations). |
Adrian Prantl | d1e6a4e | 2014-03-14 23:08:25 +0000 | [diff] [blame] | 1454 | DIScope Context = resolve(SP.getContext()); |
| 1455 | DIE *ContextDIE = getOrCreateContextDIE(Context); |
| 1456 | // Unique declarations based on the ODR, where applicable. |
| 1457 | SP = getOdrUniqueSubprogram(Context, SP); |
David Blaikie | 309ffe4 | 2013-10-04 01:39:59 +0000 | [diff] [blame] | 1458 | |
Eric Christopher | e595bae | 2013-10-04 17:08:38 +0000 | [diff] [blame] | 1459 | DIE *SPDie = getDIE(SP); |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1460 | if (SPDie) |
| 1461 | return SPDie; |
| 1462 | |
Manman Ren | 73d697c | 2013-10-29 00:58:04 +0000 | [diff] [blame] | 1463 | DISubprogram SPDecl = SP.getFunctionDeclaration(); |
| 1464 | if (SPDecl.isSubprogram()) |
| 1465 | // Add subprogram definitions to the CU die directly. |
David Blaikie | 2a80e44 | 2013-12-02 22:09:48 +0000 | [diff] [blame] | 1466 | ContextDIE = UnitDie.get(); |
Peter Collingbourne | 4d358b5 | 2012-05-27 18:36:44 +0000 | [diff] [blame] | 1467 | |
| 1468 | // DW_TAG_inlined_subroutine may refer to this DIE. |
Manman Ren | 73d697c | 2013-10-29 00:58:04 +0000 | [diff] [blame] | 1469 | SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP); |
Peter Collingbourne | 4d358b5 | 2012-05-27 18:36:44 +0000 | [diff] [blame] | 1470 | |
Rafael Espindola | 7927836 | 2011-11-10 22:34:29 +0000 | [diff] [blame] | 1471 | DIE *DeclDie = NULL; |
Manman Ren | b9512a7 | 2013-10-23 22:12:26 +0000 | [diff] [blame] | 1472 | if (SPDecl.isSubprogram()) |
Rafael Espindola | 7927836 | 2011-11-10 22:34:29 +0000 | [diff] [blame] | 1473 | DeclDie = getOrCreateSubprogramDIE(SPDecl); |
Rafael Espindola | 7927836 | 2011-11-10 22:34:29 +0000 | [diff] [blame] | 1474 | |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1475 | // Add function template parameters. |
| 1476 | addTemplateParams(*SPDie, SP.getTemplateParams()); |
| 1477 | |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1478 | // If this DIE is going to refer declaration info using AT_specification |
Eric Christopher | f20ff97 | 2013-05-09 00:42:33 +0000 | [diff] [blame] | 1479 | // then there is no need to add other attributes. |
Rafael Espindola | 7927836 | 2011-11-10 22:34:29 +0000 | [diff] [blame] | 1480 | if (DeclDie) { |
| 1481 | // Refer function declaration directly. |
Manman Ren | 4c4b69c | 2013-10-11 23:58:05 +0000 | [diff] [blame] | 1482 | addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie); |
Rafael Espindola | 7927836 | 2011-11-10 22:34:29 +0000 | [diff] [blame] | 1483 | |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1484 | return SPDie; |
Rafael Espindola | 7927836 | 2011-11-10 22:34:29 +0000 | [diff] [blame] | 1485 | } |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1486 | |
Eric Christopher | acb7115 | 2012-08-23 22:52:55 +0000 | [diff] [blame] | 1487 | // Add the linkage name if we have one. |
Michael Gottesman | c89466f | 2013-09-04 04:39:38 +0000 | [diff] [blame] | 1488 | StringRef LinkageName = SP.getLinkageName(); |
| 1489 | if (!LinkageName.empty()) |
Eric Christopher | acb7115 | 2012-08-23 22:52:55 +0000 | [diff] [blame] | 1490 | addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, |
Benjamin Kramer | 7c27564 | 2013-06-01 17:51:14 +0000 | [diff] [blame] | 1491 | GlobalValue::getRealLinkageName(LinkageName)); |
Eric Christopher | acb7115 | 2012-08-23 22:52:55 +0000 | [diff] [blame] | 1492 | |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1493 | // Constructors and operators for anonymous aggregates do not have names. |
| 1494 | if (!SP.getName().empty()) |
Nick Lewycky | d59c0ca | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1495 | addString(SPDie, dwarf::DW_AT_name, SP.getName()); |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1496 | |
| 1497 | addSourceLine(SPDie, SP); |
| 1498 | |
Eric Christopher | 5cd2a9d | 2012-02-22 08:46:21 +0000 | [diff] [blame] | 1499 | // Add the prototype if we have a prototype and we have a C like |
| 1500 | // language. |
David Blaikie | cb8e435 | 2013-11-15 23:50:53 +0000 | [diff] [blame] | 1501 | uint16_t Language = getLanguage(); |
Eric Christopher | 5cd2a9d | 2012-02-22 08:46:21 +0000 | [diff] [blame] | 1502 | if (SP.isPrototyped() && |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1503 | (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || |
Eric Christopher | 5cd2a9d | 2012-02-22 08:46:21 +0000 | [diff] [blame] | 1504 | Language == dwarf::DW_LANG_ObjC)) |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1505 | addFlag(SPDie, dwarf::DW_AT_prototyped); |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1506 | |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1507 | DICompositeType SPTy = SP.getType(); |
David Blaikie | 5174c84 | 2013-05-22 23:22:18 +0000 | [diff] [blame] | 1508 | assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type && |
| 1509 | "the type of a subprogram should be a subroutine"); |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1510 | |
David Blaikie | 5174c84 | 2013-05-22 23:22:18 +0000 | [diff] [blame] | 1511 | DIArray Args = SPTy.getTypeArray(); |
Eric Christopher | 691281b | 2013-10-21 17:48:51 +0000 | [diff] [blame] | 1512 | // Add a return type. If this is a type like a C/C++ void type we don't add a |
| 1513 | // return type. |
Eric Christopher | 0df08e2 | 2013-08-08 07:40:37 +0000 | [diff] [blame] | 1514 | if (Args.getElement(0)) |
| 1515 | addType(SPDie, DIType(Args.getElement(0))); |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1516 | |
| 1517 | unsigned VK = SP.getVirtuality(); |
| 1518 | if (VK) { |
Nick Lewycky | cfde1a2 | 2011-12-14 00:56:07 +0000 | [diff] [blame] | 1519 | addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK); |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1520 | DIELoc *Block = getDIELoc(); |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 1521 | addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); |
| 1522 | addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex()); |
| 1523 | addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block); |
Eric Christopher | 98b7f17 | 2013-11-11 18:52:36 +0000 | [diff] [blame] | 1524 | ContainingTypeMap.insert( |
| 1525 | std::make_pair(SPDie, resolve(SP.getContainingType()))); |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | if (!SP.isDefinition()) { |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1529 | addFlag(SPDie, dwarf::DW_AT_declaration); |
Eric Christopher | 92331fd | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1530 | |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1531 | // Add arguments. Do not add arguments for subprogram definition. They will |
| 1532 | // be handled while processing variables. |
Adrian Prantl | 3f49c89 | 2014-02-25 19:57:42 +0000 | [diff] [blame] | 1533 | constructSubprogramArguments(*SPDie, Args); |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | if (SP.isArtificial()) |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1537 | addFlag(SPDie, dwarf::DW_AT_artificial); |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1538 | |
| 1539 | if (!SP.isLocalToUnit()) |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1540 | addFlag(SPDie, dwarf::DW_AT_external); |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1541 | |
| 1542 | if (SP.isOptimized()) |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1543 | addFlag(SPDie, dwarf::DW_AT_APPLE_optimized); |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1544 | |
| 1545 | if (unsigned isa = Asm->getISAEncoding()) { |
| 1546 | addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa); |
| 1547 | } |
| 1548 | |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 1549 | if (SP.isLValueReference()) |
| 1550 | addFlag(SPDie, dwarf::DW_AT_reference); |
| 1551 | |
| 1552 | if (SP.isRValueReference()) |
| 1553 | addFlag(SPDie, dwarf::DW_AT_rvalue_reference); |
| 1554 | |
Adrian Prantl | ef129fb | 2014-01-18 02:12:00 +0000 | [diff] [blame] | 1555 | if (SP.isProtected()) |
| 1556 | addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
| 1557 | dwarf::DW_ACCESS_protected); |
| 1558 | else if (SP.isPrivate()) |
| 1559 | addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
| 1560 | dwarf::DW_ACCESS_private); |
| 1561 | else |
| 1562 | addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
| 1563 | dwarf::DW_ACCESS_public); |
| 1564 | |
| 1565 | if (SP.isExplicit()) |
| 1566 | addFlag(SPDie, dwarf::DW_AT_explicit); |
| 1567 | |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1568 | return SPDie; |
| 1569 | } |
| 1570 | |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1571 | // Return const expression if value is a GEP to access merged global |
| 1572 | // constant. e.g. |
| 1573 | // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) |
| 1574 | static const ConstantExpr *getMergedGlobalExpr(const Value *V) { |
| 1575 | const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V); |
| 1576 | if (!CE || CE->getNumOperands() != 3 || |
| 1577 | CE->getOpcode() != Instruction::GetElementPtr) |
| 1578 | return NULL; |
| 1579 | |
| 1580 | // First operand points to a global struct. |
| 1581 | Value *Ptr = CE->getOperand(0); |
| 1582 | if (!isa<GlobalValue>(Ptr) || |
| 1583 | !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType())) |
| 1584 | return NULL; |
| 1585 | |
| 1586 | // Second operand is zero. |
| 1587 | const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1)); |
| 1588 | if (!CI || !CI->isZero()) |
| 1589 | return NULL; |
| 1590 | |
| 1591 | // Third operand is offset. |
| 1592 | if (!isa<ConstantInt>(CE->getOperand(2))) |
| 1593 | return NULL; |
| 1594 | |
| 1595 | return CE; |
| 1596 | } |
| 1597 | |
| 1598 | /// createGlobalVariableDIE - create global variable DIE. |
Eric Christopher | 4287a49 | 2013-12-09 23:57:44 +0000 | [diff] [blame] | 1599 | void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) { |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1600 | // Check for pre-existence. |
David Blaikie | 2ad0016 | 2013-11-15 23:09:13 +0000 | [diff] [blame] | 1601 | if (getDIE(GV)) |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1602 | return; |
| 1603 | |
David Blaikie | 5e390e4 | 2014-02-04 01:23:52 +0000 | [diff] [blame] | 1604 | assert(GV.isGlobalVariable()); |
Devang Patel | 0ecbcbd | 2011-08-18 23:17:55 +0000 | [diff] [blame] | 1605 | |
Eric Christopher | 08f7c8f | 2013-10-04 23:49:26 +0000 | [diff] [blame] | 1606 | DIScope GVContext = GV.getContext(); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame^] | 1607 | DIType GTy = DD->resolve(GV.getType()); |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1608 | |
| 1609 | // If this is a static data member definition, some attributes belong |
| 1610 | // to the declaration DIE. |
| 1611 | DIE *VariableDIE = NULL; |
Manman Ren | e697d3c | 2013-02-01 23:54:37 +0000 | [diff] [blame] | 1612 | bool IsStaticMember = false; |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1613 | DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration(); |
| 1614 | if (SDMDecl.Verify()) { |
| 1615 | assert(SDMDecl.isStaticMember() && "Expected static member decl"); |
| 1616 | // We need the declaration DIE that is in the static member's class. |
Manman Ren | c6b6392 | 2013-10-14 20:33:57 +0000 | [diff] [blame] | 1617 | VariableDIE = getOrCreateStaticMemberDIE(SDMDecl); |
Manman Ren | e697d3c | 2013-02-01 23:54:37 +0000 | [diff] [blame] | 1618 | IsStaticMember = true; |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | // If this is not a static data member definition, create the variable |
| 1622 | // DIE and add the initial set of attributes to it. |
| 1623 | if (!VariableDIE) { |
Manman Ren | f6b936b | 2013-10-29 05:49:41 +0000 | [diff] [blame] | 1624 | // Construct the context before querying for the existence of the DIE in |
| 1625 | // case such construction creates the DIE. |
| 1626 | DIE *ContextDIE = getOrCreateContextDIE(GVContext); |
Manman Ren | f6b936b | 2013-10-29 05:49:41 +0000 | [diff] [blame] | 1627 | |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1628 | // Add to map. |
David Blaikie | 52c5020 | 2013-11-16 00:29:01 +0000 | [diff] [blame] | 1629 | VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV); |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1630 | |
| 1631 | // Add name and type. |
| 1632 | addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName()); |
| 1633 | addType(VariableDIE, GTy); |
| 1634 | |
| 1635 | // Add scoping info. |
Eric Christopher | d2b497b | 2013-10-16 01:37:49 +0000 | [diff] [blame] | 1636 | if (!GV.isLocalToUnit()) |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1637 | addFlag(VariableDIE, dwarf::DW_AT_external); |
| 1638 | |
| 1639 | // Add line number info. |
| 1640 | addSourceLine(VariableDIE, GV); |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1643 | // Add location. |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1644 | bool addToAccelTable = false; |
Eric Christopher | 0a917b7 | 2011-11-11 03:16:32 +0000 | [diff] [blame] | 1645 | DIE *VariableSpecDIE = NULL; |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1646 | bool isGlobalVariable = GV.getGlobal() != NULL; |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1647 | if (isGlobalVariable) { |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1648 | addToAccelTable = true; |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1649 | DIELoc *Loc = new (DIEValueAllocator) DIELoc(); |
Rafael Espindola | 79858aa | 2013-10-29 17:07:16 +0000 | [diff] [blame] | 1650 | const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal()); |
David Blaikie | f269497 | 2013-06-28 20:05:11 +0000 | [diff] [blame] | 1651 | if (GV.getGlobal()->isThreadLocal()) { |
| 1652 | // FIXME: Make this work with -gsplit-dwarf. |
| 1653 | unsigned PointerSize = Asm->getDataLayout().getPointerSize(); |
| 1654 | assert((PointerSize == 4 || PointerSize == 8) && |
| 1655 | "Add support for other sizes if necessary"); |
| 1656 | // Based on GCC's support for TLS: |
David Blaikie | 8466ca8 | 2013-07-01 23:55:52 +0000 | [diff] [blame] | 1657 | if (!DD->useSplitDwarf()) { |
| 1658 | // 1) Start with a constNu of the appropriate pointer size |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1659 | addUInt(Loc, dwarf::DW_FORM_data1, |
David Blaikie | 8466ca8 | 2013-07-01 23:55:52 +0000 | [diff] [blame] | 1660 | PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u); |
Richard Mitton | 0aafb58 | 2013-10-07 18:39:18 +0000 | [diff] [blame] | 1661 | // 2) containing the (relocated) offset of the TLS variable |
| 1662 | // within the module's TLS block. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1663 | addExpr(Loc, dwarf::DW_FORM_udata, |
David Blaikie | f1a6dea | 2014-02-15 19:34:03 +0000 | [diff] [blame] | 1664 | Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); |
David Blaikie | 8466ca8 | 2013-07-01 23:55:52 +0000 | [diff] [blame] | 1665 | } else { |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1666 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); |
| 1667 | addUInt(Loc, dwarf::DW_FORM_udata, |
David Blaikie | f1a6dea | 2014-02-15 19:34:03 +0000 | [diff] [blame] | 1668 | DU->getAddrPoolIndex(Sym, /* TLS */ true)); |
David Blaikie | 8466ca8 | 2013-07-01 23:55:52 +0000 | [diff] [blame] | 1669 | } |
Richard Mitton | 0aafb58 | 2013-10-07 18:39:18 +0000 | [diff] [blame] | 1670 | // 3) followed by a custom OP to make the debugger do a TLS lookup. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1671 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address); |
David Blaikie | bc7e0d4 | 2013-11-27 23:53:52 +0000 | [diff] [blame] | 1672 | } else { |
| 1673 | DD->addArangeLabel(SymbolCU(this, Sym)); |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1674 | addOpAddress(Loc, Sym); |
David Blaikie | bc7e0d4 | 2013-11-27 23:53:52 +0000 | [diff] [blame] | 1675 | } |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1676 | // Do not create specification DIE if context is either compile unit |
| 1677 | // or a subprogram. |
Devang Patel | 5e6b65c | 2011-09-21 23:41:11 +0000 | [diff] [blame] | 1678 | if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() && |
Manman Ren | 3eb9dff | 2013-09-09 19:05:21 +0000 | [diff] [blame] | 1679 | !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) { |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1680 | // Create specification DIE. |
David Blaikie | 2a80e44 | 2013-12-02 22:09:48 +0000 | [diff] [blame] | 1681 | VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie); |
Manman Ren | 4c4b69c | 2013-10-11 23:58:05 +0000 | [diff] [blame] | 1682 | addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE); |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1683 | addBlock(VariableSpecDIE, dwarf::DW_AT_location, Loc); |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1684 | // A static member's declaration is already flagged as such. |
| 1685 | if (!SDMDecl.Verify()) |
| 1686 | addFlag(VariableDIE, dwarf::DW_AT_declaration); |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1687 | } else { |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1688 | addBlock(VariableDIE, dwarf::DW_AT_location, Loc); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1689 | } |
Michael Gottesman | c89466f | 2013-09-04 04:39:38 +0000 | [diff] [blame] | 1690 | // Add the linkage name. |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1691 | StringRef LinkageName = GV.getLinkageName(); |
Michael Gottesman | c89466f | 2013-09-04 04:39:38 +0000 | [diff] [blame] | 1692 | if (!LinkageName.empty()) |
Eric Christopher | 3f79b8c | 2013-02-27 23:49:47 +0000 | [diff] [blame] | 1693 | // From DWARF4: DIEs to which DW_AT_linkage_name may apply include: |
| 1694 | // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and |
| 1695 | // TAG_variable. |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1696 | addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE |
| 1697 | : VariableDIE, |
Eric Christopher | af7eca2 | 2014-03-13 23:26:25 +0000 | [diff] [blame] | 1698 | DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name |
| 1699 | : dwarf::DW_AT_MIPS_linkage_name, |
Benjamin Kramer | 7c27564 | 2013-06-01 17:51:14 +0000 | [diff] [blame] | 1700 | GlobalValue::getRealLinkageName(LinkageName)); |
Eric Christopher | 92331fd | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1701 | } else if (const ConstantInt *CI = |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1702 | dyn_cast_or_null<ConstantInt>(GV.getConstant())) { |
Adrian Prantl | 322f41d | 2013-04-04 22:56:49 +0000 | [diff] [blame] | 1703 | // AT_const_value was added when the static member was created. To avoid |
Manman Ren | e697d3c | 2013-02-01 23:54:37 +0000 | [diff] [blame] | 1704 | // emitting AT_const_value multiple times, we only add AT_const_value when |
| 1705 | // it is not a static member. |
| 1706 | if (!IsStaticMember) |
Manman Ren | b338860 | 2013-10-05 01:43:03 +0000 | [diff] [blame] | 1707 | addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy)); |
David Blaikie | a781b25b | 2013-11-17 21:55:13 +0000 | [diff] [blame] | 1708 | } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) { |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1709 | addToAccelTable = true; |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1710 | // GV is a merged global. |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1711 | DIELoc *Loc = new (DIEValueAllocator) DIELoc(); |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1712 | Value *Ptr = CE->getOperand(0); |
David Blaikie | bc7e0d4 | 2013-11-27 23:53:52 +0000 | [diff] [blame] | 1713 | MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr)); |
| 1714 | DD->addArangeLabel(SymbolCU(this, Sym)); |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1715 | addOpAddress(Loc, Sym); |
| 1716 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1717 | SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end()); |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1718 | addUInt(Loc, dwarf::DW_FORM_udata, |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1719 | Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1720 | addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); |
| 1721 | addBlock(VariableDIE, dwarf::DW_AT_location, Loc); |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Eric Christopher | c12c211 | 2011-11-11 01:55:22 +0000 | [diff] [blame] | 1724 | if (addToAccelTable) { |
| 1725 | DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE; |
| 1726 | addAccelName(GV.getName(), AddrDIE); |
Eric Christopher | 4996c70 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 1727 | |
Eric Christopher | c12c211 | 2011-11-11 01:55:22 +0000 | [diff] [blame] | 1728 | // If the linkage name is different than the name, go ahead and output |
| 1729 | // that as well into the name table. |
| 1730 | if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName()) |
| 1731 | addAccelName(GV.getLinkageName(), AddrDIE); |
| 1732 | } |
Eric Christopher | d2b497b | 2013-10-16 01:37:49 +0000 | [diff] [blame] | 1733 | |
| 1734 | if (!GV.isLocalToUnit()) |
Eric Christopher | 2c8b790 | 2013-10-17 02:06:06 +0000 | [diff] [blame] | 1735 | addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE, |
| 1736 | GV.getContext()); |
Devang Patel | dfd6ec3 | 2011-08-15 17:57:41 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1739 | /// constructSubrangeDIE - Construct subrange DIE from DISubrange. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1740 | void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) { |
Manman Ren | b987e51 | 2013-10-29 00:53:03 +0000 | [diff] [blame] | 1741 | DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer); |
Manman Ren | 4c4b69c | 2013-10-11 23:58:05 +0000 | [diff] [blame] | 1742 | addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1743 | |
Bill Wendling | 28fe9e7 | 2012-12-06 07:38:10 +0000 | [diff] [blame] | 1744 | // The LowerBound value defines the lower bounds which is typically zero for |
| 1745 | // C/C++. The Count value is the number of elements. Values are 64 bit. If |
| 1746 | // Count == -1 then the array is unbounded and we do not emit |
| 1747 | // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and |
| 1748 | // Count == 0, then the array has zero elements in which case we do not emit |
| 1749 | // an upper bound. |
| 1750 | int64_t LowerBound = SR.getLo(); |
Bill Wendling | 3495f9b | 2012-12-06 07:55:19 +0000 | [diff] [blame] | 1751 | int64_t DefaultLowerBound = getDefaultLowerBound(); |
Bill Wendling | d776712 | 2012-12-04 21:34:03 +0000 | [diff] [blame] | 1752 | int64_t Count = SR.getCount(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1753 | |
Bill Wendling | 3495f9b | 2012-12-06 07:55:19 +0000 | [diff] [blame] | 1754 | if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound) |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 1755 | addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound); |
Bill Wendling | 28fe9e7 | 2012-12-06 07:38:10 +0000 | [diff] [blame] | 1756 | |
| 1757 | if (Count != -1 && Count != 0) |
Bill Wendling | d776712 | 2012-12-04 21:34:03 +0000 | [diff] [blame] | 1758 | // FIXME: An unbounded array should reference the expression that defines |
| 1759 | // the array. |
Eric Christopher | 98b7f17 | 2013-11-11 18:52:36 +0000 | [diff] [blame] | 1760 | addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None, |
| 1761 | LowerBound + Count - 1); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1765 | void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) { |
Eric Christopher | df9955d | 2013-11-11 18:52:31 +0000 | [diff] [blame] | 1766 | if (CTy.isVector()) |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1767 | addFlag(&Buffer, dwarf::DW_AT_GNU_vector); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1768 | |
Eric Christopher | 0df08e2 | 2013-08-08 07:40:37 +0000 | [diff] [blame] | 1769 | // Emit the element type. |
Eric Christopher | df9955d | 2013-11-11 18:52:31 +0000 | [diff] [blame] | 1770 | addType(&Buffer, resolve(CTy.getTypeDerivedFrom())); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1771 | |
| 1772 | // Get an anonymous type for index type. |
Eric Christopher | cad9b53 | 2013-01-04 21:51:53 +0000 | [diff] [blame] | 1773 | // FIXME: This type should be passed down from the front end |
| 1774 | // as different languages may have different sizes for indexes. |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1775 | DIE *IdxTy = getIndexTyDie(); |
| 1776 | if (!IdxTy) { |
| 1777 | // Construct an anonymous type for index type. |
David Blaikie | 2a80e44 | 2013-12-02 22:09:48 +0000 | [diff] [blame] | 1778 | IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie); |
Eric Christopher | cad9b53 | 2013-01-04 21:51:53 +0000 | [diff] [blame] | 1779 | addString(IdxTy, dwarf::DW_AT_name, "int"); |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 1780 | addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t)); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1781 | addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, |
| 1782 | dwarf::DW_ATE_signed); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1783 | setIndexTyDie(IdxTy); |
| 1784 | } |
| 1785 | |
| 1786 | // Add subranges to array type. |
Eric Christopher | df9955d | 2013-11-11 18:52:31 +0000 | [diff] [blame] | 1787 | DIArray Elements = CTy.getTypeArray(); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1788 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
| 1789 | DIDescriptor Element = Elements.getElement(i); |
| 1790 | if (Element.getTag() == dwarf::DW_TAG_subrange_type) |
| 1791 | constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy); |
| 1792 | } |
| 1793 | } |
| 1794 | |
Eric Christopher | aeb105f | 2013-11-11 18:52:39 +0000 | [diff] [blame] | 1795 | /// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1796 | void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) { |
Eric Christopher | aeb105f | 2013-11-11 18:52:39 +0000 | [diff] [blame] | 1797 | DIArray Elements = CTy.getTypeArray(); |
| 1798 | |
| 1799 | // Add enumerators to enumeration type. |
| 1800 | for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 1801 | DIEnumerator Enum(Elements.getElement(i)); |
Eric Christopher | aeb105f | 2013-11-11 18:52:39 +0000 | [diff] [blame] | 1802 | if (Enum.isEnumerator()) { |
| 1803 | DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer); |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 1804 | StringRef Name = Enum.getName(); |
Eric Christopher | aeb105f | 2013-11-11 18:52:39 +0000 | [diff] [blame] | 1805 | addString(Enumerator, dwarf::DW_AT_name, Name); |
David Blaikie | 4f6bf27a | 2013-11-18 23:33:32 +0000 | [diff] [blame] | 1806 | int64_t Value = Enum.getEnumValue(); |
Eric Christopher | a07e4f5 | 2013-11-19 09:28:34 +0000 | [diff] [blame] | 1807 | addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, |
| 1808 | Value); |
Eric Christopher | aeb105f | 2013-11-11 18:52:39 +0000 | [diff] [blame] | 1809 | } |
| 1810 | } |
| 1811 | DIType DTy = resolve(CTy.getTypeDerivedFrom()); |
| 1812 | if (DTy) { |
| 1813 | addType(&Buffer, DTy); |
| 1814 | addFlag(&Buffer, dwarf::DW_AT_enum_class); |
| 1815 | } |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1818 | /// constructContainingTypeDIEs - Construct DIEs for types that contain |
| 1819 | /// vtables. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1820 | void DwarfUnit::constructContainingTypeDIEs() { |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1821 | for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(), |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1822 | CE = ContainingTypeMap.end(); |
| 1823 | CI != CE; ++CI) { |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1824 | DIE *SPDie = CI->first; |
David Blaikie | 2ad0016 | 2013-11-15 23:09:13 +0000 | [diff] [blame] | 1825 | DIDescriptor D(CI->second); |
| 1826 | if (!D) |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1827 | continue; |
David Blaikie | 2ad0016 | 2013-11-15 23:09:13 +0000 | [diff] [blame] | 1828 | DIE *NDie = getDIE(D); |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1829 | if (!NDie) |
| 1830 | continue; |
Manman Ren | 4c4b69c | 2013-10-11 23:58:05 +0000 | [diff] [blame] | 1831 | addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie); |
Devang Patel | 8954371 | 2011-08-15 17:24:54 +0000 | [diff] [blame] | 1832 | } |
| 1833 | } |
| 1834 | |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1835 | /// constructVariableDIE - Construct a DIE for the given DbgVariable. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1836 | DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) { |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1837 | StringRef Name = DV.getName(); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1838 | |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1839 | // Define variable debug information entry. |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1840 | DIE *VariableDie = new DIE(DV.getTag()); |
| 1841 | DbgVariable *AbsVar = DV.getAbstractVariable(); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1842 | DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL; |
Manman Ren | 4213c39 | 2013-05-29 17:16:59 +0000 | [diff] [blame] | 1843 | if (AbsDIE) |
Manman Ren | 4c4b69c | 2013-10-11 23:58:05 +0000 | [diff] [blame] | 1844 | addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1845 | else { |
David Blaikie | 715528b | 2013-08-19 03:34:03 +0000 | [diff] [blame] | 1846 | if (!Name.empty()) |
| 1847 | addString(VariableDie, dwarf::DW_AT_name, Name); |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1848 | addSourceLine(VariableDie, DV.getVariable()); |
| 1849 | addType(VariableDie, DV.getType()); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1852 | if (DV.isArtificial()) |
Eric Christopher | bb69a27 | 2012-08-24 01:14:27 +0000 | [diff] [blame] | 1853 | addFlag(VariableDie, dwarf::DW_AT_artificial); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1854 | |
| 1855 | if (isScopeAbstract) { |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1856 | DV.setDIE(VariableDie); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1857 | return VariableDie; |
| 1858 | } |
| 1859 | |
| 1860 | // Add variable address. |
| 1861 | |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1862 | unsigned Offset = DV.getDotDebugLocOffset(); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1863 | if (Offset != ~0U) { |
Eric Christopher | a27220f | 2014-03-05 22:41:20 +0000 | [diff] [blame] | 1864 | addLocationList(VariableDie, dwarf::DW_AT_location, Offset); |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1865 | DV.setDIE(VariableDie); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1866 | return VariableDie; |
| 1867 | } |
| 1868 | |
Eric Christopher | cead033 | 2011-10-03 15:49:20 +0000 | [diff] [blame] | 1869 | // Check if variable is described by a DBG_VALUE instruction. |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1870 | if (const MachineInstr *DVInsn = DV.getMInsn()) { |
David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 1871 | assert(DVInsn->getNumOperands() == 3); |
| 1872 | if (DVInsn->getOperand(0).isReg()) { |
| 1873 | const MachineOperand RegOp = DVInsn->getOperand(0); |
Adrian Prantl | 1994288 | 2013-07-09 21:44:06 +0000 | [diff] [blame] | 1874 | // If the second operand is an immediate, this is an indirect value. |
Adrian Prantl | 418d1d1 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 1875 | if (DVInsn->getOperand(1).isImm()) { |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1876 | MachineLocation Location(RegOp.getReg(), |
| 1877 | DVInsn->getOperand(1).getImm()); |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1878 | addVariableAddress(DV, VariableDie, Location); |
David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 1879 | } else if (RegOp.getReg()) |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1880 | addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg())); |
David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 1881 | } else if (DVInsn->getOperand(0).isImm()) |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1882 | addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType()); |
David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 1883 | else if (DVInsn->getOperand(0).isFPImm()) |
Eric Christopher | 78fcf490 | 2013-07-03 01:08:30 +0000 | [diff] [blame] | 1884 | addConstantFPValue(VariableDie, DVInsn->getOperand(0)); |
David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 1885 | else if (DVInsn->getOperand(0).isCImm()) |
Eric Christopher | 78fcf490 | 2013-07-03 01:08:30 +0000 | [diff] [blame] | 1886 | addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(), |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1887 | isUnsignedDIType(DD, DV.getType())); |
Eric Christopher | 78fcf490 | 2013-07-03 01:08:30 +0000 | [diff] [blame] | 1888 | |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1889 | DV.setDIE(VariableDie); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1890 | return VariableDie; |
| 1891 | } else { |
| 1892 | // .. else use frame index. |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1893 | int FI = DV.getFrameIndex(); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1894 | if (FI != ~0) { |
| 1895 | unsigned FrameReg = 0; |
| 1896 | const TargetFrameLowering *TFI = Asm->TM.getFrameLowering(); |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1897 | int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1898 | MachineLocation Location(FrameReg, Offset); |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1899 | addVariableAddress(DV, VariableDie, Location); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | |
Eric Christopher | 34a2c87 | 2013-11-15 01:43:19 +0000 | [diff] [blame] | 1903 | DV.setDIE(VariableDie); |
Devang Patel | 3acc70e | 2011-08-15 22:04:40 +0000 | [diff] [blame] | 1904 | return VariableDie; |
| 1905 | } |
| 1906 | |
Manman Ren | 230ec86 | 2013-10-23 23:00:44 +0000 | [diff] [blame] | 1907 | /// constructMemberDIE - Construct member DIE from DIDerivedType. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1908 | void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) { |
Manman Ren | b987e51 | 2013-10-29 00:53:03 +0000 | [diff] [blame] | 1909 | DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1910 | StringRef Name = DT.getName(); |
| 1911 | if (!Name.empty()) |
Nick Lewycky | d59c0ca | 2011-10-27 06:44:11 +0000 | [diff] [blame] | 1912 | addString(MemberDie, dwarf::DW_AT_name, Name); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1913 | |
Manman Ren | 93b3090 | 2013-10-08 18:42:58 +0000 | [diff] [blame] | 1914 | addType(MemberDie, resolve(DT.getTypeDerivedFrom())); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1915 | |
| 1916 | addSourceLine(MemberDie, DT); |
| 1917 | |
Eric Christopher | c2697f8 | 2013-10-19 01:04:47 +0000 | [diff] [blame] | 1918 | if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) { |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1919 | |
| 1920 | // For C++, virtual base classes are not at fixed offset. Use following |
| 1921 | // expression to extract appropriate offset from vtable. |
| 1922 | // BaseAddr = ObAddr + *((*ObAddr) - Offset) |
| 1923 | |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1924 | DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc(); |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 1925 | addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); |
| 1926 | addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
| 1927 | addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); |
| 1928 | addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits()); |
| 1929 | addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); |
| 1930 | addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); |
| 1931 | addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1932 | |
David Blaikie | f244319 | 2013-10-21 17:28:37 +0000 | [diff] [blame] | 1933 | addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie); |
David Blaikie | 71d34a2 | 2013-11-01 00:25:45 +0000 | [diff] [blame] | 1934 | } else { |
| 1935 | uint64_t Size = DT.getSizeInBits(); |
| 1936 | uint64_t FieldSize = getBaseTypeSize(DD, DT); |
| 1937 | uint64_t OffsetInBytes; |
| 1938 | |
| 1939 | if (Size != FieldSize) { |
Eric Christopher | 1acdbb8 | 2014-03-12 17:14:46 +0000 | [diff] [blame] | 1940 | // Handle bitfield, assume bytes are 8 bits. |
| 1941 | addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8); |
| 1942 | addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size); |
David Blaikie | 71d34a2 | 2013-11-01 00:25:45 +0000 | [diff] [blame] | 1943 | |
| 1944 | uint64_t Offset = DT.getOffsetInBits(); |
| 1945 | uint64_t AlignMask = ~(DT.getAlignInBits() - 1); |
| 1946 | uint64_t HiMark = (Offset + FieldSize) & AlignMask; |
| 1947 | uint64_t FieldOffset = (HiMark - FieldSize); |
| 1948 | Offset -= FieldOffset; |
| 1949 | |
| 1950 | // Maybe we need to work from the other end. |
| 1951 | if (Asm->getDataLayout().isLittleEndian()) |
| 1952 | Offset = FieldSize - (Offset + Size); |
| 1953 | addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset); |
| 1954 | |
Adrian Prantl | c67655a | 2014-01-28 18:13:47 +0000 | [diff] [blame] | 1955 | // Here DW_AT_data_member_location points to the anonymous |
David Blaikie | 71d34a2 | 2013-11-01 00:25:45 +0000 | [diff] [blame] | 1956 | // field that includes this bit field. |
| 1957 | OffsetInBytes = FieldOffset >> 3; |
| 1958 | } else |
| 1959 | // This is not a bitfield. |
| 1960 | OffsetInBytes = DT.getOffsetInBits() >> 3; |
David Blaikie | 2ada116 | 2014-01-03 00:48:38 +0000 | [diff] [blame] | 1961 | |
David Blaikie | 22b29a5 | 2014-01-03 01:30:05 +0000 | [diff] [blame] | 1962 | if (DD->getDwarfVersion() <= 2) { |
Eric Christopher | 4a74104 | 2014-02-16 08:46:55 +0000 | [diff] [blame] | 1963 | DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc(); |
David Blaikie | 22b29a5 | 2014-01-03 01:30:05 +0000 | [diff] [blame] | 1964 | addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); |
| 1965 | addUInt(MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes); |
| 1966 | addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie); |
| 1967 | } else |
| 1968 | addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, |
| 1969 | OffsetInBytes); |
David Blaikie | 71d34a2 | 2013-11-01 00:25:45 +0000 | [diff] [blame] | 1970 | } |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1971 | |
| 1972 | if (DT.isProtected()) |
Nick Lewycky | cb91849 | 2011-12-13 05:09:11 +0000 | [diff] [blame] | 1973 | addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1974 | dwarf::DW_ACCESS_protected); |
| 1975 | else if (DT.isPrivate()) |
Nick Lewycky | cb91849 | 2011-12-13 05:09:11 +0000 | [diff] [blame] | 1976 | addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1977 | dwarf::DW_ACCESS_private); |
| 1978 | // Otherwise C++ member and base classes are considered public. |
Eric Christopher | 92331fd | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1979 | else |
Nick Lewycky | cb91849 | 2011-12-13 05:09:11 +0000 | [diff] [blame] | 1980 | addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1981 | dwarf::DW_ACCESS_public); |
| 1982 | if (DT.isVirtual()) |
Nick Lewycky | cfde1a2 | 2011-12-14 00:56:07 +0000 | [diff] [blame] | 1983 | addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1984 | dwarf::DW_VIRTUALITY_virtual); |
Devang Patel | 514b400 | 2011-04-16 00:11:51 +0000 | [diff] [blame] | 1985 | |
| 1986 | // Objective-C properties. |
Devang Patel | 4488217 | 2012-02-06 17:49:43 +0000 | [diff] [blame] | 1987 | if (MDNode *PNode = DT.getObjCProperty()) |
| 1988 | if (DIEEntry *PropertyDie = getDIEEntry(PNode)) |
Eric Christopher | 92331fd | 2012-11-21 00:34:38 +0000 | [diff] [blame] | 1989 | MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4, |
Devang Patel | 4488217 | 2012-02-06 17:49:43 +0000 | [diff] [blame] | 1990 | PropertyDie); |
| 1991 | |
David Blaikie | 37fefc3 | 2012-12-13 22:43:07 +0000 | [diff] [blame] | 1992 | if (DT.isArtificial()) |
| 1993 | addFlag(MemberDie, dwarf::DW_AT_artificial); |
Devang Patel | 0e821f4 | 2011-04-12 23:21:44 +0000 | [diff] [blame] | 1994 | } |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1995 | |
Manman Ren | c6b6392 | 2013-10-14 20:33:57 +0000 | [diff] [blame] | 1996 | /// getOrCreateStaticMemberDIE - Create new DIE for C++ static member. |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 1997 | DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) { |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 1998 | if (!DT.Verify()) |
| 1999 | return NULL; |
| 2000 | |
Manman Ren | c6b6392 | 2013-10-14 20:33:57 +0000 | [diff] [blame] | 2001 | // Construct the context before querying for the existence of the DIE in case |
| 2002 | // such construction creates the DIE. |
| 2003 | DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext())); |
David Blaikie | bd700e4 | 2013-11-14 21:24:34 +0000 | [diff] [blame] | 2004 | assert(dwarf::isType(ContextDIE->getTag()) && |
| 2005 | "Static member should belong to a type."); |
Manman Ren | c6b6392 | 2013-10-14 20:33:57 +0000 | [diff] [blame] | 2006 | |
| 2007 | DIE *StaticMemberDIE = getDIE(DT); |
| 2008 | if (StaticMemberDIE) |
| 2009 | return StaticMemberDIE; |
| 2010 | |
Manman Ren | b987e51 | 2013-10-29 00:53:03 +0000 | [diff] [blame] | 2011 | StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT); |
Manman Ren | c6b6392 | 2013-10-14 20:33:57 +0000 | [diff] [blame] | 2012 | |
Manman Ren | 93b3090 | 2013-10-08 18:42:58 +0000 | [diff] [blame] | 2013 | DIType Ty = resolve(DT.getTypeDerivedFrom()); |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 2014 | |
| 2015 | addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName()); |
| 2016 | addType(StaticMemberDIE, Ty); |
| 2017 | addSourceLine(StaticMemberDIE, DT); |
| 2018 | addFlag(StaticMemberDIE, dwarf::DW_AT_external); |
| 2019 | addFlag(StaticMemberDIE, dwarf::DW_AT_declaration); |
| 2020 | |
| 2021 | // FIXME: We could omit private if the parent is a class_type, and |
| 2022 | // public if the parent is something else. |
| 2023 | if (DT.isProtected()) |
| 2024 | addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
| 2025 | dwarf::DW_ACCESS_protected); |
| 2026 | else if (DT.isPrivate()) |
| 2027 | addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
| 2028 | dwarf::DW_ACCESS_private); |
| 2029 | else |
| 2030 | addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, |
| 2031 | dwarf::DW_ACCESS_public); |
| 2032 | |
| 2033 | if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant())) |
Manman Ren | b338860 | 2013-10-05 01:43:03 +0000 | [diff] [blame] | 2034 | addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty)); |
David Blaikie | a39a76e | 2013-01-20 01:18:01 +0000 | [diff] [blame] | 2035 | if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant())) |
| 2036 | addConstantFPValue(StaticMemberDIE, CFP); |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 2037 | |
Eric Christopher | 4d23a4a | 2013-01-16 01:22:23 +0000 | [diff] [blame] | 2038 | return StaticMemberDIE; |
| 2039 | } |
David Blaikie | 6b288cf | 2013-10-30 20:42:41 +0000 | [diff] [blame] | 2040 | |
Eric Christopher | a5a7942 | 2013-12-09 23:32:48 +0000 | [diff] [blame] | 2041 | void DwarfUnit::emitHeader(const MCSection *ASection, |
David Blaikie | 3332d4c | 2013-12-11 21:14:02 +0000 | [diff] [blame] | 2042 | const MCSymbol *ASectionSym) const { |
David Blaikie | 6b288cf | 2013-10-30 20:42:41 +0000 | [diff] [blame] | 2043 | Asm->OutStreamer.AddComment("DWARF version number"); |
| 2044 | Asm->EmitInt16(DD->getDwarfVersion()); |
| 2045 | Asm->OutStreamer.AddComment("Offset Into Abbrev. Section"); |
David Blaikie | 6896e19 | 2013-12-04 23:39:02 +0000 | [diff] [blame] | 2046 | // We share one abbreviations table across all units so it's always at the |
| 2047 | // start of the section. Use a relocatable offset where needed to ensure |
| 2048 | // linking doesn't invalidate that offset. |
David Blaikie | 91db9ab | 2013-12-04 18:12:28 +0000 | [diff] [blame] | 2049 | Asm->EmitSectionOffset(ASectionSym, ASectionSym); |
David Blaikie | 6b288cf | 2013-10-30 20:42:41 +0000 | [diff] [blame] | 2050 | Asm->OutStreamer.AddComment("Address Size (in bytes)"); |
| 2051 | Asm->EmitInt8(Asm->getDataLayout().getPointerSize()); |
| 2052 | } |
David Blaikie | bc56327 | 2013-12-13 21:33:40 +0000 | [diff] [blame] | 2053 | |
David Blaikie | 2494fdb | 2014-02-14 22:41:51 +0000 | [diff] [blame] | 2054 | void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) { |
| 2055 | // Define start line table label for each Compile Unit. |
| 2056 | MCSymbol *LineTableStartSym = |
| 2057 | Asm->GetTempSymbol("line_table_start", getUniqueID()); |
| 2058 | Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym, |
| 2059 | getUniqueID()); |
| 2060 | |
| 2061 | // Use a single line table if we are generating assembly. |
| 2062 | bool UseTheFirstCU = |
| 2063 | Asm->OutStreamer.hasRawTextSupport() || (getUniqueID() == 0); |
| 2064 | |
David Blaikie | 60e6386 | 2014-02-14 23:58:13 +0000 | [diff] [blame] | 2065 | stmtListIndex = UnitDie->getValues().size(); |
| 2066 | |
David Blaikie | 2494fdb | 2014-02-14 22:41:51 +0000 | [diff] [blame] | 2067 | // DW_AT_stmt_list is a offset of line number information for this |
| 2068 | // compile unit in debug_line section. For split dwarf this is |
| 2069 | // left in the skeleton CU and so not included. |
| 2070 | // The line table entries are not always emitted in assembly, so it |
| 2071 | // is not okay to use line_table_start here. |
| 2072 | if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) |
| 2073 | addSectionLabel(UnitDie.get(), dwarf::DW_AT_stmt_list, |
| 2074 | UseTheFirstCU ? DwarfLineSectionSym : LineTableStartSym); |
| 2075 | else if (UseTheFirstCU) |
| 2076 | addSectionOffset(UnitDie.get(), dwarf::DW_AT_stmt_list, 0); |
| 2077 | else |
| 2078 | addSectionDelta(UnitDie.get(), dwarf::DW_AT_stmt_list, LineTableStartSym, |
| 2079 | DwarfLineSectionSym); |
| 2080 | } |
| 2081 | |
David Blaikie | 60e6386 | 2014-02-14 23:58:13 +0000 | [diff] [blame] | 2082 | void DwarfCompileUnit::applyStmtList(DIE &D) { |
| 2083 | D.addValue(dwarf::DW_AT_stmt_list, |
| 2084 | UnitDie->getAbbrev().getData()[stmtListIndex].getForm(), |
| 2085 | UnitDie->getValues()[stmtListIndex]); |
| 2086 | } |
| 2087 | |
David Blaikie | bc56327 | 2013-12-13 21:33:40 +0000 | [diff] [blame] | 2088 | void DwarfTypeUnit::emitHeader(const MCSection *ASection, |
| 2089 | const MCSymbol *ASectionSym) const { |
| 2090 | DwarfUnit::emitHeader(ASection, ASectionSym); |
| 2091 | Asm->OutStreamer.AddComment("Type Signature"); |
| 2092 | Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature)); |
| 2093 | Asm->OutStreamer.AddComment("Type DIE Offset"); |
David Blaikie | 15ed5eb | 2014-01-10 01:38:41 +0000 | [diff] [blame] | 2094 | // In a skeleton type unit there is no type DIE so emit a zero offset. |
| 2095 | Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0, |
| 2096 | sizeof(Ty->getOffset())); |
David Blaikie | bc56327 | 2013-12-13 21:33:40 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | void DwarfTypeUnit::initSection(const MCSection *Section) { |
| 2100 | assert(!this->Section); |
| 2101 | this->Section = Section; |
| 2102 | // Since each type unit is contained in its own COMDAT section, the begin |
| 2103 | // label and the section label are the same. Using the begin label emission in |
| 2104 | // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but |
| 2105 | // the only other alternative of lazily constructing start-of-section labels |
| 2106 | // and storing a mapping in DwarfDebug (or AsmPrinter). |
| 2107 | this->SectionSym = this->LabelBegin = |
| 2108 | Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID()); |
| 2109 | this->LabelEnd = |
| 2110 | Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID()); |
| 2111 | this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID()); |
| 2112 | } |