From e397754587fb05f284c3ee46bed732794b538da8 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 30 Jan 2021 11:22:46 -0800 Subject: [PATCH 1/2] Core: Correct branch analysis truncation. --- Core/MIPS/MIPSCodeUtils.cpp | 69 +++++++++++++++---------------------- 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/Core/MIPS/MIPSCodeUtils.cpp b/Core/MIPS/MIPSCodeUtils.cpp index fd83603f492f..97aa88309b07 100644 --- a/Core/MIPS/MIPSCodeUtils.cpp +++ b/Core/MIPS/MIPSCodeUtils.cpp @@ -28,72 +28,59 @@ namespace MIPSCodeUtils #define OP_SYSCALL_MASK 0xFC00003F #define _RS ((op>>21) & 0x1F) #define _RT ((op>>16) & 0x1F) +#define _IMM16 (signed short)(op & 0xFFFF) +#define _IMM26 (op & 0x03FFFFFF) +#define TARGET16 ((int)((uint32_t)_IMM16 << 2)) +#define TARGET26 (_IMM26 << 2) - u32 GetJumpTarget(u32 addr) - { + u32 GetJumpTarget(u32 addr) { MIPSOpcode op = Memory::Read_Instruction(addr, true); - if (op != 0) - { + if (op != 0) { MIPSInfo info = MIPSGetInfo(op); if ((info & IS_JUMP) && (info & IN_IMM26)) - { - u32 target = (addr & 0xF0000000) | ((op&0x03FFFFFF) << 2); - return target; - } + return (addr & 0xF0000000) | TARGET26; else return INVALIDTARGET; - } - else + } else { return INVALIDTARGET; + } } - u32 GetBranchTarget(u32 addr) - { + u32 GetBranchTarget(u32 addr) { MIPSOpcode op = Memory::Read_Instruction(addr, true); - if (op != 0) - { + if (op != 0) { MIPSInfo info = MIPSGetInfo(op); if (info & IS_CONDBRANCH) - { - return addr + 4 + ((signed short)((op&0xFFFF)<<2)); - } + return addr + 4 + TARGET16; else return INVALIDTARGET; - } - else + } else { return INVALIDTARGET; + } } - u32 GetBranchTargetNoRA(u32 addr) - { + u32 GetBranchTargetNoRA(u32 addr) { MIPSOpcode op = Memory::Read_Instruction(addr, true); return GetBranchTargetNoRA(addr, op); } - u32 GetBranchTargetNoRA(u32 addr, MIPSOpcode op) - { - if (op != 0) - { + u32 GetBranchTargetNoRA(u32 addr, MIPSOpcode op) { + if (op != 0) { MIPSInfo info = MIPSGetInfo(op); if ((info & IS_CONDBRANCH) && !(info & OUT_RA)) - { - return addr + 4 + ((signed short)((op&0xFFFF)<<2)); - } + return addr + 4 + TARGET16; else return INVALIDTARGET; - } - else + } else { return INVALIDTARGET; + } } - u32 GetSureBranchTarget(u32 addr) - { + u32 GetSureBranchTarget(u32 addr) { MIPSOpcode op = Memory::Read_Instruction(addr, true); - if (op != 0) - { + if (op != 0) { MIPSInfo info = MIPSGetInfo(op); - if ((info & IS_CONDBRANCH) && !(info & (IN_FPUFLAG | IS_VFPU))) - { + if ((info & IS_CONDBRANCH) && !(info & (IN_FPUFLAG | IS_VFPU))) { bool sure; bool takeBranch; switch (info & CONDTYPE_MASK) @@ -125,17 +112,17 @@ namespace MIPSCodeUtils } if (sure && takeBranch) - return addr + 4 + ((signed short)((op&0xFFFF)<<2)); + return addr + 4 + TARGET16; else if (sure && !takeBranch) return addr + 8; else return INVALIDTARGET; - } - else + } else { return INVALIDTARGET; - } - else + } + } else { return INVALIDTARGET; + } } bool IsVFPUBranch(MIPSOpcode op) { From e99c69f19e3f48da08d269b0b1a06caee62d984c Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 30 Jan 2021 11:40:32 -0800 Subject: [PATCH 2/2] jit: Be very clear on sign extension. --- Core/MIPS/ARM/ArmCompBranch.cpp | 2 +- Core/MIPS/ARM64/Arm64CompBranch.cpp | 2 +- Core/MIPS/IR/IRCompBranch.cpp | 2 +- Core/MIPS/MIPSCodeUtils.cpp | 2 +- Core/MIPS/x86/CompBranch.cpp | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Core/MIPS/ARM/ArmCompBranch.cpp b/Core/MIPS/ARM/ArmCompBranch.cpp index 1780547b3f8b..25151c1729e0 100644 --- a/Core/MIPS/ARM/ArmCompBranch.cpp +++ b/Core/MIPS/ARM/ArmCompBranch.cpp @@ -49,7 +49,7 @@ #define _SIZE ((op>>11) & 0x1F) #define _IMM16 (signed short)(op & 0xFFFF) #define _IMM26 (op & 0x03FFFFFF) -#define TARGET16 ((int)((uint32_t)_IMM16 << 2)) +#define TARGET16 ((int)((uint32_t)(int)_IMM16 << 2)) #define TARGET26 (_IMM26 << 2) #define LOOPOPTIMIZATION 0 diff --git a/Core/MIPS/ARM64/Arm64CompBranch.cpp b/Core/MIPS/ARM64/Arm64CompBranch.cpp index 9212d0fc9bac..e3914ee1a63e 100644 --- a/Core/MIPS/ARM64/Arm64CompBranch.cpp +++ b/Core/MIPS/ARM64/Arm64CompBranch.cpp @@ -49,7 +49,7 @@ #define _SIZE ((op>>11) & 0x1F) #define _IMM16 (signed short)(op & 0xFFFF) #define _IMM26 (op & 0x03FFFFFF) -#define TARGET16 ((int)((uint32_t)_IMM16 << 2)) +#define TARGET16 ((int)((uint32_t)(int)_IMM16 << 2)) #define TARGET26 (_IMM26 << 2) #define LOOPOPTIMIZATION 0 diff --git a/Core/MIPS/IR/IRCompBranch.cpp b/Core/MIPS/IR/IRCompBranch.cpp index 959b7403ceff..16a158b9fe61 100644 --- a/Core/MIPS/IR/IRCompBranch.cpp +++ b/Core/MIPS/IR/IRCompBranch.cpp @@ -42,7 +42,7 @@ #define _SIZE ((op>>11) & 0x1F) #define _IMM16 (signed short)(op & 0xFFFF) #define _IMM26 (op & 0x03FFFFFF) -#define TARGET16 ((int)((uint32_t)_IMM16 << 2)) +#define TARGET16 ((int)((uint32_t)(int)_IMM16 << 2)) #define TARGET26 (_IMM26 << 2) #define LOOPOPTIMIZATION 0 diff --git a/Core/MIPS/MIPSCodeUtils.cpp b/Core/MIPS/MIPSCodeUtils.cpp index 97aa88309b07..5f1253ee0ada 100644 --- a/Core/MIPS/MIPSCodeUtils.cpp +++ b/Core/MIPS/MIPSCodeUtils.cpp @@ -30,7 +30,7 @@ namespace MIPSCodeUtils #define _RT ((op>>16) & 0x1F) #define _IMM16 (signed short)(op & 0xFFFF) #define _IMM26 (op & 0x03FFFFFF) -#define TARGET16 ((int)((uint32_t)_IMM16 << 2)) +#define TARGET16 ((int)((uint32_t)(int)_IMM16 << 2)) #define TARGET26 (_IMM26 << 2) u32 GetJumpTarget(u32 addr) { diff --git a/Core/MIPS/x86/CompBranch.cpp b/Core/MIPS/x86/CompBranch.cpp index 09b0cc09a146..4910e2965536 100644 --- a/Core/MIPS/x86/CompBranch.cpp +++ b/Core/MIPS/x86/CompBranch.cpp @@ -48,7 +48,7 @@ #define _SIZE ((op>>11) & 0x1F) #define _IMM16 (signed short)(op & 0xFFFF) #define _IMM26 (op & 0x03FFFFFF) -#define TARGET16 ((int)((uint32_t)_IMM16 << 2)) +#define TARGET16 ((int)((uint32_t)(int)_IMM16 << 2)) #define TARGET26 (_IMM26 << 2) #define LOOPOPTIMIZATION 0