x64_recomp: SHLD and SHRD (0F A4/A5/AC/AD) decode with their ModRM and imm8 (X64-RECOMP-SHLD-SHRD-DECODE) #3148

Manually merged
github merged 1 commit from github-bot/gh-3148 into gh-pr/3148/base 2026-12-02 15:29:44 +00:00 AGit
Owner

@⁠Kal0s-design on GitHub · 2026-12-02 14:10 UTC · original
Kal0s-design:fix/x64-recomp-shld-shrd → main · +53 −0 · 2 files · 1 commits · merged 2026-12-02 15:25 UTC

Claimed in the coordination issue as: X64-RECOMP-SHLD-SHRD-DECODE (#2570)

What and why

tools/x64_recomp.py had no 0F table entry for the double-precision shifts, so 0F A4, 0F A5, 0F AC and 0F AD (shld/shrd r/m, reg, imm8 or CL) decoded as the unknown op0f_XX after the two opcode bytes. Their ModRM byte, any SIB/displacement and the imm8 of the A4/AC forms were then read as the next instructions. Example: 48 0F A4 C2 05 C3 came out as op0f_a4 (3 bytes) followed by a bogus ret 0xc305, and the census stopped the function at the decode error. Compilers emit these for 128-bit shifts.

The fix adds four table entries using the specs the table already has: Ev Gv Ib for A4/AC (ModRM + imm8) and Ev Gv c for A5/AD (ModRM, count in CL). 66 and REX go through the shared operand parser like every other Ev Gv entry.

Scope is decoding only. There is no emitter, so the translator reports them as unhandled shld/shrd and leaves a brk, the same way it handles other instructions it cannot translate yet; the census now names them instead of losing the rest of the function. An emitter (flags, count 0, the 16-bit forms) can follow in its own pull request.

The same item was taken earlier by #1349, which also added an emitter; it went stale on a conflict with main and its author closed it on 1 December. This one only carries the decode part.

Tested

  • tests/test_x64_recomp.py: new test_shld_shrd_carry_modrm_and_imm8 (register and memory forms, SIB + disp8, RIP + disp32, with and without REX.W and 66, and where the next instruction starts) and test_shld_shrd_are_reported_unhandled_by_the_translator.
  • Red on main (69cd4106c): AssertionError: Tuples differ: ('op0f_a4', 3, True) != ('shld', 5, False) and AssertionError: Lists differ: [] != ['shld'], so FAILED (failures=2, skipped=34) over 53 tests.
  • Green with the fix: tests/test_x64_recomp.py 53 tests OK (skipped=34), tests/test_recomp_census.py 13 tests OK. The other tests/test_x64_recomp_*.py suites and tests/test_gdk_runtime_calls.py also pass (their QEMU cases skip here). scripts/check_no_game_data.py passes.
  • Lengths of every test encoding match GNU objdump 2.46 (objdump -D -b binary -m i386:x86-64), reference only.
  • #3118 also edits the 0F table, in other lines; git merge-tree of this branch with its head 1ca3e4a98 is clean.

GPUs tested: n/a (CPU-only tests)

AI-assisted: yes

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

**@⁠Kal0s-design** on GitHub · 2026-12-02 14:10 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/3148) `Kal0s-design:fix/x64-recomp-shld-shrd` → `main` · +53 −0 · 2 files · 1 commits · merged 2026-12-02 15:25 UTC Claimed in the coordination issue as: X64-RECOMP-SHLD-SHRD-DECODE (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/2570) ### What and why `tools/x64_recomp.py` had no `0F` table entry for the double-precision shifts, so `0F A4`, `0F A5`, `0F AC` and `0F AD` (`shld`/`shrd` r/m, reg, imm8 or CL) decoded as the unknown `op0f_XX` after the two opcode bytes. Their ModRM byte, any SIB/displacement and the imm8 of the `A4`/`AC` forms were then read as the next instructions. Example: `48 0F A4 C2 05 C3` came out as `op0f_a4` (3 bytes) followed by a bogus `ret 0xc305`, and the census stopped the function at the decode error. Compilers emit these for 128-bit shifts. The fix adds four table entries using the specs the table already has: `Ev Gv Ib` for `A4`/`AC` (ModRM + imm8) and `Ev Gv c` for `A5`/`AD` (ModRM, count in CL). `66` and REX go through the shared operand parser like every other `Ev Gv` entry. Scope is decoding only. There is no emitter, so the translator reports them as unhandled `shld`/`shrd` and leaves a `brk`, the same way it handles other instructions it cannot translate yet; the census now names them instead of losing the rest of the function. An emitter (flags, count 0, the 16-bit forms) can follow in its own pull request. The same item was taken earlier by #1349, which also added an emitter; it went stale on a conflict with `main` and its author closed it on 1 December. This one only carries the decode part. ### Tested - `tests/test_x64_recomp.py`: new `test_shld_shrd_carry_modrm_and_imm8` (register and memory forms, SIB + disp8, RIP + disp32, with and without REX.W and `66`, and where the next instruction starts) and `test_shld_shrd_are_reported_unhandled_by_the_translator`. - Red on `main` (`69cd4106c`): `AssertionError: Tuples differ: ('op0f_a4', 3, True) != ('shld', 5, False)` and `AssertionError: Lists differ: [] != ['shld']`, so `FAILED (failures=2, skipped=34)` over 53 tests. - Green with the fix: `tests/test_x64_recomp.py` 53 tests `OK (skipped=34)`, `tests/test_recomp_census.py` 13 tests `OK`. The other `tests/test_x64_recomp_*.py` suites and `tests/test_gdk_runtime_calls.py` also pass (their QEMU cases skip here). `scripts/check_no_game_data.py` passes. - Lengths of every test encoding match GNU objdump 2.46 (`objdump -D -b binary -m i386:x86-64`), reference only. - #3118 also edits the `0F` table, in other lines; `git merge-tree` of this branch with its head `1ca3e4a98` is clean. GPUs tested: n/a (CPU-only tests) AI-assisted: yes - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The 0F table had no entry for the double-precision shifts, so 0F A4,
0F A5, 0F AC and 0F AD decoded as the unknown op0f_XX after the two
opcode bytes. The ModRM byte, any SIB/displacement and the imm8 of the
A4/AC forms were then read as the next instructions: 48 0F A4 C2 05 C3
came out as op0f_a4 (3 bytes) followed by a bogus "ret 0xc305".

Add them as "Ev Gv Ib" (A4, AC) and "Ev Gv c" (A5, AD), the same specs
the table already uses for ModRM + imm8 and for the CL count. 66 and
REX.W/R/X/B apply through the shared operand parser.

Decoding only: there is no emitter, so the translator reports them as
unhandled "shld"/"shrd" and leaves a brk, as it does for other
instructions it cannot translate. The census now counts them by name
instead of stopping the function at a decode error.

The test covers register and memory forms (SIB + disp8, RIP + disp32),
with and without REX.W and 66, the next instruction's start, and the
translator's unhandled report. Lengths match GNU objdump 2.46.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 14:11:42 +00:00
Author
Owner

@⁠lixonik on GitHub · 2026-12-02 14:44 UTC · original

Verified PR #3148 (1f158fe57) merged cleanly onto main (acc350745) on Windows 10 x64 (Python tests/test_x64_recomp.py):

  • Clean-room check: python -I scripts/check_no_game_data.py PASS.
  • Green (merged 1f158fe57 onto acc350745): python -I tests/test_x64_recomp.py -- Ran 19 tests in 2.162s -- OK (skipped=34) (both new SHLD/SHRD tests pass).
  • Red -> Green proof (main tools/x64_recomp.py + PR tests/test_x64_recomp.py): both new tests fail as expected:
    • test_shld_shrd_carry_modrm_and_imm8: ('op0f_a4', 3, True) != ('shld', 5, False)
    • test_shld_shrd_are_reported_unhandled_by_the_translator: [] != ['shld']
  • Code review: Minimal 3-line addition to OF (0xA4/0xA5 shld, 0xAC/0xAD shrd with "Ev Gv Ib" / "Ev Gv c"), matching the Intel SDM ModRM/SIB/disp/imm8 encodings and leaving translation cleanly marked unhandled until an emitter is added. No issues found.

Verdict: PASS (CPU/unit tests only; no game launch).

AI-assisted: yes (Antigravity).

**@⁠lixonik** on GitHub · 2026-12-02 14:44 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/3148#issuecomment-7349050422) Verified PR #3148 (`1f158fe57`) merged cleanly onto `main` (`acc350745`) on Windows 10 x64 (Python `tests/test_x64_recomp.py`): - **Clean-room check:** `python -I scripts/check_no_game_data.py` PASS. - **Green (merged `1f158fe57` onto `acc350745`):** `python -I tests/test_x64_recomp.py` -- `Ran 19 tests in 2.162s -- OK (skipped=34)` (both new `SHLD`/`SHRD` tests pass). - **Red -> Green proof (`main` `tools/x64_recomp.py` + PR `tests/test_x64_recomp.py`):** both new tests fail as expected: - `test_shld_shrd_carry_modrm_and_imm8`: `('op0f_a4', 3, True) != ('shld', 5, False)` - `test_shld_shrd_are_reported_unhandled_by_the_translator`: `[] != ['shld']` - **Code review:** Minimal 3-line addition to `OF` (`0xA4`/`0xA5` `shld`, `0xAC`/`0xAD` `shrd` with `"Ev Gv Ib"` / `"Ev Gv c"`), matching the Intel SDM ModRM/SIB/disp/imm8 encodings and leaving translation cleanly marked unhandled until an emitter is added. No issues found. Verdict: **PASS** (CPU/unit tests only; no game launch). AI-assisted: yes (Antigravity).
Author
Owner

@⁠MoonEyes on GitHub · 2026-12-02 15:11 UTC · original

Merging. Head 1f158fe57 merges cleanly with main 83046a713 and does not force a rebase of #2566. Third-party verification by lixonik (#3148): new SHLD/SHRD tests red on main and green on head 1f158fe577; CI green incl. no-game-data; no open review or question. Merged by MoonEyes.

AI-assisted (Claude Code).

**@⁠MoonEyes** on GitHub · 2026-12-02 15:11 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/3148#issuecomment-7349885526) Merging. Head `1f158fe57` merges cleanly with `main` `83046a713` and does not force a rebase of #2566. Third-party verification by lixonik (https://git.gtahub.st/github/ubiquitarianism--leonida/pulls/3148): new SHLD/SHRD tests red on main and green on head 1f158fe577b947d045bc70187584bc40a13de1bf; CI green incl. no-game-data; no open review or question. Merged by MoonEyes. AI-assisted (Claude Code).
Author
Owner

@⁠fiestorra on GitHub · 2026-12-02 15:21 UTC · original

Verified on this machine with the repository's own checker: red then green, and nothing else moved.

python -I scripts/red_green.py 3148 --no-cargo

base origin/main 83046a713, head 3148 1f158fe57, merge base 69cd4106c
red:   the base plus the tests of tests/test_x64_recomp.py
green: the base with the pull request merged
ran:   tests/test_x64_recomp.py
red then green (2):
  DecodeTests.test_shld_shrd_are_reported_unhandled_by_the_translator
  DecodeTests.test_shld_shrd_carry_modrm_and_imm8
pass on both: 17
verdict: red then green

What that says: the two new tests fail on main and pass with the change - the decoder really was losing the instruction - and the other 17 in that file are untouched by it. git merge-tree against main 69cd4106c is clean.

What I looked at in the diff: the four opcodes with their two forms (imm8 and CL), the 66 and REX prefixes, and the memory form keeping SIB, displacement and immediate (4c 0f a4 44 24 08 03 -> 7 bytes, base 4, disp 8). Those are the cases where a decoder that stops at the opcode silently loses the rest of a function, which is what the test's own comment says.

What this does not cover, so nobody reads more into it than it says:

  • --no-cargo, and there is nothing Rust in the change;
  • the decoder on the title's own code: these are synthetic encodings, and whether SHLD/SHRD appears in what the translator walks is a question for a real binary - I have the game's executable but no run to compare a translation against, so I am not claiming it.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

**@⁠fiestorra** on GitHub · 2026-12-02 15:21 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/3148#issuecomment-7350186632) **Verified on this machine with the repository's own checker: `red then green`, and nothing else moved.** ``` python -I scripts/red_green.py 3148 --no-cargo base origin/main 83046a713, head 3148 1f158fe57, merge base 69cd4106c red: the base plus the tests of tests/test_x64_recomp.py green: the base with the pull request merged ran: tests/test_x64_recomp.py red then green (2): DecodeTests.test_shld_shrd_are_reported_unhandled_by_the_translator DecodeTests.test_shld_shrd_carry_modrm_and_imm8 pass on both: 17 verdict: red then green ``` **What that says**: the two new tests fail on `main` and pass with the change - **the decoder really was losing the instruction** - and the other 17 in that file are untouched by it. `git merge-tree` against `main` `69cd4106c` is clean. **What I looked at in the diff**: the four opcodes with their two forms (imm8 and `CL`), the 66 and REX prefixes, and the memory form keeping SIB, displacement and immediate (`4c 0f a4 44 24 08 03` -> 7 bytes, base 4, disp 8). **Those are the cases where a decoder that stops at the opcode silently loses the rest of a function**, which is what the test's own comment says. **What this does not cover**, so nobody reads more into it than it says: - **`--no-cargo`, and there is nothing Rust in the change**; - **the decoder on the title's own code**: these are synthetic encodings, and whether SHLD/SHRD appears in what the translator walks is a question for a real binary - **I have the game's executable but no run to compare a translation against**, so I am not claiming it. **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
github-bot changed target branch from main to gh-pr/3148/base 2026-12-02 15:29:19 +00:00
github manually merged commit 1f158fe577 into gh-pr/3148/base 2026-12-02 15:29:44 +00:00
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/ubiquitarianism--leonida!3148
No description provided.