Skip to content

[AUTOMATED] Set C from the shifter in Thumb-2 ands, bics, movs and mvns - #1909

Merged
mahaloz merged 5 commits into
Noelo-Lab:mainfrom
sjkim1127:fix/thumb2-logical-carry
Nov 23, 2026
Merged

mahaloz merged 5 commits into
Noelo-Lab:mainfrom
sjkim1127:fix/thumb2-logical-carry

Conversation

@sjkim1127

Copy link
Copy Markdown
Contributor

[AUTOMATED]

On Thumb-2, kuna's ands, bics, movs and mvns with a modified immediate or a shifted register leave the carry flag unchanged. The architecture sets C to the shifter's carry-out, so a bcs after them tests the wrong bit.

printf '\x11\xea\x52\x00\x01\xd2\x01\x20\x70\x47\x02\x20\x70\x47' > cy.bin
# ands.w r0,r1,r2,lsr #1; bcs 1f; movs r0,#1; bx lr; 1: movs r0,#2; bx lr
kuna decompile cy.bin 0x0 --raw-image --target ARM:LE:32:v8-m:default --base 0 --isa thumb
unsigned int sub_0(void)
{
  char v1; // cy

  if (!v1)
    return 1;
  return 2;
}
  • These six constructors now set C from shift_carry, which their immediate and shifted-register operands already compute and which orrs, eors and tst already use. The function above now prints if (!(a2 & 1)) return 1; return 2;.
  • movs.w of an unshifted register still leaves C alone, as the architecture does.
  • The ARMv8 entry in the compiler oracle is regenerated; slacomp reproduces the old digest on the old sources.

tests/stages/kuna-thumb2-logical-carry.xml has the shifted-register case above and an immediate whose carry-out is 0. All 3 of its assertions fail without the fix. Compared one instruction at a time against Unicorn, 14 of 76 assembled Thumb-2 logical forms differed on C and none do now. The datatest corpus is unchanged, and decompiling a 6 MB Cortex-M firmware takes the same time.

🤖 Generated with Claude Code

Model: anthropic/claude-opus-5-5

The Thumb-2 flag-setting and/bic/mov/mvn with a modified immediate, and
and/mvn with a shifted register, used thSBIT_ZN and so set only N and Z.
The architecture sets C to the shifter's carry-out, which ThumbExpandImm12
and thshift2 already compute as shift_carry; orrs, eors and tst use it.
A bcs after `ands.w r0,r1,r2,lsr #1` read the carry from before it. These
constructors now set C from shift_carry; movs.w of an unshifted register
still leaves C alone.

Found by comparing kuna's lift with Unicorn instruction by instruction:
on Cortex-M firmware three such forms differed on C, and on an assembled
set of 76 logical forms 14 differed; all now match. The ARM8_le
compiler-oracle digest is regenerated with slacomp, which reproduces the
old digest on the old sources.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sjkim1127
sjkim1127 force-pushed the fix/thumb2-logical-carry branch from f9c70f8 to 05d6a7b Compare November 23, 2026 13:04
mahaloz and others added 2 commits November 23, 2026 13:47
Conflicts: the stage-corpus count in xml.rs (main 883 + this PR's file =
884) and docs/baseline-stages.json (synthesized: main's assertions plus this
PR's 3).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWTdjVzTz11WxPZmJ9b
Conflicts: the stage-corpus count in xml.rs (main 884 + this PR's file =
885) and docs/baseline-stages.json (synthesized: main's assertions plus this
PR's 3).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWTdjVzTz11WxPZmJ9b
@mahaloz

mahaloz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

[AUTOMATED] Pushed merge commits on top of 05d6a7b to bring the branch up to main (60b8400). Only the stage-corpus count in xml.rs (885) and docs/baseline-stages.json (main's assertions plus this PR's three) conflicted. The ARM8_le oracle digest from your rebase (3b8c0b33…) is unchanged, since no ARM spec landed in between. The SLEIGH change itself is untouched.

mahaloz and others added 2 commits November 23, 2026 14:13
Conflicts: the stage-corpus count in xml.rs (main 885 + this PR's file =
886) and docs/baseline-stages.json (synthesized: main's assertions plus this
PR's 3).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWTdjVzTz11WxPZmJ9b
Conflicts: the stage-corpus count in xml.rs (main 886 + this PR's file =
887) and docs/baseline-stages.json (synthesized: main's assertions plus this
PR's 3). The compiler oracle merged cleanly: main's AArch64 digest and this
PR's ARM8_le digest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWTdjVzTz11WxPZmJ9b
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.

2 participants