From 3f8f886f65fe90bcbce09dbe839bd014b759d235 Mon Sep 17 00:00:00 2001 From: "Pierre-Loup A. Griffais" Date: Mon, 14 Sep 2026 21:39:18 -0700 Subject: [PATCH] Frontend: patch cmp imm8 for cache Usually imm8 seem more likely to be a function of the code around them, but this one seems used by Mono for varying data over execution of same code. Maybe an ID of some kind. --- FEXCore/Source/Interface/Core/CodeCache.cpp | 7 ++++-- FEXCore/Source/Interface/Core/Frontend.cpp | 24 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/FEXCore/Source/Interface/Core/CodeCache.cpp b/FEXCore/Source/Interface/Core/CodeCache.cpp index a35fc674d..229d6e9bf 100644 --- a/FEXCore/Source/Interface/Core/CodeCache.cpp +++ b/FEXCore/Source/Interface/Core/CodeCache.cpp @@ -545,8 +545,11 @@ static inline int64_t ReadLiveGuestData(uint64_t SiteAddress, uint8_t ValueSize) uint64_t Raw = 0; memcpy(&Raw, reinterpret_cast(SiteAddress), ValueSize); // manual sign-extension from guest live bytes - // 1/2 sizes not permitted in DetectDataMasks currently - if (ValueSize == 4) { + if (ValueSize == 1) { + return (int8_t)Raw; + } else if (ValueSize == 2) { + return (int16_t)Raw; + } else if (ValueSize == 4) { return (int32_t)Raw; } else { return (int64_t)Raw; diff --git a/FEXCore/Source/Interface/Core/Frontend.cpp b/FEXCore/Source/Interface/Core/Frontend.cpp index 9d1a97746..ac58961b6 100644 --- a/FEXCore/Source/Interface/Core/Frontend.cpp +++ b/FEXCore/Source/Interface/Core/Frontend.cpp @@ -1395,11 +1395,33 @@ bool Decoder::CheckIfCacheable(FEXCore::Core::InternalThreadState& Thread, const } void Decoder::DetectDataMasks(uint64_t OpAddress, DecodedBlocks& Block) { + FEXCore::X86Tables::DecodedOperand* LiteralToPatch = nullptr; + + // cmp *, imm8 - seen varying in mono jitted code + { + FEXCore::X86Tables::ModRMDecoded ModRM; + ModRM.Hex = DecodeInst->ModRM; + if ((DecodeInst->OPRaw == 0x80 || DecodeInst->OPRaw == 0x83) && ModRM.reg == 7 && LastFieldReadSize == 1) { + for (auto& Src : DecodeInst->Src) { + if (Src.IsLiteral()) { + LiteralToPatch = &Src; + break; + } + } + if (LiteralToPatch && LiteralToPatch->Literal() != 0) { + Block.DataMasks.push_back({OpAddress + LastFieldReadOffset, DataMaskType::MOV, LastFieldReadSize}); + + LiteralToPatch->Type = X86Tables::DecodedOperand::OpType::LiteralPatchable; + LiteralToPatch->Data.LiteralPatchable.FieldOffset = LastFieldReadOffset; + LiteralToPatch->Data.LiteralPatchable.Width = LastFieldReadSize; + } + } + } + if (LastFieldReadSize < 4) { return; } - FEXCore::X86Tables::DecodedOperand* LiteralToPatch = nullptr; DataMaskType Type = DataMaskType::NONE; // imm32 or imm64 at the end type instructions