From a0c2ce006866cd7a81a6a8637df601ee3e5b341a Mon Sep 17 00:00:00 2001 From: Lioncache Date: Fri, 3 Oct 2025 14:15:11 -0400 Subject: [PATCH] EnumUtils: Add define for default passthrough formatting Handles a normal case where printing an enum type as an integral value is still desirable. Mainly just a way to reduce boilerplate. --- .../Interface/Core/X86Tables/X86Tables.h | 9 +--- FEXCore/Source/Interface/IR/IR.h | 43 ++----------------- FEXCore/include/FEXCore/Utils/EnumUtils.h | 14 ++++++ 3 files changed, 20 insertions(+), 46 deletions(-) diff --git a/FEXCore/Source/Interface/Core/X86Tables/X86Tables.h b/FEXCore/Source/Interface/Core/X86Tables/X86Tables.h index 5a6ae05b3..f88dc25eb 100644 --- a/FEXCore/Source/Interface/Core/X86Tables/X86Tables.h +++ b/FEXCore/Source/Interface/Core/X86Tables/X86Tables.h @@ -7,6 +7,7 @@ $end_info$ #pragma once +#include #include #include @@ -606,10 +607,4 @@ constexpr static inline void GenerateX87Table(X86InstInfo *FinalTable, X86Tables } -template <> -struct fmt::formatter : formatter { - template - auto format(FEXCore::X86Tables::DecodedOperand::OpType type, FormatContext& ctx) const { - return fmt::formatter::format(static_cast(type), ctx); - } -}; +FEX_DEFINE_ENUM_FMT_PASSTHROUGH(FEXCore::X86Tables::DecodedOperand::OpType); diff --git a/FEXCore/Source/Interface/IR/IR.h b/FEXCore/Source/Interface/IR/IR.h index d3fa9ebd4..9d92963ce 100644 --- a/FEXCore/Source/Interface/IR/IR.h +++ b/FEXCore/Source/Interface/IR/IR.h @@ -715,42 +715,7 @@ struct fmt::formatter : fmt::formatter -struct fmt::formatter : fmt::formatter> { - using Base = fmt::formatter>; - - template - auto format(const FEXCore::IR::RegClass& Class, FormatContext& ctx) const { - return Base::format(FEXCore::ToUnderlying(Class), ctx); - } -}; - -template<> -struct fmt::formatter : fmt::formatter> { - using Base = fmt::formatter>; - - template - auto format(const FEXCore::IR::FenceType& Fence, FormatContext& ctx) const { - return Base::format(FEXCore::ToUnderlying(Fence), ctx); - } -}; - -template<> -struct fmt::formatter : fmt::formatter> { - using Base = fmt::formatter>; - - template - auto format(const FEXCore::IR::MemOffsetType& Type, FormatContext& ctx) const { - return Base::format(FEXCore::ToUnderlying(Type), ctx); - } -}; - -template<> -struct fmt::formatter : fmt::formatter> { - using Base = fmt::formatter>; - - template - auto format(const FEXCore::IR::OpSize& OpSize, FormatContext& ctx) const { - return Base::format(FEXCore::ToUnderlying(OpSize), ctx); - } -}; +FEX_DEFINE_ENUM_FMT_PASSTHROUGH(FEXCore::IR::FenceType); +FEX_DEFINE_ENUM_FMT_PASSTHROUGH(FEXCore::IR::MemOffsetType); +FEX_DEFINE_ENUM_FMT_PASSTHROUGH(FEXCore::IR::OpSize); +FEX_DEFINE_ENUM_FMT_PASSTHROUGH(FEXCore::IR::RegClass); diff --git a/FEXCore/include/FEXCore/Utils/EnumUtils.h b/FEXCore/include/FEXCore/Utils/EnumUtils.h index 26723b198..aedb16212 100644 --- a/FEXCore/include/FEXCore/Utils/EnumUtils.h +++ b/FEXCore/include/FEXCore/Utils/EnumUtils.h @@ -3,6 +3,7 @@ // Header for various utilities related to operating with enums +#include #include namespace FEXCore { @@ -54,6 +55,19 @@ namespace FEXCore { return static_cast(key) == 0; \ } +// Macro that defines a fmt formatter for a reasonable case where an enum +// is formatted as a purely integral type based on its underlying type. +#define FEX_DEFINE_ENUM_FMT_PASSTHROUGH(type) \ + template<> \ + struct fmt::formatter : fmt::formatter> { \ + using Base = fmt::formatter>; \ + \ + template \ + auto format(const type& Value, FormatContext& ctx) const { \ + return Base::format(FEXCore::ToUnderlying(Value), ctx); \ + } \ + } + // Equivalent to C++23's std::to_underlying. template [[nodiscard]]