DwarfExpression.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. //===- llvm/CodeGen/DwarfExpression.cpp - Dwarf Debug Framework -----------===//
  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. //
  9. // This file contains support for writing dwarf debug info into asm files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "DwarfExpression.h"
  13. #include "DwarfCompileUnit.h"
  14. #include "llvm/ADT/APInt.h"
  15. #include "llvm/ADT/SmallBitVector.h"
  16. #include "llvm/BinaryFormat/Dwarf.h"
  17. #include "llvm/CodeGen/Register.h"
  18. #include "llvm/CodeGen/TargetRegisterInfo.h"
  19. #include "llvm/IR/DataLayout.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include <algorithm>
  22. using namespace llvm;
  23. #define DEBUG_TYPE "dwarfdebug"
  24. void DwarfExpression::emitConstu(uint64_t Value) {
  25. if (Value < 32)
  26. emitOp(dwarf::DW_OP_lit0 + Value);
  27. else if (Value == std::numeric_limits<uint64_t>::max()) {
  28. // Only do this for 64-bit values as the DWARF expression stack uses
  29. // target-address-size values.
  30. emitOp(dwarf::DW_OP_lit0);
  31. emitOp(dwarf::DW_OP_not);
  32. } else {
  33. emitOp(dwarf::DW_OP_constu);
  34. emitUnsigned(Value);
  35. }
  36. }
  37. void DwarfExpression::addReg(int DwarfReg, const char *Comment) {
  38. assert(DwarfReg >= 0 && "invalid negative dwarf register number");
  39. assert((isUnknownLocation() || isRegisterLocation()) &&
  40. "location description already locked down");
  41. LocationKind = Register;
  42. if (DwarfReg < 32) {
  43. emitOp(dwarf::DW_OP_reg0 + DwarfReg, Comment);
  44. } else {
  45. emitOp(dwarf::DW_OP_regx, Comment);
  46. emitUnsigned(DwarfReg);
  47. }
  48. }
  49. void DwarfExpression::addBReg(int DwarfReg, int Offset) {
  50. assert(DwarfReg >= 0 && "invalid negative dwarf register number");
  51. assert(!isRegisterLocation() && "location description already locked down");
  52. if (DwarfReg < 32) {
  53. emitOp(dwarf::DW_OP_breg0 + DwarfReg);
  54. } else {
  55. emitOp(dwarf::DW_OP_bregx);
  56. emitUnsigned(DwarfReg);
  57. }
  58. emitSigned(Offset);
  59. }
  60. void DwarfExpression::addFBReg(int Offset) {
  61. emitOp(dwarf::DW_OP_fbreg);
  62. emitSigned(Offset);
  63. }
  64. void DwarfExpression::addOpPiece(unsigned SizeInBits, unsigned OffsetInBits) {
  65. if (!SizeInBits)
  66. return;
  67. const unsigned SizeOfByte = 8;
  68. if (OffsetInBits > 0 || SizeInBits % SizeOfByte) {
  69. emitOp(dwarf::DW_OP_bit_piece);
  70. emitUnsigned(SizeInBits);
  71. emitUnsigned(OffsetInBits);
  72. } else {
  73. emitOp(dwarf::DW_OP_piece);
  74. unsigned ByteSize = SizeInBits / SizeOfByte;
  75. emitUnsigned(ByteSize);
  76. }
  77. this->OffsetInBits += SizeInBits;
  78. }
  79. void DwarfExpression::addShr(unsigned ShiftBy) {
  80. emitConstu(ShiftBy);
  81. emitOp(dwarf::DW_OP_shr);
  82. }
  83. void DwarfExpression::addAnd(unsigned Mask) {
  84. emitConstu(Mask);
  85. emitOp(dwarf::DW_OP_and);
  86. }
  87. bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
  88. llvm::Register MachineReg,
  89. unsigned MaxSize) {
  90. if (!llvm::Register::isPhysicalRegister(MachineReg)) {
  91. if (isFrameRegister(TRI, MachineReg)) {
  92. DwarfRegs.push_back(Register::createRegister(-1, nullptr));
  93. return true;
  94. }
  95. return false;
  96. }
  97. int Reg = TRI.getDwarfRegNum(MachineReg, false);
  98. // If this is a valid register number, emit it.
  99. if (Reg >= 0) {
  100. DwarfRegs.push_back(Register::createRegister(Reg, nullptr));
  101. return true;
  102. }
  103. // Walk up the super-register chain until we find a valid number.
  104. // For example, EAX on x86_64 is a 32-bit fragment of RAX with offset 0.
  105. for (MCSuperRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
  106. Reg = TRI.getDwarfRegNum(*SR, false);
  107. if (Reg >= 0) {
  108. unsigned Idx = TRI.getSubRegIndex(*SR, MachineReg);
  109. unsigned Size = TRI.getSubRegIdxSize(Idx);
  110. unsigned RegOffset = TRI.getSubRegIdxOffset(Idx);
  111. DwarfRegs.push_back(Register::createRegister(Reg, "super-register"));
  112. // Use a DW_OP_bit_piece to describe the sub-register.
  113. setSubRegisterPiece(Size, RegOffset);
  114. return true;
  115. }
  116. }
  117. // Otherwise, attempt to find a covering set of sub-register numbers.
  118. // For example, Q0 on ARM is a composition of D0+D1.
  119. unsigned CurPos = 0;
  120. // The size of the register in bits.
  121. const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(MachineReg);
  122. unsigned RegSize = TRI.getRegSizeInBits(*RC);
  123. // Keep track of the bits in the register we already emitted, so we
  124. // can avoid emitting redundant aliasing subregs. Because this is
  125. // just doing a greedy scan of all subregisters, it is possible that
  126. // this doesn't find a combination of subregisters that fully cover
  127. // the register (even though one may exist).
  128. SmallBitVector Coverage(RegSize, false);
  129. for (MCSubRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
  130. unsigned Idx = TRI.getSubRegIndex(MachineReg, *SR);
  131. unsigned Size = TRI.getSubRegIdxSize(Idx);
  132. unsigned Offset = TRI.getSubRegIdxOffset(Idx);
  133. Reg = TRI.getDwarfRegNum(*SR, false);
  134. if (Reg < 0)
  135. continue;
  136. // Used to build the intersection between the bits we already
  137. // emitted and the bits covered by this subregister.
  138. SmallBitVector CurSubReg(RegSize, false);
  139. CurSubReg.set(Offset, Offset + Size);
  140. // If this sub-register has a DWARF number and we haven't covered
  141. // its range, and its range covers the value, emit a DWARF piece for it.
  142. if (Offset < MaxSize && CurSubReg.test(Coverage)) {
  143. // Emit a piece for any gap in the coverage.
  144. if (Offset > CurPos)
  145. DwarfRegs.push_back(Register::createSubRegister(
  146. -1, Offset - CurPos, "no DWARF register encoding"));
  147. if (Offset == 0 && Size >= MaxSize)
  148. DwarfRegs.push_back(Register::createRegister(Reg, "sub-register"));
  149. else
  150. DwarfRegs.push_back(Register::createSubRegister(
  151. Reg, std::min<unsigned>(Size, MaxSize - Offset), "sub-register"));
  152. }
  153. // Mark it as emitted.
  154. Coverage.set(Offset, Offset + Size);
  155. CurPos = Offset + Size;
  156. }
  157. // Failed to find any DWARF encoding.
  158. if (CurPos == 0)
  159. return false;
  160. // Found a partial or complete DWARF encoding.
  161. if (CurPos < RegSize)
  162. DwarfRegs.push_back(Register::createSubRegister(
  163. -1, RegSize - CurPos, "no DWARF register encoding"));
  164. return true;
  165. }
  166. void DwarfExpression::addStackValue() {
  167. if (DwarfVersion >= 4)
  168. emitOp(dwarf::DW_OP_stack_value);
  169. }
  170. void DwarfExpression::addSignedConstant(int64_t Value) {
  171. assert(isImplicitLocation() || isUnknownLocation());
  172. LocationKind = Implicit;
  173. emitOp(dwarf::DW_OP_consts);
  174. emitSigned(Value);
  175. }
  176. void DwarfExpression::addUnsignedConstant(uint64_t Value) {
  177. assert(isImplicitLocation() || isUnknownLocation());
  178. LocationKind = Implicit;
  179. emitConstu(Value);
  180. }
  181. void DwarfExpression::addUnsignedConstant(const APInt &Value) {
  182. assert(isImplicitLocation() || isUnknownLocation());
  183. LocationKind = Implicit;
  184. unsigned Size = Value.getBitWidth();
  185. const uint64_t *Data = Value.getRawData();
  186. // Chop it up into 64-bit pieces, because that's the maximum that
  187. // addUnsignedConstant takes.
  188. unsigned Offset = 0;
  189. while (Offset < Size) {
  190. addUnsignedConstant(*Data++);
  191. if (Offset == 0 && Size <= 64)
  192. break;
  193. addStackValue();
  194. addOpPiece(std::min(Size - Offset, 64u), Offset);
  195. Offset += 64;
  196. }
  197. }
  198. void DwarfExpression::addConstantFP(const APFloat &APF, const AsmPrinter &AP) {
  199. assert(isImplicitLocation() || isUnknownLocation());
  200. APInt API = APF.bitcastToAPInt();
  201. int NumBytes = API.getBitWidth() / 8;
  202. if (NumBytes == 4 /*float*/ || NumBytes == 8 /*double*/) {
  203. // FIXME: Add support for `long double`.
  204. emitOp(dwarf::DW_OP_implicit_value);
  205. emitUnsigned(NumBytes /*Size of the block in bytes*/);
  206. // The loop below is emitting the value starting at least significant byte,
  207. // so we need to perform a byte-swap to get the byte order correct in case
  208. // of a big-endian target.
  209. if (AP.getDataLayout().isBigEndian())
  210. API = API.byteSwap();
  211. for (int i = 0; i < NumBytes; ++i) {
  212. emitData1(API.getZExtValue() & 0xFF);
  213. API = API.lshr(8);
  214. }
  215. return;
  216. }
  217. LLVM_DEBUG(
  218. dbgs() << "Skipped DW_OP_implicit_value creation for ConstantFP of size: "
  219. << API.getBitWidth() << " bits\n");
  220. }
  221. bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI,
  222. DIExpressionCursor &ExprCursor,
  223. llvm::Register MachineReg,
  224. unsigned FragmentOffsetInBits) {
  225. auto Fragment = ExprCursor.getFragmentInfo();
  226. if (!addMachineReg(TRI, MachineReg, Fragment ? Fragment->SizeInBits : ~1U)) {
  227. LocationKind = Unknown;
  228. return false;
  229. }
  230. bool HasComplexExpression = false;
  231. auto Op = ExprCursor.peek();
  232. if (Op && Op->getOp() != dwarf::DW_OP_LLVM_fragment)
  233. HasComplexExpression = true;
  234. // If the register can only be described by a complex expression (i.e.,
  235. // multiple subregisters) it doesn't safely compose with another complex
  236. // expression. For example, it is not possible to apply a DW_OP_deref
  237. // operation to multiple DW_OP_pieces, since composite location descriptions
  238. // do not push anything on the DWARF stack.
  239. //
  240. // DW_OP_entry_value operations can only hold a DWARF expression or a
  241. // register location description, so we can't emit a single entry value
  242. // covering a composite location description. In the future we may want to
  243. // emit entry value operations for each register location in the composite
  244. // location, but until that is supported do not emit anything.
  245. if ((HasComplexExpression || IsEmittingEntryValue) && DwarfRegs.size() > 1) {
  246. if (IsEmittingEntryValue)
  247. cancelEntryValue();
  248. DwarfRegs.clear();
  249. LocationKind = Unknown;
  250. return false;
  251. }
  252. // Handle simple register locations. If we are supposed to emit
  253. // a call site parameter expression and if that expression is just a register
  254. // location, emit it with addBReg and offset 0, because we should emit a DWARF
  255. // expression representing a value, rather than a location.
  256. if ((!isParameterValue() && !isMemoryLocation() && !HasComplexExpression) ||
  257. isEntryValue()) {
  258. auto FragmentInfo = ExprCursor.getFragmentInfo();
  259. unsigned RegSize = 0;
  260. for (auto &Reg : DwarfRegs) {
  261. RegSize += Reg.SubRegSize;
  262. if (Reg.DwarfRegNo >= 0)
  263. addReg(Reg.DwarfRegNo, Reg.Comment);
  264. if (FragmentInfo)
  265. if (RegSize > FragmentInfo->SizeInBits)
  266. // If the register is larger than the current fragment stop
  267. // once the fragment is covered.
  268. break;
  269. addOpPiece(Reg.SubRegSize);
  270. }
  271. if (isEntryValue()) {
  272. finalizeEntryValue();
  273. if (!isIndirect() && !isParameterValue() && !HasComplexExpression &&
  274. DwarfVersion >= 4)
  275. emitOp(dwarf::DW_OP_stack_value);
  276. }
  277. DwarfRegs.clear();
  278. // If we need to mask out a subregister, do it now, unless the next
  279. // operation would emit an OpPiece anyway.
  280. auto NextOp = ExprCursor.peek();
  281. if (SubRegisterSizeInBits && NextOp &&
  282. (NextOp->getOp() != dwarf::DW_OP_LLVM_fragment))
  283. maskSubRegister();
  284. return true;
  285. }
  286. // Don't emit locations that cannot be expressed without DW_OP_stack_value.
  287. if (DwarfVersion < 4)
  288. if (any_of(ExprCursor, [](DIExpression::ExprOperand Op) -> bool {
  289. return Op.getOp() == dwarf::DW_OP_stack_value;
  290. })) {
  291. DwarfRegs.clear();
  292. LocationKind = Unknown;
  293. return false;
  294. }
  295. assert(DwarfRegs.size() == 1);
  296. auto Reg = DwarfRegs[0];
  297. bool FBReg = isFrameRegister(TRI, MachineReg);
  298. int SignedOffset = 0;
  299. assert(!Reg.isSubRegister() && "full register expected");
  300. // Pattern-match combinations for which more efficient representations exist.
  301. // [Reg, DW_OP_plus_uconst, Offset] --> [DW_OP_breg, Offset].
  302. if (Op && (Op->getOp() == dwarf::DW_OP_plus_uconst)) {
  303. uint64_t Offset = Op->getArg(0);
  304. uint64_t IntMax = static_cast<uint64_t>(std::numeric_limits<int>::max());
  305. if (Offset <= IntMax) {
  306. SignedOffset = Offset;
  307. ExprCursor.take();
  308. }
  309. }
  310. // [Reg, DW_OP_constu, Offset, DW_OP_plus] --> [DW_OP_breg, Offset]
  311. // [Reg, DW_OP_constu, Offset, DW_OP_minus] --> [DW_OP_breg,-Offset]
  312. // If Reg is a subregister we need to mask it out before subtracting.
  313. if (Op && Op->getOp() == dwarf::DW_OP_constu) {
  314. uint64_t Offset = Op->getArg(0);
  315. uint64_t IntMax = static_cast<uint64_t>(std::numeric_limits<int>::max());
  316. auto N = ExprCursor.peekNext();
  317. if (N && N->getOp() == dwarf::DW_OP_plus && Offset <= IntMax) {
  318. SignedOffset = Offset;
  319. ExprCursor.consume(2);
  320. } else if (N && N->getOp() == dwarf::DW_OP_minus &&
  321. !SubRegisterSizeInBits && Offset <= IntMax + 1) {
  322. SignedOffset = -static_cast<int64_t>(Offset);
  323. ExprCursor.consume(2);
  324. }
  325. }
  326. if (FBReg)
  327. addFBReg(SignedOffset);
  328. else
  329. addBReg(Reg.DwarfRegNo, SignedOffset);
  330. DwarfRegs.clear();
  331. // If we need to mask out a subregister, do it now, unless the next
  332. // operation would emit an OpPiece anyway.
  333. auto NextOp = ExprCursor.peek();
  334. if (SubRegisterSizeInBits && NextOp &&
  335. (NextOp->getOp() != dwarf::DW_OP_LLVM_fragment))
  336. maskSubRegister();
  337. return true;
  338. }
  339. void DwarfExpression::setEntryValueFlags(const MachineLocation &Loc) {
  340. LocationFlags |= EntryValue;
  341. if (Loc.isIndirect())
  342. LocationFlags |= Indirect;
  343. }
  344. void DwarfExpression::setLocation(const MachineLocation &Loc,
  345. const DIExpression *DIExpr) {
  346. if (Loc.isIndirect())
  347. setMemoryLocationKind();
  348. if (DIExpr->isEntryValue())
  349. setEntryValueFlags(Loc);
  350. }
  351. void DwarfExpression::beginEntryValueExpression(
  352. DIExpressionCursor &ExprCursor) {
  353. auto Op = ExprCursor.take();
  354. (void)Op;
  355. assert(Op && Op->getOp() == dwarf::DW_OP_LLVM_entry_value);
  356. assert(!IsEmittingEntryValue && "Already emitting entry value?");
  357. assert(Op->getArg(0) == 1 &&
  358. "Can currently only emit entry values covering a single operation");
  359. SavedLocationKind = LocationKind;
  360. LocationKind = Register;
  361. IsEmittingEntryValue = true;
  362. enableTemporaryBuffer();
  363. }
  364. void DwarfExpression::finalizeEntryValue() {
  365. assert(IsEmittingEntryValue && "Entry value not open?");
  366. disableTemporaryBuffer();
  367. emitOp(CU.getDwarf5OrGNULocationAtom(dwarf::DW_OP_entry_value));
  368. // Emit the entry value's size operand.
  369. unsigned Size = getTemporaryBufferSize();
  370. emitUnsigned(Size);
  371. // Emit the entry value's DWARF block operand.
  372. commitTemporaryBuffer();
  373. LocationFlags &= ~EntryValue;
  374. LocationKind = SavedLocationKind;
  375. IsEmittingEntryValue = false;
  376. }
  377. void DwarfExpression::cancelEntryValue() {
  378. assert(IsEmittingEntryValue && "Entry value not open?");
  379. disableTemporaryBuffer();
  380. // The temporary buffer can't be emptied, so for now just assert that nothing
  381. // has been emitted to it.
  382. assert(getTemporaryBufferSize() == 0 &&
  383. "Began emitting entry value block before cancelling entry value");
  384. LocationKind = SavedLocationKind;
  385. IsEmittingEntryValue = false;
  386. }
  387. unsigned DwarfExpression::getOrCreateBaseType(unsigned BitSize,
  388. dwarf::TypeKind Encoding) {
  389. // Reuse the base_type if we already have one in this CU otherwise we
  390. // create a new one.
  391. unsigned I = 0, E = CU.ExprRefedBaseTypes.size();
  392. for (; I != E; ++I)
  393. if (CU.ExprRefedBaseTypes[I].BitSize == BitSize &&
  394. CU.ExprRefedBaseTypes[I].Encoding == Encoding)
  395. break;
  396. if (I == E)
  397. CU.ExprRefedBaseTypes.emplace_back(BitSize, Encoding);
  398. return I;
  399. }
  400. /// Assuming a well-formed expression, match "DW_OP_deref*
  401. /// DW_OP_LLVM_fragment?".
  402. static bool isMemoryLocation(DIExpressionCursor ExprCursor) {
  403. while (ExprCursor) {
  404. auto Op = ExprCursor.take();
  405. switch (Op->getOp()) {
  406. case dwarf::DW_OP_deref:
  407. case dwarf::DW_OP_LLVM_fragment:
  408. break;
  409. default:
  410. return false;
  411. }
  412. }
  413. return true;
  414. }
  415. void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor) {
  416. addExpression(std::move(ExprCursor),
  417. [](unsigned Idx, DIExpressionCursor &Cursor) -> bool {
  418. llvm_unreachable("unhandled opcode found in expression");
  419. });
  420. }
  421. bool DwarfExpression::addExpression(
  422. DIExpressionCursor &&ExprCursor,
  423. llvm::function_ref<bool(unsigned, DIExpressionCursor &)> InsertArg) {
  424. // Entry values can currently only cover the initial register location,
  425. // and not any other parts of the following DWARF expression.
  426. assert(!IsEmittingEntryValue && "Can't emit entry value around expression");
  427. Optional<DIExpression::ExprOperand> PrevConvertOp = None;
  428. while (ExprCursor) {
  429. auto Op = ExprCursor.take();
  430. uint64_t OpNum = Op->getOp();
  431. if (OpNum >= dwarf::DW_OP_reg0 && OpNum <= dwarf::DW_OP_reg31) {
  432. emitOp(OpNum);
  433. continue;
  434. } else if (OpNum >= dwarf::DW_OP_breg0 && OpNum <= dwarf::DW_OP_breg31) {
  435. addBReg(OpNum - dwarf::DW_OP_breg0, Op->getArg(0));
  436. continue;
  437. }
  438. switch (OpNum) {
  439. case dwarf::DW_OP_LLVM_arg:
  440. if (!InsertArg(Op->getArg(0), ExprCursor)) {
  441. LocationKind = Unknown;
  442. return false;
  443. }
  444. break;
  445. case dwarf::DW_OP_LLVM_fragment: {
  446. unsigned SizeInBits = Op->getArg(1);
  447. unsigned FragmentOffset = Op->getArg(0);
  448. // The fragment offset must have already been adjusted by emitting an
  449. // empty DW_OP_piece / DW_OP_bit_piece before we emitted the base
  450. // location.
  451. assert(OffsetInBits >= FragmentOffset && "fragment offset not added?");
  452. assert(SizeInBits >= OffsetInBits - FragmentOffset && "size underflow");
  453. // If addMachineReg already emitted DW_OP_piece operations to represent
  454. // a super-register by splicing together sub-registers, subtract the size
  455. // of the pieces that was already emitted.
  456. SizeInBits -= OffsetInBits - FragmentOffset;
  457. // If addMachineReg requested a DW_OP_bit_piece to stencil out a
  458. // sub-register that is smaller than the current fragment's size, use it.
  459. if (SubRegisterSizeInBits)
  460. SizeInBits = std::min<unsigned>(SizeInBits, SubRegisterSizeInBits);
  461. // Emit a DW_OP_stack_value for implicit location descriptions.
  462. if (isImplicitLocation())
  463. addStackValue();
  464. // Emit the DW_OP_piece.
  465. addOpPiece(SizeInBits, SubRegisterOffsetInBits);
  466. setSubRegisterPiece(0, 0);
  467. // Reset the location description kind.
  468. LocationKind = Unknown;
  469. return true;
  470. }
  471. case dwarf::DW_OP_plus_uconst:
  472. assert(!isRegisterLocation());
  473. emitOp(dwarf::DW_OP_plus_uconst);
  474. emitUnsigned(Op->getArg(0));
  475. break;
  476. case dwarf::DW_OP_plus:
  477. case dwarf::DW_OP_minus:
  478. case dwarf::DW_OP_mul:
  479. case dwarf::DW_OP_div:
  480. case dwarf::DW_OP_mod:
  481. case dwarf::DW_OP_or:
  482. case dwarf::DW_OP_and:
  483. case dwarf::DW_OP_xor:
  484. case dwarf::DW_OP_shl:
  485. case dwarf::DW_OP_shr:
  486. case dwarf::DW_OP_shra:
  487. case dwarf::DW_OP_lit0:
  488. case dwarf::DW_OP_not:
  489. case dwarf::DW_OP_dup:
  490. case dwarf::DW_OP_push_object_address:
  491. case dwarf::DW_OP_over:
  492. emitOp(OpNum);
  493. break;
  494. case dwarf::DW_OP_deref:
  495. assert(!isRegisterLocation());
  496. if (!isMemoryLocation() && ::isMemoryLocation(ExprCursor))
  497. // Turning this into a memory location description makes the deref
  498. // implicit.
  499. LocationKind = Memory;
  500. else
  501. emitOp(dwarf::DW_OP_deref);
  502. break;
  503. case dwarf::DW_OP_constu:
  504. assert(!isRegisterLocation());
  505. emitConstu(Op->getArg(0));
  506. break;
  507. case dwarf::DW_OP_consts:
  508. assert(!isRegisterLocation());
  509. emitOp(dwarf::DW_OP_consts);
  510. emitSigned(Op->getArg(0));
  511. break;
  512. case dwarf::DW_OP_LLVM_convert: {
  513. unsigned BitSize = Op->getArg(0);
  514. dwarf::TypeKind Encoding = static_cast<dwarf::TypeKind>(Op->getArg(1));
  515. if (DwarfVersion >= 5 && CU.getDwarfDebug().useOpConvert()) {
  516. emitOp(dwarf::DW_OP_convert);
  517. // If targeting a location-list; simply emit the index into the raw
  518. // byte stream as ULEB128, DwarfDebug::emitDebugLocEntry has been
  519. // fitted with means to extract it later.
  520. // If targeting a inlined DW_AT_location; insert a DIEBaseTypeRef
  521. // (containing the index and a resolve mechanism during emit) into the
  522. // DIE value list.
  523. emitBaseTypeRef(getOrCreateBaseType(BitSize, Encoding));
  524. } else {
  525. if (PrevConvertOp && PrevConvertOp->getArg(0) < BitSize) {
  526. if (Encoding == dwarf::DW_ATE_signed)
  527. emitLegacySExt(PrevConvertOp->getArg(0));
  528. else if (Encoding == dwarf::DW_ATE_unsigned)
  529. emitLegacyZExt(PrevConvertOp->getArg(0));
  530. PrevConvertOp = None;
  531. } else {
  532. PrevConvertOp = Op;
  533. }
  534. }
  535. break;
  536. }
  537. case dwarf::DW_OP_stack_value:
  538. LocationKind = Implicit;
  539. break;
  540. case dwarf::DW_OP_swap:
  541. assert(!isRegisterLocation());
  542. emitOp(dwarf::DW_OP_swap);
  543. break;
  544. case dwarf::DW_OP_xderef:
  545. assert(!isRegisterLocation());
  546. emitOp(dwarf::DW_OP_xderef);
  547. break;
  548. case dwarf::DW_OP_deref_size:
  549. emitOp(dwarf::DW_OP_deref_size);
  550. emitData1(Op->getArg(0));
  551. break;
  552. case dwarf::DW_OP_LLVM_tag_offset:
  553. TagOffset = Op->getArg(0);
  554. break;
  555. case dwarf::DW_OP_regx:
  556. emitOp(dwarf::DW_OP_regx);
  557. emitUnsigned(Op->getArg(0));
  558. break;
  559. case dwarf::DW_OP_bregx:
  560. emitOp(dwarf::DW_OP_bregx);
  561. emitUnsigned(Op->getArg(0));
  562. emitSigned(Op->getArg(1));
  563. break;
  564. default:
  565. llvm_unreachable("unhandled opcode found in expression");
  566. }
  567. }
  568. if (isImplicitLocation() && !isParameterValue())
  569. // Turn this into an implicit location description.
  570. addStackValue();
  571. return true;
  572. }
  573. /// add masking operations to stencil out a subregister.
  574. void DwarfExpression::maskSubRegister() {
  575. assert(SubRegisterSizeInBits && "no subregister was registered");
  576. if (SubRegisterOffsetInBits > 0)
  577. addShr(SubRegisterOffsetInBits);
  578. uint64_t Mask = (1ULL << (uint64_t)SubRegisterSizeInBits) - 1ULL;
  579. addAnd(Mask);
  580. }
  581. void DwarfExpression::finalize() {
  582. assert(DwarfRegs.size() == 0 && "dwarf registers not emitted");
  583. // Emit any outstanding DW_OP_piece operations to mask out subregisters.
  584. if (SubRegisterSizeInBits == 0)
  585. return;
  586. // Don't emit a DW_OP_piece for a subregister at offset 0.
  587. if (SubRegisterOffsetInBits == 0)
  588. return;
  589. addOpPiece(SubRegisterSizeInBits, SubRegisterOffsetInBits);
  590. }
  591. void DwarfExpression::addFragmentOffset(const DIExpression *Expr) {
  592. if (!Expr || !Expr->isFragment())
  593. return;
  594. uint64_t FragmentOffset = Expr->getFragmentInfo()->OffsetInBits;
  595. assert(FragmentOffset >= OffsetInBits &&
  596. "overlapping or duplicate fragments");
  597. if (FragmentOffset > OffsetInBits)
  598. addOpPiece(FragmentOffset - OffsetInBits);
  599. OffsetInBits = FragmentOffset;
  600. }
  601. void DwarfExpression::emitLegacySExt(unsigned FromBits) {
  602. // (((X >> (FromBits - 1)) * (~0)) << FromBits) | X
  603. emitOp(dwarf::DW_OP_dup);
  604. emitOp(dwarf::DW_OP_constu);
  605. emitUnsigned(FromBits - 1);
  606. emitOp(dwarf::DW_OP_shr);
  607. emitOp(dwarf::DW_OP_lit0);
  608. emitOp(dwarf::DW_OP_not);
  609. emitOp(dwarf::DW_OP_mul);
  610. emitOp(dwarf::DW_OP_constu);
  611. emitUnsigned(FromBits);
  612. emitOp(dwarf::DW_OP_shl);
  613. emitOp(dwarf::DW_OP_or);
  614. }
  615. void DwarfExpression::emitLegacyZExt(unsigned FromBits) {
  616. // Heuristic to decide the most efficient encoding.
  617. // A ULEB can encode 7 1-bits per byte.
  618. if (FromBits / 7 < 1+1+1+1+1) {
  619. // (X & (1 << FromBits - 1))
  620. emitOp(dwarf::DW_OP_constu);
  621. emitUnsigned((1ULL << FromBits) - 1);
  622. } else {
  623. // Note that the DWARF 4 stack consists of pointer-sized elements,
  624. // so technically it doesn't make sense to shift left more than 64
  625. // bits. We leave that for the consumer to decide though. LLDB for
  626. // example uses APInt for the stack elements and can still deal
  627. // with this.
  628. emitOp(dwarf::DW_OP_lit1);
  629. emitOp(dwarf::DW_OP_constu);
  630. emitUnsigned(FromBits);
  631. emitOp(dwarf::DW_OP_shl);
  632. emitOp(dwarf::DW_OP_lit1);
  633. emitOp(dwarf::DW_OP_minus);
  634. }
  635. emitOp(dwarf::DW_OP_and);
  636. }
  637. void DwarfExpression::addWasmLocation(unsigned Index, uint64_t Offset) {
  638. emitOp(dwarf::DW_OP_WASM_location);
  639. emitUnsigned(Index == 4/*TI_LOCAL_INDIRECT*/ ? 0/*TI_LOCAL*/ : Index);
  640. emitUnsigned(Offset);
  641. if (Index == 4 /*TI_LOCAL_INDIRECT*/) {
  642. assert(LocationKind == Unknown);
  643. LocationKind = Memory;
  644. } else {
  645. assert(LocationKind == Implicit || LocationKind == Unknown);
  646. LocationKind = Implicit;
  647. }
  648. }