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