[PR #1349] x64_recomp: add shld and shrd double-precision shift emitters #1379

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

📋 Pull Request Information

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

Base: main ← Head: feat/x64-recomp-shld-shrd


📝 Commits (1)

  • 395103b x64_recomp: add shld and shrd double-precision shift emitters

📊 Changes

2 files changed (+349 additions, -1 deletions)

View changed files

➕ tests/test_x64_recomp_shld_shrd.py (+253 -0)
📝 tools/x64_recomp.py (+96 -1)

📄 Description

Item ID: X64-RECOMP-SHLD-SHRD

Summary

Adds decoding, classification, and ARM64 code generation for x86-64 double-precision shifts shld (0F A4 imm8 / 0F A5 CL) and shrd (0F AC imm8 / 0F AD CL) in tools/x64_recomp.py:

  • Decoder (OF table): adds 0xA4 (shld, Ev,Gv,Ib), 0xA5 (shld, Ev,Gv,CL), 0xAC (shrd, Ev,Gv,Ib), and 0xAD (shrd, Ev,Gv,CL).
  • Flag tracking & classification: registers shld/shrd in FLAG_WRITERS ("NZCVP") and classifies them as ins.kind = "dshift".
  • Emitter (_emit_dshift):
    • Count == 0: guaranteed no-op (leaves destination and all flags untouched, including materialized ctx.eflags).
    • 32-bit and 64-bit immediate counts: emits single-instruction ARM64 EXTR (extr(rd, src, dst, 32-c) / extr(rd, dst, src, c) with c &= 31 or c &= 63).
    • 32-bit and 64-bit CL variable counts: computes (dst << c) | ((src >> 1) >> (W - 1 - c)) (and symmetric right shift) so c == 0 / c == W - 1 never produce an out-of-range shift width.
    • 16-bit (0x66) operands: concatenates (dst << 16) | src (or (src << 16) | dst for shrd) in a 32-bit register and masks count & 31 (matching Intel/AMD hardware behavior when count == 16, where dst becomes src and CF receives bit 0 of dst for shld / bit 15 of dst for shrd).
    • Flags (SF, ZF, CF, OF, PF): computes the exact last-shifted-out bit into CF (bit W - c for shld, bit c - 1 for shrd), OF = old_sign ^ new_sign (for 1-bit shifts), updates live ARM NZCV and CTX_PF, and when flags are materialized writes ctx.eflags preserving AF.
    • Operand aliasing & memory: loads source and count before overwriting destination (safe for dst == src, dst == rcx, and [base+disp] memory operands, including 128-bit __shiftleft128/__shiftright128 pairs).

Tests (tests/test_x64_recomp_shld_shrd.py)

  • 3 static decode/compile/encoding tests (decoding all 4 opcodes across 16/32/64-bit + memory, verifying ARM64 EXTR emission, and verifying FLAG_WRITERS & classification).
  • 3 hardware execution suites covering 32/64-bit imm & CL + count==0 flag preservation, 16-bit imm & CL including count == 16, and memory operands / register aliasing (dst == src, dst == rcx) / 128-bit __shiftleft128.

Verification

  • Host: macOS (Apple M5, arm64), Python 3.
  • Standalone suite (no game files):
    • python3 -I tests/test_x64_recomp_shld_shrd.py — 6 tests (3 static OK, 3 exec skipped without native 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_shld_shrd.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 3 static test methods (14 failures).
  • Native Apple M5 hardware execution (13-way scratch merge with #1174 and open x64_recomp PRs, 0 conflicts):
    • tests/test_x64_recomp_shld_shrd.py: 6/6 passed (0 skipped)
    • All 13 x64_recomp test suites combined: 148/148 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/1349 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/x64-recomp-shld-shrd` --- ### 📝 Commits (1) - [`395103b`](https://github.com/ubiquitarianism/leonida/commit/395103b0de38e2e60628c76a2a71d75ec9a2fdc8) x64_recomp: add shld and shrd double-precision shift emitters ### 📊 Changes **2 files changed** (+349 additions, -1 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_shld_shrd.py` (+253 -0) 📝 `tools/x64_recomp.py` (+96 -1) </details> ### 📄 Description Item ID: `X64-RECOMP-SHLD-SHRD` ### Summary Adds decoding, classification, and ARM64 code generation for x86-64 double-precision shifts `shld` (`0F A4` imm8 / `0F A5` `CL`) and `shrd` (`0F AC` imm8 / `0F AD` `CL`) in `tools/x64_recomp.py`: - **Decoder (`OF` table)**: adds `0xA4` (`shld`, `Ev,Gv,Ib`), `0xA5` (`shld`, `Ev,Gv,CL`), `0xAC` (`shrd`, `Ev,Gv,Ib`), and `0xAD` (`shrd`, `Ev,Gv,CL`). - **Flag tracking & classification**: registers `shld`/`shrd` in `FLAG_WRITERS` (`"NZCVP"`) and classifies them as `ins.kind = "dshift"`. - **Emitter (`_emit_dshift`)**: - **Count == 0**: guaranteed no-op (leaves destination and all flags untouched, including materialized `ctx.eflags`). - **32-bit and 64-bit immediate counts**: emits single-instruction ARM64 `EXTR` (`extr(rd, src, dst, 32-c)` / `extr(rd, dst, src, c)` with `c &= 31` or `c &= 63`). - **32-bit and 64-bit `CL` variable counts**: computes `(dst << c) | ((src >> 1) >> (W - 1 - c))` (and symmetric right shift) so `c == 0` / `c == W - 1` never produce an out-of-range shift width. - **16-bit (`0x66`) operands**: concatenates `(dst << 16) | src` (or `(src << 16) | dst` for `shrd`) in a 32-bit register and masks count `& 31` (matching Intel/AMD hardware behavior when `count == 16`, where `dst` becomes `src` and `CF` receives bit 0 of `dst` for `shld` / bit 15 of `dst` for `shrd`). - **Flags (`SF`, `ZF`, `CF`, `OF`, `PF`)**: computes the exact last-shifted-out bit into `CF` (bit `W - c` for `shld`, bit `c - 1` for `shrd`), `OF = old_sign ^ new_sign` (for 1-bit shifts), updates live ARM `NZCV` and `CTX_PF`, and when flags are materialized writes `ctx.eflags` preserving `AF`. - **Operand aliasing & memory**: loads source and count before overwriting destination (safe for `dst == src`, `dst == rcx`, and `[base+disp]` memory operands, including 128-bit `__shiftleft128`/`__shiftright128` pairs). ### Tests (`tests/test_x64_recomp_shld_shrd.py`) - 3 static decode/compile/encoding tests (decoding all 4 opcodes across 16/32/64-bit + memory, verifying ARM64 `EXTR` emission, and verifying `FLAG_WRITERS` & classification). - 3 hardware execution suites covering 32/64-bit imm & `CL` + count==0 flag preservation, 16-bit imm & `CL` including `count == 16`, and memory operands / register aliasing (`dst == src`, `dst == rcx`) / 128-bit `__shiftleft128`. ### Verification - **Host**: macOS (Apple M5, arm64), Python 3. - **Standalone suite (no game files)**: - `python3 -I tests/test_x64_recomp_shld_shrd.py` — 6 tests (3 static OK, 3 exec skipped without native 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_shld_shrd.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 3 static test methods (14 failures). - **Native Apple M5 hardware execution (13-way scratch merge with #1174 and open `x64_recomp` PRs, 0 conflicts)**: - `tests/test_x64_recomp_shld_shrd.py`: **6/6 passed** (0 skipped) - All 13 `x64_recomp` test suites combined: **148/148 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#1379
No description provided.