Skip to content

codegen: use GPR_S64 for EE sign-tested branches (BLEZ/BGTZ/BLTZ/BGEZ + variants)#114

Closed
jlsandri wants to merge 1 commit intoran-j:mainfrom
jlsandri:pr/codegen-ee-branch-gpr-s64
Closed

codegen: use GPR_S64 for EE sign-tested branches (BLEZ/BGTZ/BLTZ/BGEZ + variants)#114
jlsandri wants to merge 1 commit intoran-j:mainfrom
jlsandri:pr/codegen-ee-branch-gpr-s64

Conversation

@jlsandri
Copy link
Copy Markdown

@jlsandri jlsandri commented Apr 6, 2026

Problem

The codegen for EE sign-tested branches — BLEZ, BGTZ, BLTZ, BGEZ, and all of their *L (likely) and *AL (link) variants — was comparing GPR_S32, i.e. only the low 32 bits of the register treated as signed.

The PS2 EE evaluates these branches against the full 64-bit GPR. A GPR with non-zero upper bits whose low 32 bits happened to look positive (or negative) under truncation would branch the opposite way from how real hardware does.

Fix

Switch the comparisons for all eight instructions (BLEZ, BGTZ, BLTZ, BGEZ, BLEZL, BGTZL, BLTZL, BGEZL — plus BLTZAL, BGEZAL and their likely variants) from GPR_S32 to GPR_S64.

Scope

  • One file: ps2xRecomp/src/lib/code_generator.cpp
  • +12 / -12 (one format-string change per branch opcode)

Rationale

The EE's GPRs are 64-bit, and blez/bgtz/bltz/bgez sign-test the whole register per the R5900 Core User's Manual. The GPR_S32 emission was a latent bug that only manifests when a register legitimately holds a value whose 32-bit truncation flips the sign — uncommon in hand-written asm, but not rare in compiler output that manipulates 64-bit quantities.

Fully standalone; no dependencies on other PRs.

BLEZ/BGTZ/BLTZ/BGEZ and their likely/link variants were comparing
GPR_S32, but the EE evaluates the full 64-bit register for these
sign-tested branches. Switch the emitted comparisons to GPR_S64.
@jlsandri jlsandri force-pushed the pr/codegen-ee-branch-gpr-s64 branch from e8c4e50 to 18b6327 Compare April 6, 2026 08:56
@jlsandri
Copy link
Copy Markdown
Author

jlsandri commented Apr 6, 2026

Closing as part of a batch cleanup after #107 landed. The runtime ecosystem refactor in #107 substantially reworked the files this PR touched, and I would like to re-audit the underlying fix against the new code structure before putting it back in front of you. If the fix is still needed after that re-audit, I will re-open as a focused PR rebased onto current main. Thanks for your patience.

@jlsandri jlsandri closed this Apr 6, 2026
@jlsandri jlsandri deleted the pr/codegen-ee-branch-gpr-s64 branch April 6, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant