104 lines · cpp
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-exception6//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 21using namespace llvm;22 23namespace {24 25struct Context {26 static constexpr char TripleName[] = "systemz-unknown";27 Triple TT;28 std::unique_ptr<MCRegisterInfo> MRI;29 std::unique_ptr<MCAsmInfo> MAI;30 std::unique_ptr<MCContext> Ctx;31 std::unique_ptr<MCSubtargetInfo> STI;32 std::unique_ptr<MCDisassembler> DisAsm;33 34 Context() : TT(TripleName) {35 LLVMInitializeSystemZTargetInfo();36 LLVMInitializeSystemZTargetMC();37 LLVMInitializeSystemZDisassembler();38 39 // If we didn't build SystemZ, do not run the test.40 std::string Error;41 const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);42 if (!TheTarget)43 return;44 45 MRI.reset(TheTarget->createMCRegInfo(TT));46 MAI.reset(TheTarget->createMCAsmInfo(*MRI, TT, MCTargetOptions()));47 STI.reset(TheTarget->createMCSubtargetInfo(TT, "", ""));48 Ctx = std::make_unique<MCContext>(TT, MAI.get(), MRI.get(), STI.get());49 50 DisAsm.reset(TheTarget->createMCDisassembler(*STI, *Ctx));51 }52 53 operator MCContext &() { return *Ctx; };54};55 56Context &getContext() {57 static Context Ctxt;58 return Ctxt;59}60 61class SystemZMCSymbolizerTest : public MCSymbolizer {62public:63 SystemZMCSymbolizerTest(MCContext &MC) : MCSymbolizer(MC, nullptr) {}64 ~SystemZMCSymbolizerTest() override = default;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 void78 tryAddingPcLoadReferenceComment([[maybe_unused]] raw_ostream &cStream,79 [[maybe_unused]] int64_t Value,80 [[maybe_unused]] uint64_t Address) override {}81};82 83} // namespace84 85TEST(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 calling95 // MCDisassembler::tryAddingSymbolicOperand. This will fail an assert if it96 // does not do that.97 MCDisassembler::DecodeStatus Status = getContext().DisAsm->getInstruction(98 Inst, InstSize,99 // lgrl %r1, 0x1234100 {0xc4, 0x18, 0x00, 0x00, 0x9a, 0x1a}, 0, nulls());101 ASSERT_TRUE(Status == MCDisassembler::Success);102 EXPECT_EQ(InstSize, uint64_t{6});103}104