summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/frontend/ir/opcodes.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/frontend/ir/opcodes.cpp')
-rw-r--r--src/shader_recompiler/frontend/ir/opcodes.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/shader_recompiler/frontend/ir/opcodes.cpp b/src/shader_recompiler/frontend/ir/opcodes.cpp
new file mode 100644
index 000000000..1f188411a
--- /dev/null
+++ b/src/shader_recompiler/frontend/ir/opcodes.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/opcodes.h"
11
12namespace Shader::IR {
13namespace {
14struct OpcodeMeta {
15 std::string_view name;
16 Type type;
17 std::array<Type, 4> arg_types;
18};
19
20using enum Type;
21
22constexpr 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 "opcodes.inc"
30#undef OPCODE
31};
32
33void 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
41Type TypeOf(Opcode op) {
42 ValidateOpcode(op);
43 return META_TABLE[static_cast<size_t>(op)].type;
44}
45
46size_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
53Type 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
62std::string_view NameOf(Opcode op) {
63 ValidateOpcode(op);
64 return META_TABLE[static_cast<size_t>(op)].name;
65}
66
67} // namespace Shader::IR