summaryrefslogtreecommitdiff
path: root/src/shader_recompiler/frontend/ir
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader_recompiler/frontend/ir')
-rw-r--r--src/shader_recompiler/frontend/ir/microinstruction.cpp18
-rw-r--r--src/shader_recompiler/frontend/ir/microinstruction.h13
-rw-r--r--src/shader_recompiler/frontend/ir/opcodes.cpp90
-rw-r--r--src/shader_recompiler/frontend/ir/opcodes.h74
4 files changed, 83 insertions, 112 deletions
diff --git a/src/shader_recompiler/frontend/ir/microinstruction.cpp b/src/shader_recompiler/frontend/ir/microinstruction.cpp
index 7555ac00a..41f9fa0cd 100644
--- a/src/shader_recompiler/frontend/ir/microinstruction.cpp
+++ b/src/shader_recompiler/frontend/ir/microinstruction.cpp
@@ -221,28 +221,10 @@ Inst* Inst::GetAssociatedPseudoOperation(IR::Opcode opcode) {
221 } 221 }
222} 222}
223 223
224size_t Inst::NumArgs() const {
225 return op == Opcode::Phi ? phi_args.size() : NumArgsOf(op);
226}
227
228IR::Type Inst::Type() const { 224IR::Type Inst::Type() const {
229 return TypeOf(op); 225 return TypeOf(op);
230} 226}
231 227
232Value Inst::Arg(size_t index) const {
233 if (op == Opcode::Phi) {
234 if (index >= phi_args.size()) {
235 throw InvalidArgument("Out of bounds argument index {} in phi instruction", index);
236 }
237 return phi_args[index].second;
238 } else {
239 if (index >= NumArgsOf(op)) {
240 throw InvalidArgument("Out of bounds argument index {} in opcode {}", index, op);
241 }
242 return args[index];
243 }
244}
245
246void Inst::SetArg(size_t index, Value value) { 228void Inst::SetArg(size_t index, Value value) {
247 if (index >= NumArgs()) { 229 if (index >= NumArgs()) {
248 throw InvalidArgument("Out of bounds argument index {} in opcode {}", index, op); 230 throw InvalidArgument("Out of bounds argument index {} in opcode {}", index, op);
diff --git a/src/shader_recompiler/frontend/ir/microinstruction.h b/src/shader_recompiler/frontend/ir/microinstruction.h
index dc9f683fe..ea55fc29c 100644
--- a/src/shader_recompiler/frontend/ir/microinstruction.h
+++ b/src/shader_recompiler/frontend/ir/microinstruction.h
@@ -73,10 +73,19 @@ public:
73 [[nodiscard]] IR::Type Type() const; 73 [[nodiscard]] IR::Type Type() const;
74 74
75 /// Get the number of arguments this instruction has. 75 /// Get the number of arguments this instruction has.
76 [[nodiscard]] size_t NumArgs() const; 76 [[nodiscard]] size_t NumArgs() const {
77 return op == Opcode::Phi ? phi_args.size() : NumArgsOf(op);
78 }
77 79
78 /// Get the value of a given argument index. 80 /// Get the value of a given argument index.
79 [[nodiscard]] Value Arg(size_t index) const; 81 [[nodiscard]] Value Arg(size_t index) const noexcept {
82 if (op == Opcode::Phi) {
83 return phi_args[index].second;
84 } else {
85 return args[index];
86 }
87 }
88
80 /// Set the value of a given argument index. 89 /// Set the value of a given argument index.
81 void SetArg(size_t index, Value value); 90 void SetArg(size_t index, Value value);
82 91
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
12namespace Shader::IR { 9namespace Shader::IR {
13namespace {
14struct OpcodeMeta {
15 std::string_view name;
16 Type type;
17 std::array<Type, 5> arg_types;
18};
19
20// using enum Type;
21constexpr Type Void{Type::Void};
22constexpr Type Opaque{Type::Opaque};
23constexpr Type Label{Type::Label};
24constexpr Type Reg{Type::Reg};
25constexpr Type Pred{Type::Pred};
26constexpr Type Attribute{Type::Attribute};
27constexpr Type Patch{Type::Patch};
28constexpr Type U1{Type::U1};
29constexpr Type U8{Type::U8};
30constexpr Type U16{Type::U16};
31constexpr Type U32{Type::U32};
32constexpr Type U64{Type::U64};
33constexpr Type F16{Type::F16};
34constexpr Type F32{Type::F32};
35constexpr Type F64{Type::F64};
36constexpr Type U32x2{Type::U32x2};
37constexpr Type U32x3{Type::U32x3};
38constexpr Type U32x4{Type::U32x4};
39constexpr Type F16x2{Type::F16x2};
40constexpr Type F16x3{Type::F16x3};
41constexpr Type F16x4{Type::F16x4};
42constexpr Type F32x2{Type::F32x2};
43constexpr Type F32x3{Type::F32x3};
44constexpr Type F32x4{Type::F32x4};
45constexpr Type F64x2{Type::F64x2};
46constexpr Type F64x3{Type::F64x3};
47constexpr Type F64x4{Type::F64x4};
48
49constexpr 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
60constexpr 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
65constexpr std::array NUM_ARGS{
66#define OPCODE(name_token, type_token, ...) CalculateNumArgsOf(Opcode::name_token),
67#include "opcodes.inc"
68#undef OPCODE
69};
70
71void 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
79Type TypeOf(Opcode op) {
80 ValidateOpcode(op);
81 return META_TABLE[static_cast<size_t>(op)].type;
82}
83
84size_t NumArgsOf(Opcode op) {
85 ValidateOpcode(op);
86 return NUM_ARGS[static_cast<size_t>(op)];
87}
88
89Type 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
98std::string_view NameOf(Opcode op) { 11std::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
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);