1*c15e04eeSOliver Stannard //===- llvm/unittest/DebugInfo/DWARFExpressionCompactPrinterTest.cpp ------===//
2*c15e04eeSOliver Stannard //
3*c15e04eeSOliver Stannard // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*c15e04eeSOliver Stannard // See https://llvm.org/LICENSE.txt for license information.
5*c15e04eeSOliver Stannard // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*c15e04eeSOliver Stannard //
7*c15e04eeSOliver Stannard //===----------------------------------------------------------------------===//
8*c15e04eeSOliver Stannard 
9*c15e04eeSOliver Stannard #include "llvm/ADT/ArrayRef.h"
10*c15e04eeSOliver Stannard #include "llvm/DebugInfo/DWARF/DWARFContext.h"
11*c15e04eeSOliver Stannard #include "llvm/DebugInfo/DWARF/DWARFDie.h"
12*c15e04eeSOliver Stannard #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
13*c15e04eeSOliver Stannard #include "llvm/MC/MCInstrInfo.h"
14*c15e04eeSOliver Stannard #include "llvm/Support/DataExtractor.h"
15*c15e04eeSOliver Stannard #include "llvm/Support/TargetRegistry.h"
16*c15e04eeSOliver Stannard #include "llvm/Support/TargetSelect.h"
17*c15e04eeSOliver Stannard #include "llvm/Testing/Support/Error.h"
18*c15e04eeSOliver Stannard #include "gtest/gtest.h"
19*c15e04eeSOliver Stannard #include "DwarfGenerator.h"
20*c15e04eeSOliver Stannard 
21*c15e04eeSOliver Stannard using namespace llvm;
22*c15e04eeSOliver Stannard using namespace dwarf;
23*c15e04eeSOliver Stannard 
24*c15e04eeSOliver Stannard namespace {
25*c15e04eeSOliver Stannard class DWARFExpressionCompactPrinterTest : public ::testing::Test {
26*c15e04eeSOliver Stannard public:
27*c15e04eeSOliver Stannard   std::unique_ptr<MCRegisterInfo> MRI;
28*c15e04eeSOliver Stannard 
29*c15e04eeSOliver Stannard   DWARFExpressionCompactPrinterTest() {
30*c15e04eeSOliver Stannard     InitializeAllTargets();
31*c15e04eeSOliver Stannard     InitializeAllTargetMCs();
32*c15e04eeSOliver Stannard     InitializeAllAsmPrinters();
33*c15e04eeSOliver Stannard 
34*c15e04eeSOliver Stannard     std::string TripleName = "armv8a-linux-gnueabi";
35*c15e04eeSOliver Stannard     std::string ErrorStr;
36*c15e04eeSOliver Stannard 
37*c15e04eeSOliver Stannard     const Target *TheTarget =
38*c15e04eeSOliver Stannard         TargetRegistry::lookupTarget(TripleName, ErrorStr);
39*c15e04eeSOliver Stannard 
40*c15e04eeSOliver Stannard     if (!TheTarget)
41*c15e04eeSOliver Stannard       return;
42*c15e04eeSOliver Stannard 
43*c15e04eeSOliver Stannard     MRI.reset(TheTarget->createMCRegInfo(TripleName));
44*c15e04eeSOliver Stannard   }
45*c15e04eeSOliver Stannard 
46*c15e04eeSOliver Stannard   void TestExprPrinter(ArrayRef<uint8_t> ExprData, StringRef Expected);
47*c15e04eeSOliver Stannard };
48*c15e04eeSOliver Stannard } // namespace
49*c15e04eeSOliver Stannard 
50*c15e04eeSOliver Stannard void DWARFExpressionCompactPrinterTest::TestExprPrinter(
51*c15e04eeSOliver Stannard     ArrayRef<uint8_t> ExprData, StringRef Expected) {
52*c15e04eeSOliver Stannard   // If we didn't build ARM, do not run the test.
53*c15e04eeSOliver Stannard   if (!MRI)
54*c15e04eeSOliver Stannard     return;
55*c15e04eeSOliver Stannard 
56*c15e04eeSOliver Stannard   // Print the expression, passing in the subprogram DIE, and check that the
57*c15e04eeSOliver Stannard   // result is as expected.
58*c15e04eeSOliver Stannard   std::string Result;
59*c15e04eeSOliver Stannard   raw_string_ostream OS(Result);
60*c15e04eeSOliver Stannard   DataExtractor DE(ExprData, true, 8);
61*c15e04eeSOliver Stannard   DWARFExpression Expr(DE, 8);
62*c15e04eeSOliver Stannard   Expr.printCompact(OS, *MRI);
63*c15e04eeSOliver Stannard   EXPECT_EQ(OS.str(), Expected);
64*c15e04eeSOliver Stannard }
65*c15e04eeSOliver Stannard 
66*c15e04eeSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg0) {
67*c15e04eeSOliver Stannard   TestExprPrinter({DW_OP_reg0}, "R0");
68*c15e04eeSOliver Stannard }
69*c15e04eeSOliver Stannard 
70*c15e04eeSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_reg10) {
71*c15e04eeSOliver Stannard   TestExprPrinter({DW_OP_reg10}, "R10");
72*c15e04eeSOliver Stannard }
73*c15e04eeSOliver Stannard 
74*c15e04eeSOliver Stannard TEST_F(DWARFExpressionCompactPrinterTest, Test_OP_regx) {
75*c15e04eeSOliver Stannard   TestExprPrinter({DW_OP_regx, 0x80, 0x02}, "D0");
76*c15e04eeSOliver Stannard }
77