From dd49a04809c8a158aeaa7a6860ef5697e23e7f99 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sat, 17 Oct 2020 20:19:50 -0700 Subject: [PATCH] Fixes RIP literal sign extending on 32bit RIP Literal isn't actually RIP literal on 32bit, it is RIP absolute. 64bit treats the 32bit as a signed offset, while 32bit treats it as a 32bit unsigned offset --- External/FEXCore/Source/Interface/Core/Frontend.cpp | 2 +- .../FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp | 8 ++++---- External/FEXCore/include/FEXCore/Debug/X86Tables.h | 5 ++++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/External/FEXCore/Source/Interface/Core/Frontend.cpp b/External/FEXCore/Source/Interface/Core/Frontend.cpp index 4d9cddf87..8240a86fb 100644 --- a/External/FEXCore/Source/Interface/Core/Frontend.cpp +++ b/External/FEXCore/Source/Interface/Core/Frontend.cpp @@ -424,7 +424,7 @@ bool Decoder::NormalOp(FEXCore::X86Tables::X86InstInfo const *Info, uint16_t Op) Bytes -= 4; NonGPR.TypeRIPLiteral.Type = DecodedOperand::TYPE_RIP_RELATIVE; - NonGPR.TypeRIPLiteral.Literal = Literal; + NonGPR.TypeRIPLiteral.Literal.u = Literal; } else { // Register-direct addressing diff --git a/External/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp b/External/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp index 767c08860..ee76bb6e2 100644 --- a/External/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp +++ b/External/FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp @@ -4505,11 +4505,11 @@ OrderedNode *OpDispatchBuilder::LoadSource_WithOpSize(FEXCore::IR::RegisterClass } else if (Operand.TypeNone.Type == FEXCore::X86Tables::DecodedOperand::TYPE_RIP_RELATIVE) { if (CTX->Config.Is64BitMode) { - Src = _Constant(GPRSize * 8, Operand.TypeRIPLiteral.Literal + Op->PC + Op->InstSize); + Src = _Constant(GPRSize * 8, Operand.TypeRIPLiteral.Literal.s + Op->PC + Op->InstSize); } else { // 32bit this isn't RIP relative but instead absolute - Src = _Constant(GPRSize * 8, Operand.TypeRIPLiteral.Literal); + Src = _Constant(GPRSize * 8, Operand.TypeRIPLiteral.Literal.u); } LoadableType = true; @@ -4637,11 +4637,11 @@ void OpDispatchBuilder::StoreResult_WithOpSize(FEXCore::IR::RegisterClassType Cl } else if (Operand.TypeNone.Type == FEXCore::X86Tables::DecodedOperand::TYPE_RIP_RELATIVE) { if (CTX->Config.Is64BitMode) { - MemStoreDst = _Constant(GPRSize * 8, Operand.TypeRIPLiteral.Literal + Op->PC + Op->InstSize); + MemStoreDst = _Constant(GPRSize * 8, Operand.TypeRIPLiteral.Literal.s + Op->PC + Op->InstSize); } else { // 32bit this isn't RIP relative but instead absolute - MemStoreDst = _Constant(GPRSize * 8, Operand.TypeRIPLiteral.Literal); + MemStoreDst = _Constant(GPRSize * 8, Operand.TypeRIPLiteral.Literal.u); } MemStore = true; } diff --git a/External/FEXCore/include/FEXCore/Debug/X86Tables.h b/External/FEXCore/include/FEXCore/Debug/X86Tables.h index 858b4d93b..c3245dd73 100644 --- a/External/FEXCore/include/FEXCore/Debug/X86Tables.h +++ b/External/FEXCore/include/FEXCore/Debug/X86Tables.h @@ -128,7 +128,10 @@ union DecodedOperand { struct { uint8_t Type; - int32_t Literal; + union { + int32_t s; + uint32_t u; + } Literal; } TypeRIPLiteral; struct {