diff options
| author | 2021-07-25 11:39:04 -0700 | |
|---|---|---|
| committer | 2021-07-25 11:39:04 -0700 | |
| commit | 98b26b6e126d4775fdf3f773fe8a8ac808a8ff8f (patch) | |
| tree | 816faa96c2c4d291825063433331a8ea4b3d08f1 /src/shader_recompiler/frontend/maxwell/decode.cpp | |
| parent | Merge pull request #6699 from lat9nq/common-threads (diff) | |
| parent | shader: Support out of bound local memory reads and immediate writes (diff) | |
| download | yuzu-98b26b6e126d4775fdf3f773fe8a8ac808a8ff8f.tar.gz yuzu-98b26b6e126d4775fdf3f773fe8a8ac808a8ff8f.tar.xz yuzu-98b26b6e126d4775fdf3f773fe8a8ac808a8ff8f.zip | |
Merge pull request #6585 from ameerj/hades
Shader Decompiler Rewrite
Diffstat (limited to 'src/shader_recompiler/frontend/maxwell/decode.cpp')
| -rw-r--r-- | src/shader_recompiler/frontend/maxwell/decode.cpp | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/shader_recompiler/frontend/maxwell/decode.cpp b/src/shader_recompiler/frontend/maxwell/decode.cpp new file mode 100644 index 000000000..972f677dc --- /dev/null +++ b/src/shader_recompiler/frontend/maxwell/decode.cpp | |||
| @@ -0,0 +1,149 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <array> | ||
| 7 | #include <bit> | ||
| 8 | #include <memory> | ||
| 9 | #include <string_view> | ||
| 10 | |||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "shader_recompiler/exception.h" | ||
| 13 | #include "shader_recompiler/frontend/maxwell/decode.h" | ||
| 14 | #include "shader_recompiler/frontend/maxwell/opcodes.h" | ||
| 15 | |||
| 16 | namespace Shader::Maxwell { | ||
| 17 | namespace { | ||
| 18 | struct MaskValue { | ||
| 19 | u64 mask; | ||
| 20 | u64 value; | ||
| 21 | }; | ||
| 22 | |||
| 23 | constexpr MaskValue MaskValueFromEncoding(const char* encoding) { | ||
| 24 | u64 mask{}; | ||
| 25 | u64 value{}; | ||
| 26 | u64 bit{u64(1) << 63}; | ||
| 27 | while (*encoding) { | ||
| 28 | switch (*encoding) { | ||
| 29 | case '0': | ||
| 30 | mask |= bit; | ||
| 31 | break; | ||
| 32 | case '1': | ||
| 33 | mask |= bit; | ||
| 34 | value |= bit; | ||
| 35 | break; | ||
| 36 | case '-': | ||
| 37 | break; | ||
| 38 | case ' ': | ||
| 39 | break; | ||
| 40 | default: | ||
| 41 | throw LogicError("Invalid encoding character '{}'", *encoding); | ||
| 42 | } | ||
| 43 | ++encoding; | ||
| 44 | if (*encoding != ' ') { | ||
| 45 | bit >>= 1; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | return MaskValue{.mask = mask, .value = value}; | ||
| 49 | } | ||
| 50 | |||
| 51 | struct InstEncoding { | ||
| 52 | MaskValue mask_value; | ||
| 53 | Opcode opcode; | ||
| 54 | }; | ||
| 55 | constexpr std::array UNORDERED_ENCODINGS{ | ||
| 56 | #define INST(name, cute, encode) \ | ||
| 57 | InstEncoding{ \ | ||
| 58 | .mask_value{MaskValueFromEncoding(encode)}, \ | ||
| 59 | .opcode = Opcode::name, \ | ||
| 60 | }, | ||
| 61 | #include "maxwell.inc" | ||
| 62 | #undef INST | ||
| 63 | }; | ||
| 64 | |||
| 65 | constexpr auto SortedEncodings() { | ||
| 66 | std::array encodings{UNORDERED_ENCODINGS}; | ||
| 67 | std::ranges::sort(encodings, [](const InstEncoding& lhs, const InstEncoding& rhs) { | ||
| 68 | return std::popcount(lhs.mask_value.mask) > std::popcount(rhs.mask_value.mask); | ||
| 69 | }); | ||
| 70 | return encodings; | ||
| 71 | } | ||
| 72 | constexpr auto ENCODINGS{SortedEncodings()}; | ||
| 73 | |||
| 74 | constexpr int WidestLeftBits() { | ||
| 75 | int bits{64}; | ||
| 76 | for (const InstEncoding& encoding : ENCODINGS) { | ||
| 77 | bits = std::min(bits, std::countr_zero(encoding.mask_value.mask)); | ||
| 78 | } | ||
| 79 | return 64 - bits; | ||
| 80 | } | ||
| 81 | constexpr int WIDEST_LEFT_BITS{WidestLeftBits()}; | ||
| 82 | constexpr int MASK_SHIFT{64 - WIDEST_LEFT_BITS}; | ||
| 83 | |||
| 84 | constexpr size_t ToFastLookupIndex(u64 value) { | ||
| 85 | return static_cast<size_t>(value >> MASK_SHIFT); | ||
| 86 | } | ||
| 87 | |||
| 88 | constexpr size_t FastLookupSize() { | ||
| 89 | size_t max_width{}; | ||
| 90 | for (const InstEncoding& encoding : ENCODINGS) { | ||
| 91 | max_width = std::max(max_width, ToFastLookupIndex(encoding.mask_value.mask)); | ||
| 92 | } | ||
| 93 | return max_width + 1; | ||
| 94 | } | ||
| 95 | constexpr size_t FAST_LOOKUP_SIZE{FastLookupSize()}; | ||
| 96 | |||
| 97 | struct InstInfo { | ||
| 98 | [[nodiscard]] u64 Mask() const noexcept { | ||
| 99 | return static_cast<u64>(high_mask) << MASK_SHIFT; | ||
| 100 | } | ||
| 101 | |||
| 102 | [[nodiscard]] u64 Value() const noexcept { | ||
| 103 | return static_cast<u64>(high_value) << MASK_SHIFT; | ||
| 104 | } | ||
| 105 | |||
| 106 | u16 high_mask; | ||
| 107 | u16 high_value; | ||
| 108 | Opcode opcode; | ||
| 109 | }; | ||
| 110 | |||
| 111 | constexpr auto MakeFastLookupTableIndex(size_t index) { | ||
| 112 | std::array<InstInfo, 2> encodings{}; | ||
| 113 | size_t element{}; | ||
| 114 | for (const auto& encoding : ENCODINGS) { | ||
| 115 | const size_t mask{ToFastLookupIndex(encoding.mask_value.mask)}; | ||
| 116 | const size_t value{ToFastLookupIndex(encoding.mask_value.value)}; | ||
| 117 | if ((index & mask) == value) { | ||
| 118 | encodings.at(element) = InstInfo{ | ||
| 119 | .high_mask = static_cast<u16>(encoding.mask_value.mask >> MASK_SHIFT), | ||
| 120 | .high_value = static_cast<u16>(encoding.mask_value.value >> MASK_SHIFT), | ||
| 121 | .opcode = encoding.opcode, | ||
| 122 | }; | ||
| 123 | ++element; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | return encodings; | ||
| 127 | } | ||
| 128 | |||
| 129 | /*constexpr*/ auto MakeFastLookupTable() { | ||
| 130 | auto encodings{std::make_unique<std::array<std::array<InstInfo, 2>, FAST_LOOKUP_SIZE>>()}; | ||
| 131 | for (size_t index = 0; index < FAST_LOOKUP_SIZE; ++index) { | ||
| 132 | (*encodings)[index] = MakeFastLookupTableIndex(index); | ||
| 133 | } | ||
| 134 | return encodings; | ||
| 135 | } | ||
| 136 | const auto FAST_LOOKUP_TABLE{MakeFastLookupTable()}; | ||
| 137 | } // Anonymous namespace | ||
| 138 | |||
| 139 | Opcode Decode(u64 insn) { | ||
| 140 | const auto& table{(*FAST_LOOKUP_TABLE)[ToFastLookupIndex(insn)]}; | ||
| 141 | const auto it{std::ranges::find_if( | ||
| 142 | table, [insn](const InstInfo& info) { return (insn & info.Mask()) == info.Value(); })}; | ||
| 143 | if (it == table.end()) { | ||
| 144 | throw NotImplementedException("Instruction 0x{:016x} is unknown / unimplemented", insn); | ||
| 145 | } | ||
| 146 | return it->opcode; | ||
| 147 | } | ||
| 148 | |||
| 149 | } // namespace Shader::Maxwell | ||