summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/frontend/ir/opcodes.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/frontend/ir/opcodes.h')
-rw-r--r--src/shader_recompiler/frontend/ir/opcodes.h74
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
23namespace Detail {
24
25struct OpcodeMeta {
26 std::string_view name;
27 Type type;
28 std::array<Type, 5> arg_types;
29};
30
31// using enum Type;
32constexpr Type Void{Type::Void};
33constexpr Type Opaque{Type::Opaque};
34constexpr Type Label{Type::Label};
35constexpr Type Reg{Type::Reg};
36constexpr Type Pred{Type::Pred};
37constexpr Type Attribute{Type::Attribute};
38constexpr Type Patch{Type::Patch};
39constexpr Type U1{Type::U1};
40constexpr Type U8{Type::U8};
41constexpr Type U16{Type::U16};
42constexpr Type U32{Type::U32};
43constexpr Type U64{Type::U64};
44constexpr Type F16{Type::F16};
45constexpr Type F32{Type::F32};
46constexpr Type F64{Type::F64};
47constexpr Type U32x2{Type::U32x2};
48constexpr Type U32x3{Type::U32x3};
49constexpr Type U32x4{Type::U32x4};
50constexpr Type F16x2{Type::F16x2};
51constexpr Type F16x3{Type::F16x3};
52constexpr Type F16x4{Type::F16x4};
53constexpr Type F32x2{Type::F32x2};
54constexpr Type F32x3{Type::F32x3};
55constexpr Type F32x4{Type::F32x4};
56constexpr Type F64x2{Type::F64x2};
57constexpr Type F64x3{Type::F64x3};
58constexpr Type F64x4{Type::F64x4};
59
60constexpr 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
71constexpr 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
76constexpr 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);