decoder_tests.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* This file is part of the dynarmic project.
  2. * Copyright (c) 2020 MerryMage
  3. * SPDX-License-Identifier: 0BSD
  4. */
  5. #include <cstring>
  6. #include <iomanip>
  7. #include <iostream>
  8. #include <catch2/catch_test_macros.hpp>
  9. #include <mcl/assert.hpp>
  10. #include "dynarmic/frontend/A32/decoder/asimd.h"
  11. #include "dynarmic/frontend/A32/translate/impl/a32_translate_impl.h"
  12. #include "dynarmic/interface/A32/config.h"
  13. #include "dynarmic/ir/opcodes.h"
  14. using namespace Dynarmic;
  15. TEST_CASE("ASIMD Decoder: Ensure table order correctness", "[decode][a32][.]") {
  16. const auto table = A32::GetASIMDDecodeTable<A32::TranslatorVisitor>();
  17. const auto get_ir = [](const A32::ASIMDMatcher<A32::TranslatorVisitor>& matcher, u32 instruction) {
  18. ASSERT(matcher.Matches(instruction));
  19. const A32::LocationDescriptor location{0, {}, {}};
  20. IR::Block block{location};
  21. A32::TranslatorVisitor visitor{block, location, {}};
  22. matcher.call(visitor, instruction);
  23. return block;
  24. };
  25. const auto is_decode_error = [&get_ir](const A32::ASIMDMatcher<A32::TranslatorVisitor>& matcher, u32 instruction) {
  26. const auto block = get_ir(matcher, instruction);
  27. for (const auto& ir_inst : block) {
  28. if (ir_inst.GetOpcode() == IR::Opcode::A32ExceptionRaised) {
  29. if (static_cast<A32::Exception>(ir_inst.GetArg(1).GetU64()) == A32::Exception::DecodeError) {
  30. return true;
  31. }
  32. }
  33. }
  34. return false;
  35. };
  36. for (auto iter = table.cbegin(); iter != table.cend(); ++iter) {
  37. if (std::strncmp(iter->GetName(), "UNALLOCATED", 11) == 0) {
  38. continue;
  39. }
  40. const u32 expect = iter->GetExpected();
  41. const u32 mask = iter->GetMask();
  42. u32 x = 0;
  43. do {
  44. const u32 instruction = expect | x;
  45. const bool iserr = is_decode_error(*iter, instruction);
  46. const auto alternative = std::find_if(table.cbegin(), iter, [instruction](const auto& m) { return m.Matches(instruction); });
  47. const bool altiserr = is_decode_error(*alternative, instruction);
  48. INFO("Instruction: " << std::hex << std::setfill('0') << std::setw(8) << instruction);
  49. INFO("Expect: " << std::hex << std::setfill('0') << std::setw(8) << expect);
  50. INFO("Fill: " << std::hex << std::setfill('0') << std::setw(8) << x);
  51. INFO("Name: " << iter->GetName());
  52. INFO("iserr: " << iserr);
  53. INFO("alternative: " << alternative->GetName());
  54. INFO("altiserr: " << altiserr);
  55. REQUIRE(((!iserr && alternative == iter) || (iserr && alternative != iter && !altiserr)));
  56. x = ((x | mask) + 1) & ~mask;
  57. } while (x != 0);
  58. }
  59. }