[PR #1357] x64_recomp: add cmpxchg8b and cmpxchg16b emitters #1389

Open
opened 2026-11-26 20:21:05 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1357
Author: @IlyassRS3
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: feat/x64-recomp-cmpxchg8b-16b


📝 Commits (1)

  • 750fcb6 x64_recomp: add cmpxchg8b and cmpxchg16b emitters

📊 Changes

2 files changed (+590 additions, -2 deletions)

View changed files

➕ tests/test_x64_recomp_cmpxchg16b.py (+488 -0)
📝 tools/x64_recomp.py (+102 -2)

📄 Description

Item ID: X64-RECOMP-CMPXCHG8B-16B

Summary

Adds decoding, classification, ARMv8.0 exclusive-pair assembler helpers, and code generation for x86-64 double-width compare-and-exchange instructions cmpxchg8b (0F C7 /1) and cmpxchg16b (REX.W + 0F C7 /1) in tools/x64_recomp.py:

  • Decoder (OF[0xC7] / _finish_group grp9):
    • Decodes 0F C7 /1 with mod != 3 as cmpxchg8b (m64, size 8) or, when REX.W is set, cmpxchg16b (m128, size 16).
    • Resolves RIP-relative abs targets, ignores REX.R for the /1 opcode extension and ignores 66 operand-size prefix per Intel SDM, and marks mod == 3 or non-/1 group 9 forms (rdrand, rdseed, vmptrld, etc.) as unknown with accurate instruction lengths.
  • Flag tracking & classification:
    • Registers "cmpxchg8b": "Z" and "cmpxchg16b": "Z" in FLAG_WRITERS and classifies them as ins.kind = "cas2", ins.flags_w = "Z".
  • ARMv8.0 pair exclusive helpers (Asm.ldaxp, Asm.stlxp, Asm.clrex):
    • Bit-verified against clang -arch arm64 + otool -t: LDAXP (0x887F8000 / 0xC87F8000), STLXP (0x88208000 / 0xC8208000), and CLREX (0xD5033F5F), with register-constraint assertions (rt1 != rt2, rs not in (rt1, rt2, rn)).
  • Emitter (_emit_cas2):
    • Computes effective address before touching RAX/RDX (safe when the base/index register aliases RAX/RDX, e.g., lock cmpxchg8b [rax]).
    • For LOCK-prefixed instructions, checks alignment via non-flag-setting and_i + cbnz (8-byte for cmpxchg8b, 16-byte for cmpxchg16b), runs an ARMv8.0 LDAXP/STLXP retry loop (calling CLREX on mismatch), and falls back to non-exclusive ldr/str pairs on unaligned addresses to avoid SIGBUS. Unlocked instructions emit direct ldr/str pairs without LL/SC overhead.
    • Compares without clobbering NZCV (eor_r + orr_r + cbnz). For cmpxchg8b, compares low 32 bits (EDX:EAX), preserves upper 32 bits of RAX/RDX on match, and zero-extends RAX/RDX on mismatch.
    • Updates only ZF: patches bit 30 of ARM NZCV via mrs_nzcv + bfi + msr_nzcv when Z is live, and patches bit 6 (EF_ZF) of ctx.eflags when materialized — preserving CF, SF, OF, PF, and AF (including when ZF is dead).

Tests (tests/test_x64_recomp_cmpxchg16b.py)

  • 3 static tests: group-9 decode/rejection/RIP-relative checks, bit-exact LDAXP/STLXP/CLREX encodings and constraint assertions, and LOCK vs unlocked LL/SC emission checks.
  • 3 hardware execution suites: cmpxchg8b match/mismatch/upper-32-bit zero-extension/unaligned +1/+4/[rax] aliasing, cmpxchg16b 128-bit match/bit-63 mismatch/unaligned +8/Windows _InterlockedCompareExchange128 retry loop, and CF/SF/OF/PF preservation across same-block ARM NZCV, cross-block materialized ctx.eflags, and dead-ZF paths.

Verification

  • Host: macOS (Apple M5, arm64), Python 3.
  • Standalone checks (no game files):
    • python3 -I tests/test_x64_recomp_cmpxchg16b.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_cmpxchg16b.py — OK
    • python3 -I tests/test_ci_registration.py — 15/15 OK
    • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main fails all static test methods (10 failures, 1 error).
  • Native Apple M5 hardware execution (14-way scratch merge with #1174 and all open x64_recomp PRs, 0 conflicts):
    • tests/test_x64_recomp_cmpxchg16b.py: 6/6 passed (0 skipped)
    • All 13 test_x64_recomp_*.py suites combined: 108/108 passed (0 skipped)

AI-assisted: yes


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1357 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/x64-recomp-cmpxchg8b-16b` --- ### 📝 Commits (1) - [`750fcb6`](https://github.com/ubiquitarianism/leonida/commit/750fcb669082f12a0cb4db9d5211632891af0c05) x64_recomp: add cmpxchg8b and cmpxchg16b emitters ### 📊 Changes **2 files changed** (+590 additions, -2 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_cmpxchg16b.py` (+488 -0) 📝 `tools/x64_recomp.py` (+102 -2) </details> ### 📄 Description Item ID: `X64-RECOMP-CMPXCHG8B-16B` ### Summary Adds decoding, classification, ARMv8.0 exclusive-pair assembler helpers, and code generation for x86-64 double-width compare-and-exchange instructions `cmpxchg8b` (`0F C7 /1`) and `cmpxchg16b` (`REX.W + 0F C7 /1`) in `tools/x64_recomp.py`: - **Decoder (`OF[0xC7]` / `_finish_group` `grp9`)**: - Decodes `0F C7 /1` with `mod != 3` as `cmpxchg8b` (`m64`, size 8) or, when `REX.W` is set, `cmpxchg16b` (`m128`, size 16). - Resolves RIP-relative `abs` targets, ignores `REX.R` for the `/1` opcode extension and ignores `66` operand-size prefix per Intel SDM, and marks `mod == 3` or non-`/1` group 9 forms (`rdrand`, `rdseed`, `vmptrld`, etc.) as `unknown` with accurate instruction lengths. - **Flag tracking & classification**: - Registers `"cmpxchg8b": "Z"` and `"cmpxchg16b": "Z"` in `FLAG_WRITERS` and classifies them as `ins.kind = "cas2"`, `ins.flags_w = "Z"`. - **ARMv8.0 pair exclusive helpers (`Asm.ldaxp`, `Asm.stlxp`, `Asm.clrex`)**: - Bit-verified against `clang -arch arm64` + `otool -t`: `LDAXP` (`0x887F8000` / `0xC87F8000`), `STLXP` (`0x88208000` / `0xC8208000`), and `CLREX` (`0xD5033F5F`), with register-constraint assertions (`rt1 != rt2`, `rs not in (rt1, rt2, rn)`). - **Emitter (`_emit_cas2`)**: - Computes effective address before touching `RAX`/`RDX` (safe when the base/index register aliases `RAX`/`RDX`, e.g., `lock cmpxchg8b [rax]`). - For `LOCK`-prefixed instructions, checks alignment via non-flag-setting `and_i + cbnz` (8-byte for `cmpxchg8b`, 16-byte for `cmpxchg16b`), runs an ARMv8.0 `LDAXP`/`STLXP` retry loop (calling `CLREX` on mismatch), and falls back to non-exclusive `ldr`/`str` pairs on unaligned addresses to avoid `SIGBUS`. Unlocked instructions emit direct `ldr`/`str` pairs without LL/SC overhead. - Compares without clobbering `NZCV` (`eor_r + orr_r + cbnz`). For `cmpxchg8b`, compares low 32 bits (`EDX:EAX`), preserves upper 32 bits of `RAX`/`RDX` on match, and zero-extends `RAX`/`RDX` on mismatch. - Updates **only** `ZF`: patches bit 30 of ARM `NZCV` via `mrs_nzcv + bfi + msr_nzcv` when `Z` is live, and patches bit 6 (`EF_ZF`) of `ctx.eflags` when materialized — preserving `CF`, `SF`, `OF`, `PF`, and `AF` (including when `ZF` is dead). ### Tests (`tests/test_x64_recomp_cmpxchg16b.py`) - 3 static tests: group-9 decode/rejection/RIP-relative checks, bit-exact `LDAXP`/`STLXP`/`CLREX` encodings and constraint assertions, and LOCK vs unlocked LL/SC emission checks. - 3 hardware execution suites: `cmpxchg8b` match/mismatch/upper-32-bit zero-extension/unaligned `+1`/`+4`/`[rax]` aliasing, `cmpxchg16b` 128-bit match/bit-63 mismatch/unaligned `+8`/Windows `_InterlockedCompareExchange128` retry loop, and `CF`/`SF`/`OF`/`PF` preservation across same-block ARM `NZCV`, cross-block materialized `ctx.eflags`, and dead-`ZF` paths. ### Verification - **Host**: macOS (Apple M5, arm64), Python 3. - **Standalone checks (no game files)**: - `python3 -I tests/test_x64_recomp_cmpxchg16b.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_cmpxchg16b.py` — OK - `python3 -I tests/test_ci_registration.py` — 15/15 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - **Mutation proof**: reverting `tools/x64_recomp.py` to `upstream/main` fails all static test methods (10 failures, 1 error). - **Native Apple M5 hardware execution (14-way scratch merge with #1174 and all open `x64_recomp` PRs, 0 conflicts)**: - `tests/test_x64_recomp_cmpxchg16b.py`: **6/6 passed** (0 skipped) - All 13 `test_x64_recomp_*.py` suites combined: **108/108 passed** (0 skipped) AI-assisted: yes --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#1389
No description provided.