diff options
Diffstat (limited to '')
| -rw-r--r-- | src/shader_recompiler/frontend/ir/opcodes.h | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/shader_recompiler/frontend/ir/opcodes.h b/src/shader_recompiler/frontend/ir/opcodes.h new file mode 100644 index 000000000..9ab108292 --- /dev/null +++ b/src/shader_recompiler/frontend/ir/opcodes.h | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <array> | ||
| 9 | #include <string_view> | ||
| 10 | |||
| 11 | #include <fmt/format.h> | ||
| 12 | |||
| 13 | #include "shader_recompiler/frontend/ir/type.h" | ||
| 14 | |||
| 15 | namespace Shader::IR { | ||
| 16 | |||
| 17 | enum class Opcode { | ||
| 18 | #define OPCODE(name, ...) name, | ||
| 19 | #include "opcodes.inc" | ||
| 20 | #undef OPCODE | ||
| 21 | }; | ||
| 22 | |||
| 23 | namespace Detail { | ||
| 24 | struct OpcodeMeta { | ||
| 25 | std::string_view name; | ||
| 26 | Type type; | ||
| 27 | std::array<Type, 5> arg_types; | ||
| 28 | }; | ||
| 29 | |||
| 30 | // using enum Type; | ||
| 31 | constexpr Type Void{Type::Void}; | ||
| 32 | constexpr Type Opaque{Type::Opaque}; | ||
| 33 | constexpr Type Reg{Type::Reg}; | ||
| 34 | constexpr Type Pred{Type::Pred}; | ||
| 35 | constexpr Type Attribute{Type::Attribute}; | ||
| 36 | constexpr Type Patch{Type::Patch}; | ||
| 37 | constexpr Type U1{Type::U1}; | ||
| 38 | constexpr Type U8{Type::U8}; | ||
| 39 | constexpr Type U16{Type::U16}; | ||
| 40 | constexpr Type U32{Type::U32}; | ||
| 41 | constexpr Type U64{Type::U64}; | ||
| 42 | constexpr Type F16{Type::F16}; | ||
| 43 | constexpr Type F32{Type::F32}; | ||
| 44 | constexpr Type F64{Type::F64}; | ||
| 45 | constexpr Type U32x2{Type::U32x2}; | ||
| 46 | constexpr Type U32x3{Type::U32x3}; | ||
| 47 | constexpr Type U32x4{Type::U32x4}; | ||
| 48 | constexpr Type F16x2{Type::F16x2}; | ||
| 49 | constexpr Type F16x3{Type::F16x3}; | ||
| 50 | constexpr Type F16x4{Type::F16x4}; | ||
| 51 | constexpr Type F32x2{Type::F32x2}; | ||
| 52 | constexpr Type F32x3{Type::F32x3}; | ||
| 53 | constexpr Type F32x4{Type::F32x4}; | ||
| 54 | constexpr Type F64x2{Type::F64x2}; | ||
| 55 | constexpr Type F64x3{Type::F64x3}; | ||
| 56 | constexpr Type F64x4{Type::F64x4}; | ||
| 57 | |||
| 58 | constexpr OpcodeMeta META_TABLE[]{ | ||
| 59 | #define OPCODE(name_token, type_token, ...) \ | ||
| 60 | { \ | ||
| 61 | .name{#name_token}, \ | ||
| 62 | .type = type_token, \ | ||
| 63 | .arg_types{__VA_ARGS__}, \ | ||
| 64 | }, | ||
| 65 | #include "opcodes.inc" | ||
| 66 | #undef OPCODE | ||
| 67 | }; | ||
| 68 | constexpr size_t CalculateNumArgsOf(Opcode op) { | ||
| 69 | const auto& arg_types{META_TABLE[static_cast<size_t>(op)].arg_types}; | ||
| 70 | return static_cast<size_t>( | ||
| 71 | std::distance(arg_types.begin(), std::ranges::find(arg_types, Type::Void))); | ||
| 72 | } | ||
| 73 | |||
| 74 | constexpr u8 NUM_ARGS[]{ | ||
| 75 | #define OPCODE(name_token, type_token, ...) static_cast<u8>(CalculateNumArgsOf(Opcode::name_token)), | ||
| 76 | #include "opcodes.inc" | ||
| 77 | #undef OPCODE | ||
| 78 | }; | ||
| 79 | } // namespace Detail | ||
| 80 | |||
| 81 | /// Get return type of an opcode | ||
| 82 | [[nodiscard]] inline Type TypeOf(Opcode op) noexcept { | ||
| 83 | return Detail::META_TABLE[static_cast<size_t>(op)].type; | ||
| 84 | } | ||
| 85 | |||
| 86 | /// Get the number of arguments an opcode accepts | ||
| 87 | [[nodiscard]] inline size_t NumArgsOf(Opcode op) noexcept { | ||
| 88 | return static_cast<size_t>(Detail::NUM_ARGS[static_cast<size_t>(op)]); | ||
| 89 | } | ||
| 90 | |||
| 91 | /// Get the required type of an argument of an opcode | ||
| 92 | [[nodiscard]] inline Type ArgTypeOf(Opcode op, size_t arg_index) noexcept { | ||
| 93 | return Detail::META_TABLE[static_cast<size_t>(op)].arg_types[arg_index]; | ||
| 94 | } | ||
| 95 | |||
| 96 | /// Get the name of an opcode | ||
| 97 | [[nodiscard]] std::string_view NameOf(Opcode op); | ||
| 98 | |||
| 99 | } // namespace Shader::IR | ||
| 100 | |||
| 101 | template <> | ||
| 102 | struct fmt::formatter<Shader::IR::Opcode> { | ||
| 103 | constexpr auto parse(format_parse_context& ctx) { | ||
| 104 | return ctx.begin(); | ||
| 105 | } | ||
| 106 | template <typename FormatContext> | ||
| 107 | auto format(const Shader::IR::Opcode& op, FormatContext& ctx) { | ||
| 108 | return format_to(ctx.out(), "{}", Shader::IR::NameOf(op)); | ||
| 109 | } | ||
| 110 | }; | ||