10b57cec5SDimitry Andric //===-- DWARFExpression.cpp -----------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
1006c3fb27SDimitry Andric #include "llvm/ADT/SmallString.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
120b57cec5SDimitry Andric #include "llvm/Support/Format.h"
130b57cec5SDimitry Andric #include <cassert>
140b57cec5SDimitry Andric #include <cstdint>
150b57cec5SDimitry Andric #include <vector>
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric using namespace llvm;
180b57cec5SDimitry Andric using namespace dwarf;
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric namespace llvm {
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric typedef DWARFExpression::Operation Op;
230b57cec5SDimitry Andric typedef Op::Description Desc;
240b57cec5SDimitry Andric
getOpDescriptions()2506c3fb27SDimitry Andric static std::vector<Desc> getOpDescriptions() {
2606c3fb27SDimitry Andric std::vector<Desc> Descriptions;
270b57cec5SDimitry Andric Descriptions.resize(0xff);
280b57cec5SDimitry Andric Descriptions[DW_OP_addr] = Desc(Op::Dwarf2, Op::SizeAddr);
290b57cec5SDimitry Andric Descriptions[DW_OP_deref] = Desc(Op::Dwarf2);
300b57cec5SDimitry Andric Descriptions[DW_OP_const1u] = Desc(Op::Dwarf2, Op::Size1);
310b57cec5SDimitry Andric Descriptions[DW_OP_const1s] = Desc(Op::Dwarf2, Op::SignedSize1);
320b57cec5SDimitry Andric Descriptions[DW_OP_const2u] = Desc(Op::Dwarf2, Op::Size2);
330b57cec5SDimitry Andric Descriptions[DW_OP_const2s] = Desc(Op::Dwarf2, Op::SignedSize2);
340b57cec5SDimitry Andric Descriptions[DW_OP_const4u] = Desc(Op::Dwarf2, Op::Size4);
350b57cec5SDimitry Andric Descriptions[DW_OP_const4s] = Desc(Op::Dwarf2, Op::SignedSize4);
360b57cec5SDimitry Andric Descriptions[DW_OP_const8u] = Desc(Op::Dwarf2, Op::Size8);
370b57cec5SDimitry Andric Descriptions[DW_OP_const8s] = Desc(Op::Dwarf2, Op::SignedSize8);
380b57cec5SDimitry Andric Descriptions[DW_OP_constu] = Desc(Op::Dwarf2, Op::SizeLEB);
390b57cec5SDimitry Andric Descriptions[DW_OP_consts] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
400b57cec5SDimitry Andric Descriptions[DW_OP_dup] = Desc(Op::Dwarf2);
410b57cec5SDimitry Andric Descriptions[DW_OP_drop] = Desc(Op::Dwarf2);
420b57cec5SDimitry Andric Descriptions[DW_OP_over] = Desc(Op::Dwarf2);
430b57cec5SDimitry Andric Descriptions[DW_OP_pick] = Desc(Op::Dwarf2, Op::Size1);
440b57cec5SDimitry Andric Descriptions[DW_OP_swap] = Desc(Op::Dwarf2);
450b57cec5SDimitry Andric Descriptions[DW_OP_rot] = Desc(Op::Dwarf2);
460b57cec5SDimitry Andric Descriptions[DW_OP_xderef] = Desc(Op::Dwarf2);
470b57cec5SDimitry Andric Descriptions[DW_OP_abs] = Desc(Op::Dwarf2);
480b57cec5SDimitry Andric Descriptions[DW_OP_and] = Desc(Op::Dwarf2);
490b57cec5SDimitry Andric Descriptions[DW_OP_div] = Desc(Op::Dwarf2);
500b57cec5SDimitry Andric Descriptions[DW_OP_minus] = Desc(Op::Dwarf2);
510b57cec5SDimitry Andric Descriptions[DW_OP_mod] = Desc(Op::Dwarf2);
520b57cec5SDimitry Andric Descriptions[DW_OP_mul] = Desc(Op::Dwarf2);
530b57cec5SDimitry Andric Descriptions[DW_OP_neg] = Desc(Op::Dwarf2);
540b57cec5SDimitry Andric Descriptions[DW_OP_not] = Desc(Op::Dwarf2);
550b57cec5SDimitry Andric Descriptions[DW_OP_or] = Desc(Op::Dwarf2);
560b57cec5SDimitry Andric Descriptions[DW_OP_plus] = Desc(Op::Dwarf2);
570b57cec5SDimitry Andric Descriptions[DW_OP_plus_uconst] = Desc(Op::Dwarf2, Op::SizeLEB);
580b57cec5SDimitry Andric Descriptions[DW_OP_shl] = Desc(Op::Dwarf2);
590b57cec5SDimitry Andric Descriptions[DW_OP_shr] = Desc(Op::Dwarf2);
600b57cec5SDimitry Andric Descriptions[DW_OP_shra] = Desc(Op::Dwarf2);
610b57cec5SDimitry Andric Descriptions[DW_OP_xor] = Desc(Op::Dwarf2);
620b57cec5SDimitry Andric Descriptions[DW_OP_skip] = Desc(Op::Dwarf2, Op::SignedSize2);
630b57cec5SDimitry Andric Descriptions[DW_OP_bra] = Desc(Op::Dwarf2, Op::SignedSize2);
640b57cec5SDimitry Andric Descriptions[DW_OP_eq] = Desc(Op::Dwarf2);
650b57cec5SDimitry Andric Descriptions[DW_OP_ge] = Desc(Op::Dwarf2);
660b57cec5SDimitry Andric Descriptions[DW_OP_gt] = Desc(Op::Dwarf2);
670b57cec5SDimitry Andric Descriptions[DW_OP_le] = Desc(Op::Dwarf2);
680b57cec5SDimitry Andric Descriptions[DW_OP_lt] = Desc(Op::Dwarf2);
690b57cec5SDimitry Andric Descriptions[DW_OP_ne] = Desc(Op::Dwarf2);
700b57cec5SDimitry Andric for (uint16_t LA = DW_OP_lit0; LA <= DW_OP_lit31; ++LA)
710b57cec5SDimitry Andric Descriptions[LA] = Desc(Op::Dwarf2);
720b57cec5SDimitry Andric for (uint16_t LA = DW_OP_reg0; LA <= DW_OP_reg31; ++LA)
730b57cec5SDimitry Andric Descriptions[LA] = Desc(Op::Dwarf2);
740b57cec5SDimitry Andric for (uint16_t LA = DW_OP_breg0; LA <= DW_OP_breg31; ++LA)
750b57cec5SDimitry Andric Descriptions[LA] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
760b57cec5SDimitry Andric Descriptions[DW_OP_regx] = Desc(Op::Dwarf2, Op::SizeLEB);
770b57cec5SDimitry Andric Descriptions[DW_OP_fbreg] = Desc(Op::Dwarf2, Op::SignedSizeLEB);
780b57cec5SDimitry Andric Descriptions[DW_OP_bregx] = Desc(Op::Dwarf2, Op::SizeLEB, Op::SignedSizeLEB);
790b57cec5SDimitry Andric Descriptions[DW_OP_piece] = Desc(Op::Dwarf2, Op::SizeLEB);
800b57cec5SDimitry Andric Descriptions[DW_OP_deref_size] = Desc(Op::Dwarf2, Op::Size1);
810b57cec5SDimitry Andric Descriptions[DW_OP_xderef_size] = Desc(Op::Dwarf2, Op::Size1);
820b57cec5SDimitry Andric Descriptions[DW_OP_nop] = Desc(Op::Dwarf2);
830b57cec5SDimitry Andric Descriptions[DW_OP_push_object_address] = Desc(Op::Dwarf3);
840b57cec5SDimitry Andric Descriptions[DW_OP_call2] = Desc(Op::Dwarf3, Op::Size2);
850b57cec5SDimitry Andric Descriptions[DW_OP_call4] = Desc(Op::Dwarf3, Op::Size4);
860b57cec5SDimitry Andric Descriptions[DW_OP_call_ref] = Desc(Op::Dwarf3, Op::SizeRefAddr);
870b57cec5SDimitry Andric Descriptions[DW_OP_form_tls_address] = Desc(Op::Dwarf3);
880b57cec5SDimitry Andric Descriptions[DW_OP_call_frame_cfa] = Desc(Op::Dwarf3);
890b57cec5SDimitry Andric Descriptions[DW_OP_bit_piece] = Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeLEB);
900b57cec5SDimitry Andric Descriptions[DW_OP_implicit_value] =
910b57cec5SDimitry Andric Desc(Op::Dwarf3, Op::SizeLEB, Op::SizeBlock);
920b57cec5SDimitry Andric Descriptions[DW_OP_stack_value] = Desc(Op::Dwarf3);
93480093f4SDimitry Andric Descriptions[DW_OP_WASM_location] =
945ffd83dbSDimitry Andric Desc(Op::Dwarf4, Op::SizeLEB, Op::WasmLocationArg);
950b57cec5SDimitry Andric Descriptions[DW_OP_GNU_push_tls_address] = Desc(Op::Dwarf3);
960b57cec5SDimitry Andric Descriptions[DW_OP_GNU_addr_index] = Desc(Op::Dwarf4, Op::SizeLEB);
970b57cec5SDimitry Andric Descriptions[DW_OP_GNU_const_index] = Desc(Op::Dwarf4, Op::SizeLEB);
980b57cec5SDimitry Andric Descriptions[DW_OP_GNU_entry_value] = Desc(Op::Dwarf4, Op::SizeLEB);
9906c3fb27SDimitry Andric Descriptions[DW_OP_addrx] = Desc(Op::Dwarf5, Op::SizeLEB);
10006c3fb27SDimitry Andric Descriptions[DW_OP_constx] = Desc(Op::Dwarf5, Op::SizeLEB);
1010b57cec5SDimitry Andric Descriptions[DW_OP_convert] = Desc(Op::Dwarf5, Op::BaseTypeRef);
1020b57cec5SDimitry Andric Descriptions[DW_OP_entry_value] = Desc(Op::Dwarf5, Op::SizeLEB);
1035ffd83dbSDimitry Andric Descriptions[DW_OP_regval_type] =
1045ffd83dbSDimitry Andric Desc(Op::Dwarf5, Op::SizeLEB, Op::BaseTypeRef);
10506c3fb27SDimitry Andric // This Description acts as a marker that getSubOpDesc must be called
10606c3fb27SDimitry Andric // to fetch the final Description for the operation. Each such final
10706c3fb27SDimitry Andric // Description must share the same first SizeSubOpLEB operand.
10806c3fb27SDimitry Andric Descriptions[DW_OP_LLVM_user] = Desc(Op::Dwarf5, Op::SizeSubOpLEB);
1090b57cec5SDimitry Andric return Descriptions;
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric
getDescImpl(ArrayRef<Desc> Descriptions,unsigned Opcode)11206c3fb27SDimitry Andric static Desc getDescImpl(ArrayRef<Desc> Descriptions, unsigned Opcode) {
1130b57cec5SDimitry Andric // Handle possible corrupted or unsupported operation.
11406c3fb27SDimitry Andric if (Opcode >= Descriptions.size())
1150b57cec5SDimitry Andric return {};
11606c3fb27SDimitry Andric return Descriptions[Opcode];
11706c3fb27SDimitry Andric }
11806c3fb27SDimitry Andric
getOpDesc(unsigned Opcode)11906c3fb27SDimitry Andric static Desc getOpDesc(unsigned Opcode) {
12006c3fb27SDimitry Andric static std::vector<Desc> Descriptions = getOpDescriptions();
12106c3fb27SDimitry Andric return getDescImpl(Descriptions, Opcode);
12206c3fb27SDimitry Andric }
12306c3fb27SDimitry Andric
getSubOpDescriptions()12406c3fb27SDimitry Andric static std::vector<Desc> getSubOpDescriptions() {
12506c3fb27SDimitry Andric static constexpr unsigned LlvmUserDescriptionsSize = 1
12606c3fb27SDimitry Andric #define HANDLE_DW_OP_LLVM_USEROP(ID, NAME) +1
12706c3fb27SDimitry Andric #include "llvm/BinaryFormat/Dwarf.def"
12806c3fb27SDimitry Andric ;
12906c3fb27SDimitry Andric std::vector<Desc> Descriptions;
13006c3fb27SDimitry Andric Descriptions.resize(LlvmUserDescriptionsSize);
13106c3fb27SDimitry Andric Descriptions[DW_OP_LLVM_nop] = Desc(Op::Dwarf5, Op::SizeSubOpLEB);
13206c3fb27SDimitry Andric return Descriptions;
13306c3fb27SDimitry Andric }
13406c3fb27SDimitry Andric
getSubOpDesc(unsigned Opcode,unsigned SubOpcode)13506c3fb27SDimitry Andric static Desc getSubOpDesc(unsigned Opcode, unsigned SubOpcode) {
13606c3fb27SDimitry Andric assert(Opcode == DW_OP_LLVM_user);
13706c3fb27SDimitry Andric static std::vector<Desc> Descriptions = getSubOpDescriptions();
13806c3fb27SDimitry Andric return getDescImpl(Descriptions, SubOpcode);
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric
extract(DataExtractor Data,uint8_t AddressSize,uint64_t Offset,std::optional<DwarfFormat> Format)1415ffd83dbSDimitry Andric bool DWARFExpression::Operation::extract(DataExtractor Data,
1425ffd83dbSDimitry Andric uint8_t AddressSize, uint64_t Offset,
143bdd1243dSDimitry Andric std::optional<DwarfFormat> Format) {
1445ffd83dbSDimitry Andric EndOffset = Offset;
1450b57cec5SDimitry Andric Opcode = Data.getU8(&Offset);
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric Desc = getOpDesc(Opcode);
1485ffd83dbSDimitry Andric if (Desc.Version == Operation::DwarfNA)
1490b57cec5SDimitry Andric return false;
1500b57cec5SDimitry Andric
15106c3fb27SDimitry Andric Operands.resize(Desc.Op.size());
15206c3fb27SDimitry Andric OperandEndOffsets.resize(Desc.Op.size());
15306c3fb27SDimitry Andric for (unsigned Operand = 0; Operand < Desc.Op.size(); ++Operand) {
1540b57cec5SDimitry Andric unsigned Size = Desc.Op[Operand];
1550b57cec5SDimitry Andric unsigned Signed = Size & Operation::SignBit;
1560b57cec5SDimitry Andric
1570b57cec5SDimitry Andric switch (Size & ~Operation::SignBit) {
15806c3fb27SDimitry Andric case Operation::SizeSubOpLEB:
15906c3fb27SDimitry Andric assert(Operand == 0 && "SubOp operand must be the first operand");
16006c3fb27SDimitry Andric Operands[Operand] = Data.getULEB128(&Offset);
16106c3fb27SDimitry Andric Desc = getSubOpDesc(Opcode, Operands[Operand]);
16206c3fb27SDimitry Andric if (Desc.Version == Operation::DwarfNA)
16306c3fb27SDimitry Andric return false;
16406c3fb27SDimitry Andric assert(Desc.Op[Operand] == Operation::SizeSubOpLEB &&
16506c3fb27SDimitry Andric "SizeSubOpLEB Description must begin with SizeSubOpLEB operand");
16606c3fb27SDimitry Andric break;
1670b57cec5SDimitry Andric case Operation::Size1:
1680b57cec5SDimitry Andric Operands[Operand] = Data.getU8(&Offset);
1690b57cec5SDimitry Andric if (Signed)
1700b57cec5SDimitry Andric Operands[Operand] = (int8_t)Operands[Operand];
1710b57cec5SDimitry Andric break;
1720b57cec5SDimitry Andric case Operation::Size2:
1730b57cec5SDimitry Andric Operands[Operand] = Data.getU16(&Offset);
1740b57cec5SDimitry Andric if (Signed)
1750b57cec5SDimitry Andric Operands[Operand] = (int16_t)Operands[Operand];
1760b57cec5SDimitry Andric break;
1770b57cec5SDimitry Andric case Operation::Size4:
1780b57cec5SDimitry Andric Operands[Operand] = Data.getU32(&Offset);
1790b57cec5SDimitry Andric if (Signed)
1800b57cec5SDimitry Andric Operands[Operand] = (int32_t)Operands[Operand];
1810b57cec5SDimitry Andric break;
1820b57cec5SDimitry Andric case Operation::Size8:
1830b57cec5SDimitry Andric Operands[Operand] = Data.getU64(&Offset);
1840b57cec5SDimitry Andric break;
1850b57cec5SDimitry Andric case Operation::SizeAddr:
1865ffd83dbSDimitry Andric Operands[Operand] = Data.getUnsigned(&Offset, AddressSize);
1870b57cec5SDimitry Andric break;
1880b57cec5SDimitry Andric case Operation::SizeRefAddr:
1895ffd83dbSDimitry Andric if (!Format)
1905ffd83dbSDimitry Andric return false;
1915ffd83dbSDimitry Andric Operands[Operand] =
1925ffd83dbSDimitry Andric Data.getUnsigned(&Offset, dwarf::getDwarfOffsetByteSize(*Format));
1930b57cec5SDimitry Andric break;
1940b57cec5SDimitry Andric case Operation::SizeLEB:
1950b57cec5SDimitry Andric if (Signed)
1960b57cec5SDimitry Andric Operands[Operand] = Data.getSLEB128(&Offset);
1970b57cec5SDimitry Andric else
1980b57cec5SDimitry Andric Operands[Operand] = Data.getULEB128(&Offset);
1990b57cec5SDimitry Andric break;
2000b57cec5SDimitry Andric case Operation::BaseTypeRef:
2010b57cec5SDimitry Andric Operands[Operand] = Data.getULEB128(&Offset);
2020b57cec5SDimitry Andric break;
2035ffd83dbSDimitry Andric case Operation::WasmLocationArg:
2045ffd83dbSDimitry Andric assert(Operand == 1);
2055ffd83dbSDimitry Andric switch (Operands[0]) {
206fe6060f1SDimitry Andric case 0:
207fe6060f1SDimitry Andric case 1:
208fe6060f1SDimitry Andric case 2:
209fe6060f1SDimitry Andric case 4:
2105ffd83dbSDimitry Andric Operands[Operand] = Data.getULEB128(&Offset);
2115ffd83dbSDimitry Andric break;
2125ffd83dbSDimitry Andric case 3: // global as uint32
2135ffd83dbSDimitry Andric Operands[Operand] = Data.getU32(&Offset);
2145ffd83dbSDimitry Andric break;
2155ffd83dbSDimitry Andric default:
2165ffd83dbSDimitry Andric return false; // Unknown Wasm location
2175ffd83dbSDimitry Andric }
2185ffd83dbSDimitry Andric break;
2190b57cec5SDimitry Andric case Operation::SizeBlock:
2200b57cec5SDimitry Andric // We need a size, so this cannot be the first operand
2210b57cec5SDimitry Andric if (Operand == 0)
2220b57cec5SDimitry Andric return false;
2230b57cec5SDimitry Andric // Store the offset of the block as the value.
2240b57cec5SDimitry Andric Operands[Operand] = Offset;
2250b57cec5SDimitry Andric Offset += Operands[Operand - 1];
2260b57cec5SDimitry Andric break;
2270b57cec5SDimitry Andric default:
2280b57cec5SDimitry Andric llvm_unreachable("Unknown DWARFExpression Op size");
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric
2310b57cec5SDimitry Andric OperandEndOffsets[Operand] = Offset;
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric
2340b57cec5SDimitry Andric EndOffset = Offset;
2350b57cec5SDimitry Andric return true;
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric
prettyPrintBaseTypeRef(DWARFUnit * U,raw_ostream & OS,DIDumpOptions DumpOpts,ArrayRef<uint64_t> Operands,unsigned Operand)2385ffd83dbSDimitry Andric static void prettyPrintBaseTypeRef(DWARFUnit *U, raw_ostream &OS,
239349cc55cSDimitry Andric DIDumpOptions DumpOpts,
24006c3fb27SDimitry Andric ArrayRef<uint64_t> Operands,
241e8d8bef9SDimitry Andric unsigned Operand) {
24206c3fb27SDimitry Andric assert(Operand < Operands.size() && "operand out of bounds");
243*0fca6ea1SDimitry Andric if (!U) {
244*0fca6ea1SDimitry Andric OS << format(" <base_type ref: 0x%" PRIx64 ">", Operands[Operand]);
245*0fca6ea1SDimitry Andric return;
246*0fca6ea1SDimitry Andric }
2475ffd83dbSDimitry Andric auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]);
2485ffd83dbSDimitry Andric if (Die && Die.getTag() == dwarf::DW_TAG_base_type) {
249e8d8bef9SDimitry Andric OS << " (";
250e8d8bef9SDimitry Andric if (DumpOpts.Verbose)
251e8d8bef9SDimitry Andric OS << format("0x%08" PRIx64 " -> ", Operands[Operand]);
252e8d8bef9SDimitry Andric OS << format("0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]);
2530eae32dcSDimitry Andric if (auto Name = dwarf::toString(Die.find(dwarf::DW_AT_name)))
2540eae32dcSDimitry Andric OS << " \"" << *Name << "\"";
2555ffd83dbSDimitry Andric } else {
256*0fca6ea1SDimitry Andric OS << format(" <invalid base_type ref: 0x%" PRIx64 ">", Operands[Operand]);
2575ffd83dbSDimitry Andric }
2585ffd83dbSDimitry Andric }
2595ffd83dbSDimitry Andric
prettyPrintRegisterOp(DWARFUnit * U,raw_ostream & OS,DIDumpOptions DumpOpts,uint8_t Opcode,ArrayRef<uint64_t> Operands)260bdd1243dSDimitry Andric bool DWARFExpression::prettyPrintRegisterOp(DWARFUnit *U, raw_ostream &OS,
261bdd1243dSDimitry Andric DIDumpOptions DumpOpts,
262bdd1243dSDimitry Andric uint8_t Opcode,
26306c3fb27SDimitry Andric ArrayRef<uint64_t> Operands) {
264bdd1243dSDimitry Andric if (!DumpOpts.GetNameForDWARFReg)
2650b57cec5SDimitry Andric return false;
2660b57cec5SDimitry Andric
2670b57cec5SDimitry Andric uint64_t DwarfRegNum;
2680b57cec5SDimitry Andric unsigned OpNum = 0;
2690b57cec5SDimitry Andric
2705ffd83dbSDimitry Andric if (Opcode == DW_OP_bregx || Opcode == DW_OP_regx ||
2715ffd83dbSDimitry Andric Opcode == DW_OP_regval_type)
2720b57cec5SDimitry Andric DwarfRegNum = Operands[OpNum++];
2730b57cec5SDimitry Andric else if (Opcode >= DW_OP_breg0 && Opcode < DW_OP_bregx)
2740b57cec5SDimitry Andric DwarfRegNum = Opcode - DW_OP_breg0;
2750b57cec5SDimitry Andric else
2760b57cec5SDimitry Andric DwarfRegNum = Opcode - DW_OP_reg0;
2770b57cec5SDimitry Andric
278bdd1243dSDimitry Andric auto RegName = DumpOpts.GetNameForDWARFReg(DwarfRegNum, DumpOpts.IsEH);
279bdd1243dSDimitry Andric if (!RegName.empty()) {
2800b57cec5SDimitry Andric if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||
2810b57cec5SDimitry Andric Opcode == DW_OP_bregx)
282bdd1243dSDimitry Andric OS << ' ' << RegName << format("%+" PRId64, Operands[OpNum]);
2830b57cec5SDimitry Andric else
284bdd1243dSDimitry Andric OS << ' ' << RegName.data();
2855ffd83dbSDimitry Andric
2865ffd83dbSDimitry Andric if (Opcode == DW_OP_regval_type)
287e8d8bef9SDimitry Andric prettyPrintBaseTypeRef(U, OS, DumpOpts, Operands, 1);
2880b57cec5SDimitry Andric return true;
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric
2910b57cec5SDimitry Andric return false;
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric
getSubCode() const29406c3fb27SDimitry Andric std::optional<unsigned> DWARFExpression::Operation::getSubCode() const {
29506c3fb27SDimitry Andric if (!Desc.Op.size() || Desc.Op[0] != Operation::SizeSubOpLEB)
29606c3fb27SDimitry Andric return std::nullopt;
29706c3fb27SDimitry Andric return Operands[0];
29806c3fb27SDimitry Andric }
29906c3fb27SDimitry Andric
print(raw_ostream & OS,DIDumpOptions DumpOpts,const DWARFExpression * Expr,DWARFUnit * U) const300e8d8bef9SDimitry Andric bool DWARFExpression::Operation::print(raw_ostream &OS, DIDumpOptions DumpOpts,
3010b57cec5SDimitry Andric const DWARFExpression *Expr,
302bdd1243dSDimitry Andric DWARFUnit *U) const {
3030b57cec5SDimitry Andric if (Error) {
3040b57cec5SDimitry Andric OS << "<decoding error>";
3050b57cec5SDimitry Andric return false;
3060b57cec5SDimitry Andric }
3070b57cec5SDimitry Andric
3080b57cec5SDimitry Andric StringRef Name = OperationEncodingString(Opcode);
3090b57cec5SDimitry Andric assert(!Name.empty() && "DW_OP has no name!");
3100b57cec5SDimitry Andric OS << Name;
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andric if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||
3130b57cec5SDimitry Andric (Opcode >= DW_OP_reg0 && Opcode <= DW_OP_reg31) ||
3145ffd83dbSDimitry Andric Opcode == DW_OP_bregx || Opcode == DW_OP_regx ||
3155ffd83dbSDimitry Andric Opcode == DW_OP_regval_type)
316bdd1243dSDimitry Andric if (prettyPrintRegisterOp(U, OS, DumpOpts, Opcode, Operands))
3170b57cec5SDimitry Andric return true;
3180b57cec5SDimitry Andric
31906c3fb27SDimitry Andric for (unsigned Operand = 0; Operand < Desc.Op.size(); ++Operand) {
3200b57cec5SDimitry Andric unsigned Size = Desc.Op[Operand];
3210b57cec5SDimitry Andric unsigned Signed = Size & Operation::SignBit;
3220b57cec5SDimitry Andric
32306c3fb27SDimitry Andric if (Size == Operation::SizeSubOpLEB) {
32406c3fb27SDimitry Andric StringRef SubName = SubOperationEncodingString(Opcode, Operands[Operand]);
32506c3fb27SDimitry Andric assert(!SubName.empty() && "DW_OP SubOp has no name!");
32606c3fb27SDimitry Andric OS << " " << SubName;
32706c3fb27SDimitry Andric } else if (Size == Operation::BaseTypeRef && U) {
3285ffd83dbSDimitry Andric // For DW_OP_convert the operand may be 0 to indicate that conversion to
3295ffd83dbSDimitry Andric // the generic type should be done. The same holds for DW_OP_reinterpret,
3305ffd83dbSDimitry Andric // which is currently not supported.
3315ffd83dbSDimitry Andric if (Opcode == DW_OP_convert && Operands[Operand] == 0)
3325ffd83dbSDimitry Andric OS << " 0x0";
3335ffd83dbSDimitry Andric else
334e8d8bef9SDimitry Andric prettyPrintBaseTypeRef(U, OS, DumpOpts, Operands, Operand);
3355ffd83dbSDimitry Andric } else if (Size == Operation::WasmLocationArg) {
3365ffd83dbSDimitry Andric assert(Operand == 1);
3375ffd83dbSDimitry Andric switch (Operands[0]) {
338fe6060f1SDimitry Andric case 0:
339fe6060f1SDimitry Andric case 1:
340fe6060f1SDimitry Andric case 2:
3415ffd83dbSDimitry Andric case 3: // global as uint32
342fe6060f1SDimitry Andric case 4:
3435ffd83dbSDimitry Andric OS << format(" 0x%" PRIx64, Operands[Operand]);
3445ffd83dbSDimitry Andric break;
3455ffd83dbSDimitry Andric default: assert(false);
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric } else if (Size == Operation::SizeBlock) {
3488bcb0991SDimitry Andric uint64_t Offset = Operands[Operand];
3490b57cec5SDimitry Andric for (unsigned i = 0; i < Operands[Operand - 1]; ++i)
3500b57cec5SDimitry Andric OS << format(" 0x%02x", Expr->Data.getU8(&Offset));
3510b57cec5SDimitry Andric } else {
3520b57cec5SDimitry Andric if (Signed)
3530b57cec5SDimitry Andric OS << format(" %+" PRId64, (int64_t)Operands[Operand]);
3540b57cec5SDimitry Andric else if (Opcode != DW_OP_entry_value &&
3550b57cec5SDimitry Andric Opcode != DW_OP_GNU_entry_value)
3560b57cec5SDimitry Andric OS << format(" 0x%" PRIx64, Operands[Operand]);
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric }
3590b57cec5SDimitry Andric return true;
3600b57cec5SDimitry Andric }
3610b57cec5SDimitry Andric
print(raw_ostream & OS,DIDumpOptions DumpOpts,DWARFUnit * U,bool IsEH) const362e8d8bef9SDimitry Andric void DWARFExpression::print(raw_ostream &OS, DIDumpOptions DumpOpts,
363bdd1243dSDimitry Andric DWARFUnit *U, bool IsEH) const {
3640b57cec5SDimitry Andric uint32_t EntryValExprSize = 0;
365fe6060f1SDimitry Andric uint64_t EntryValStartOffset = 0;
366fe6060f1SDimitry Andric if (Data.getData().empty())
367fe6060f1SDimitry Andric OS << "<empty>";
368fe6060f1SDimitry Andric
3690b57cec5SDimitry Andric for (auto &Op : *this) {
370bdd1243dSDimitry Andric DumpOpts.IsEH = IsEH;
371bdd1243dSDimitry Andric if (!Op.print(OS, DumpOpts, this, U)) {
3728bcb0991SDimitry Andric uint64_t FailOffset = Op.getEndOffset();
3730b57cec5SDimitry Andric while (FailOffset < Data.getData().size())
3740b57cec5SDimitry Andric OS << format(" %02x", Data.getU8(&FailOffset));
3750b57cec5SDimitry Andric return;
3760b57cec5SDimitry Andric }
3770b57cec5SDimitry Andric
3780b57cec5SDimitry Andric if (Op.getCode() == DW_OP_entry_value ||
3790b57cec5SDimitry Andric Op.getCode() == DW_OP_GNU_entry_value) {
3800b57cec5SDimitry Andric OS << "(";
3810b57cec5SDimitry Andric EntryValExprSize = Op.getRawOperand(0);
382fe6060f1SDimitry Andric EntryValStartOffset = Op.getEndOffset();
3830b57cec5SDimitry Andric continue;
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric
3860b57cec5SDimitry Andric if (EntryValExprSize) {
387fe6060f1SDimitry Andric EntryValExprSize -= Op.getEndOffset() - EntryValStartOffset;
3880b57cec5SDimitry Andric if (EntryValExprSize == 0)
3890b57cec5SDimitry Andric OS << ")";
3900b57cec5SDimitry Andric }
3910b57cec5SDimitry Andric
3920b57cec5SDimitry Andric if (Op.getEndOffset() < Data.getData().size())
3930b57cec5SDimitry Andric OS << ", ";
3940b57cec5SDimitry Andric }
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric
verify(const Operation & Op,DWARFUnit * U)397349cc55cSDimitry Andric bool DWARFExpression::Operation::verify(const Operation &Op, DWARFUnit *U) {
39806c3fb27SDimitry Andric for (unsigned Operand = 0; Operand < Op.Desc.Op.size(); ++Operand) {
399349cc55cSDimitry Andric unsigned Size = Op.Desc.Op[Operand];
4000b57cec5SDimitry Andric
4010b57cec5SDimitry Andric if (Size == Operation::BaseTypeRef) {
4025ffd83dbSDimitry Andric // For DW_OP_convert the operand may be 0 to indicate that conversion to
4035ffd83dbSDimitry Andric // the generic type should be done, so don't look up a base type in that
4045ffd83dbSDimitry Andric // case. The same holds for DW_OP_reinterpret, which is currently not
4055ffd83dbSDimitry Andric // supported.
406349cc55cSDimitry Andric if (Op.Opcode == DW_OP_convert && Op.Operands[Operand] == 0)
4075ffd83dbSDimitry Andric continue;
408349cc55cSDimitry Andric auto Die = U->getDIEForOffset(U->getOffset() + Op.Operands[Operand]);
409349cc55cSDimitry Andric if (!Die || Die.getTag() != dwarf::DW_TAG_base_type)
4100b57cec5SDimitry Andric return false;
4110b57cec5SDimitry Andric }
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric
4140b57cec5SDimitry Andric return true;
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric
verify(DWARFUnit * U)4170b57cec5SDimitry Andric bool DWARFExpression::verify(DWARFUnit *U) {
4180b57cec5SDimitry Andric for (auto &Op : *this)
419349cc55cSDimitry Andric if (!Operation::verify(Op, U))
4200b57cec5SDimitry Andric return false;
4210b57cec5SDimitry Andric
4220b57cec5SDimitry Andric return true;
4230b57cec5SDimitry Andric }
4240b57cec5SDimitry Andric
4255ffd83dbSDimitry Andric /// A user-facing string representation of a DWARF expression. This might be an
4265ffd83dbSDimitry Andric /// Address expression, in which case it will be implicitly dereferenced, or a
4275ffd83dbSDimitry Andric /// Value expression.
4285ffd83dbSDimitry Andric struct PrintedExpr {
4295ffd83dbSDimitry Andric enum ExprKind {
4305ffd83dbSDimitry Andric Address,
4315ffd83dbSDimitry Andric Value,
4325ffd83dbSDimitry Andric };
4335ffd83dbSDimitry Andric ExprKind Kind;
4345ffd83dbSDimitry Andric SmallString<16> String;
4355ffd83dbSDimitry Andric
PrintedExprllvm::PrintedExpr4365ffd83dbSDimitry Andric PrintedExpr(ExprKind K = Address) : Kind(K) {}
4375ffd83dbSDimitry Andric };
4385ffd83dbSDimitry Andric
printCompactDWARFExpr(raw_ostream & OS,DWARFExpression::iterator I,const DWARFExpression::iterator E,std::function<StringRef (uint64_t RegNum,bool IsEH)> GetNameForDWARFReg=nullptr)439bdd1243dSDimitry Andric static bool printCompactDWARFExpr(
440bdd1243dSDimitry Andric raw_ostream &OS, DWARFExpression::iterator I,
4415ffd83dbSDimitry Andric const DWARFExpression::iterator E,
442bdd1243dSDimitry Andric std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg =
443bdd1243dSDimitry Andric nullptr) {
4445ffd83dbSDimitry Andric SmallVector<PrintedExpr, 4> Stack;
4455ffd83dbSDimitry Andric
4465ffd83dbSDimitry Andric while (I != E) {
447349cc55cSDimitry Andric const DWARFExpression::Operation &Op = *I;
4485ffd83dbSDimitry Andric uint8_t Opcode = Op.getCode();
4495ffd83dbSDimitry Andric switch (Opcode) {
4505ffd83dbSDimitry Andric case dwarf::DW_OP_regx: {
4515ffd83dbSDimitry Andric // DW_OP_regx: A register, with the register num given as an operand.
4525ffd83dbSDimitry Andric // Printed as the plain register name.
4535ffd83dbSDimitry Andric uint64_t DwarfRegNum = Op.getRawOperand(0);
454bdd1243dSDimitry Andric auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
455bdd1243dSDimitry Andric if (RegName.empty())
4565ffd83dbSDimitry Andric return false;
4575ffd83dbSDimitry Andric raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);
458bdd1243dSDimitry Andric S << RegName;
4595ffd83dbSDimitry Andric break;
4605ffd83dbSDimitry Andric }
4615ffd83dbSDimitry Andric case dwarf::DW_OP_bregx: {
4625ffd83dbSDimitry Andric int DwarfRegNum = Op.getRawOperand(0);
4635ffd83dbSDimitry Andric int64_t Offset = Op.getRawOperand(1);
464bdd1243dSDimitry Andric auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
465bdd1243dSDimitry Andric if (RegName.empty())
4665ffd83dbSDimitry Andric return false;
4675ffd83dbSDimitry Andric raw_svector_ostream S(Stack.emplace_back().String);
468bdd1243dSDimitry Andric S << RegName;
4695ffd83dbSDimitry Andric if (Offset)
4705ffd83dbSDimitry Andric S << format("%+" PRId64, Offset);
4715ffd83dbSDimitry Andric break;
4725ffd83dbSDimitry Andric }
4735ffd83dbSDimitry Andric case dwarf::DW_OP_entry_value:
4745ffd83dbSDimitry Andric case dwarf::DW_OP_GNU_entry_value: {
4755ffd83dbSDimitry Andric // DW_OP_entry_value contains a sub-expression which must be rendered
4765ffd83dbSDimitry Andric // separately.
4775ffd83dbSDimitry Andric uint64_t SubExprLength = Op.getRawOperand(0);
4785ffd83dbSDimitry Andric DWARFExpression::iterator SubExprEnd = I.skipBytes(SubExprLength);
4795ffd83dbSDimitry Andric ++I;
4805ffd83dbSDimitry Andric raw_svector_ostream S(Stack.emplace_back().String);
4815ffd83dbSDimitry Andric S << "entry(";
482bdd1243dSDimitry Andric printCompactDWARFExpr(S, I, SubExprEnd, GetNameForDWARFReg);
4835ffd83dbSDimitry Andric S << ")";
4845ffd83dbSDimitry Andric I = SubExprEnd;
4855ffd83dbSDimitry Andric continue;
4865ffd83dbSDimitry Andric }
4875ffd83dbSDimitry Andric case dwarf::DW_OP_stack_value: {
4885ffd83dbSDimitry Andric // The top stack entry should be treated as the actual value of tne
4895ffd83dbSDimitry Andric // variable, rather than the address of the variable in memory.
4905ffd83dbSDimitry Andric assert(!Stack.empty());
4915ffd83dbSDimitry Andric Stack.back().Kind = PrintedExpr::Value;
4925ffd83dbSDimitry Andric break;
4935ffd83dbSDimitry Andric }
49406c3fb27SDimitry Andric case dwarf::DW_OP_nop: {
49506c3fb27SDimitry Andric break;
49606c3fb27SDimitry Andric }
49706c3fb27SDimitry Andric case dwarf::DW_OP_LLVM_user: {
49806c3fb27SDimitry Andric assert(Op.getSubCode() && *Op.getSubCode() == dwarf::DW_OP_LLVM_nop);
49906c3fb27SDimitry Andric break;
50006c3fb27SDimitry Andric }
5015ffd83dbSDimitry Andric default:
5025ffd83dbSDimitry Andric if (Opcode >= dwarf::DW_OP_reg0 && Opcode <= dwarf::DW_OP_reg31) {
5035ffd83dbSDimitry Andric // DW_OP_reg<N>: A register, with the register num implied by the
5045ffd83dbSDimitry Andric // opcode. Printed as the plain register name.
5055ffd83dbSDimitry Andric uint64_t DwarfRegNum = Opcode - dwarf::DW_OP_reg0;
506bdd1243dSDimitry Andric auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
507bdd1243dSDimitry Andric if (RegName.empty())
5085ffd83dbSDimitry Andric return false;
5095ffd83dbSDimitry Andric raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);
510bdd1243dSDimitry Andric S << RegName;
5115ffd83dbSDimitry Andric } else if (Opcode >= dwarf::DW_OP_breg0 &&
5125ffd83dbSDimitry Andric Opcode <= dwarf::DW_OP_breg31) {
5135ffd83dbSDimitry Andric int DwarfRegNum = Opcode - dwarf::DW_OP_breg0;
5145ffd83dbSDimitry Andric int64_t Offset = Op.getRawOperand(0);
515bdd1243dSDimitry Andric auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
516bdd1243dSDimitry Andric if (RegName.empty())
5175ffd83dbSDimitry Andric return false;
5185ffd83dbSDimitry Andric raw_svector_ostream S(Stack.emplace_back().String);
519bdd1243dSDimitry Andric S << RegName;
5205ffd83dbSDimitry Andric if (Offset)
5215ffd83dbSDimitry Andric S << format("%+" PRId64, Offset);
5225ffd83dbSDimitry Andric } else {
5235ffd83dbSDimitry Andric // If we hit an unknown operand, we don't know its effect on the stack,
5245ffd83dbSDimitry Andric // so bail out on the whole expression.
5255ffd83dbSDimitry Andric OS << "<unknown op " << dwarf::OperationEncodingString(Opcode) << " ("
5265ffd83dbSDimitry Andric << (int)Opcode << ")>";
5275ffd83dbSDimitry Andric return false;
5285ffd83dbSDimitry Andric }
5295ffd83dbSDimitry Andric break;
5305ffd83dbSDimitry Andric }
5315ffd83dbSDimitry Andric ++I;
5325ffd83dbSDimitry Andric }
5335ffd83dbSDimitry Andric
53406c3fb27SDimitry Andric if (Stack.size() != 1) {
53506c3fb27SDimitry Andric OS << "<stack of size " << Stack.size() << ", expected 1>";
53606c3fb27SDimitry Andric return false;
53706c3fb27SDimitry Andric }
5385ffd83dbSDimitry Andric
5395ffd83dbSDimitry Andric if (Stack.front().Kind == PrintedExpr::Address)
5405ffd83dbSDimitry Andric OS << "[" << Stack.front().String << "]";
5415ffd83dbSDimitry Andric else
5425ffd83dbSDimitry Andric OS << Stack.front().String;
5435ffd83dbSDimitry Andric
5445ffd83dbSDimitry Andric return true;
5455ffd83dbSDimitry Andric }
5465ffd83dbSDimitry Andric
printCompact(raw_ostream & OS,std::function<StringRef (uint64_t RegNum,bool IsEH)> GetNameForDWARFReg)547bdd1243dSDimitry Andric bool DWARFExpression::printCompact(
548bdd1243dSDimitry Andric raw_ostream &OS,
549bdd1243dSDimitry Andric std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {
550bdd1243dSDimitry Andric return printCompactDWARFExpr(OS, begin(), end(), GetNameForDWARFReg);
5515ffd83dbSDimitry Andric }
5525ffd83dbSDimitry Andric
operator ==(const DWARFExpression & RHS) const553fe6060f1SDimitry Andric bool DWARFExpression::operator==(const DWARFExpression &RHS) const {
554fe6060f1SDimitry Andric if (AddressSize != RHS.AddressSize || Format != RHS.Format)
555fe6060f1SDimitry Andric return false;
556fe6060f1SDimitry Andric return Data.getData() == RHS.Data.getData();
557fe6060f1SDimitry Andric }
558fe6060f1SDimitry Andric
5590b57cec5SDimitry Andric } // namespace llvm
560