| 1 |
//===- SystemZMCDisassemblerTest.cpp - Tests for SystemZ MCDisassembler ---===// |
| 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/MC/MCAsmInfo.h" |
| 10 |
#include "llvm/MC/MCContext.h" |
| 11 |
#include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 12 |
#include "llvm/MC/MCDisassembler/MCSymbolizer.h" |
| 13 |
#include "llvm/MC/MCInst.h" |
| 14 |
#include "llvm/MC/MCRegisterInfo.h" |
| 15 |
#include "llvm/MC/MCSubtargetInfo.h" |
| 16 |
#include "llvm/MC/MCTargetOptions.h" |
| 17 |
#include "llvm/MC/TargetRegistry.h" |
| 18 |
#include "llvm/Support/TargetSelect.h" |
| 19 |
#include "gtest/gtest.h" |
| 20 |
|
| 21 |
using namespace llvm; |
| 22 |
|
| 23 |
namespace { |
| 24 |
|
| 25 |
struct Context { |
| 26 |
const char *TripleName = "systemz-unknown"; |
| 27 |
std::unique_ptr<MCRegisterInfo> MRI; |
| 28 |
std::unique_ptr<MCAsmInfo> MAI; |
| 29 |
std::unique_ptr<MCContext> Ctx; |
| 30 |
std::unique_ptr<MCSubtargetInfo> STI; |
| 31 |
std::unique_ptr<MCDisassembler> DisAsm; |
| 32 |
|
| 33 |
Context() { |
| 34 |
LLVMInitializeSystemZTargetInfo(); |
| 35 |
LLVMInitializeSystemZTargetMC(); |
| 36 |
LLVMInitializeSystemZDisassembler(); |
| 37 |
|
| 38 |
// If we didn't build SystemZ, do not run the test. |
| 39 |
std::string Error; |
| 40 |
const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 41 |
if (!TheTarget) |
| 42 |
return; |
| 43 |
|
| 44 |
MRI.reset(TheTarget->createMCRegInfo(TripleName)); |
| 45 |
MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCTargetOptions())); |
| 46 |
STI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 47 |
Ctx = std::make_unique<MCContext>(Triple(TripleName), MAI.get(), MRI.get(), |
| 48 |
STI.get()); |
| 49 |
|
| 50 |
DisAsm.reset(TheTarget->createMCDisassembler(*STI, *Ctx)); |
| 51 |
} |
| 52 |
|
| 53 |
operator MCContext &() { return *Ctx; }; |
| 54 |
}; |
| 55 |
|
| 56 |
Context &getContext() { |
| 57 |
static Context Ctxt; |
| 58 |
return Ctxt; |
| 59 |
} |
| 60 |
|
| 61 |
class SystemZMCSymbolizerTest : public MCSymbolizer { |
| 62 |
public: |
| 63 |
SystemZMCSymbolizerTest(MCContext &MC) : MCSymbolizer(MC, nullptr) {} |
| 64 |
~SystemZMCSymbolizerTest() {} |
| 65 |
|
| 66 |
bool tryAddingSymbolicOperand([[maybe_unused]] MCInst &Inst, |
| 67 |
[[maybe_unused]] raw_ostream &CStream, |
| 68 |
[[maybe_unused]] int64_t Value, |
| 69 |
[[maybe_unused]] uint64_t Address, |
| 70 |
[[maybe_unused]] bool IsBranch, |
| 71 |
[[maybe_unused]] uint64_t Offset, |
| 72 |
[[maybe_unused]] uint64_t OpSize, |
| 73 |
[[maybe_unused]] uint64_t InstSize) override { |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
void |
| 78 |
tryAddingPcLoadReferenceComment([[maybe_unused]] raw_ostream &cStream, |
| 79 |
[[maybe_unused]] int64_t Value, |
| 80 |
[[maybe_unused]] uint64_t Address) override {} |
| 81 |
}; |
| 82 |
|
| 83 |
} // namespace |
| 84 |
|
| 85 |
TEST(SystemZDisassembler, SystemZMCSymbolizerTest) { |
| 86 |
SystemZMCSymbolizerTest *TestSymbolizer = |
| 87 |
new SystemZMCSymbolizerTest(getContext()); |
| 88 |
getContext().DisAsm->setSymbolizer( |
| 89 |
std::unique_ptr<MCSymbolizer>(TestSymbolizer)); |
| 90 |
|
| 91 |
MCInst Inst; |
| 92 |
uint64_t InstSize; |
| 93 |
|
| 94 |
// Check that the SystemZ disassembler sets the comment stream before calling |
| 95 |
// MCDisassembler::tryAddingSymbolicOperand. This will fail an assert if it |
| 96 |
// does not do that. |
| 97 |
MCDisassembler::DecodeStatus Status = getContext().DisAsm->getInstruction( |
| 98 |
Inst, InstSize, |
| 99 |
// lgrl %r1, 0x1234 |
| 100 |
{0xc4, 0x18, 0x00, 0x00, 0x9a, 0x1a}, 0, nulls()); |
| 101 |
ASSERT_TRUE(Status == MCDisassembler::Success); |
| 102 |
EXPECT_EQ(InstSize, uint64_t{6}); |
| 103 |
} |