From e2bc7eeb93c3494d06fc0fab8ae5653781237f08 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 1 Feb 2021 15:10:08 -0500 Subject: [PATCH] thumb32: Implement REVSH --- src/frontend/A32/decoder/thumb32.h | 2 +- src/frontend/A32/translate/impl/thumb32_misc.cpp | 12 ++++++++++++ src/frontend/A32/translate/impl/translate_thumb.h | 1 + tests/A32/fuzz_thumb.cpp | 9 ++++++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/frontend/A32/decoder/thumb32.h b/src/frontend/A32/decoder/thumb32.h index bf36e254..8acdbc85 100644 --- a/src/frontend/A32/decoder/thumb32.h +++ b/src/frontend/A32/decoder/thumb32.h @@ -282,7 +282,7 @@ std::optional>> DecodeThumb32(u32 //INST(&V::thumb32_REV, "REV", "111110101001----1111----1000----"), //INST(&V::thumb32_REV16, "REV16", "111110101001----1111----1001----"), //INST(&V::thumb32_RBIT, "RBIT", "111110101001----1111----1010----"), - //INST(&V::thumb32_REVSH, "REVSH", "111110101001----1111----1011----"), + INST(&V::thumb32_REVSH, "REVSH", "111110101001nnnn1111dddd1011mmmm"), INST(&V::thumb32_SEL, "SEL", "111110101010nnnn1111dddd1000mmmm"), INST(&V::thumb32_CLZ, "CLZ", "111110101011nnnn1111dddd1000mmmm"), diff --git a/src/frontend/A32/translate/impl/thumb32_misc.cpp b/src/frontend/A32/translate/impl/thumb32_misc.cpp index a269cae3..9f0b228a 100644 --- a/src/frontend/A32/translate/impl/thumb32_misc.cpp +++ b/src/frontend/A32/translate/impl/thumb32_misc.cpp @@ -19,6 +19,18 @@ bool ThumbTranslatorVisitor::thumb32_CLZ(Reg n, Reg d, Reg m) { return true; } +bool ThumbTranslatorVisitor::thumb32_REVSH(Reg n, Reg d, Reg m) { + if (m != n || d == Reg::PC || m == Reg::PC) { + return UnpredictableInstruction(); + } + + const auto reg_m = ir.GetRegister(m); + const auto rev_half = ir.ByteReverseHalf(ir.LeastSignificantHalf(reg_m)); + + ir.SetRegister(d, ir.SignExtendHalfToWord(rev_half)); + return true; +} + bool ThumbTranslatorVisitor::thumb32_SEL(Reg n, Reg d, Reg m) { if (d == Reg::PC || n == Reg::PC || m == Reg::PC) { return UnpredictableInstruction(); diff --git a/src/frontend/A32/translate/impl/translate_thumb.h b/src/frontend/A32/translate/impl/translate_thumb.h index 4ddc0354..5866bab2 100644 --- a/src/frontend/A32/translate/impl/translate_thumb.h +++ b/src/frontend/A32/translate/impl/translate_thumb.h @@ -118,6 +118,7 @@ struct ThumbTranslatorVisitor final { // thumb32 miscellaneous instructions bool thumb32_CLZ(Reg n, Reg d, Reg m); + bool thumb32_REVSH(Reg n, Reg d, Reg m); bool thumb32_SEL(Reg n, Reg d, Reg m); }; diff --git a/tests/A32/fuzz_thumb.cpp b/tests/A32/fuzz_thumb.cpp index e4d1b984..f1b9ca0e 100644 --- a/tests/A32/fuzz_thumb.cpp +++ b/tests/A32/fuzz_thumb.cpp @@ -369,7 +369,14 @@ TEST_CASE("Fuzz Thumb32 instructions set", "[JitX64][Thumb][Thumb32]") { const auto n = Common::Bits<16, 19>(inst); return m == n && d != 15 && m != 15; }), - ThumbInstGen("111110101010nnnn1111dddd1000mmmm", + ThumbInstGen("111110101001nnnn1111dddd1011mmmm", // REVSH + [](u32 inst) { + const auto d = Common::Bits<8, 11>(inst); + const auto m = Common::Bits<0, 3>(inst); + const auto n = Common::Bits<16, 19>(inst); + return m == n && d != 15 && m != 15; + }), + ThumbInstGen("111110101010nnnn1111dddd1000mmmm", // SEL [](u32 inst) { const auto d = Common::Bits<8, 11>(inst); const auto m = Common::Bits<0, 3>(inst);