Skip to content

x86/tier1: honour the zero register and 32-bit forms in AND and ORR (shifted register) - #1

Open
bluetoe-99 wants to merge 2 commits into
feature/tier1-orr-shifted-registerfrom
feature/tier1-logical-32bit-xzr
Open

bluetoe-99 wants to merge 2 commits into
feature/tier1-orr-shifted-registerfrom
feature/tier1-logical-32bit-xzr

Conversation

@bluetoe-99

@bluetoe-99 bluetoe-99 commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

What this changes

The logical (shifted register) group (AND 0x8A..., ORR 0xAA..., ANDS 0xEA..., W-forms at sf=0) obeys two ARM rules that the Tier 1 handlers did not:

  1. Register field 31 is the zero register in this group, not the stack-pointer slot cpu.x[31] (ARM DDI0487, "Use of the zero and stack pointers"). translate_and_reg fed field 31 into register allocation, so AND X8, X9, XZR computed X9 & SP. translate_or_reg (added in upstream PR jit: translate ORR (shifted register) instead of a plain MOV pound-emu/ballistic#108) follows this rule but not rule 2, so this PR fixes its own gap too.
  2. A write to a W register zeroes bits 63:32, and register-operand shifts follow 32-bit semantics. Both handlers computed W-forms 64 bits wide: AND W8, W9, W10, LSR #4 slides stale upper-half bits into the result, and ASR sign-extends from bit 63 instead of bit 31.

The differential corpus never flagged either bug because its seeds start with all registers zeroed. With real state these are silent wrong-value corruptions, the same class as upstream PR pound-emu#108.

Encodings that cannot be done correctly without machinery that does not exist yet are rejected explicitly (BAL_ERROR_UNKNOWN_INSTRUCTION, same pattern as the AND-immediate reject in upstream PR pound-emu#108): W-form ROR at any amount (a 32-bit rotate is not a truncated 64-bit rotate, and rotate-#0 is reserved at sf=0, meaning ASR pound-emu#63 only in the 64-bit form), W-form shift amounts over 31 (undefined in the 32-bit encodings), and flag-setting forms that read XZR or use W-form (flags would need 32-bit compare semantics). These previously compiled with wrong 64-bit semantics. A clean skip is better than a silent wrong value.

Tests

10 new AND tests and 2 new ORR tests exercise both rules with non-zero register state and a stack-pointer sentinel. Every new test is red on the old handler (AND X8, XZR, XZR gives SP & SP, AND W8, W9, W10 keeps the upper half) and green after (0x80000010 ASR #4 gives 0xF8000001 via SHL #32 plus SAR #(amount+32)). ctest: 16/16.

No regressions (full corpus, same seeds.bin)

before: 60389 seeds | 465 passed | 38 failed | 28171 skipped | 6 errors
after:  60389 seeds | 441 passed | 38 failed | 28195 skipped | 6 errors
  • The 38 failing seeds (with per-seed mismatch counts) and the 6 error seeds are byte-identical.
  • The -24 passes is fully accounted for, seed by seed: 21 ANDS W... words, 2 ANDS X words with a slot-31 source, 1 AND W... ROR #0 (reserved at sf=0). Each was probed against a worker rebuilt from the branch before these two commits (it compiled the word) and the post-change worker (it rejects cleanly). All were vacuous passes: with every corpus register zeroed, the 64-bit-rule garbage happened to match the reference. They are now honest skips. (Two more seeds, 0A00FC00 and 6A00FC00, move from a logged Unicorn skip to the ballistic skip; counters unchanged.)

Not touched

The 38 WIP failures, the 6 error seeds, and src/generated/*. No decoder regeneration; the AND/ANDS W-entries already reach translate_and_reg through the existing REGISTER_32 guard.

Follow-ups (out of scope)

W-form ROR and 32-bit flag-setting (they need 32-bit shift/compare macros), ANDS/BICS with XZR sources, OR/EOR (immediate) translation.

@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-orr-shifted-register branch from 90b8cdd to 1572cdb Compare October 29, 2026 15:50
@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-logical-32bit-xzr branch from 8ec5c57 to 2b35652 Compare October 29, 2026 15:50
@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-orr-shifted-register branch from 1572cdb to 145baf2 Compare October 29, 2026 19:21
@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-logical-32bit-xzr branch from 2b35652 to c0b0948 Compare October 29, 2026 19:21
In the logical (shifted register) group a register field of 31 selects
the zero register, not the stack pointer stored at guest slot 31, and a
write to a 32-bit W register clears bits 63:32 of the 64-bit register.
The AND handler read guest slot 31 for register operands and computed
every result 64 bits wide, and the ORR handler had the same 32-bit gap;
both silently corrupted guest state because the corpus seeds start with
zeroed registers.

AND now clears the destination when either source is the zero register.
The 32-bit forms zero-extend their result with a 32-bit register copy;
LSR zero-extends its source first, and ASR realigns bit 31 into the
64-bit sign position before a single SAR. Flag-setting forms needing
32-bit compare semantics, W-form shift amounts above 31, and W-form
rotates needing a 32-bit rotate macro are rejected explicitly instead
of being computed with 64-bit rules.

Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
Ten tests exercise the zero-register sources, the discarded
zero-register destination, and the 32-bit shift forms with non-zero
state and a stack pointer deliberately set to a sentinel value that
the old code would have read through register field 31.

Two further tests cover the 32-bit ORR destination: an all-ones source
pair must clear bits 63:32, and a left shift of a W register must drop
the bits it shifts past bit 31.

Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-orr-shifted-register branch from 145baf2 to 4b7a2dd Compare October 29, 2026 19:46
@bluetoe-99
bluetoe-99 force-pushed the feature/tier1-logical-32bit-xzr branch from c0b0948 to f3da7a0 Compare October 29, 2026 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant