x64_recomp: add movbe and SSE4.2 crc32 emitters #1362

Open
github-bot wants to merge 1 commit from github-bot/gh-1362 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-24 22:32 UTC · original
IlyassRS3:feat/x64-recomp-movbe-crc32 → main · +510 −1 · 2 files · 1 commits · open

Item ID: X64-RECOMP-MOVBE-CRC32

Summary

tools/x64_recomp.py previously decoded every 0F 38 xx three-byte opcode as ("simd", "V W"), which misdecoded movbe and SSE4.2 crc32 (both baseline Zen 2 / Xbox Series instructions) with XMM operands and rejected them during translation.

This PR adds decoding, classification, and ARM64 lowering for 0F 38 F0 and 0F 38 F1:

  • Decoder (decode_one 0F 38 map):
    • movbe (rep == ""): decodes 0F 38 F0 (load) and 0F 38 F1 (store) across 16/32/64-bit widths (REX.W taking precedence over 66, resolving RIP-relative addresses). Rejects invalid mod == 3 register forms and F3 0F 38 F0/F1 as unknown with accurate instruction length; unrelated 0F 38 SIMD opcodes remain unchanged.
    • crc32 (rep == "f2"): decodes F2 0F 38 F0 (Gd/Gq, Eb, ignoring 66 and preserving hi8 vs REX low8 register selection) and F2 0F 38 F1 (Gd, Ew with 66, Gd, Ed default, Gq, Eq with REX.W overriding 66). Note that 66 F2 0F 38 F1 always writes a 32-bit Gd destination (zero-extended to 64 bits), not 16-bit Gw.
  • Assembler (Asm.rev16):
    • Adds ARM64 REV16 (0x5AC00400 / 0xDAC00400), bit-verified against clang -arch arm64 + otool -t.
  • Emitters (_emit_movbe, _emit_crc32):
    • _emit_movbe: swaps 16-bit via REV16 (preserving upper 48 bits of destination on load and isolating 2-byte stores) and 32/64-bit via REV (zero-extending 32-bit loads), safe for base/index register aliasing (movbe rcx, [rcx] and movbe [rcx], rcx).
    • _emit_crc32: lowers all four widths (8/16/32/64-bit) with a unified portable ARMv8.0 Castagnoli (0x82F63B78) bit loop using only non-flag-setting instructions (and_i, sub_r from XZR, and_r, lsr, eor_r, sub, cbnz), clearing bits 63:32 of the destination register and preserving every CPU flag (NZCV and ctx.eflags). Safe for dst == src aliasing (crc32 eax, eax, crc32 eax, ah, crc32 rax, rax).

Tests (tests/test_x64_recomp_movbe_crc32.py)

  • 3 static tests: decode/prefix/rejection checks for movbe and crc32, bit-exact Asm.rev16 encoding, and ARM64 codegen checks.
  • 3 hardware execution suites: movbe 16/32/64-bit load/store/partial-store isolation/aliasing; crc32 against the RFC 3720 / iSCSI Castagnoli vector (b"123456789" -> 0xE3069283) across 1/2/4/8-byte & mixed chunks, hi8/REX low8, upper-32-bit zero-extension, and aliasing; and full CF/ZF/SF/OF/PF flag preservation across both same-block ARM NZCV and cross-block ctx.eflags.

Verification

  • Host: macOS (Apple M5, arm64), Python 3.
  • Standalone checks (no game files):
    • python3 -I tests/test_x64_recomp_movbe_crc32.py — 6 tests (3 static OK, 3 exec skipped without runner)
    • python3 -I tests/test_x64_recomp.py — OK
    • python3 -m compileall -q make_game.py tools tests — OK
    • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_movbe_crc32.py — OK
    • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
    • Note: tests/test_ci_registration.py currently has an unrelated pre-existing failure on main (51a6074d2, tests/semantic/test_sub_co_same_operand.py from #930, tracked under TEST-CI-REG-LOCAL-MIXIN); all other 14/15 registration checks (including our new test file) pass.
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main fails all static test methods (22 failures, 1 error).
  • Native Apple M5 hardware execution (15-way scratch merge with #1174 and all open x64_recomp PRs, 0 conflicts):
    • tests/test_x64_recomp_movbe_crc32.py: 6/6 passed (0 skipped)
    • All 14 test_x64_recomp_*.py suites combined: 114/114 passed (0 skipped)

AI-assisted: yes

**@⁠IlyassRS3** on GitHub · 2026-11-24 22:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1362) `IlyassRS3:feat/x64-recomp-movbe-crc32` → `main` · +510 −1 · 2 files · 1 commits · open Item ID: `X64-RECOMP-MOVBE-CRC32` ### Summary `tools/x64_recomp.py` previously decoded every `0F 38 xx` three-byte opcode as `("simd", "V W")`, which misdecoded `movbe` and SSE4.2 `crc32` (both baseline Zen 2 / Xbox Series instructions) with XMM operands and rejected them during translation. This PR adds decoding, classification, and ARM64 lowering for `0F 38 F0` and `0F 38 F1`: - **Decoder (`decode_one` `0F 38` map)**: - **`movbe` (`rep == ""`)**: decodes `0F 38 F0` (load) and `0F 38 F1` (store) across 16/32/64-bit widths (`REX.W` taking precedence over `66`, resolving RIP-relative addresses). Rejects invalid `mod == 3` register forms and `F3 0F 38 F0/F1` as `unknown` with accurate instruction length; unrelated `0F 38` SIMD opcodes remain unchanged. - **`crc32` (`rep == "f2"`)**: decodes `F2 0F 38 F0` (`Gd/Gq, Eb`, ignoring `66` and preserving `hi8` vs REX `low8` register selection) and `F2 0F 38 F1` (`Gd, Ew` with `66`, `Gd, Ed` default, `Gq, Eq` with `REX.W` overriding `66`). Note that `66 F2 0F 38 F1` always writes a 32-bit `Gd` destination (zero-extended to 64 bits), not 16-bit `Gw`. - **Assembler (`Asm.rev16`)**: - Adds ARM64 `REV16` (`0x5AC00400` / `0xDAC00400`), bit-verified against `clang -arch arm64` + `otool -t`. - **Emitters (`_emit_movbe`, `_emit_crc32`)**: - `_emit_movbe`: swaps 16-bit via `REV16` (preserving upper 48 bits of destination on load and isolating 2-byte stores) and 32/64-bit via `REV` (zero-extending 32-bit loads), safe for base/index register aliasing (`movbe rcx, [rcx]` and `movbe [rcx], rcx`). - `_emit_crc32`: lowers all four widths (8/16/32/64-bit) with a unified portable ARMv8.0 Castagnoli (`0x82F63B78`) bit loop using only non-flag-setting instructions (`and_i`, `sub_r` from `XZR`, `and_r`, `lsr`, `eor_r`, `sub`, `cbnz`), clearing bits 63:32 of the destination register and preserving every CPU flag (`NZCV` and `ctx.eflags`). Safe for `dst == src` aliasing (`crc32 eax, eax`, `crc32 eax, ah`, `crc32 rax, rax`). ### Tests (`tests/test_x64_recomp_movbe_crc32.py`) - 3 static tests: decode/prefix/rejection checks for `movbe` and `crc32`, bit-exact `Asm.rev16` encoding, and ARM64 codegen checks. - 3 hardware execution suites: `movbe` 16/32/64-bit load/store/partial-store isolation/aliasing; `crc32` against the RFC 3720 / iSCSI Castagnoli vector (`b"123456789"` -> `0xE3069283`) across 1/2/4/8-byte & mixed chunks, `hi8`/`REX` low8, upper-32-bit zero-extension, and aliasing; and full `CF`/`ZF`/`SF`/`OF`/`PF` flag preservation across both same-block ARM `NZCV` and cross-block `ctx.eflags`. ### Verification - **Host**: macOS (Apple M5, arm64), Python 3. - **Standalone checks (no game files)**: - `python3 -I tests/test_x64_recomp_movbe_crc32.py` — 6 tests (3 static OK, 3 exec skipped without runner) - `python3 -I tests/test_x64_recomp.py` — OK - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_movbe_crc32.py` — OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - Note: `tests/test_ci_registration.py` currently has an unrelated pre-existing failure on `main` (`51a6074d2`, `tests/semantic/test_sub_co_same_operand.py` from #930, tracked under `TEST-CI-REG-LOCAL-MIXIN`); all other 14/15 registration checks (including our new test file) pass. - **Mutation proof**: reverting `tools/x64_recomp.py` to `upstream/main` fails all static test methods (22 failures, 1 error). - **Native Apple M5 hardware execution (15-way scratch merge with #1174 and all open `x64_recomp` PRs, 0 conflicts)**: - `tests/test_x64_recomp_movbe_crc32.py`: **6/6 passed** (0 skipped) - All 14 `test_x64_recomp_*.py` suites combined: **114/114 passed** (0 skipped) AI-assisted: yes
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:48:46 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 22:32 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 22:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1362#pullrequestreview-6048443914) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-26 03:09 UTC · original

Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged main at 7f3d0095a into the branch locally (902 commits behind, no conflicts). Head tested: 7014994d4.

Against a reference model crc32 and movbe are right in all 174 checks.

  • Test file. As shipped under QEMU, python -I tests/test_x64_recomp_movbe_crc32.py is 6 run, 6 failures, from the wiring bug in #1409 (xr.run_kernel where xr.run_result is needed). With RUNNER swapped for xr.run_result in memory (no file changed): 6/6.

  • Beyond the file, generated cases under QEMU against a small Python model:

    • crc32 register forms eax, cl / rax, cl / eax, cx / eax, ecx / rax, rcx: five starting CRCs (0, 1, 0xffffffff, 0x12345678, 0x80000000) by six sources, against a bitwise CRC-32C; the destination starts with junk in its upper half, which must come back zero (150 checks);
    • movbe load and store at [rsp] at 16, 32 and 64 bits, four values each: the full destination register on a load (untouched upper bits at 16, zero-extension at 32) and the full memory qword on a store (neighbouring bytes untouched) (24 checks).
    tool checks wrong
    this branch 174 0

    There is no main column: main treats 0f 38 f0/f1 as SIMD and does not translate them.

  • Counts. tests/test_x64_recomp.py 44/44 and tests/test_recomp_census.py 11/11, the same as main. Full standalone suite: 514 run, 512 pass; the 2 that fail are main's test_mod_pack_output_alias and the new file, for the wiring reason.

Not tested: crc32 with a memory source beyond the file's own cases, movbe with other addressing modes, and flags (neither instruction writes any; the file has its own flag-preservation test, which passes). No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 03:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1362#issuecomment-7181444779) Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged `main` at `7f3d0095a` into the branch locally (902 commits behind, no conflicts). Head tested: `7014994d4`. **Against a reference model `crc32` and `movbe` are right in all 174 checks.** - **Test file.** As shipped under QEMU, `python -I tests/test_x64_recomp_movbe_crc32.py` is 6 run, 6 failures, from the wiring bug in #1409 (`xr.run_kernel` where `xr.run_result` is needed). With `RUNNER` swapped for `xr.run_result` in memory (no file changed): **6/6**. - Beyond the file, generated cases under QEMU against a small Python model: - `crc32` register forms `eax, cl` / `rax, cl` / `eax, cx` / `eax, ecx` / `rax, rcx`: five starting CRCs (0, 1, `0xffffffff`, `0x12345678`, `0x80000000`) by six sources, against a bitwise CRC-32C; the destination starts with junk in its upper half, which must come back zero (150 checks); - `movbe` load and store at `[rsp]` at 16, 32 and 64 bits, four values each: the full destination register on a load (untouched upper bits at 16, zero-extension at 32) and the full memory qword on a store (neighbouring bytes untouched) (24 checks). | tool | checks | wrong | |---|---|---| | this branch | 174 | 0 | There is no `main` column: `main` treats `0f 38 f0`/`f1` as SIMD and does not translate them. - **Counts.** `tests/test_x64_recomp.py` 44/44 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 514 run, 512 pass; the 2 that fail are `main`'s `test_mod_pack_output_alias` and the new file, for the wiring reason. Not tested: `crc32` with a memory source beyond the file's own cases, `movbe` with other addressing modes, and flags (neither instruction writes any; the file has its own flag-preservation test, which passes). No game files used. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1362/head:github-bot/gh-1362'
git switch 'github-bot/gh-1362'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1362'
git switch 'github-bot/gh-1362'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1362'
git switch 'github-bot/gh-1362'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1362'
git switch 'main'
git merge --squash 'github-bot/gh-1362'
git switch 'main'
git merge --ff-only 'github-bot/gh-1362'
git switch 'main'
git merge 'github-bot/gh-1362'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.