diff options
| author | 2021-01-09 03:30:07 -0300 | |
|---|---|---|
| committer | 2021-07-22 21:51:21 -0400 | |
| commit | 2d48a7b4d0666ad16d03a22d85712617a0849046 (patch) | |
| tree | dd1069afca86f66e77e3438da77421a43adf5091 /src/shader_recompiler/frontend/ir/opcode.cpp | |
| parent | thread_worker: Fix compile time error (diff) | |
| download | yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.tar.gz yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.tar.xz yuzu-2d48a7b4d0666ad16d03a22d85712617a0849046.zip | |
shader: Initial recompiler work
Diffstat (limited to 'src/shader_recompiler/frontend/ir/opcode.cpp')
| -rw-r--r-- | src/shader_recompiler/frontend/ir/opcode.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/shader_recompiler/frontend/ir/opcode.cpp b/src/shader_recompiler/frontend/ir/opcode.cpp new file mode 100644 index 000000000..65d074029 --- /dev/null +++ b/src/shader_recompiler/frontend/ir/opcode.cpp | |||
| @@ -0,0 +1,67 @@ | |||
| 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 <string_view> | ||
| 8 | |||
| 9 | #include "shader_recompiler/exception.h" | ||
| 10 | #include "shader_recompiler/frontend/ir/opcode.h" | ||
| 11 | |||
| 12 | namespace Shader::IR { | ||
| 13 | namespace { | ||
| 14 | struct OpcodeMeta { | ||
| 15 | std::string_view name; | ||
| 16 | Type type; | ||
| 17 | std::array<Type, 4> arg_types; | ||
| 18 | }; | ||
| 19 | |||
| 20 | using enum Type; | ||
| 21 | |||
| 22 | constexpr std::array META_TABLE{ | ||
| 23 | #define OPCODE(name_token, type_token, ...) \ | ||
| 24 | OpcodeMeta{ \ | ||
| 25 | .name{#name_token}, \ | ||
| 26 | .type{type_token}, \ | ||
| 27 | .arg_types{__VA_ARGS__}, \ | ||
| 28 | }, | ||
| 29 | #include "opcode.inc" | ||
| 30 | #undef OPCODE | ||
| 31 | }; | ||
| 32 | |||
| 33 | void ValidateOpcode(Opcode op) { | ||
| 34 | const size_t raw{static_cast<size_t>(op)}; | ||
| 35 | if (raw >= META_TABLE.size()) { | ||
| 36 | throw InvalidArgument("Invalid opcode with raw value {}", raw); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } // Anonymous namespace | ||
| 40 | |||
| 41 | Type TypeOf(Opcode op) { | ||
| 42 | ValidateOpcode(op); | ||
| 43 | return META_TABLE[static_cast<size_t>(op)].type; | ||
| 44 | } | ||
| 45 | |||
| 46 | size_t NumArgsOf(Opcode op) { | ||
| 47 | ValidateOpcode(op); | ||
| 48 | const auto& arg_types{META_TABLE[static_cast<size_t>(op)].arg_types}; | ||
| 49 | const auto distance{std::distance(arg_types.begin(), std::ranges::find(arg_types, Type::Void))}; | ||
| 50 | return static_cast<size_t>(distance); | ||
| 51 | } | ||
| 52 | |||
| 53 | Type ArgTypeOf(Opcode op, size_t arg_index) { | ||
| 54 | ValidateOpcode(op); | ||
| 55 | const auto& arg_types{META_TABLE[static_cast<size_t>(op)].arg_types}; | ||
| 56 | if (arg_index >= arg_types.size() || arg_types[arg_index] == Type::Void) { | ||
| 57 | throw InvalidArgument("Out of bounds argument"); | ||
| 58 | } | ||
| 59 | return arg_types[arg_index]; | ||
| 60 | } | ||
| 61 | |||
| 62 | std::string_view NameOf(Opcode op) { | ||
| 63 | ValidateOpcode(op); | ||
| 64 | return META_TABLE[static_cast<size_t>(op)].name; | ||
| 65 | } | ||
| 66 | |||
| 67 | } // namespace Shader::IR | ||