From 9b81a8389405e52e6bc48717ecd8bc9527acbfde Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Tue, 7 Nov 2023 10:02:28 -0400 Subject: [PATCH] OpcodeDispatcher: Add DeriveOp helper The "create op with wrong opcode, then change the opcode" pattern is REALLY dangerous. This does not address that. But when we start doing NZCV trickery, it will get /more/ dangerous, and so it's time to add a helper and make the convenient thing the safe(r) thing. This helper correctly saves NZCV /before/ the instruction like the real builders would. It also provides a spot for future safety asserts if someone is motivated. Signed-off-by: Alyssa Rosenzweig --- FEXCore/Source/Interface/Core/OpcodeDispatcher.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/FEXCore/Source/Interface/Core/OpcodeDispatcher.h b/FEXCore/Source/Interface/Core/OpcodeDispatcher.h index 244e28016b..c83e81af14 100644 --- a/FEXCore/Source/Interface/Core/OpcodeDispatcher.h +++ b/FEXCore/Source/Interface/Core/OpcodeDispatcher.h @@ -1329,6 +1329,16 @@ friend class FEXCore::IR::PassManager; } } + // Helper to derive Dest by a given builder-using Expression with the opcode + // replaced with NewOp. Useful for generic building code. Not safe in general. + // but does the right handling of ImplicitFlagClobber at least and must be + // used instead of raw Op mutation. +#define DeriveOp(Dest, NewOp, Expr) \ + if (ImplicitFlagClobber(NewOp)) \ + SaveNZCV(); \ + auto Dest = (Expr); \ + Dest.first->Header.Op = (NewOp) + // Named constant cache for the current block. // Different arrays for sizes 1,2,4,8,16,32. OrderedNode *CachedNamedVectorConstants[FEXCore::IR::NamedVectorConstant::NAMED_VECTOR_MAX][6]{};