| 1 |
//===- llvm/unittest/DebugInfo/DWARFExpressionCompactPrinterTest.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 |
|
| 9 |
#include "DwarfGenerator.h" |
| 10 |
#include "llvm/ADT/ArrayRef.h" |
| 11 |
#include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 12 |
#include "llvm/DebugInfo/DWARF/DWARFDie.h" |
| 13 |
#include "llvm/DebugInfo/DWARF/DWARFExpressionPrinter.h" |
| 14 |
#include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h" |
| 15 |
#include "llvm/MC/MCInstrInfo.h" |
| 16 |
#include "llvm/MC/MCRegisterInfo.h" |
| 17 |
#include "llvm/MC/TargetRegistry.h" |
| 18 |
#include "llvm/Support/DataExtractor.h" |
| 19 |
#include "llvm/Support/TargetSelect.h" |
| 20 |
#include "llvm/Testing/Support/Error.h" |
| 21 |
#include "gtest/gtest.h" |
| 22 |
|
| 23 |
using namespace llvm; |
| 24 |
using namespace dwarf; |
| 25 |
|
| 26 |
namespace { |
| 27 |
class DWARFExpressionCompactPrinterTest : public ::testing::Test { |
| 28 |
public: |
| 29 |
std::unique_ptr<MCRegisterInfo> MRI; |
| 30 |
|
| 31 |
DWARFExpressionCompactPrinterTest() { |
| 32 |
InitializeAllTargets(); |
| 33 |
InitializeAllTargetMCs(); |
| 34 |
InitializeAllAsmPrinters(); |
| 35 |
|
| 36 |
std::string TripleName = "armv8a-linux-gnueabi"; |
| 37 |
std::string ErrorStr; |
| 38 |
|
| 39 |
const Target *TheTarget = |
| 40 |
TargetRegistry::lookupTarget(TripleName, ErrorStr); |
| 41 |
|
| 42 |
if (!TheTarget) |
| 43 |
return; |
| 44 |
|
| 45 |
MRI.reset(TheTarget->createMCRegInfo(TripleName)); |
| 46 |
} |
| 47 |
|
| 48 |
void TestExprPrinter(ArrayRef<uint8_t> ExprData, StringRef Expected); |
| 49 |
}; |
| 50 |
} // namespace |
| 51 |
|
| 52 |
void DWARFExpressionCompactPrinterTest::TestExprPrinter( |
| 53 |
ArrayRef<uint8_t> ExprData, StringRef Expected) { |
| 54 |
// If we didn't build ARM, do not run the test. |
| 55 |
if (!MRI) |
| 56 |
GTEST_SKIP(); |
| 57 |
|
| 58 |
// Print the expression, passing in the subprogram DIE, and check that the |
| 59 |
// result is as expected. |
| 60 |
std::string Result; |
| 61 |
raw_string_ostream OS(Result); |
| 62 |
DataExtractor DE(ExprData, true, 8); |
| 63 |
DWARFExpression Expr(DE, 8); |
| 64 |
|
| 65 |
auto GetRegName = [&](uint64_t DwarfRegNum, bool IsEH) -> StringRef { |
| 66 |
if (std::optional<MCRegister> LLVMRegNum = |
| 67 |
this->MRI->getLLVMRegNum(DwarfRegNum, IsEH)) |
| 68 |
if (const char *RegName = this->MRI->getName(*LLVMRegNum)) |
| 69 |
return llvm::StringRef(RegName); |
| 70 |
OS << "<unknown register " << DwarfRegNum << ">"; |
| 71 |
return {}; |
| 72 |
}; |
| 73 |
|
| 74 |
printDwarfExpressionCompact(&Expr, OS, GetRegName); |
| 75 |
EXPECT_EQ(OS.str(), Expected); |
| 76 |
} |
| 77 |
|
| 78 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg0) { |
| 79 |
TestExprPrinter({DW_OP_reg0}, "R0"); |
| 80 |
} |
| 81 |
|
| 82 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg10) { |
| 83 |
TestExprPrinter({DW_OP_reg10}, "R10"); |
| 84 |
} |
| 85 |
|
| 86 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_regx) { |
| 87 |
TestExprPrinter({DW_OP_regx, 0x80, 0x02}, "D0"); |
| 88 |
} |
| 89 |
|
| 90 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0) { |
| 91 |
TestExprPrinter({DW_OP_breg0, 0x04}, "[R0+4]"); |
| 92 |
} |
| 93 |
|
| 94 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0_large_offset) { |
| 95 |
TestExprPrinter({DW_OP_breg0, 0x80, 0x02}, "[R0+256]"); |
| 96 |
} |
| 97 |
|
| 98 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg13) { |
| 99 |
TestExprPrinter({DW_OP_breg13, 0x10}, "[SP+16]"); |
| 100 |
} |
| 101 |
|
| 102 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg13_zero_offset) { |
| 103 |
TestExprPrinter({DW_OP_breg13, 0x00}, "[SP]"); |
| 104 |
} |
| 105 |
|
| 106 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0_negative) { |
| 107 |
TestExprPrinter({DW_OP_breg0, 0x70}, "[R0-16]"); |
| 108 |
} |
| 109 |
|
| 110 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_bregx) { |
| 111 |
TestExprPrinter({DW_OP_bregx, 0x0d, 0x28}, "[SP+40]"); |
| 112 |
} |
| 113 |
|
| 114 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_stack_value) { |
| 115 |
TestExprPrinter({DW_OP_breg13, 0x04, DW_OP_stack_value}, "SP+4"); |
| 116 |
} |
| 117 |
|
| 118 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_entry_value) { |
| 119 |
TestExprPrinter({DW_OP_entry_value, 0x01, DW_OP_reg0, DW_OP_stack_value}, |
| 120 |
"entry(R0)"); |
| 121 |
} |
| 122 |
|
| 123 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_entry_value_mem) { |
| 124 |
TestExprPrinter( |
| 125 |
{DW_OP_entry_value, 0x02, DW_OP_breg13, 0x10, DW_OP_stack_value}, |
| 126 |
"entry([SP+16])"); |
| 127 |
} |
| 128 |
|
| 129 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_nop) { |
| 130 |
TestExprPrinter({DW_OP_nop}, "<stack of size 0, expected 1>"); |
| 131 |
} |
| 132 |
|
| 133 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_LLVM_nop) { |
| 134 |
TestExprPrinter({DW_OP_LLVM_user, DW_OP_LLVM_nop}, |
| 135 |
"<stack of size 0, expected 1>"); |
| 136 |
} |
| 137 |
|
| 138 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_nop_OP_reg) { |
| 139 |
TestExprPrinter({DW_OP_nop, DW_OP_reg0}, "R0"); |
| 140 |
} |
| 141 |
|
| 142 |
TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_LLVM_nop_OP_reg) { |
| 143 |
TestExprPrinter({DW_OP_LLVM_user, DW_OP_LLVM_nop, DW_OP_reg0}, "R0"); |
| 144 |
} |