Carry #1488, #1485, #1246: x64_recomp decoder fixes (in/out imm8, missing 0F SIMD ModRM opcodes, SSE4a 0F 78 imm8s) #3118

Merged
MoonEyes merged 12 commits from upstream-pr/3118 into main 2026-12-03 16:22:34 +00:00
MoonEyes commented 2026-12-02 11:45:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T11:45:49Z.


Claimed in the coordination issue as: X64-RECOMP-IN-IMM8, X64-RECOMP-SIMD-TABLE and X64-RECOMP-INSERTQ-IMM8 (the claims of the original author, https://github.com/ProjectLeonida/mandy/issues/14#issuecomment-7149745585, https://github.com/ProjectLeonida/mandy/issues/14#issuecomment-7149569635 and https://github.com/ProjectLeonida/mandy/issues/14#issuecomment-7144092346)

Depends on: #1120 (merged)
Depends on: #1121 (merged)

What and why

Carry of #1488, #1485 and #1246 (kett1m), all in conflict with main since tests/test_x64_recomp.py changed on both sides (the three PRs also conflict with each other there). #1485 says it depends on #1488, so one grouped pull request. This branch is main merged with the head of each PR, in the order #1488, #1485, #1246, so the authors' commits are kept (git merge-base --is-ancestor holds for all three heads). My only change is the three merges: in each one the conflict was the same block of tests/test_x64_recomp.py, and I kept main's tests and the PR's new tests side by side. No change to tools/x64_recomp.py or docs/mobile.md beyond the authors'.

The changes themselves are the authors':

  • #1488: E4-E7 (immediate-port in/out) took no operand byte and decoded one byte short; they now read their imm8 (EC-EF stay one byte). The docs/mobile.md note no longer quotes retail-image addresses (the maintainer's request on #1488, fixed in the author's last commit).
  • #1485: the generated 0F SIMD opcode ranges skipped eight ModRM opcodes (7C/7D, D0, D7, E6, E7, F0, F7), which decoded as unknown and left the walk one byte early; they are now in the table.
  • #1246: SSE4a 0F 78 (insertq/extrq) reads two imm8 bytes, not one; tests/test_recomp_census.py gains the census checks.

Tested

Windows 11, no GPU, no game files, on current main (40ac04183) plus this branch:

  • python -I tests/test_x64_recomp.py: 54 tests, OK (34 skipped: no qemu-system-aarch64).
  • python -I tests/test_recomp_census.py: 15 tests, OK.
  • Every tests/test_x64_recomp*.py file and tests/test_recomp_census.py, one by one with python -I: 22 files, 22 OK.
  • Red: the same two commands with main's tools/x64_recomp.py: test_x64_recomp.py 3 failures (test_in_out_immediate_port_consumes_imm8, test_simd_opcodes_outside_the_ranges_carry_modrm, test_sse4a_0f78_two_imm8), test_recomp_census.py 2 failures (test_insertq_census_has_no_resync_garbage, test_insertq_fn1_decodes_to_end). The tool was restored afterwards and the tree is clean.
  • python -I scripts/check_no_game_data.py docs/mobile.md tests/test_recomp_census.py tests/test_x64_recomp.py tools/x64_recomp.py: clean.
  • Not run: the full suite, CI.

GPUs tested: n/a (decoder unit tests only)

AI-assisted: yes. Author: Claude Opus (Claude Code). Reviewer: a separate read-only Claude Opus reviewer agent.

Checklist

  • Based on current main and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md).
  • A new translator rule is in the contract table of docs/design.md. (n/a: x64 decoder fixes, no translator rule)

Supersedes #1488, #1485 and #1246 once merged; the originals stay open until a maintainer decides.


Generated by Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5605730817 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3118). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T11:45:49Z. --- Claimed in the coordination issue as: X64-RECOMP-IN-IMM8, X64-RECOMP-SIMD-TABLE and X64-RECOMP-INSERTQ-IMM8 (the claims of the original author, https://github.com/ProjectLeonida/mandy/issues/14#issuecomment-7149745585, https://github.com/ProjectLeonida/mandy/issues/14#issuecomment-7149569635 and https://github.com/ProjectLeonida/mandy/issues/14#issuecomment-7144092346) Depends on: #1120 (merged) Depends on: #1121 (merged) ### What and why Carry of #1488, #1485 and #1246 (kett1m), all in conflict with `main` since `tests/test_x64_recomp.py` changed on both sides (the three PRs also conflict with each other there). #1485 says it depends on #1488, so one grouped pull request. This branch is `main` merged with the head of each PR, in the order #1488, #1485, #1246, so the authors' commits are kept (`git merge-base --is-ancestor` holds for all three heads). My only change is the three merges: in each one the conflict was the same block of `tests/test_x64_recomp.py`, and I kept main's tests and the PR's new tests side by side. No change to `tools/x64_recomp.py` or `docs/mobile.md` beyond the authors'. The changes themselves are the authors': - #1488: `E4`-`E7` (immediate-port `in`/`out`) took no operand byte and decoded one byte short; they now read their imm8 (`EC`-`EF` stay one byte). The `docs/mobile.md` note no longer quotes retail-image addresses (the maintainer's request on #1488, fixed in the author's last commit). - #1485: the generated `0F` SIMD opcode ranges skipped eight ModRM opcodes (`7C`/`7D`, `D0`, `D7`, `E6`, `E7`, `F0`, `F7`), which decoded as unknown and left the walk one byte early; they are now in the table. - #1246: SSE4a `0F 78` (`insertq`/`extrq`) reads two imm8 bytes, not one; `tests/test_recomp_census.py` gains the census checks. ### Tested Windows 11, no GPU, no game files, on current `main` (`40ac04183`) plus this branch: - `python -I tests/test_x64_recomp.py`: 54 tests, OK (34 skipped: no `qemu-system-aarch64`). - `python -I tests/test_recomp_census.py`: 15 tests, OK. - Every `tests/test_x64_recomp*.py` file and `tests/test_recomp_census.py`, one by one with `python -I`: 22 files, 22 OK. - Red: the same two commands with main's `tools/x64_recomp.py`: `test_x64_recomp.py` 3 failures (`test_in_out_immediate_port_consumes_imm8`, `test_simd_opcodes_outside_the_ranges_carry_modrm`, `test_sse4a_0f78_two_imm8`), `test_recomp_census.py` 2 failures (`test_insertq_census_has_no_resync_garbage`, `test_insertq_fn1_decodes_to_end`). The tool was restored afterwards and the tree is clean. - `python -I scripts/check_no_game_data.py docs/mobile.md tests/test_recomp_census.py tests/test_x64_recomp.py tools/x64_recomp.py`: clean. - Not run: the full suite, CI. GPUs tested: n/a (decoder unit tests only) AI-assisted: yes. Author: Claude Opus (Claude Code). Reviewer: a separate read-only Claude Opus reviewer agent. ### Checklist - [x] Based on current `main` and merges cleanly. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md). - [ ] A new translator rule is in the contract table of `docs/design.md`. (n/a: x64 decoder fixes, no translator rule) Supersedes #1488, #1485 and #1246 once merged; the originals stay open until a maintainer decides. --- _Generated by [Claude Code](https://claude.ai/code)_
Kal0s-design commented 2026-12-02 12:56:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T12:56:06Z.


Ran natively on Windows 11, Python 3.13, head 1ca3e4a98 merged into current main (23a51c494), no conflicts.

  • py -3.13 -I tests/test_x64_recomp.py: OK (skipped=34, no qemu-system-aarch64 here).
  • py -3.13 -I tests/test_recomp_census.py: OK.

The body already shows red on main, so I checked the decoder lengths against an independent disassembler instead. Throwaway buffer: every 0F xx opcode (except the 38/3A escapes) with no prefix, 66, F2 and F3, each with five ModRM forms (register, [rax], SIB + disp8, RIP + disp32, disp32), plus E4..E7 and EC..EF with and without 66. 5,076 encodings, each in its own 32-byte slot padded with nop. Lengths compared with GNU objdump 2.46 (-b binary -mi386:x86-64), only on encodings objdump does not print as (bad) (3,570 of them):

this PR main
same length as objdump 3,096 3,025
decoder says unknown 389 450
decoder gives a different length 85 95

All the opcodes this PR adds now match objdump on their valid forms: 66/F2 7C, 7D, D0; D7, F7 (MMX register form and 66 register form); E6 with 66, F2, F3; E7 memory form; F2 F0 memory form; 66 0F 78 /0 ib ib and F2 0F 78 /r ib ib (6 bytes); E4..E7 (2 bytes). The 10 encodings that drop out of the "different length" row are exactly in/out imm8 and the two SSE4a register forms.

The 85 left are not from this PR:

  • 80 are 66 0F 80..8F (Jcc with an operand-size prefix): the decoder gives rel32 (7 bytes, Intel ignores 66 here in 64-bit mode), objdump gives rel16 (5 bytes, AMD behaviour). Compilers don't emit this.
  • 5 are no-prefix 0F 78 (VMREAD): the decoder still reads an imm8 there, so it comes out 1 byte too long (4 vs 3 for a register operand). The new comment says VMREAD keeps the entry; that entry is Ev Ib. VMREAD is VMX-root only so it won't show up in game code, but the comment could say the length is wrong for it.

Two small notes, neither blocking:

  • 66 0F 78 and F2 0F 78 with a memory ModRM (mod != 3) are invalid (objdump prints (bad)), but the decoder accepts them as 8-byte instructions with two imm8. Only matters if the walk lands in data.
  • Among the 389 still unknown are 0F A4/A5/AC/AD, i.e. shld/shrd (48 0F A4 C2 05 decodes as op0f_a4, unknown). Those do show up in compiled x64 code (128-bit shifts), so they look like a good next table entry. Out of scope here.

Not checked: the qemu-backed tests (no qemu here), VEX/EVEX and 0F 38/0F 3A maps, real executables (no game files used).

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

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7346103037 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3118#issuecomment-7346103037). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-02T12:56:06Z. --- Ran natively on Windows 11, Python 3.13, head `1ca3e4a98` merged into current `main` (`23a51c494`), no conflicts. - `py -3.13 -I tests/test_x64_recomp.py`: OK (skipped=34, no `qemu-system-aarch64` here). - `py -3.13 -I tests/test_recomp_census.py`: OK. The body already shows red on main, so I checked the decoder lengths against an independent disassembler instead. Throwaway buffer: every `0F xx` opcode (except the 38/3A escapes) with no prefix, `66`, `F2` and `F3`, each with five ModRM forms (register, `[rax]`, SIB + disp8, RIP + disp32, disp32), plus `E4`..`E7` and `EC`..`EF` with and without `66`. 5,076 encodings, each in its own 32-byte slot padded with `nop`. Lengths compared with GNU objdump 2.46 (`-b binary -mi386:x86-64`), only on encodings objdump does not print as `(bad)` (3,570 of them): | | this PR | main | |---|---|---| | same length as objdump | 3,096 | 3,025 | | decoder says unknown | 389 | 450 | | decoder gives a different length | 85 | 95 | All the opcodes this PR adds now match objdump on their valid forms: `66`/`F2` `7C`, `7D`, `D0`; `D7`, `F7` (MMX register form and `66` register form); `E6` with `66`, `F2`, `F3`; `E7` memory form; `F2 F0` memory form; `66 0F 78 /0 ib ib` and `F2 0F 78 /r ib ib` (6 bytes); `E4`..`E7` (2 bytes). The 10 encodings that drop out of the "different length" row are exactly `in`/`out` imm8 and the two SSE4a register forms. The 85 left are not from this PR: - 80 are `66 0F 80..8F` (Jcc with an operand-size prefix): the decoder gives rel32 (7 bytes, Intel ignores `66` here in 64-bit mode), objdump gives rel16 (5 bytes, AMD behaviour). Compilers don't emit this. - 5 are no-prefix `0F 78` (VMREAD): the decoder still reads an imm8 there, so it comes out 1 byte too long (4 vs 3 for a register operand). The new comment says VMREAD keeps the entry; that entry is `Ev Ib`. VMREAD is VMX-root only so it won't show up in game code, but the comment could say the length is wrong for it. Two small notes, neither blocking: - `66 0F 78` and `F2 0F 78` with a memory ModRM (mod != 3) are invalid (objdump prints `(bad)`), but the decoder accepts them as 8-byte instructions with two imm8. Only matters if the walk lands in data. - Among the 389 still unknown are `0F A4`/`A5`/`AC`/`AD`, i.e. `shld`/`shrd` (`48 0F A4 C2 05` decodes as `op0f_a4`, unknown). Those do show up in compiled x64 code (128-bit shifts), so they look like a good next table entry. Out of scope here. Not checked: the qemu-backed tests (no qemu here), VEX/EVEX and `0F 38`/`0F 3A` maps, real executables (no game files used). GPUs tested: n/a (CPU-only tests). AI-assisted: yes (Claude Code).
fiestorra commented 2026-12-02 23:29:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T23:29:52Z.


VERIFY-3118 — Windows nativo: red then green en las tres piezas del decodificador

Own commit 1ca3e4a98 — a merge of #1246 into carry/x64: tools/x64_recomp.py (+6), tests/test_recomp_census.py (+39), tests/test_x64_recomp.py (+22/−1)

Ran at the head

python -I tests/test_recomp_census.py      Ran 15 tests — OK
python -I tests/test_x64_recomp.py         Ran 54 tests — OK (skipped=34)

Negative control: git apply -R of the commit does not apply (the branch also carries a merged main on the same file), so I put main's version of tools/x64_recomp.py back and re-ran:

FAIL: test_insertq_census_has_no_resync_garbage
FAIL: test_insertq_fn1_decodes_to_end
FAIL: test_in_out_immediate_port_consumes_imm8
FAIL: test_simd_opcodes_outside_the_ranges_carry_modrm
FAIL: test_sse4a_0f78_two_imm8

Five bite, one per piece of the change.

What the change is, in three pieces:

  1. Opcode ranges that are ModRM forms and were being skipped — 7C/7D (haddp*/hsubp*), D0 (addsubp*), D7 (pmovmskb), E6 (cvt*dq2pd/pd2dq), E7 (movntdq), F0 (lddqu), F7 (maskmovdqu) — listed explicitly, with the reason in a comment.
  2. in/out with an immediate port (E4–E7) now consume their imm8 — a one-byte omission that desynchronises everything after it.
  3. SSE4a F2 0F 78 /r ib ib (insertq) and 66 0F 78 /0 ib ib (extrq) read two imm8, not one — and the comment handles the collision: without a mandatory prefix 0F 78 is Intel VMREAD, which the decoder does not model, so it keeps the entry. That sentence is why the test test_sse4a_0f78_two_imm8 can exist at all.

Why the census test is the interesting one: test_insertq_census_has_no_resync_garbage does not check one instruction — it checks that the decoder did not desynchronise, which is what a wrong operand length does. That is the failure mode of a decoder, and it is tested as a property rather than an example.

What I could not test

  • The 34 skipped tests: they need generated code and hardware, not available here.
  • Recompiled code running: no title.

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7362074801 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3118#issuecomment-7362074801). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T23:29:52Z. --- ## VERIFY-3118 — Windows nativo: `red then green` en las tres piezas del decodificador | | | |---|---| | Own commit | `1ca3e4a98` — a merge of #1246 into `carry/x64`: `tools/x64_recomp.py` (+6), `tests/test_recomp_census.py` (+39), `tests/test_x64_recomp.py` (+22/−1) | **Ran at the head** ``` python -I tests/test_recomp_census.py Ran 15 tests — OK python -I tests/test_x64_recomp.py Ran 54 tests — OK (skipped=34) ``` **Negative control**: `git apply -R` of the commit does not apply (the branch also carries a merged `main` on the same file), so I put **`main`'s version of `tools/x64_recomp.py`** back and re-ran: ``` FAIL: test_insertq_census_has_no_resync_garbage FAIL: test_insertq_fn1_decodes_to_end FAIL: test_in_out_immediate_port_consumes_imm8 FAIL: test_simd_opcodes_outside_the_ranges_carry_modrm FAIL: test_sse4a_0f78_two_imm8 ``` **Five bite, one per piece of the change.** **What the change is, in three pieces**: 1. **Opcode ranges that are ModRM forms and were being skipped** — `7C/7D` (`haddp*`/`hsubp*`), `D0` (`addsubp*`), `D7` (`pmovmskb`), `E6` (`cvt*dq2pd`/`pd2dq`), `E7` (`movntdq`), `F0` (`lddqu`), `F7` (`maskmovdqu`) — **listed explicitly, with the reason in a comment**. 2. **`in`/`out` with an immediate port** (`E4`–`E7`) **now consume their `imm8`** — a **one-byte** omission that desynchronises everything after it. 3. **SSE4a `F2 0F 78 /r ib ib` (`insertq`) and `66 0F 78 /0 ib ib` (`extrq`) read two imm8, not one** — and the comment handles the collision: **without a mandatory prefix `0F 78` is Intel `VMREAD`, which the decoder does not model, so it keeps the entry.** **That sentence is why the test `test_sse4a_0f78_two_imm8` can exist at all.** **Why the census test is the interesting one**: `test_insertq_census_has_no_resync_garbage` does not check one instruction — **it checks that the decoder did not desynchronise**, which is what a wrong operand length does. **That is the failure mode of a decoder, and it is tested as a property rather than an example.** ## What I could not test - **The 34 skipped tests**: they need generated code and hardware, not available here. - **Recompiled code running**: no title. 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!3118
No description provided.