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/DWARFExpression.h"
14 #include "llvm/MC/MCInstrInfo.h"
15 #include "llvm/MC/MCRegisterInfo.h"
16 #include "llvm/MC/TargetRegistry.h"
17 #include "llvm/Support/DataExtractor.h"
18 #include "llvm/Support/TargetSelect.h"
19 #include "llvm/Testing/Support/Error.h"
20 #include "gtest/gtest.h"
21
22 using namespace llvm;
23 using namespace dwarf;
24
25 namespace {
26 class DWARFExpressionCompactPrinterTest : public ::testing::Test {
27 public:
28 std::unique_ptr<MCRegisterInfo> MRI;
29
DWARFExpressionCompactPrinterTest()30 DWARFExpressionCompactPrinterTest() {
31 InitializeAllTargets();
32 InitializeAllTargetMCs();
33 InitializeAllAsmPrinters();
34
35 std::string TripleName = "armv8a-linux-gnueabi";
36 std::string ErrorStr;
37
38 const Target *TheTarget =
39 TargetRegistry::lookupTarget(TripleName, ErrorStr);
40
41 if (!TheTarget)
42 return;
43
44 MRI.reset(TheTarget->createMCRegInfo(TripleName));
45 }
46
47 void TestExprPrinter(ArrayRef<uint8_t> ExprData, StringRef Expected);
48 };
49 } // namespace
50
TestExprPrinter(ArrayRef<uint8_t> ExprData,StringRef Expected)51 void DWARFExpressionCompactPrinterTest::TestExprPrinter(
52 ArrayRef<uint8_t> ExprData, StringRef Expected) {
53 // If we didn't build ARM, do not run the test.
54 if (!MRI)
55 GTEST_SKIP();
56
57 // Print the expression, passing in the subprogram DIE, and check that the
58 // result is as expected.
59 std::string Result;
60 raw_string_ostream OS(Result);
61 DataExtractor DE(ExprData, true, 8);
62 DWARFExpression Expr(DE, 8);
63 Expr.printCompact(OS, *MRI);
64 EXPECT_EQ(OS.str(), Expected);
65 }
66
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_reg0)67 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg0) {
68 TestExprPrinter({DW_OP_reg0}, "R0");
69 }
70
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_reg10)71 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg10) {
72 TestExprPrinter({DW_OP_reg10}, "R10");
73 }
74
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_regx)75 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_regx) {
76 TestExprPrinter({DW_OP_regx, 0x80, 0x02}, "D0");
77 }
78
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_breg0)79 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0) {
80 TestExprPrinter({DW_OP_breg0, 0x04}, "[R0+4]");
81 }
82
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_breg0_large_offset)83 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0_large_offset) {
84 TestExprPrinter({DW_OP_breg0, 0x80, 0x02}, "[R0+256]");
85 }
86
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_breg13)87 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg13) {
88 TestExprPrinter({DW_OP_breg13, 0x10}, "[SP+16]");
89 }
90
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_breg13_zero_offset)91 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg13_zero_offset) {
92 TestExprPrinter({DW_OP_breg13, 0x00}, "[SP]");
93 }
94
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_breg0_negative)95 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0_negative) {
96 TestExprPrinter({DW_OP_breg0, 0x70}, "[R0-16]");
97 }
98
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_bregx)99 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_bregx) {
100 TestExprPrinter({DW_OP_bregx, 0x0d, 0x28}, "[SP+40]");
101 }
102
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_stack_value)103 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_stack_value) {
104 TestExprPrinter({DW_OP_breg13, 0x04, DW_OP_stack_value}, "SP+4");
105 }
106
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_entry_value)107 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_entry_value) {
108 TestExprPrinter({DW_OP_entry_value, 0x01, DW_OP_reg0, DW_OP_stack_value},
109 "entry(R0)");
110 }
111
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_entry_value_mem)112 TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_entry_value_mem) {
113 TestExprPrinter(
114 {DW_OP_entry_value, 0x02, DW_OP_breg13, 0x10, DW_OP_stack_value},
115 "entry([SP+16])");
116 }
117