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"
15*ef736a1cSserge-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 
DWARFExpressionCompactPrinterTest()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 
TestExprPrinter(ArrayRef<uint8_t> ExprData,StringRef Expected)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);
63c15e04eeSOliver Stannard   Expr.printCompact(OS, *MRI);
64c15e04eeSOliver Stannard   EXPECT_EQ(OS.str(), Expected);
65c15e04eeSOliver Stannard }
66c15e04eeSOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_reg0)67c15e04eeSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg0) {
68c15e04eeSOliver Stannard   TestExprPrinter({DW_OP_reg0}, "R0");
69c15e04eeSOliver Stannard }
70c15e04eeSOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_reg10)71c15e04eeSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg10) {
72c15e04eeSOliver Stannard   TestExprPrinter({DW_OP_reg10}, "R10");
73c15e04eeSOliver Stannard }
74c15e04eeSOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_regx)75c15e04eeSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_regx) {
76c15e04eeSOliver Stannard   TestExprPrinter({DW_OP_regx, 0x80, 0x02}, "D0");
77c15e04eeSOliver Stannard }
781d7311e0SOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_breg0)791d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0) {
801d7311e0SOliver Stannard   TestExprPrinter({DW_OP_breg0, 0x04}, "[R0+4]");
811d7311e0SOliver Stannard }
821d7311e0SOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_breg0_large_offset)831d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0_large_offset) {
841d7311e0SOliver Stannard   TestExprPrinter({DW_OP_breg0, 0x80, 0x02}, "[R0+256]");
851d7311e0SOliver Stannard }
861d7311e0SOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_breg13)871d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg13) {
881d7311e0SOliver Stannard   TestExprPrinter({DW_OP_breg13, 0x10}, "[SP+16]");
891d7311e0SOliver Stannard }
901d7311e0SOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_breg13_zero_offset)911d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg13_zero_offset) {
921d7311e0SOliver Stannard   TestExprPrinter({DW_OP_breg13, 0x00}, "[SP]");
931d7311e0SOliver Stannard }
941d7311e0SOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_breg0_negative)951d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_breg0_negative) {
961d7311e0SOliver Stannard   TestExprPrinter({DW_OP_breg0, 0x70}, "[R0-16]");
971d7311e0SOliver Stannard }
981d7311e0SOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_bregx)991d7311e0SOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_bregx) {
1001d7311e0SOliver Stannard   TestExprPrinter({DW_OP_bregx, 0x0d, 0x28}, "[SP+40]");
1011d7311e0SOliver Stannard }
10257909b0aSOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_stack_value)10357909b0aSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_stack_value) {
10457909b0aSOliver Stannard   TestExprPrinter({DW_OP_breg13, 0x04, DW_OP_stack_value}, "SP+4");
10557909b0aSOliver Stannard }
10657909b0aSOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_entry_value)10757909b0aSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_entry_value) {
10857909b0aSOliver Stannard   TestExprPrinter({DW_OP_entry_value, 0x01, DW_OP_reg0, DW_OP_stack_value},
10957909b0aSOliver Stannard                   "entry(R0)");
11057909b0aSOliver Stannard }
11157909b0aSOliver Stannard 
TEST_F(DWARFExpressionCompactPrinterTest,Test_OP_entry_value_mem)11257909b0aSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_entry_value_mem) {
11357909b0aSOliver Stannard   TestExprPrinter(
11457909b0aSOliver Stannard       {DW_OP_entry_value, 0x02, DW_OP_breg13, 0x10, DW_OP_stack_value},
11557909b0aSOliver Stannard       "entry([SP+16])");
11657909b0aSOliver Stannard }
117