diff options
Diffstat (limited to 'src/shader_recompiler/frontend/ir/opcodes.cpp')
| -rw-r--r-- | src/shader_recompiler/frontend/ir/opcodes.cpp | 90 |
1 files changed, 1 insertions, 89 deletions
diff --git a/src/shader_recompiler/frontend/ir/opcodes.cpp b/src/shader_recompiler/frontend/ir/opcodes.cpp index 4207d548c..24d024ad7 100644 --- a/src/shader_recompiler/frontend/ir/opcodes.cpp +++ b/src/shader_recompiler/frontend/ir/opcodes.cpp | |||
| @@ -2,102 +2,14 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | ||
| 6 | #include <array> | ||
| 7 | #include <string_view> | 5 | #include <string_view> |
| 8 | 6 | ||
| 9 | #include "shader_recompiler/exception.h" | ||
| 10 | #include "shader_recompiler/frontend/ir/opcodes.h" | 7 | #include "shader_recompiler/frontend/ir/opcodes.h" |
| 11 | 8 | ||
| 12 | namespace Shader::IR { | 9 | namespace Shader::IR { |
| 13 | namespace { | ||
| 14 | struct OpcodeMeta { | ||
| 15 | std::string_view name; | ||
| 16 | Type type; | ||
| 17 | std::array<Type, 5> arg_types; | ||
| 18 | }; | ||
| 19 | |||
| 20 | // using enum Type; | ||
| 21 | constexpr Type Void{Type::Void}; | ||
| 22 | constexpr Type Opaque{Type::Opaque}; | ||
| 23 | constexpr Type Label{Type::Label}; | ||
| 24 | constexpr Type Reg{Type::Reg}; | ||
| 25 | constexpr Type Pred{Type::Pred}; | ||
| 26 | constexpr Type Attribute{Type::Attribute}; | ||
| 27 | constexpr Type Patch{Type::Patch}; | ||
| 28 | constexpr Type U1{Type::U1}; | ||
| 29 | constexpr Type U8{Type::U8}; | ||
| 30 | constexpr Type U16{Type::U16}; | ||
| 31 | constexpr Type U32{Type::U32}; | ||
| 32 | constexpr Type U64{Type::U64}; | ||
| 33 | constexpr Type F16{Type::F16}; | ||
| 34 | constexpr Type F32{Type::F32}; | ||
| 35 | constexpr Type F64{Type::F64}; | ||
| 36 | constexpr Type U32x2{Type::U32x2}; | ||
| 37 | constexpr Type U32x3{Type::U32x3}; | ||
| 38 | constexpr Type U32x4{Type::U32x4}; | ||
| 39 | constexpr Type F16x2{Type::F16x2}; | ||
| 40 | constexpr Type F16x3{Type::F16x3}; | ||
| 41 | constexpr Type F16x4{Type::F16x4}; | ||
| 42 | constexpr Type F32x2{Type::F32x2}; | ||
| 43 | constexpr Type F32x3{Type::F32x3}; | ||
| 44 | constexpr Type F32x4{Type::F32x4}; | ||
| 45 | constexpr Type F64x2{Type::F64x2}; | ||
| 46 | constexpr Type F64x3{Type::F64x3}; | ||
| 47 | constexpr Type F64x4{Type::F64x4}; | ||
| 48 | |||
| 49 | constexpr std::array META_TABLE{ | ||
| 50 | #define OPCODE(name_token, type_token, ...) \ | ||
| 51 | OpcodeMeta{ \ | ||
| 52 | .name{#name_token}, \ | ||
| 53 | .type = type_token, \ | ||
| 54 | .arg_types{__VA_ARGS__}, \ | ||
| 55 | }, | ||
| 56 | #include "opcodes.inc" | ||
| 57 | #undef OPCODE | ||
| 58 | }; | ||
| 59 | |||
| 60 | constexpr size_t CalculateNumArgsOf(Opcode op) { | ||
| 61 | const auto& arg_types{META_TABLE[static_cast<size_t>(op)].arg_types}; | ||
| 62 | return std::distance(arg_types.begin(), std::ranges::find(arg_types, Type::Void)); | ||
| 63 | } | ||
| 64 | |||
| 65 | constexpr std::array NUM_ARGS{ | ||
| 66 | #define OPCODE(name_token, type_token, ...) CalculateNumArgsOf(Opcode::name_token), | ||
| 67 | #include "opcodes.inc" | ||
| 68 | #undef OPCODE | ||
| 69 | }; | ||
| 70 | |||
| 71 | void ValidateOpcode(Opcode op) { | ||
| 72 | const size_t raw{static_cast<size_t>(op)}; | ||
| 73 | if (raw >= META_TABLE.size()) { | ||
| 74 | throw InvalidArgument("Invalid opcode with raw value {}", raw); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } // Anonymous namespace | ||
| 78 | |||
| 79 | Type TypeOf(Opcode op) { | ||
| 80 | ValidateOpcode(op); | ||
| 81 | return META_TABLE[static_cast<size_t>(op)].type; | ||
| 82 | } | ||
| 83 | |||
| 84 | size_t NumArgsOf(Opcode op) { | ||
| 85 | ValidateOpcode(op); | ||
| 86 | return NUM_ARGS[static_cast<size_t>(op)]; | ||
| 87 | } | ||
| 88 | |||
| 89 | Type ArgTypeOf(Opcode op, size_t arg_index) { | ||
| 90 | ValidateOpcode(op); | ||
| 91 | const auto& arg_types{META_TABLE[static_cast<size_t>(op)].arg_types}; | ||
| 92 | if (arg_index >= arg_types.size() || arg_types[arg_index] == Type::Void) { | ||
| 93 | throw InvalidArgument("Out of bounds argument"); | ||
| 94 | } | ||
| 95 | return arg_types[arg_index]; | ||
| 96 | } | ||
| 97 | 10 | ||
| 98 | std::string_view NameOf(Opcode op) { | 11 | std::string_view NameOf(Opcode op) { |
| 99 | ValidateOpcode(op); | 12 | return Detail::META_TABLE[static_cast<size_t>(op)].name; |
| 100 | return META_TABLE[static_cast<size_t>(op)].name; | ||
| 101 | } | 13 | } |
| 102 | 14 | ||
| 103 | } // namespace Shader::IR | 15 | } // namespace Shader::IR |