Lift SETcc and CMOVcc in the jump-table translator #172

Merged
Vonsek merged 2 commits from upstream-pr/173 into main 2026-11-21 20:26:02 +00:00
Vonsek commented 2026-11-21 17:56:32 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T17:56:32Z.


Change

tools/cpu_switch_aot.py rejected every SETcc and CMOVcc, and modelled flags only for a conditional jump placed directly after its producer. Compilers emit both instructions for comparisons, selects, min/max and clamps, often with a MOV between the compare and the consumer.

  • Each flag consumer (Jcc, SETcc, CMOVcc) now looks back for its producer through instructions that leave the flags unchanged (MOV, MOVZX, MOVSX, MOVSXD, LEA, NOP, NOT, SETcc, CMOVcc), up to 16 instructions, inside one straight-line run that no edge enters. The supported producers are unchanged: CMP, TEST, ADD, SUB, AND, OR, XOR, INC, DEC. Any other flag writer in between (for example NEG) is rejected, as are the overflow and parity conditions.
  • SETcc writes a memory byte or a low byte register and keeps bits 8 to 63 of that register. AH-DH are still rejected.
  • CMOVcc with 32- or 64-bit operands. A 32-bit CMOVcc clears bits 32 to 63 of the destination even when the condition is false. A memory source is loaded on both outcomes, as on hardware.
  • tests/cpu/switch.asm gains flag_select, an eight-entry LLVM-layout table to SETL, SETBE, SETG into memory, SETNE after a 16-bit compare, 32-bit CMOVA, 64-bit CMOVNS after TEST, CMOVGE from memory with a MOV between compare and consumer, and CMOVE after TEST with an immediate. It compares EDX with bits 32 to 63 of RDX and returns RAX whole, so the upper-half behaviour of both instructions is observable.
  • tests/cpu/switch_check.py runs 4,000 inputs at the signed, unsigned and 16-bit boundaries through the original, the translation and a Python reference. The SWAP_TABLE build exchanges the SETL and CMOVA entries and must follow the swapped reference. Four new rejection controls patch the linked DLL: SETO, SETL AH, a NEG between CMP and CMOVGE, and a branch into SETBE (flags across a join).
  • README CPU row and docs/experiments.md (switch dispatch section) describe the new coverage.

The test stays cpu_aot_jump_tables; flag_select is added to its export list in CMakeLists.txt.

Verification

  • Windows: ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V at the rebased head (main with #86; the earlier run at 173c476 gave the same result) on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207, ML64 14.44.35230), Windows SDK 10.0.26100: passed. ML64 assembles flag_select in both the original and SWAP_TABLE builds without changes. Output: 4000 SETcc/CMOVcc inputs match and the swapped table changed 762, 13 rejection controls and the leaf-without-unwind translation passed, 4 flag-consumer rejection controls passed, and the existing select, run_ops and shift_narrow comparisons still pass. The full ctest --preset dev --no-tests=error passed 86/86, with bvh_wave64_hardware, bvh_wave64_warp and wave_shuffle_warp skipped.
  • Linux stand-in: switch.asm converted to GNU syntax, assembled with llvm-mc -triple x86_64-pc-windows-msvc, linked with lld-link /dll /noentry /def:tests/cpu/switch.def, original and swapped. The translated C++ was built with clang as a shared library. All 4,000 flag_select inputs match the original and the reference, and the swapped table changed 762 results; the existing select, run_ops and shift_narrow checks still pass. switch_check.py rejections passed all 13 existing controls and the 4 new ones.
  • Fail before: with tools/cpu_switch_aot.py from main, translation stops with unsupported instruction at RVA 0x11c1: setl al.
  • Mutation checks: writing the full register for a byte SETcc fails at flag_select(0x6, 0x100000000); keeping the 64-bit destination for a false 32-bit CMOVcc fails at flag_select(0x3, 0x0).
  • Compiler output: five small C functions built by clang for x86_64-pc-windows-msvc at -O1 and -O2 contain 14 SETcc/CMOVcc instructions, including SETE R8B and both operand widths. Their translations matched the original code on 25,000 calls per level.

Evidence and follow-up

  • Not covered: SETcc/CMOVcc after NEG, shifts, IMUL or BT producers; 16-bit CMOVcc; ADC/SBB; flags carried across a conditional-jump fallthrough.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6532419132 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/173). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T17:56:32Z. --- ## Change `tools/cpu_switch_aot.py` rejected every `SETcc` and `CMOVcc`, and modelled flags only for a conditional jump placed directly after its producer. Compilers emit both instructions for comparisons, selects, min/max and clamps, often with a `MOV` between the compare and the consumer. - Each flag consumer (`Jcc`, `SETcc`, `CMOVcc`) now looks back for its producer through instructions that leave the flags unchanged (`MOV`, `MOVZX`, `MOVSX`, `MOVSXD`, `LEA`, `NOP`, `NOT`, `SETcc`, `CMOVcc`), up to 16 instructions, inside one straight-line run that no edge enters. The supported producers are unchanged: `CMP`, `TEST`, `ADD`, `SUB`, `AND`, `OR`, `XOR`, `INC`, `DEC`. Any other flag writer in between (for example `NEG`) is rejected, as are the overflow and parity conditions. - `SETcc` writes a memory byte or a low byte register and keeps bits 8 to 63 of that register. `AH`-`DH` are still rejected. - `CMOVcc` with 32- or 64-bit operands. A 32-bit `CMOVcc` clears bits 32 to 63 of the destination even when the condition is false. A memory source is loaded on both outcomes, as on hardware. - `tests/cpu/switch.asm` gains `flag_select`, an eight-entry LLVM-layout table to `SETL`, `SETBE`, `SETG` into memory, `SETNE` after a 16-bit compare, 32-bit `CMOVA`, 64-bit `CMOVNS` after `TEST`, `CMOVGE` from memory with a `MOV` between compare and consumer, and `CMOVE` after `TEST` with an immediate. It compares EDX with bits 32 to 63 of RDX and returns RAX whole, so the upper-half behaviour of both instructions is observable. - `tests/cpu/switch_check.py` runs 4,000 inputs at the signed, unsigned and 16-bit boundaries through the original, the translation and a Python reference. The `SWAP_TABLE` build exchanges the `SETL` and `CMOVA` entries and must follow the swapped reference. Four new rejection controls patch the linked DLL: `SETO`, `SETL AH`, a `NEG` between `CMP` and `CMOVGE`, and a branch into `SETBE` (flags across a join). - README CPU row and `docs/experiments.md` (switch dispatch section) describe the new coverage. The test stays `cpu_aot_jump_tables`; `flag_select` is added to its export list in `CMakeLists.txt`. ## Verification - Windows: `ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V` at the rebased head (main with #86; the earlier run at 173c476 gave the same result) on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207, ML64 14.44.35230), Windows SDK 10.0.26100: passed. ML64 assembles `flag_select` in both the original and `SWAP_TABLE` builds without changes. Output: `4000 SETcc/CMOVcc inputs match and the swapped table changed 762`, `13 rejection controls and the leaf-without-unwind translation passed`, `4 flag-consumer rejection controls passed`, and the existing select, `run_ops` and `shift_narrow` comparisons still pass. The full `ctest --preset dev --no-tests=error` passed 86/86, with `bvh_wave64_hardware`, `bvh_wave64_warp` and `wave_shuffle_warp` skipped. - Linux stand-in: `switch.asm` converted to GNU syntax, assembled with `llvm-mc -triple x86_64-pc-windows-msvc`, linked with `lld-link /dll /noentry /def:tests/cpu/switch.def`, original and swapped. The translated C++ was built with clang as a shared library. All 4,000 `flag_select` inputs match the original and the reference, and the swapped table changed 762 results; the existing select, run_ops and shift_narrow checks still pass. `switch_check.py rejections` passed all 13 existing controls and the 4 new ones. - Fail before: with `tools/cpu_switch_aot.py` from `main`, translation stops with `unsupported instruction at RVA 0x11c1: setl al`. - Mutation checks: writing the full register for a byte `SETcc` fails at `flag_select(0x6, 0x100000000)`; keeping the 64-bit destination for a false 32-bit `CMOVcc` fails at `flag_select(0x3, 0x0)`. - Compiler output: five small C functions built by clang for `x86_64-pc-windows-msvc` at `-O1` and `-O2` contain 14 `SETcc`/`CMOVcc` instructions, including `SETE R8B` and both operand widths. Their translations matched the original code on 25,000 calls per level. ## Evidence and follow-up - Not covered: `SETcc`/`CMOVcc` after `NEG`, shifts, `IMUL` or `BT` producers; 16-bit `CMOVcc`; `ADC`/`SBB`; flags carried across a conditional-jump fallthrough.
upstream-sync closed this pull request 2026-11-24 06:41:40 +00:00
chatgpt-codex-connector[bot commented 2026-11-24 06:41:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T18:33:52Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3311b0b6a3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6007292387 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/173#pullrequestreview-6007292387). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T18:33:52Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `3311b0b6a3` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". </details>
Vonsek commented 2026-11-24 06:41:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T19:43:32Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6007739235 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/173#pullrequestreview-6007739235). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T19:43:32Z. **Review:** COMMENTED --- _No description provided._
R0Y00 commented 2026-11-24 06:47:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T18:16:14Z.


GitHub reports a conflict on 173c4765. Please reconcile the conditional flag-producer logic and jump-table fixture with the current translator, keeping the existing jump-table controls. Run the focused jump-table CTest on the reconciled head before marking ready.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067129619 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/173#issuecomment-7067129619). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T18:16:14Z. --- GitHub reports a conflict on `173c4765`. Please reconcile the conditional flag-producer logic and jump-table fixture with the current translator, keeping the existing jump-table controls. Run the focused jump-table CTest on the reconciled head before marking ready.
chatgpt-codex-connector[bot commented 2026-11-24 06:47:49 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T18:24:20Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T18:33:54.372027Z 3311b0b Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067230031 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/173#issuecomment-7067230031). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T18:24:20Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"3311b0b6a397e8f42c9b0633d8b27d33246125f3","has_any_findings":true,"has_unresolved_findings":true,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-21T18:33:54.372027Z">2026-11-21T18:33:54.372027Z</relative-time> | `3311b0b` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
Vonsek commented 2026-11-24 06:47:55 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T19:43:34Z.


Rebased on main 7233cec (b5ef0f9, plus 1e9969a for the review finding). The only conflict was the README "CPU translation" row; the current row is kept with the SETcc/CMOVcc sentence added. The flag-producer logic and the jump-table fixture rebased cleanly onto the current translator, and the existing jump-table controls are unchanged.

On Windows 11 26200.9974 (MSVC 14.44.35207, ML64 14.44.35230, SDK 10.0.26100): ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V passed (select tables, run_ops, shift_narrow, 4,000 flag_select inputs, cmov_read, 13 rejection controls and 4 flag-consumer rejection controls), in Debug and in a Release build. The full suite passed 121/121.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7068231739 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/173#issuecomment-7068231739). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T19:43:34Z. --- Rebased on main 7233cec (b5ef0f9, plus 1e9969a for the review finding). The only conflict was the README "CPU translation" row; the current row is kept with the SETcc/CMOVcc sentence added. The flag-producer logic and the jump-table fixture rebased cleanly onto the current translator, and the existing jump-table controls are unchanged. On Windows 11 26200.9974 (MSVC 14.44.35207, ML64 14.44.35230, SDK 10.0.26100): `ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V` passed (select tables, `run_ops`, `shift_narrow`, 4,000 `flag_select` inputs, `cmov_read`, 13 rejection controls and 4 flag-consumer rejection controls), in Debug and in a Release build. The full suite passed 121/121.
R0Y00 commented 2026-11-24 06:47:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T19:59:30Z.


At 1e9969a, only docs/experiments.md conflicts. The translator merges automatically. Please retain the addr32 and conditional-move results in the combined document; the open inline review still needs resolution.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7068423856 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/173#issuecomment-7068423856). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T19:59:30Z. --- At `1e9969a`, only `docs/experiments.md` conflicts. The translator merges automatically. Please retain the addr32 and conditional-move results in the combined document; the open inline review still needs resolution.
chatgpt-codex-connector[bot commented 2026-11-24 06:50:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T18:33:52Z.

Code review location: tools/cpu_switch_aot.py; line 511

@@ -472,6 +486,30 @@ def note(*numbers):
             else:
                 a, b = self.operand(ins, 1, width), self.operand(ins, 2, width)
             return self.write(ins, width, f"{a} * {b}"), reads, writes
+        if mnemonic in SETCC:
+            condition = self.condition(producer, ins)
+            if ins.op0_kind == OpKind.MEMORY:
+                return f"store<std::uint8_t>({self.address(ins)}, static_cast<std::uint8_t>({condition}));", reads, writes
+            register = ins.op0_register
+            if register in HIGH_BYTES or RegisterExt.size(register) != 1:
+                raise ValueError(f"unsupported register {register}")
+            # A byte write keeps bits 8..63 of the full register.
+            name = NAMES[full(register)]
+            self.used.add(full(register))
+            reads.add(full(register))
+            return f"{name} = ({name} & ~0xffull) | static_cast<word>({condition});", reads, writes
+        if mnemonic in CMOVCC:
+            if width not in (4, 8):
+                raise ValueError(f"unsupported partial register write at RVA {ins.ip:#x}")
+            note(0, 1)
+            condition = self.condition(producer, ins)
+            source, destination = self.operand(ins, 1, width), self.operand(ins, 0, width)
+            # A 32-bit CMOVcc zero-extends the destination even when the condition is false,
+            # and a memory source is read either way.
+            if ins.op1_kind == OpKind.MEMORY:
+                line = self.write(ins, width, f"({condition}) ? source : {destination}")
+                return f"{{ const {unsigned(width)} source = {source}; {line} }}", reads, writes

P2 Badge Make the unconditional CMOV source read non-elidable

When an optimized translation's condition folds to false, this ordinary memcpy-backed load can disappear despite being outside the ternary. For example, the emitted fragment for mov rax, rdx; cmp ecx, ecx; cmovne eax, [r8], compiled with Clang 17 at -O2 on Linux, contains no memory read: it returns normally for r8 == 0, whereas the original instruction faults. This violates the documented unconditional-memory-source behavior. Make the access non-elidable and add a constant-false, inaccessible-source control.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4654315687 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/173#discussion_r4654315687). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T18:33:52Z. **Code review location:** `tools/cpu_switch_aot.py`; line 511 ~~~~diff @@ -472,6 +486,30 @@ def note(*numbers): else: a, b = self.operand(ins, 1, width), self.operand(ins, 2, width) return self.write(ins, width, f"{a} * {b}"), reads, writes + if mnemonic in SETCC: + condition = self.condition(producer, ins) + if ins.op0_kind == OpKind.MEMORY: + return f"store<std::uint8_t>({self.address(ins)}, static_cast<std::uint8_t>({condition}));", reads, writes + register = ins.op0_register + if register in HIGH_BYTES or RegisterExt.size(register) != 1: + raise ValueError(f"unsupported register {register}") + # A byte write keeps bits 8..63 of the full register. + name = NAMES[full(register)] + self.used.add(full(register)) + reads.add(full(register)) + return f"{name} = ({name} & ~0xffull) | static_cast<word>({condition});", reads, writes + if mnemonic in CMOVCC: + if width not in (4, 8): + raise ValueError(f"unsupported partial register write at RVA {ins.ip:#x}") + note(0, 1) + condition = self.condition(producer, ins) + source, destination = self.operand(ins, 1, width), self.operand(ins, 0, width) + # A 32-bit CMOVcc zero-extends the destination even when the condition is false, + # and a memory source is read either way. + if ins.op1_kind == OpKind.MEMORY: + line = self.write(ins, width, f"({condition}) ? source : {destination}") + return f"{{ const {unsigned(width)} source = {source}; {line} }}", reads, writes ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Make the unconditional CMOV source read non-elidable** When an optimized translation's condition folds to false, this ordinary `memcpy`-backed load can disappear despite being outside the ternary. For example, the emitted fragment for `mov rax, rdx; cmp ecx, ecx; cmovne eax, [r8]`, compiled with Clang 17 at `-O2` on Linux, contains no memory read: it returns normally for `r8 == 0`, whereas the original instruction faults. This violates the documented unconditional-memory-source behavior. Make the access non-elidable and add a constant-false, inaccessible-source control. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
Vonsek commented 2026-11-24 06:50:35 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T19:43:32Z.

Code review location: tools/cpu_switch_aot.py; line 511

@@ -472,6 +486,30 @@ def note(*numbers):
             else:
                 a, b = self.operand(ins, 1, width), self.operand(ins, 2, width)
             return self.write(ins, width, f"{a} * {b}"), reads, writes
+        if mnemonic in SETCC:
+            condition = self.condition(producer, ins)
+            if ins.op0_kind == OpKind.MEMORY:
+                return f"store<std::uint8_t>({self.address(ins)}, static_cast<std::uint8_t>({condition}));", reads, writes
+            register = ins.op0_register
+            if register in HIGH_BYTES or RegisterExt.size(register) != 1:
+                raise ValueError(f"unsupported register {register}")
+            # A byte write keeps bits 8..63 of the full register.
+            name = NAMES[full(register)]
+            self.used.add(full(register))
+            reads.add(full(register))
+            return f"{name} = ({name} & ~0xffull) | static_cast<word>({condition});", reads, writes
+        if mnemonic in CMOVCC:
+            if width not in (4, 8):
+                raise ValueError(f"unsupported partial register write at RVA {ins.ip:#x}")
+            note(0, 1)
+            condition = self.condition(producer, ins)
+            source, destination = self.operand(ins, 1, width), self.operand(ins, 0, width)
+            # A 32-bit CMOVcc zero-extends the destination even when the condition is false,
+            # and a memory source is read either way.
+            if ins.op1_kind == OpKind.MEMORY:
+                line = self.write(ins, width, f"({condition}) ? source : {destination}")
+                return f"{{ const {unsigned(width)} source = {source}; {line} }}", reads, writes

Fixed in 1e9969a. A CMOVcc memory source is now read through a volatile load (load_always), so it is kept when the condition folds to false. The new export cmov_read (mov rax, rdx; cmp ecx, ecx; cmovne eax, [r8]) is the constant-false, inaccessible-source control: with a valid R8 it must return the zero-extended low dword of RDX, and with a null R8 it must fault, in the original, the translation and the swapped-table build. In an MSVC Release build of the translation without the volatile load the null call returns (translation cmov_read with a null source did not fault); with it, the test passes in Debug and Release.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4654637828 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/173#discussion_r4654637828). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T19:43:32Z. **Code review location:** `tools/cpu_switch_aot.py`; line 511 ~~~~diff @@ -472,6 +486,30 @@ def note(*numbers): else: a, b = self.operand(ins, 1, width), self.operand(ins, 2, width) return self.write(ins, width, f"{a} * {b}"), reads, writes + if mnemonic in SETCC: + condition = self.condition(producer, ins) + if ins.op0_kind == OpKind.MEMORY: + return f"store<std::uint8_t>({self.address(ins)}, static_cast<std::uint8_t>({condition}));", reads, writes + register = ins.op0_register + if register in HIGH_BYTES or RegisterExt.size(register) != 1: + raise ValueError(f"unsupported register {register}") + # A byte write keeps bits 8..63 of the full register. + name = NAMES[full(register)] + self.used.add(full(register)) + reads.add(full(register)) + return f"{name} = ({name} & ~0xffull) | static_cast<word>({condition});", reads, writes + if mnemonic in CMOVCC: + if width not in (4, 8): + raise ValueError(f"unsupported partial register write at RVA {ins.ip:#x}") + note(0, 1) + condition = self.condition(producer, ins) + source, destination = self.operand(ins, 1, width), self.operand(ins, 0, width) + # A 32-bit CMOVcc zero-extends the destination even when the condition is false, + # and a memory source is read either way. + if ins.op1_kind == OpKind.MEMORY: + line = self.write(ins, width, f"({condition}) ? source : {destination}") + return f"{{ const {unsigned(width)} source = {source}; {line} }}", reads, writes ~~~~ --- Fixed in 1e9969a. A `CMOVcc` memory source is now read through a `volatile` load (`load_always`), so it is kept when the condition folds to false. The new export `cmov_read` (`mov rax, rdx; cmp ecx, ecx; cmovne eax, [r8]`) is the constant-false, inaccessible-source control: with a valid R8 it must return the zero-extended low dword of RDX, and with a null R8 it must fault, in the original, the translation and the swapped-table build. In an MSVC Release build of the translation without the volatile load the null call returns (`translation cmov_read with a null source did not fault`); with it, the test passes in Debug and Release.
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
R0Y00/Xbox2Windows!172
No description provided.