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