DWARFExpression.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. //===-- DWARFExpression.cpp -----------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
  9. #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
  10. #include "llvm/MC/MCRegisterInfo.h"
  11. #include "llvm/Support/Format.h"
  12. #include <cassert>
  13. #include <cstdint>
  14. #include <vector>
  15. using namespace llvm;
  16. using namespace dwarf;
  17. namespace llvm {
  18. typedef std::vector<DWARFExpression::Operation::Description> DescVector;
  19. static DescVector getDescriptions() {
  20. DescVector Descriptions;
  21. typedef DWARFExpression::Operation Op;
  22. typedef Op::Description Desc;
  23. Descriptions.resize(0xff);
  24. Descriptions[DW_OP_addr] = Desc(Op::Dwarf2, Op::SizeAddr);
  25. Descriptions[DW_OP_deref] = Desc(Op::Dwarf2);
  26. Descriptions[DW_OP_const1u] = Desc(Op::Dwarf2, Op::Size1);
  27. Descriptions[DW_OP_const1s] = Desc(Op::Dwarf2, Op::SignedSize1);
  28. Descriptions[DW_OP_const2u] = Desc(Op::Dwarf2, Op::Size2);
  29. Descriptions[DW_OP_const2s] = Desc(Op::Dwarf2, Op::SignedSize2);
  30. Descriptions[DW_OP_const4u] = Desc(Op::Dwarf2, Op::Size4);
  31. Descriptions[DW_OP_const4s] = Desc(Op::Dwarf2, Op::SignedSize4);
  32. Descriptions[DW_OP_const8u] = Desc(Op::Dwarf2, Op::Size8);
  33. Descriptions[DW_OP_const8s] = Desc(Op::Dwarf2, Op::SignedSize8);
  34. Descriptions[DW_OP_constu] = Desc(Op::Dwarf2, Op::SizeLEB);
  35. Descriptions[DW_OP_consts] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
  36. Descriptions[DW_OP_dup] = Desc(Op::Dwarf2);
  37. Descriptions[DW_OP_drop] = Desc(Op::Dwarf2);
  38. Descriptions[DW_OP_over] = Desc(Op::Dwarf2);
  39. Descriptions[DW_OP_pick] = Desc(Op::Dwarf2, Op::Size1);
  40. Descriptions[DW_OP_swap] = Desc(Op::Dwarf2);
  41. Descriptions[DW_OP_rot] = Desc(Op::Dwarf2);
  42. Descriptions[DW_OP_xderef] = Desc(Op::Dwarf2);
  43. Descriptions[DW_OP_abs] = Desc(Op::Dwarf2);
  44. Descriptions[DW_OP_and] = Desc(Op::Dwarf2);
  45. Descriptions[DW_OP_div] = Desc(Op::Dwarf2);
  46. Descriptions[DW_OP_minus] = Desc(Op::Dwarf2);
  47. Descriptions[DW_OP_mod] = Desc(Op::Dwarf2);
  48. Descriptions[DW_OP_mul] = Desc(Op::Dwarf2);
  49. Descriptions[DW_OP_neg] = Desc(Op::Dwarf2);
  50. Descriptions[DW_OP_not] = Desc(Op::Dwarf2);
  51. Descriptions[DW_OP_or] = Desc(Op::Dwarf2);
  52. Descriptions[DW_OP_plus] = Desc(Op::Dwarf2);
  53. Descriptions[DW_OP_plus_uconst] = Desc(Op::Dwarf2, Op::SizeLEB);
  54. Descriptions[DW_OP_shl] = Desc(Op::Dwarf2);
  55. Descriptions[DW_OP_shr] = Desc(Op::Dwarf2);
  56. Descriptions[DW_OP_shra] = Desc(Op::Dwarf2);
  57. Descriptions[DW_OP_xor] = Desc(Op::Dwarf2);
  58. Descriptions[DW_OP_skip] = Desc(Op::Dwarf2, Op::SignedSize2);
  59. Descriptions[DW_OP_bra] = Desc(Op::Dwarf2, Op::SignedSize2);
  60. Descriptions[DW_OP_eq] = Desc(Op::Dwarf2);
  61. Descriptions[DW_OP_ge] = Desc(Op::Dwarf2);
  62. Descriptions[DW_OP_gt] = Desc(Op::Dwarf2);
  63. Descriptions[DW_OP_le] = Desc(Op::Dwarf2);
  64. Descriptions[DW_OP_lt] = Desc(Op::Dwarf2);
  65. Descriptions[DW_OP_ne] = Desc(Op::Dwarf2);
  66. for (uint16_t LA = DW_OP_lit0; LA <= DW_OP_lit31; ++LA)
  67. Descriptions[LA] = Desc(Op::Dwarf2);
  68. for (uint16_t LA = DW_OP_reg0; LA <= DW_OP_reg31; ++LA)
  69. Descriptions[LA] = Desc(Op::Dwarf2);
  70. for (uint16_t LA = DW_OP_breg0; LA <= DW_OP_breg31; ++LA)
  71. Descriptions[LA] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
  72. Descriptions[DW_OP_regx] = Desc(Op::Dwarf2, Op::SizeLEB);
  73. Descriptions[DW_OP_fbreg] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
  74. Descriptions[DW_OP_bregx] = Desc(Op::Dwarf2, Op::SizeLEB, Op::SignedSizeLEB);
  75. Descriptions[DW_OP_piece] = Desc(Op::Dwarf2, Op::SizeLEB);
  76. Descriptions[DW_OP_deref_size] = Desc(Op::Dwarf2, Op::Size1);
  77. Descriptions[DW_OP_xderef_size] = Desc(Op::Dwarf2, Op::Size1);
  78. Descriptions[DW_OP_nop] = Desc(Op::Dwarf2);
  79. Descriptions[DW_OP_push_object_address] = Desc(Op::Dwarf3);
  80. Descriptions[DW_OP_call2] = Desc(Op::Dwarf3, Op::Size2);
  81. Descriptions[DW_OP_call4] = Desc(Op::Dwarf3, Op::Size4);
  82. Descriptions[DW_OP_call_ref] = Desc(Op::Dwarf3, Op::SizeRefAddr);
  83. Descriptions[DW_OP_form_tls_address] = Desc(Op::Dwarf3);
  84. Descriptions[DW_OP_call_frame_cfa] = Desc(Op::Dwarf3);
  85. Descriptions[DW_OP_bit_piece] = Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeLEB);
  86. Descriptions[DW_OP_implicit_value] =
  87. Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeBlock);
  88. Descriptions[DW_OP_stack_value] = Desc(Op::Dwarf3);
  89. Descriptions[DW_OP_WASM_location] =
  90. Desc(Op::Dwarf4, Op::SizeLEB, Op::WasmLocationArg);
  91. Descriptions[DW_OP_GNU_push_tls_address] = Desc(Op::Dwarf3);
  92. Descriptions[DW_OP_addrx] = Desc(Op::Dwarf4, Op::SizeLEB);
  93. Descriptions[DW_OP_GNU_addr_index] = Desc(Op::Dwarf4, Op::SizeLEB);
  94. Descriptions[DW_OP_GNU_const_index] = Desc(Op::Dwarf4, Op::SizeLEB);
  95. Descriptions[DW_OP_GNU_entry_value] = Desc(Op::Dwarf4, Op::SizeLEB);
  96. Descriptions[DW_OP_convert] = Desc(Op::Dwarf5, Op::BaseTypeRef);
  97. Descriptions[DW_OP_entry_value] = Desc(Op::Dwarf5, Op::SizeLEB);
  98. Descriptions[DW_OP_regval_type] =
  99. Desc(Op::Dwarf5, Op::SizeLEB, Op::BaseTypeRef);
  100. return Descriptions;
  101. }
  102. static DWARFExpression::Operation::Description getOpDesc(unsigned OpCode) {
  103. // FIXME: Make this constexpr once all compilers are smart enough to do it.
  104. static DescVector Descriptions = getDescriptions();
  105. // Handle possible corrupted or unsupported operation.
  106. if (OpCode >= Descriptions.size())
  107. return {};
  108. return Descriptions[OpCode];
  109. }
  110. bool DWARFExpression::Operation::extract(DataExtractor Data,
  111. uint8_t AddressSize, uint64_t Offset,
  112. Optional<DwarfFormat> Format) {
  113. EndOffset = Offset;
  114. Opcode = Data.getU8(&Offset);
  115. Desc = getOpDesc(Opcode);
  116. if (Desc.Version == Operation::DwarfNA)
  117. return false;
  118. for (unsigned Operand = 0; Operand < 2; ++Operand) {
  119. unsigned Size = Desc.Op[Operand];
  120. unsigned Signed = Size & Operation::SignBit;
  121. if (Size == Operation::SizeNA)
  122. break;
  123. switch (Size & ~Operation::SignBit) {
  124. case Operation::Size1:
  125. Operands[Operand] = Data.getU8(&Offset);
  126. if (Signed)
  127. Operands[Operand] = (int8_t)Operands[Operand];
  128. break;
  129. case Operation::Size2:
  130. Operands[Operand] = Data.getU16(&Offset);
  131. if (Signed)
  132. Operands[Operand] = (int16_t)Operands[Operand];
  133. break;
  134. case Operation::Size4:
  135. Operands[Operand] = Data.getU32(&Offset);
  136. if (Signed)
  137. Operands[Operand] = (int32_t)Operands[Operand];
  138. break;
  139. case Operation::Size8:
  140. Operands[Operand] = Data.getU64(&Offset);
  141. break;
  142. case Operation::SizeAddr:
  143. Operands[Operand] = Data.getUnsigned(&Offset, AddressSize);
  144. break;
  145. case Operation::SizeRefAddr:
  146. if (!Format)
  147. return false;
  148. Operands[Operand] =
  149. Data.getUnsigned(&Offset, dwarf::getDwarfOffsetByteSize(*Format));
  150. break;
  151. case Operation::SizeLEB:
  152. if (Signed)
  153. Operands[Operand] = Data.getSLEB128(&Offset);
  154. else
  155. Operands[Operand] = Data.getULEB128(&Offset);
  156. break;
  157. case Operation::BaseTypeRef:
  158. Operands[Operand] = Data.getULEB128(&Offset);
  159. break;
  160. case Operation::WasmLocationArg:
  161. assert(Operand == 1);
  162. switch (Operands[0]) {
  163. case 0:
  164. case 1:
  165. case 2:
  166. case 4:
  167. Operands[Operand] = Data.getULEB128(&Offset);
  168. break;
  169. case 3: // global as uint32
  170. Operands[Operand] = Data.getU32(&Offset);
  171. break;
  172. default:
  173. return false; // Unknown Wasm location
  174. }
  175. break;
  176. case Operation::SizeBlock:
  177. // We need a size, so this cannot be the first operand
  178. if (Operand == 0)
  179. return false;
  180. // Store the offset of the block as the value.
  181. Operands[Operand] = Offset;
  182. Offset += Operands[Operand - 1];
  183. break;
  184. default:
  185. llvm_unreachable("Unknown DWARFExpression Op size");
  186. }
  187. OperandEndOffsets[Operand] = Offset;
  188. }
  189. EndOffset = Offset;
  190. return true;
  191. }
  192. static void prettyPrintBaseTypeRef(DWARFUnit *U, raw_ostream &OS,
  193. DIDumpOptions DumpOpts,
  194. const uint64_t Operands[2],
  195. unsigned Operand) {
  196. assert(Operand < 2 && "operand out of bounds");
  197. auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]);
  198. if (Die && Die.getTag() == dwarf::DW_TAG_base_type) {
  199. OS << " (";
  200. if (DumpOpts.Verbose)
  201. OS << format("0x%08" PRIx64 " -> ", Operands[Operand]);
  202. OS << format("0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]);
  203. if (auto Name = dwarf::toString(Die.find(dwarf::DW_AT_name)))
  204. OS << " \"" << *Name << "\"";
  205. } else {
  206. OS << format(" <invalid base_type ref: 0x%" PRIx64 ">",
  207. Operands[Operand]);
  208. }
  209. }
  210. static bool prettyPrintRegisterOp(DWARFUnit *U, raw_ostream &OS,
  211. DIDumpOptions DumpOpts, uint8_t Opcode,
  212. const uint64_t Operands[2],
  213. const MCRegisterInfo *MRI, bool isEH) {
  214. if (!MRI)
  215. return false;
  216. uint64_t DwarfRegNum;
  217. unsigned OpNum = 0;
  218. if (Opcode == DW_OP_bregx || Opcode == DW_OP_regx ||
  219. Opcode == DW_OP_regval_type)
  220. DwarfRegNum = Operands[OpNum++];
  221. else if (Opcode >= DW_OP_breg0 && Opcode < DW_OP_bregx)
  222. DwarfRegNum = Opcode - DW_OP_breg0;
  223. else
  224. DwarfRegNum = Opcode - DW_OP_reg0;
  225. if (Optional<unsigned> LLVMRegNum = MRI->getLLVMRegNum(DwarfRegNum, isEH)) {
  226. if (const char *RegName = MRI->getName(*LLVMRegNum)) {
  227. if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||
  228. Opcode == DW_OP_bregx)
  229. OS << format(" %s%+" PRId64, RegName, Operands[OpNum]);
  230. else
  231. OS << ' ' << RegName;
  232. if (Opcode == DW_OP_regval_type)
  233. prettyPrintBaseTypeRef(U, OS, DumpOpts, Operands, 1);
  234. return true;
  235. }
  236. }
  237. return false;
  238. }
  239. bool DWARFExpression::Operation::print(raw_ostream &OS, DIDumpOptions DumpOpts,
  240. const DWARFExpression *Expr,
  241. const MCRegisterInfo *RegInfo,
  242. DWARFUnit *U, bool isEH) const {
  243. if (Error) {
  244. OS << "<decoding error>";
  245. return false;
  246. }
  247. StringRef Name = OperationEncodingString(Opcode);
  248. assert(!Name.empty() && "DW_OP has no name!");
  249. OS << Name;
  250. if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||
  251. (Opcode >= DW_OP_reg0 && Opcode <= DW_OP_reg31) ||
  252. Opcode == DW_OP_bregx || Opcode == DW_OP_regx ||
  253. Opcode == DW_OP_regval_type)
  254. if (prettyPrintRegisterOp(U, OS, DumpOpts, Opcode, Operands, RegInfo, isEH))
  255. return true;
  256. for (unsigned Operand = 0; Operand < 2; ++Operand) {
  257. unsigned Size = Desc.Op[Operand];
  258. unsigned Signed = Size & Operation::SignBit;
  259. if (Size == Operation::SizeNA)
  260. break;
  261. if (Size == Operation::BaseTypeRef && U) {
  262. // For DW_OP_convert the operand may be 0 to indicate that conversion to
  263. // the generic type should be done. The same holds for DW_OP_reinterpret,
  264. // which is currently not supported.
  265. if (Opcode == DW_OP_convert && Operands[Operand] == 0)
  266. OS << " 0x0";
  267. else
  268. prettyPrintBaseTypeRef(U, OS, DumpOpts, Operands, Operand);
  269. } else if (Size == Operation::WasmLocationArg) {
  270. assert(Operand == 1);
  271. switch (Operands[0]) {
  272. case 0:
  273. case 1:
  274. case 2:
  275. case 3: // global as uint32
  276. case 4:
  277. OS << format(" 0x%" PRIx64, Operands[Operand]);
  278. break;
  279. default: assert(false);
  280. }
  281. } else if (Size == Operation::SizeBlock) {
  282. uint64_t Offset = Operands[Operand];
  283. for (unsigned i = 0; i < Operands[Operand - 1]; ++i)
  284. OS << format(" 0x%02x", Expr->Data.getU8(&Offset));
  285. } else {
  286. if (Signed)
  287. OS << format(" %+" PRId64, (int64_t)Operands[Operand]);
  288. else if (Opcode != DW_OP_entry_value &&
  289. Opcode != DW_OP_GNU_entry_value)
  290. OS << format(" 0x%" PRIx64, Operands[Operand]);
  291. }
  292. }
  293. return true;
  294. }
  295. void DWARFExpression::print(raw_ostream &OS, DIDumpOptions DumpOpts,
  296. const MCRegisterInfo *RegInfo, DWARFUnit *U,
  297. bool IsEH) const {
  298. uint32_t EntryValExprSize = 0;
  299. uint64_t EntryValStartOffset = 0;
  300. if (Data.getData().empty())
  301. OS << "<empty>";
  302. for (auto &Op : *this) {
  303. if (!Op.print(OS, DumpOpts, this, RegInfo, U, IsEH)) {
  304. uint64_t FailOffset = Op.getEndOffset();
  305. while (FailOffset < Data.getData().size())
  306. OS << format(" %02x", Data.getU8(&FailOffset));
  307. return;
  308. }
  309. if (Op.getCode() == DW_OP_entry_value ||
  310. Op.getCode() == DW_OP_GNU_entry_value) {
  311. OS << "(";
  312. EntryValExprSize = Op.getRawOperand(0);
  313. EntryValStartOffset = Op.getEndOffset();
  314. continue;
  315. }
  316. if (EntryValExprSize) {
  317. EntryValExprSize -= Op.getEndOffset() - EntryValStartOffset;
  318. if (EntryValExprSize == 0)
  319. OS << ")";
  320. }
  321. if (Op.getEndOffset() < Data.getData().size())
  322. OS << ", ";
  323. }
  324. }
  325. bool DWARFExpression::Operation::verify(const Operation &Op, DWARFUnit *U) {
  326. for (unsigned Operand = 0; Operand < 2; ++Operand) {
  327. unsigned Size = Op.Desc.Op[Operand];
  328. if (Size == Operation::SizeNA)
  329. break;
  330. if (Size == Operation::BaseTypeRef) {
  331. // For DW_OP_convert the operand may be 0 to indicate that conversion to
  332. // the generic type should be done, so don't look up a base type in that
  333. // case. The same holds for DW_OP_reinterpret, which is currently not
  334. // supported.
  335. if (Op.Opcode == DW_OP_convert && Op.Operands[Operand] == 0)
  336. continue;
  337. auto Die = U->getDIEForOffset(U->getOffset() + Op.Operands[Operand]);
  338. if (!Die || Die.getTag() != dwarf::DW_TAG_base_type)
  339. return false;
  340. }
  341. }
  342. return true;
  343. }
  344. bool DWARFExpression::verify(DWARFUnit *U) {
  345. for (auto &Op : *this)
  346. if (!Operation::verify(Op, U))
  347. return false;
  348. return true;
  349. }
  350. /// A user-facing string representation of a DWARF expression. This might be an
  351. /// Address expression, in which case it will be implicitly dereferenced, or a
  352. /// Value expression.
  353. struct PrintedExpr {
  354. enum ExprKind {
  355. Address,
  356. Value,
  357. };
  358. ExprKind Kind;
  359. SmallString<16> String;
  360. PrintedExpr(ExprKind K = Address) : Kind(K) {}
  361. };
  362. static bool printCompactDWARFExpr(raw_ostream &OS, DWARFExpression::iterator I,
  363. const DWARFExpression::iterator E,
  364. const MCRegisterInfo &MRI) {
  365. SmallVector<PrintedExpr, 4> Stack;
  366. while (I != E) {
  367. const DWARFExpression::Operation &Op = *I;
  368. uint8_t Opcode = Op.getCode();
  369. switch (Opcode) {
  370. case dwarf::DW_OP_regx: {
  371. // DW_OP_regx: A register, with the register num given as an operand.
  372. // Printed as the plain register name.
  373. uint64_t DwarfRegNum = Op.getRawOperand(0);
  374. Optional<unsigned> LLVMRegNum = MRI.getLLVMRegNum(DwarfRegNum, false);
  375. if (!LLVMRegNum) {
  376. OS << "<unknown register " << DwarfRegNum << ">";
  377. return false;
  378. }
  379. raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);
  380. S << MRI.getName(*LLVMRegNum);
  381. break;
  382. }
  383. case dwarf::DW_OP_bregx: {
  384. int DwarfRegNum = Op.getRawOperand(0);
  385. int64_t Offset = Op.getRawOperand(1);
  386. Optional<unsigned> LLVMRegNum = MRI.getLLVMRegNum(DwarfRegNum, false);
  387. if (!LLVMRegNum) {
  388. OS << "<unknown register " << DwarfRegNum << ">";
  389. return false;
  390. }
  391. raw_svector_ostream S(Stack.emplace_back().String);
  392. S << MRI.getName(*LLVMRegNum);
  393. if (Offset)
  394. S << format("%+" PRId64, Offset);
  395. break;
  396. }
  397. case dwarf::DW_OP_entry_value:
  398. case dwarf::DW_OP_GNU_entry_value: {
  399. // DW_OP_entry_value contains a sub-expression which must be rendered
  400. // separately.
  401. uint64_t SubExprLength = Op.getRawOperand(0);
  402. DWARFExpression::iterator SubExprEnd = I.skipBytes(SubExprLength);
  403. ++I;
  404. raw_svector_ostream S(Stack.emplace_back().String);
  405. S << "entry(";
  406. printCompactDWARFExpr(S, I, SubExprEnd, MRI);
  407. S << ")";
  408. I = SubExprEnd;
  409. continue;
  410. }
  411. case dwarf::DW_OP_stack_value: {
  412. // The top stack entry should be treated as the actual value of tne
  413. // variable, rather than the address of the variable in memory.
  414. assert(!Stack.empty());
  415. Stack.back().Kind = PrintedExpr::Value;
  416. break;
  417. }
  418. default:
  419. if (Opcode >= dwarf::DW_OP_reg0 && Opcode <= dwarf::DW_OP_reg31) {
  420. // DW_OP_reg<N>: A register, with the register num implied by the
  421. // opcode. Printed as the plain register name.
  422. uint64_t DwarfRegNum = Opcode - dwarf::DW_OP_reg0;
  423. Optional<unsigned> LLVMRegNum = MRI.getLLVMRegNum(DwarfRegNum, false);
  424. if (!LLVMRegNum) {
  425. OS << "<unknown register " << DwarfRegNum << ">";
  426. return false;
  427. }
  428. raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);
  429. S << MRI.getName(*LLVMRegNum);
  430. } else if (Opcode >= dwarf::DW_OP_breg0 &&
  431. Opcode <= dwarf::DW_OP_breg31) {
  432. int DwarfRegNum = Opcode - dwarf::DW_OP_breg0;
  433. int64_t Offset = Op.getRawOperand(0);
  434. Optional<unsigned> LLVMRegNum = MRI.getLLVMRegNum(DwarfRegNum, false);
  435. if (!LLVMRegNum) {
  436. OS << "<unknown register " << DwarfRegNum << ">";
  437. return false;
  438. }
  439. raw_svector_ostream S(Stack.emplace_back().String);
  440. S << MRI.getName(*LLVMRegNum);
  441. if (Offset)
  442. S << format("%+" PRId64, Offset);
  443. } else {
  444. // If we hit an unknown operand, we don't know its effect on the stack,
  445. // so bail out on the whole expression.
  446. OS << "<unknown op " << dwarf::OperationEncodingString(Opcode) << " ("
  447. << (int)Opcode << ")>";
  448. return false;
  449. }
  450. break;
  451. }
  452. ++I;
  453. }
  454. assert(Stack.size() == 1 && "expected one value on stack");
  455. if (Stack.front().Kind == PrintedExpr::Address)
  456. OS << "[" << Stack.front().String << "]";
  457. else
  458. OS << Stack.front().String;
  459. return true;
  460. }
  461. bool DWARFExpression::printCompact(raw_ostream &OS, const MCRegisterInfo &MRI) {
  462. return printCompactDWARFExpr(OS, begin(), end(), MRI);
  463. }
  464. bool DWARFExpression::operator==(const DWARFExpression &RHS) const {
  465. if (AddressSize != RHS.AddressSize || Format != RHS.Format)
  466. return false;
  467. return Data.getData() == RHS.Data.getData();
  468. }
  469. } // namespace llvm