Skip to content

Commit c5c0da8

Browse files
committed
Reland "[llvm][SystemZ] Set comment stream in SystemZDisassembler::getInstruction" (#148639)"
This reverts commit e10db15. This adds the MCDisassembler which was missing, breaking shared library builds.
1 parent d749095 commit c5c0da8

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

llvm/lib/Target/SystemZ/Disassembler/SystemZDisassembler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ DecodeStatus SystemZDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
327327
ArrayRef<uint8_t> Bytes,
328328
uint64_t Address,
329329
raw_ostream &CS) const {
330+
CommentStream = &CS;
331+
330332
// Get the first two bytes of the instruction.
331333
Size = 0;
332334
if (Bytes.size() < 2)

llvm/unittests/MC/SystemZ/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ include_directories(
44

55
set(LLVM_LINK_COMPONENTS
66
SystemZ
7+
MCDisassembler
78
MCParser
89
MC
910
Support
1011
TargetParser
1112
)
1213

13-
add_llvm_unittest(SystemZAsmLexerTests
14+
add_llvm_unittest(SystemZMCTests
1415
SystemZAsmLexerTest.cpp
16+
SystemZMCDisassemblerTest.cpp
1517
)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

0 commit comments

Comments
 (0)