xref: /llvm-project/llvm/unittests/DebugInfo/DWARF/DWARFExpressionCompactPrinterTest.cpp (revision f78a48cfaf9d3e616d9bbedd81f68ff159342ffb)
1c15e04eeSOliver Stannard //===- llvm/unittest/DebugInfo/DWARFExpressionCompactPrinterTest.cpp ------===//
2c15e04eeSOliver Stannard //
3c15e04eeSOliver Stannard // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c15e04eeSOliver Stannard // See https://llvm.org/LICENSE.txt for license information.
5c15e04eeSOliver Stannard // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c15e04eeSOliver Stannard //
7c15e04eeSOliver Stannard //===----------------------------------------------------------------------===//
8c15e04eeSOliver Stannard 
989b57061SReid Kleckner #include "DwarfGenerator.h"
10c15e04eeSOliver Stannard #include "llvm/ADT/ArrayRef.h"
11c15e04eeSOliver Stannard #include "llvm/DebugInfo/DWARF/DWARFContext.h"
12c15e04eeSOliver Stannard #include "llvm/DebugInfo/DWARF/DWARFDie.h"
13c15e04eeSOliver Stannard #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
14c15e04eeSOliver Stannard #include "llvm/MC/MCInstrInfo.h"
15ef736a1cSserge-sans-paille #include "llvm/MC/MCRegisterInfo.h"
1689b57061SReid Kleckner #include "llvm/MC/TargetRegistry.h"
17c15e04eeSOliver Stannard #include "llvm/Support/DataExtractor.h"
18c15e04eeSOliver Stannard #include "llvm/Support/TargetSelect.h"
19c15e04eeSOliver Stannard #include "llvm/Testing/Support/Error.h"
20c15e04eeSOliver Stannard #include "gtest/gtest.h"
21c15e04eeSOliver Stannard 
22c15e04eeSOliver Stannard using namespace llvm;
23c15e04eeSOliver Stannard using namespace dwarf;
24c15e04eeSOliver Stannard 
25c15e04eeSOliver Stannard namespace {
26c15e04eeSOliver Stannard class DWARFExpressionCompactPrinterTest : public ::testing::Test {
27c15e04eeSOliver Stannard public:
28c15e04eeSOliver Stannard   std::unique_ptr<MCRegisterInfo> MRI;
29c15e04eeSOliver Stannard 
30c15e04eeSOliver Stannard   DWARFExpressionCompactPrinterTest() {
31c15e04eeSOliver Stannard     InitializeAllTargets();
32c15e04eeSOliver Stannard     InitializeAllTargetMCs();
33c15e04eeSOliver Stannard     InitializeAllAsmPrinters();
34c15e04eeSOliver Stannard 
35c15e04eeSOliver Stannard     std::string TripleName = "armv8a-linux-gnueabi";
36c15e04eeSOliver Stannard     std::string ErrorStr;
37c15e04eeSOliver Stannard 
38c15e04eeSOliver Stannard     const Target *TheTarget =
39c15e04eeSOliver Stannard         TargetRegistry::lookupTarget(TripleName, ErrorStr);
40c15e04eeSOliver Stannard 
41c15e04eeSOliver Stannard     if (!TheTarget)
42c15e04eeSOliver Stannard       return;
43c15e04eeSOliver Stannard 
44c15e04eeSOliver Stannard     MRI.reset(TheTarget->createMCRegInfo(TripleName));
45c15e04eeSOliver Stannard   }
46c15e04eeSOliver Stannard 
47c15e04eeSOliver Stannard   void TestExprPrinter(ArrayRef<uint8_t> ExprData, StringRef Expected);
48c15e04eeSOliver Stannard };
49c15e04eeSOliver Stannard } // namespace
50c15e04eeSOliver Stannard 
51c15e04eeSOliver Stannard void DWARFExpressionCompactPrinterTest::TestExprPrinter(
52c15e04eeSOliver Stannard     ArrayRef<uint8_t> ExprData, StringRef Expected) {
53c15e04eeSOliver Stannard   // If we didn't build ARM, do not run the test.
54c15e04eeSOliver Stannard   if (!MRI)
55f5907ea1SIgor Kudrin     GTEST_SKIP();
56c15e04eeSOliver Stannard 
57c15e04eeSOliver Stannard   // Print the expression, passing in the subprogram DIE, and check that the
58c15e04eeSOliver Stannard   // result is as expected.
59c15e04eeSOliver Stannard   std::string Result;
60c15e04eeSOliver Stannard   raw_string_ostream OS(Result);
61c15e04eeSOliver Stannard   DataExtractor DE(ExprData, true, 8);
62c15e04eeSOliver Stannard   DWARFExpression Expr(DE, 8);
63617ed4f0SShubham Sandeep Rastogi 
64617ed4f0SShubham Sandeep Rastogi   auto GetRegName = [&](uint64_t DwarfRegNum, bool IsEH) -> StringRef {
65*f78a48cfSCraig Topper     if (std::optional<MCRegister> LLVMRegNum =
66617ed4f0SShubham Sandeep Rastogi             this->MRI->getLLVMRegNum(DwarfRegNum, IsEH))
67617ed4f0SShubham Sandeep Rastogi       if (const char *RegName = this->MRI->getName(*LLVMRegNum))
68617ed4f0SShubham Sandeep Rastogi         return llvm::StringRef(RegName);
69617ed4f0SShubham Sandeep Rastogi     OS << "<unknown register " << DwarfRegNum << ">";
70617ed4f0SShubham Sandeep Rastogi     return {};
71617ed4f0SShubham Sandeep Rastogi   };
72617ed4f0SShubham Sandeep Rastogi 
73617ed4f0SShubham Sandeep Rastogi   Expr.printCompact(OS, GetRegName);
74c15e04eeSOliver Stannard   EXPECT_EQ(OS.str(), Expected);
75c15e04eeSOliver Stannard }
76c15e04eeSOliver Stannard 
77c15e04eeSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg0) {
78c15e04eeSOliver Stannard   TestExprPrinter({DW_OP_reg0}, "R0");
79c15e04eeSOliver Stannard }
80c15e04eeSOliver Stannard 
81c15e04eeSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg10) {
82c15e04eeSOliver Stannard   TestExprPrinter({DW_OP_reg10}, "R10");
83c15e04eeSOliver Stannard }
84c15e04eeSOliver Stannard 
85c15e04eeSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_regx) {
86c15e04eeSOliver Stannard   TestExprPrinter({DW_OP_regx, 0x80, 0x02}, "D0");
87c15e04eeSOliver Stannard }
881d7311e0SOliver Stannard 
891d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0) {
901d7311e0SOliver Stannard   TestExprPrinter({DW_OP_breg0, 0x04}, "[R0+4]");
911d7311e0SOliver Stannard }
921d7311e0SOliver Stannard 
931d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0_large_offset) {
941d7311e0SOliver Stannard   TestExprPrinter({DW_OP_breg0, 0x80, 0x02}, "[R0+256]");
951d7311e0SOliver Stannard }
961d7311e0SOliver Stannard 
971d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg13) {
981d7311e0SOliver Stannard   TestExprPrinter({DW_OP_breg13, 0x10}, "[SP+16]");
991d7311e0SOliver Stannard }
1001d7311e0SOliver Stannard 
1011d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg13_zero_offset) {
1021d7311e0SOliver Stannard   TestExprPrinter({DW_OP_breg13, 0x00}, "[SP]");
1031d7311e0SOliver Stannard }
1041d7311e0SOliver Stannard 
1051d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0_negative) {
1061d7311e0SOliver Stannard   TestExprPrinter({DW_OP_breg0, 0x70}, "[R0-16]");
1071d7311e0SOliver Stannard }
1081d7311e0SOliver Stannard 
1091d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_bregx) {
1101d7311e0SOliver Stannard   TestExprPrinter({DW_OP_bregx, 0x0d, 0x28}, "[SP+40]");
1111d7311e0SOliver Stannard }
11257909b0aSOliver Stannard 
11357909b0aSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_stack_value) {
11457909b0aSOliver Stannard   TestExprPrinter({DW_OP_breg13, 0x04, DW_OP_stack_value}, "SP+4");
11557909b0aSOliver Stannard }
11657909b0aSOliver Stannard 
11757909b0aSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_entry_value) {
11857909b0aSOliver Stannard   TestExprPrinter({DW_OP_entry_value, 0x01, DW_OP_reg0, DW_OP_stack_value},
11957909b0aSOliver Stannard                   "entry(R0)");
12057909b0aSOliver Stannard }
12157909b0aSOliver Stannard 
12257909b0aSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_entry_value_mem) {
12357909b0aSOliver Stannard   TestExprPrinter(
12457909b0aSOliver Stannard       {DW_OP_entry_value, 0x02, DW_OP_breg13, 0x10, DW_OP_stack_value},
12557909b0aSOliver Stannard       "entry([SP+16])");
12657909b0aSOliver Stannard }
1277f596bb5SScott Linder 
1287f596bb5SScott Linder TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_nop) {
1297f596bb5SScott Linder   TestExprPrinter({DW_OP_nop}, "<stack of size 0, expected 1>");
1307f596bb5SScott Linder }
1317f596bb5SScott Linder 
13258669354SScott Linder TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_LLVM_nop) {
13358669354SScott Linder   TestExprPrinter({DW_OP_LLVM_user, DW_OP_LLVM_nop},
13458669354SScott Linder                   "<stack of size 0, expected 1>");
13558669354SScott Linder }
13658669354SScott Linder 
1377f596bb5SScott Linder TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_nop_OP_reg) {
1387f596bb5SScott Linder   TestExprPrinter({DW_OP_nop, DW_OP_reg0}, "R0");
1397f596bb5SScott Linder }
14058669354SScott Linder 
14158669354SScott Linder TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_LLVM_nop_OP_reg) {
14258669354SScott Linder   TestExprPrinter({DW_OP_LLVM_user, DW_OP_LLVM_nop, DW_OP_reg0}, "R0");
14358669354SScott Linder }
144