2
0

DIE.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Data structures for DWARF info entries.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/DIE.h"
  14. #include "DwarfCompileUnit.h"
  15. #include "DwarfDebug.h"
  16. #include "DwarfUnit.h"
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/CodeGen/AsmPrinter.h"
  19. #include "llvm/IR/DataLayout.h"
  20. #include "llvm/MC/MCAsmInfo.h"
  21. #include "llvm/MC/MCContext.h"
  22. #include "llvm/MC/MCStreamer.h"
  23. #include "llvm/MC/MCSymbol.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include "llvm/Support/Format.h"
  27. #include "llvm/Support/FormattedStream.h"
  28. #include "llvm/Support/LEB128.h"
  29. #include "llvm/Support/MD5.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. using namespace llvm;
  32. //===----------------------------------------------------------------------===//
  33. // DIEAbbrevData Implementation
  34. //===----------------------------------------------------------------------===//
  35. /// Profile - Used to gather unique data for the abbreviation folding set.
  36. ///
  37. void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
  38. // Explicitly cast to an integer type for which FoldingSetNodeID has
  39. // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
  40. ID.AddInteger(unsigned(Attribute));
  41. ID.AddInteger(unsigned(Form));
  42. }
  43. //===----------------------------------------------------------------------===//
  44. // DIEAbbrev Implementation
  45. //===----------------------------------------------------------------------===//
  46. /// Profile - Used to gather unique data for the abbreviation folding set.
  47. ///
  48. void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
  49. ID.AddInteger(unsigned(Tag));
  50. ID.AddInteger(unsigned(Children));
  51. // For each attribute description.
  52. for (unsigned i = 0, N = Data.size(); i < N; ++i)
  53. Data[i].Profile(ID);
  54. }
  55. /// Emit - Print the abbreviation using the specified asm printer.
  56. ///
  57. void DIEAbbrev::Emit(const AsmPrinter *AP) const {
  58. // Emit its Dwarf tag type.
  59. AP->EmitULEB128(Tag, dwarf::TagString(Tag));
  60. // Emit whether it has children DIEs.
  61. AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
  62. // For each attribute description.
  63. for (unsigned i = 0, N = Data.size(); i < N; ++i) {
  64. const DIEAbbrevData &AttrData = Data[i];
  65. // Emit attribute type.
  66. AP->EmitULEB128(AttrData.getAttribute(),
  67. dwarf::AttributeString(AttrData.getAttribute()));
  68. // Emit form type.
  69. AP->EmitULEB128(AttrData.getForm(),
  70. dwarf::FormEncodingString(AttrData.getForm()));
  71. }
  72. // Mark end of abbreviation.
  73. AP->EmitULEB128(0, "EOM(1)");
  74. AP->EmitULEB128(0, "EOM(2)");
  75. }
  76. #ifndef NDEBUG
  77. void DIEAbbrev::print(raw_ostream &O) {
  78. O << "Abbreviation @"
  79. << format("0x%lx", (long)(intptr_t)this)
  80. << " "
  81. << dwarf::TagString(Tag)
  82. << " "
  83. << dwarf::ChildrenString(Children)
  84. << '\n';
  85. for (unsigned i = 0, N = Data.size(); i < N; ++i) {
  86. O << " "
  87. << dwarf::AttributeString(Data[i].getAttribute())
  88. << " "
  89. << dwarf::FormEncodingString(Data[i].getForm())
  90. << '\n';
  91. }
  92. }
  93. void DIEAbbrev::dump() { print(dbgs()); }
  94. #endif
  95. DIEAbbrev DIE::generateAbbrev() const {
  96. DIEAbbrev Abbrev(Tag, hasChildren());
  97. for (const DIEValue &V : Values)
  98. Abbrev.AddAttribute(V.getAttribute(), V.getForm());
  99. return Abbrev;
  100. }
  101. /// Climb up the parent chain to get the unit DIE to which this DIE
  102. /// belongs.
  103. const DIE *DIE::getUnit() const {
  104. const DIE *Cu = getUnitOrNull();
  105. assert(Cu && "We should not have orphaned DIEs.");
  106. return Cu;
  107. }
  108. /// Climb up the parent chain to get the unit DIE this DIE belongs
  109. /// to. Return NULL if DIE is not added to an owner yet.
  110. const DIE *DIE::getUnitOrNull() const {
  111. const DIE *p = this;
  112. while (p) {
  113. if (p->getTag() == dwarf::DW_TAG_compile_unit ||
  114. p->getTag() == dwarf::DW_TAG_type_unit)
  115. return p;
  116. p = p->getParent();
  117. }
  118. return nullptr;
  119. }
  120. DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
  121. // Iterate through all the attributes until we find the one we're
  122. // looking for, if we can't find it return NULL.
  123. for (const auto &V : values())
  124. if (V.getAttribute() == Attribute)
  125. return V;
  126. return DIEValue();
  127. }
  128. #ifndef NDEBUG
  129. void DIE::print(raw_ostream &O, unsigned IndentCount) const {
  130. const std::string Indent(IndentCount, ' ');
  131. bool isBlock = getTag() == 0;
  132. if (!isBlock) {
  133. O << Indent
  134. << "Die: "
  135. << format("0x%lx", (long)(intptr_t)this)
  136. << ", Offset: " << Offset
  137. << ", Size: " << Size << "\n";
  138. O << Indent
  139. << dwarf::TagString(getTag())
  140. << " "
  141. << dwarf::ChildrenString(hasChildren()) << "\n";
  142. } else {
  143. O << "Size: " << Size << "\n";
  144. }
  145. IndentCount += 2;
  146. unsigned I = 0;
  147. for (const auto &V : Values) {
  148. O << Indent;
  149. if (!isBlock)
  150. O << dwarf::AttributeString(V.getAttribute());
  151. else
  152. O << "Blk[" << I++ << "]";
  153. O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
  154. V.print(O);
  155. O << "\n";
  156. }
  157. IndentCount -= 2;
  158. for (const auto &Child : children())
  159. Child.print(O, IndentCount + 4);
  160. if (!isBlock) O << "\n";
  161. }
  162. void DIE::dump() {
  163. print(dbgs());
  164. }
  165. #endif
  166. void DIEValue::EmitValue(const AsmPrinter *AP) const {
  167. switch (Ty) {
  168. case isNone:
  169. llvm_unreachable("Expected valid DIEValue");
  170. #define HANDLE_DIEVALUE(T) \
  171. case is##T: \
  172. getDIE##T().EmitValue(AP, Form); \
  173. break;
  174. #include "llvm/CodeGen/DIEValue.def"
  175. }
  176. }
  177. unsigned DIEValue::SizeOf(const AsmPrinter *AP) const {
  178. switch (Ty) {
  179. case isNone:
  180. llvm_unreachable("Expected valid DIEValue");
  181. #define HANDLE_DIEVALUE(T) \
  182. case is##T: \
  183. return getDIE##T().SizeOf(AP, Form);
  184. #include "llvm/CodeGen/DIEValue.def"
  185. }
  186. llvm_unreachable("Unknown DIE kind");
  187. }
  188. #ifndef NDEBUG
  189. void DIEValue::print(raw_ostream &O) const {
  190. switch (Ty) {
  191. case isNone:
  192. llvm_unreachable("Expected valid DIEValue");
  193. #define HANDLE_DIEVALUE(T) \
  194. case is##T: \
  195. getDIE##T().print(O); \
  196. break;
  197. #include "llvm/CodeGen/DIEValue.def"
  198. }
  199. }
  200. void DIEValue::dump() const {
  201. print(dbgs());
  202. }
  203. #endif
  204. //===----------------------------------------------------------------------===//
  205. // DIEInteger Implementation
  206. //===----------------------------------------------------------------------===//
  207. /// EmitValue - Emit integer of appropriate size.
  208. ///
  209. void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
  210. unsigned Size = ~0U;
  211. switch (Form) {
  212. case dwarf::DW_FORM_flag_present:
  213. // Emit something to keep the lines and comments in sync.
  214. // FIXME: Is there a better way to do this?
  215. Asm->OutStreamer->AddBlankLine();
  216. return;
  217. case dwarf::DW_FORM_flag: // Fall thru
  218. case dwarf::DW_FORM_ref1: // Fall thru
  219. case dwarf::DW_FORM_data1: Size = 1; break;
  220. case dwarf::DW_FORM_ref2: // Fall thru
  221. case dwarf::DW_FORM_data2: Size = 2; break;
  222. case dwarf::DW_FORM_sec_offset: // Fall thru
  223. case dwarf::DW_FORM_strp: // Fall thru
  224. case dwarf::DW_FORM_ref4: // Fall thru
  225. case dwarf::DW_FORM_data4: Size = 4; break;
  226. case dwarf::DW_FORM_ref8: // Fall thru
  227. case dwarf::DW_FORM_ref_sig8: // Fall thru
  228. case dwarf::DW_FORM_data8: Size = 8; break;
  229. case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
  230. case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
  231. case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
  232. case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
  233. case dwarf::DW_FORM_addr:
  234. Size = Asm->getDataLayout().getPointerSize(); break;
  235. case dwarf::DW_FORM_ref_addr:
  236. Size = SizeOf(Asm, dwarf::DW_FORM_ref_addr);
  237. break;
  238. default: llvm_unreachable("DIE Value form not supported yet");
  239. }
  240. Asm->OutStreamer->EmitIntValue(Integer, Size);
  241. }
  242. /// SizeOf - Determine size of integer value in bytes.
  243. ///
  244. unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  245. switch (Form) {
  246. case dwarf::DW_FORM_flag_present: return 0;
  247. case dwarf::DW_FORM_flag: // Fall thru
  248. case dwarf::DW_FORM_ref1: // Fall thru
  249. case dwarf::DW_FORM_data1: return sizeof(int8_t);
  250. case dwarf::DW_FORM_ref2: // Fall thru
  251. case dwarf::DW_FORM_data2: return sizeof(int16_t);
  252. case dwarf::DW_FORM_sec_offset: // Fall thru
  253. case dwarf::DW_FORM_strp: // Fall thru
  254. case dwarf::DW_FORM_ref4: // Fall thru
  255. case dwarf::DW_FORM_data4: return sizeof(int32_t);
  256. case dwarf::DW_FORM_ref8: // Fall thru
  257. case dwarf::DW_FORM_ref_sig8: // Fall thru
  258. case dwarf::DW_FORM_data8: return sizeof(int64_t);
  259. case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
  260. case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
  261. case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
  262. case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
  263. case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
  264. case dwarf::DW_FORM_ref_addr:
  265. if (AP->OutStreamer->getContext().getDwarfVersion() == 2)
  266. return AP->getDataLayout().getPointerSize();
  267. return sizeof(int32_t);
  268. default: llvm_unreachable("DIE Value form not supported yet");
  269. }
  270. }
  271. #ifndef NDEBUG
  272. void DIEInteger::print(raw_ostream &O) const {
  273. O << "Int: " << (int64_t)Integer << " 0x";
  274. O.write_hex(Integer);
  275. }
  276. #endif
  277. //===----------------------------------------------------------------------===//
  278. // DIEExpr Implementation
  279. //===----------------------------------------------------------------------===//
  280. /// EmitValue - Emit expression value.
  281. ///
  282. void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  283. AP->OutStreamer->EmitValue(Expr, SizeOf(AP, Form));
  284. }
  285. /// SizeOf - Determine size of expression value in bytes.
  286. ///
  287. unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  288. if (Form == dwarf::DW_FORM_data4) return 4;
  289. if (Form == dwarf::DW_FORM_sec_offset) return 4;
  290. if (Form == dwarf::DW_FORM_strp) return 4;
  291. return AP->getDataLayout().getPointerSize();
  292. }
  293. #ifndef NDEBUG
  294. void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
  295. #endif
  296. //===----------------------------------------------------------------------===//
  297. // DIELabel Implementation
  298. //===----------------------------------------------------------------------===//
  299. /// EmitValue - Emit label value.
  300. ///
  301. void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  302. AP->EmitLabelReference(Label, SizeOf(AP, Form),
  303. Form == dwarf::DW_FORM_strp ||
  304. Form == dwarf::DW_FORM_sec_offset ||
  305. Form == dwarf::DW_FORM_ref_addr);
  306. }
  307. /// SizeOf - Determine size of label value in bytes.
  308. ///
  309. unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  310. if (Form == dwarf::DW_FORM_data4) return 4;
  311. if (Form == dwarf::DW_FORM_sec_offset) return 4;
  312. if (Form == dwarf::DW_FORM_strp) return 4;
  313. return AP->getDataLayout().getPointerSize();
  314. }
  315. #ifndef NDEBUG
  316. void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
  317. #endif
  318. //===----------------------------------------------------------------------===//
  319. // DIEDelta Implementation
  320. //===----------------------------------------------------------------------===//
  321. /// EmitValue - Emit delta value.
  322. ///
  323. void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  324. AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
  325. }
  326. /// SizeOf - Determine size of delta value in bytes.
  327. ///
  328. unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  329. if (Form == dwarf::DW_FORM_data4) return 4;
  330. if (Form == dwarf::DW_FORM_sec_offset) return 4;
  331. if (Form == dwarf::DW_FORM_strp) return 4;
  332. return AP->getDataLayout().getPointerSize();
  333. }
  334. #ifndef NDEBUG
  335. void DIEDelta::print(raw_ostream &O) const {
  336. O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
  337. }
  338. #endif
  339. //===----------------------------------------------------------------------===//
  340. // DIEString Implementation
  341. //===----------------------------------------------------------------------===//
  342. /// EmitValue - Emit string value.
  343. ///
  344. void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  345. assert(
  346. (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
  347. "Expected valid string form");
  348. // Index of string in symbol table.
  349. if (Form == dwarf::DW_FORM_GNU_str_index) {
  350. DIEInteger(S.getIndex()).EmitValue(AP, Form);
  351. return;
  352. }
  353. // Relocatable symbol.
  354. assert(Form == dwarf::DW_FORM_strp);
  355. if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
  356. DIELabel(S.getSymbol()).EmitValue(AP, Form);
  357. return;
  358. }
  359. // Offset into symbol table.
  360. DIEInteger(S.getOffset()).EmitValue(AP, Form);
  361. }
  362. /// SizeOf - Determine size of delta value in bytes.
  363. ///
  364. unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  365. assert(
  366. (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
  367. "Expected valid string form");
  368. // Index of string in symbol table.
  369. if (Form == dwarf::DW_FORM_GNU_str_index)
  370. return DIEInteger(S.getIndex()).SizeOf(AP, Form);
  371. // Relocatable symbol.
  372. if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
  373. return DIELabel(S.getSymbol()).SizeOf(AP, Form);
  374. // Offset into symbol table.
  375. return DIEInteger(S.getOffset()).SizeOf(AP, Form);
  376. }
  377. #ifndef NDEBUG
  378. void DIEString::print(raw_ostream &O) const {
  379. O << "String: " << S.getString();
  380. }
  381. #endif
  382. //===----------------------------------------------------------------------===//
  383. // DIEEntry Implementation
  384. //===----------------------------------------------------------------------===//
  385. /// EmitValue - Emit debug information entry offset.
  386. ///
  387. void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  388. if (Form == dwarf::DW_FORM_ref_addr) {
  389. const DwarfDebug *DD = AP->getDwarfDebug();
  390. unsigned Addr = Entry->getOffset();
  391. assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
  392. // For DW_FORM_ref_addr, output the offset from beginning of debug info
  393. // section. Entry->getOffset() returns the offset from start of the
  394. // compile unit.
  395. DwarfCompileUnit *CU = DD->lookupUnit(Entry->getUnit());
  396. assert(CU && "CUDie should belong to a CU.");
  397. Addr += CU->getDebugInfoOffset();
  398. if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
  399. AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
  400. DIEEntry::getRefAddrSize(AP));
  401. else
  402. AP->OutStreamer->EmitIntValue(Addr, DIEEntry::getRefAddrSize(AP));
  403. } else
  404. AP->EmitInt32(Entry->getOffset());
  405. }
  406. unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
  407. // DWARF4: References that use the attribute form DW_FORM_ref_addr are
  408. // specified to be four bytes in the DWARF 32-bit format and eight bytes
  409. // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
  410. // references have the same size as an address on the target system.
  411. const DwarfDebug *DD = AP->getDwarfDebug();
  412. assert(DD && "Expected Dwarf Debug info to be available");
  413. if (DD->getDwarfVersion() == 2)
  414. return AP->getDataLayout().getPointerSize();
  415. return sizeof(int32_t);
  416. }
  417. #ifndef NDEBUG
  418. void DIEEntry::print(raw_ostream &O) const {
  419. O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
  420. }
  421. #endif
  422. //===----------------------------------------------------------------------===//
  423. // DIETypeSignature Implementation
  424. //===----------------------------------------------------------------------===//
  425. void DIETypeSignature::EmitValue(const AsmPrinter *Asm,
  426. dwarf::Form Form) const {
  427. assert(Form == dwarf::DW_FORM_ref_sig8);
  428. Asm->OutStreamer->EmitIntValue(Unit->getTypeSignature(), 8);
  429. }
  430. #ifndef NDEBUG
  431. void DIETypeSignature::print(raw_ostream &O) const {
  432. O << format("Type Unit: 0x%lx", Unit->getTypeSignature());
  433. }
  434. #endif
  435. //===----------------------------------------------------------------------===//
  436. // DIELoc Implementation
  437. //===----------------------------------------------------------------------===//
  438. /// ComputeSize - calculate the size of the location expression.
  439. ///
  440. unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
  441. if (!Size) {
  442. for (const auto &V : Values)
  443. Size += V.SizeOf(AP);
  444. }
  445. return Size;
  446. }
  447. /// EmitValue - Emit location data.
  448. ///
  449. void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
  450. switch (Form) {
  451. default: llvm_unreachable("Improper form for block");
  452. case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
  453. case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
  454. case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
  455. case dwarf::DW_FORM_block:
  456. case dwarf::DW_FORM_exprloc:
  457. Asm->EmitULEB128(Size); break;
  458. }
  459. for (const auto &V : Values)
  460. V.EmitValue(Asm);
  461. }
  462. /// SizeOf - Determine size of location data in bytes.
  463. ///
  464. unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  465. switch (Form) {
  466. case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
  467. case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
  468. case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
  469. case dwarf::DW_FORM_block:
  470. case dwarf::DW_FORM_exprloc:
  471. return Size + getULEB128Size(Size);
  472. default: llvm_unreachable("Improper form for block");
  473. }
  474. }
  475. #ifndef NDEBUG
  476. void DIELoc::print(raw_ostream &O) const {
  477. O << "ExprLoc: ";
  478. DIE::print(O, 5);
  479. }
  480. #endif
  481. //===----------------------------------------------------------------------===//
  482. // DIEBlock Implementation
  483. //===----------------------------------------------------------------------===//
  484. /// ComputeSize - calculate the size of the block.
  485. ///
  486. unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
  487. if (!Size) {
  488. for (const auto &V : Values)
  489. Size += V.SizeOf(AP);
  490. }
  491. return Size;
  492. }
  493. /// EmitValue - Emit block data.
  494. ///
  495. void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
  496. switch (Form) {
  497. default: llvm_unreachable("Improper form for block");
  498. case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
  499. case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
  500. case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
  501. case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
  502. }
  503. for (const auto &V : Values)
  504. V.EmitValue(Asm);
  505. }
  506. /// SizeOf - Determine size of block data in bytes.
  507. ///
  508. unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  509. switch (Form) {
  510. case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
  511. case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
  512. case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
  513. case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
  514. default: llvm_unreachable("Improper form for block");
  515. }
  516. }
  517. #ifndef NDEBUG
  518. void DIEBlock::print(raw_ostream &O) const {
  519. O << "Blk: ";
  520. DIE::print(O, 5);
  521. }
  522. #endif
  523. //===----------------------------------------------------------------------===//
  524. // DIELocList Implementation
  525. //===----------------------------------------------------------------------===//
  526. unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
  527. if (Form == dwarf::DW_FORM_data4)
  528. return 4;
  529. if (Form == dwarf::DW_FORM_sec_offset)
  530. return 4;
  531. return AP->getDataLayout().getPointerSize();
  532. }
  533. /// EmitValue - Emit label value.
  534. ///
  535. void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
  536. DwarfDebug *DD = AP->getDwarfDebug();
  537. MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
  538. AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
  539. }
  540. #ifndef NDEBUG
  541. void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }
  542. #endif