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

Merged
Kal0s-design merged 1 commit from upstream-pr/3148 into main 2026-12-02 15:25:41 +00:00
Kal0s-design commented 2026-12-02 14:10:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T14:10:48Z.


Claimed in the coordination issue as: X64-RECOMP-SHLD-SHRD-DECODE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7348110712)

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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5608148159 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3148). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-02T14:10:48Z. --- Claimed in the coordination issue as: X64-RECOMP-SHLD-SHRD-DECODE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7348110712) ### 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)
lixonik commented 2026-12-02 14:44:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-02T14:44:11Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7349050422 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3148#issuecomment-7349050422). Originally posted by [lixonik](https://github.com/lixonik) on 2026-12-02T14:44:11Z. --- 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).
MoonEyes commented 2026-12-02 15:11:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T15:11:12Z.


Merging. Head 1f158fe57 merges cleanly with main 83046a713 and does not force a rebase of #2566. Third-party verification by lixonik (https://github.com/ProjectLeonida/mandy/pull/3148#issuecomment-7349050422): 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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7349885526 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3148#issuecomment-7349885526). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T15:11:12Z. --- Merging. Head `1f158fe57` merges cleanly with `main` `83046a713` and does not force a rebase of #2566. Third-party verification by lixonik (https://github.com/ProjectLeonida/mandy/pull/3148#issuecomment-7349050422): 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).
fiestorra commented 2026-12-02 15:21:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T15:21:30Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7350186632 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3148#issuecomment-7350186632). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T15:21:30Z. --- **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.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
ProjectLeonida/mandy!3148
No description provided.