shader: Calculate number of arguments in an opcode at compile time

This commit is contained in:
ReinUsesLisp 2021-04-20 22:20:46 -03:00 committed by ameerj
parent dd860b684c
commit 79c2e43fcd
1 changed files with 12 additions and 3 deletions

View File

@ -57,6 +57,17 @@ constexpr std::array META_TABLE{
#undef OPCODE #undef OPCODE
}; };
constexpr size_t CalculateNumArgsOf(Opcode op) {
const auto& arg_types{META_TABLE[static_cast<size_t>(op)].arg_types};
return std::distance(arg_types.begin(), std::ranges::find(arg_types, Type::Void));
}
constexpr std::array NUM_ARGS{
#define OPCODE(name_token, type_token, ...) CalculateNumArgsOf(Opcode::name_token),
#include "opcodes.inc"
#undef OPCODE
};
void ValidateOpcode(Opcode op) { void ValidateOpcode(Opcode op) {
const size_t raw{static_cast<size_t>(op)}; const size_t raw{static_cast<size_t>(op)};
if (raw >= META_TABLE.size()) { if (raw >= META_TABLE.size()) {
@ -72,9 +83,7 @@ Type TypeOf(Opcode op) {
size_t NumArgsOf(Opcode op) { size_t NumArgsOf(Opcode op) {
ValidateOpcode(op); ValidateOpcode(op);
const auto& arg_types{META_TABLE[static_cast<size_t>(op)].arg_types}; return NUM_ARGS[static_cast<size_t>(op)];
const auto distance{std::distance(arg_types.begin(), std::ranges::find(arg_types, Type::Void))};
return static_cast<size_t>(distance);
} }
Type ArgTypeOf(Opcode op, size_t arg_index) { Type ArgTypeOf(Opcode op, size_t arg_index) {