diff options
Diffstat (limited to 'src/shader_recompiler/frontend/ir/opcodes.h')
| -rw-r--r-- | src/shader_recompiler/frontend/ir/opcodes.h | 74 |
1 files changed, 71 insertions, 3 deletions
diff --git a/src/shader_recompiler/frontend/ir/opcodes.h b/src/shader_recompiler/frontend/ir/opcodes.h index 999fb2e77..b5697c7f9 100644 --- a/src/shader_recompiler/frontend/ir/opcodes.h +++ b/src/shader_recompiler/frontend/ir/opcodes.h | |||
| @@ -4,6 +4,8 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <array> | ||
| 8 | #include <algorithm> | ||
| 7 | #include <string_view> | 9 | #include <string_view> |
| 8 | 10 | ||
| 9 | #include <fmt/format.h> | 11 | #include <fmt/format.h> |
| @@ -18,14 +20,80 @@ enum class Opcode { | |||
| 18 | #undef OPCODE | 20 | #undef OPCODE |
| 19 | }; | 21 | }; |
| 20 | 22 | ||
| 23 | namespace Detail { | ||
| 24 | |||
| 25 | struct OpcodeMeta { | ||
| 26 | std::string_view name; | ||
| 27 | Type type; | ||
| 28 | std::array<Type, 5> arg_types; | ||
| 29 | }; | ||
| 30 | |||
| 31 | // using enum Type; | ||
| 32 | constexpr Type Void{Type::Void}; | ||
| 33 | constexpr Type Opaque{Type::Opaque}; | ||
| 34 | constexpr Type Label{Type::Label}; | ||
| 35 | constexpr Type Reg{Type::Reg}; | ||
| 36 | constexpr Type Pred{Type::Pred}; | ||
| 37 | constexpr Type Attribute{Type::Attribute}; | ||
| 38 | constexpr Type Patch{Type::Patch}; | ||
| 39 | constexpr Type U1{Type::U1}; | ||
| 40 | constexpr Type U8{Type::U8}; | ||
| 41 | constexpr Type U16{Type::U16}; | ||
| 42 | constexpr Type U32{Type::U32}; | ||
| 43 | constexpr Type U64{Type::U64}; | ||
| 44 | constexpr Type F16{Type::F16}; | ||
| 45 | constexpr Type F32{Type::F32}; | ||
| 46 | constexpr Type F64{Type::F64}; | ||
| 47 | constexpr Type U32x2{Type::U32x2}; | ||
| 48 | constexpr Type U32x3{Type::U32x3}; | ||
| 49 | constexpr Type U32x4{Type::U32x4}; | ||
| 50 | constexpr Type F16x2{Type::F16x2}; | ||
| 51 | constexpr Type F16x3{Type::F16x3}; | ||
| 52 | constexpr Type F16x4{Type::F16x4}; | ||
| 53 | constexpr Type F32x2{Type::F32x2}; | ||
| 54 | constexpr Type F32x3{Type::F32x3}; | ||
| 55 | constexpr Type F32x4{Type::F32x4}; | ||
| 56 | constexpr Type F64x2{Type::F64x2}; | ||
| 57 | constexpr Type F64x3{Type::F64x3}; | ||
| 58 | constexpr Type F64x4{Type::F64x4}; | ||
| 59 | |||
| 60 | constexpr std::array META_TABLE{ | ||
| 61 | #define OPCODE(name_token, type_token, ...) \ | ||
| 62 | OpcodeMeta{ \ | ||
| 63 | .name{#name_token}, \ | ||
| 64 | .type = type_token, \ | ||
| 65 | .arg_types{__VA_ARGS__}, \ | ||
| 66 | }, | ||
| 67 | #include "opcodes.inc" | ||
| 68 | #undef OPCODE | ||
| 69 | }; | ||
| 70 | |||
| 71 | constexpr size_t CalculateNumArgsOf(Opcode op) { | ||
| 72 | const auto& arg_types{META_TABLE[static_cast<size_t>(op)].arg_types}; | ||
| 73 | return std::distance(arg_types.begin(), std::ranges::find(arg_types, Type::Void)); | ||
| 74 | } | ||
| 75 | |||
| 76 | constexpr std::array NUM_ARGS{ | ||
| 77 | #define OPCODE(name_token, type_token, ...) CalculateNumArgsOf(Opcode::name_token), | ||
| 78 | #include "opcodes.inc" | ||
| 79 | #undef OPCODE | ||
| 80 | }; | ||
| 81 | } // namespace Detail | ||
| 82 | |||
| 21 | /// Get return type of an opcode | 83 | /// Get return type of an opcode |
| 22 | [[nodiscard]] Type TypeOf(Opcode op); | 84 | [[nodiscard]] inline Type TypeOf(Opcode op) noexcept { |
| 85 | return Detail::META_TABLE[static_cast<size_t>(op)].type; | ||
| 86 | } | ||
| 23 | 87 | ||
| 24 | /// Get the number of arguments an opcode accepts | 88 | /// Get the number of arguments an opcode accepts |
| 25 | [[nodiscard]] size_t NumArgsOf(Opcode op); | 89 | [[nodiscard]] inline size_t NumArgsOf(Opcode op) noexcept { |
| 90 | return Detail::NUM_ARGS[static_cast<size_t>(op)]; | ||
| 91 | } | ||
| 26 | 92 | ||
| 27 | /// Get the required type of an argument of an opcode | 93 | /// Get the required type of an argument of an opcode |
| 28 | [[nodiscard]] Type ArgTypeOf(Opcode op, size_t arg_index); | 94 | [[nodiscard]] inline Type ArgTypeOf(Opcode op, size_t arg_index) noexcept { |
| 95 | return Detail::META_TABLE[static_cast<size_t>(op)].arg_types[arg_index]; | ||
| 96 | } | ||
| 29 | 97 | ||
| 30 | /// Get the name of an opcode | 98 | /// Get the name of an opcode |
| 31 | [[nodiscard]] std::string_view NameOf(Opcode op); | 99 | [[nodiscard]] std::string_view NameOf(Opcode op); |