Commit Graph

323 Commits

Author SHA1 Message Date
sp00nz 206b733d3c lifter: lift every SSE compare, and single cmps/scas
Only four packed compares (cmpeq/lt/le/neqps) were lifted. Everything
else -- cmpltss above all -- fell to RECOMP_UNIMPL and left its
destination unchanged. RenderWare's frustum build normalises each side
plane as `cmpltss mask, len2; rsqrtss; andps mask`, so with the mask
stuck at zero every side plane came out (0,0,0,0) and every sector box
failed the frustum test: Burnout 3's race view drew the sky and nothing
else, while its axis-aligned cube-map cameras (built elsewhere) drew the
world. Found by tracing the empty per-group visibility flags back to the
camera's planes.

- All eight predicates, packed and scalar, named (cmpnless, ...) or by
  immediate (cmpps/cmpss), through recomp_cmp_pred / XMM_CMP_PRED. SSE
  compares no longer clear the tracked EFLAGS setter; they don't touch
  EFLAGS.
- sqrtps/rsqrtps/rcpps compute all four lanes (were lane 0 only).
- A bare cmpsb/w/d or scasb/w/d is one iteration of the rep form, ecx
  untouched, and sets the tracked flags. These were RECOMP_UNIMPL too;
  MSVC unrolls short memcmps into `cmpsd; jne` chains. The translator
  now declares _flags for any function with a string compare.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 02:04:31 -07:00
sp00nz a0069051ae lifter: x87 compare-to-EFLAGS reads unordered as below, equal and parity
fcomi/fucomi/sahf treated an unordered result (g_fp_cmp == 2) as
"greater" -- ja/jae true on NaN, jp/jnp constants. Unordered now reads
as below, equal and parity, as the hardware sets ZF, PF and CF.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 02:04:31 -07:00
sp00nz 894ceefd80 lifter: implement lahf, and give comiss its unordered flags
lahf lifted to a comment, so AH kept whatever EAX held. MSVC's "is this
float zero" test is `ucomiss x, 0; lahf; test ah, 0x44; jnp`, and it
answered at random. Burnout 3 guards a divide with exactly that: on 0 the
guard fell through, 0/0 became the player's position, and the race camera,
every matrix and the stunt readout ("-2147483648ft") went NaN. Found by
unmasking the SSE invalid exception and catching the first 0/0.

lahf now builds AH (SF ZF PF CF, bit 1 set; AF not modelled) from the
tracked flag setter through the same conditions the jcc forms use.

An unordered comiss/ucomiss sets ZF, PF and CF. The conditions used the
C relational operators, which are all false on NaN, and hardcoded
jp/jnp as "ordered". jb/jbe/je/jne/jp/jnp now include the unordered case
as the hardware does; ja/jae were already right.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 02:04:30 -07:00
sp00nz 64796dff20 lifter: honour x87 precision control on arithmetic results
The FPU stack is double-backed, which is right for PC=53 and near enough
for PC=64, but the Xbox runs PC=24, where every fadd/fsub/fmul/fdiv/fsqrt
result is rounded to float. Code that compares a freshly computed value
with the same value stored as a float depends on that rounding.

Burnout 3's sorted draw list re-inserts a node by walking forward while
v > next and back while v < prev. With v kept at double precision it sat
between its own float-stored copy and a neighbour and walked back and
forth forever: the race froze at the start line with the GPU idle.

Arithmetic results now go through RECOMP_FP_PC, which rounds to float
only when the guest control word selects 24-bit precision; other modes
are unchanged. Verified: BO3 gets past the start line and renders
in-race frames; lifter tests updated for the new emitted form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 02:04:30 -07:00
sp00nz 21d9ce75b8 Lift switch tables whose displacement is not slot 0
MSVC's CRT memcpy/memmove dispatch unaligned lead and trail bytes as

    and  eax, 3              ; 1..3 on this path, never 0
    jmp  [eax*4 + LeadUpVec - 4]

    sub  ecx, 4              ; -4..-1 when fewer than 4 dwords remain
    jmp  [ecx*4 + TrailUpVec + 16]

Reading forward from the displacement finds the jmp's own bytes in the
first form and the code after the table in the second, so both were
lifted as unresolvable indirect tail jumps. At runtime the target is
not a function, the ICALL is skipped, and memcpy returns without
copying whenever the destination is unaligned.

The emitted switch compares the loaded value against its arms instead
of indexing, so only the set of arms matters. When slot 0 is not an arm,
the lifter now also tries the slots after it and the slots below it,
and CFG recovery's census tries skipping slot 0. A census entry stays
authoritative: the lifter never rediscovers arms recovery rejected.

Surfaced in Burnout 3: its memcpy's jump-table arms were reached as
indirect targets, and feeding them back as seeds split memcpy and sent
XAPI down its dashboard-update path. Any title linking the MSVC CRT
has these two jumps (Half-Life 2 still carries one such ITAIL).

tools/recomp/test_switch_table_offset_index.py covers both forms; it
fails without the change. pytest tools/: 562 -> 564 passed, 17 skipped
before and after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 04:11:43 -07:00
sp00nz ddeb2ad2cc Fixups: #122's fused jcc reads the normalised form too; frndint test finds clang; apu_mmio_hook includes stdlib.h
The fused test+jcc emitted its inline condition from the raw test while
the snapshot beside it was now cmp X,0, so every fused test/jcc tested
X & 0 -- 11 conformance vector mismatches. Build the condition from the
same normalised form.

getenv without <stdlib.h> is implicit int on MSVC, which truncates the
pointer on x64 (C4013). #128 carries the same fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 01:01:04 -07:00
sp00nz 1eed5857fa Fixups for the v0.12.0 merge batch
- #89: guest-buffer floor was g_fs_base, which is per-thread; a worker
  thread's TIB sits high, so its reads into the image were refused
- #92: watch_is_code used one title's section end; use g_xbox_code_lo/hi
- #100: a negative --force-return value emitted 0x-1U
- #106: Alt+F4 stopped closing the window; pass sys keys through
- #116: an iret anywhere aborted the whole build; a linear sweep reads
  one out of data in Wreckless. It lifts as a RECOMP_UNIMPL marker now
- #122: the fused test+jcc path snapshotted test X,X but recorded
  cmp X,0, so a later jl compared X with itself; regression test added
- #125: __builtin_return_address does not exist in MSVC
- #96: holdoff comment still said 20 ms

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 00:55:21 -07:00
sp00nz f1acab4a2b Merge branch 'pr/131' into work/v0.12.0
# Conflicts:
#	tools/recomp/translator.py
2026-09-27 00:47:35 -07:00
sp00nz 98d96e930b Merge branch 'pr/130' into work/v0.12.0
# Conflicts:
#	tools/recomp/translator.py
2026-09-27 00:47:18 -07:00
sp00nz c8940c5f70 Merge branch 'pr/129' into work/v0.12.0 2026-09-27 00:46:54 -07:00
sp00nz 2e2ca6037c Merge branch 'pr/126' into work/v0.12.0 2026-09-27 00:46:47 -07:00
sp00nz 9b73b60d34 Merge branch 'pr/124' into work/v0.12.0 2026-09-27 00:46:46 -07:00
sp00nz 0f65529fee Merge branch 'pr/122' into work/v0.12.0 2026-09-27 00:46:45 -07:00
sp00nz e2ff585806 Merge branch 'pr/120' into work/v0.12.0 2026-09-27 00:46:45 -07:00
sp00nz 124719206e Merge branch 'pr/117' into work/v0.12.0 2026-09-27 00:46:36 -07:00
sp00nz 60c5ae018c Merge branch 'pr/116' into work/v0.12.0
# Conflicts:
#	tools/recomp/__main__.py
#	tools/recomp/translator.py
2026-09-27 00:46:36 -07:00
sp00nz d70307f574 Merge branch 'pr/110' into work/v0.12.0 2026-09-27 00:46:14 -07:00
sp00nz bdbfecdc32 Merge branch 'pr/104' into work/v0.12.0 2026-09-27 00:45:36 -07:00
NoRain211 f6adf8661a fix(recomp): register a switch arm only when it can run alone
A recovered arm was accepted once any instruction terminated it. An arm
could still run off its range, call a missing or empty body, or read flags
the dispatcher set, which a new C entry starts cleared. Each of those
misbehaves silently. Check every path, call and branch, and the flags read
on entry; otherwise leave the arm unresolved so dispatch stops loudly.
2026-09-26 16:09:24 -07:00
NoRain211 2a2a1444c9 fix(recomp): give switch arms outside their function an entry
A function list can cut one function at its own switch, so some arms land
past the dispatching entry's end. The lifter then emits the indexed jump as
an indirect tail call and the runtime dispatches the selected arm by
address. An arm at a function start resolves; any other arm has no
generated body, and the runtime stops on an unresolved address.

After CFG ownership settles, recover each such arm's reachable code, up to
its table, as a function of its own. A table is read only up to the next
indexed jump's table, so arms of an adjacent switch are not picked up. An
arm that branches to code with no generated body is left unresolved: that
branch would become a call to an empty stub, and a loud unresolved dispatch
is better than a silently skipped arm. Existing bodies are unchanged.

In Dead or Alive Xtreme Beach Volleyball, leaving the View Collection
screen reached 0xDE030, the shared exit of a split 4-way switch.
2026-09-26 16:02:26 -07:00
andeecollard 55115ce0c3 feat(recomp): indirect calls name their site, and small recorded sets get direct-call guards
The icall feedback database records which targets a title reached, never
from where, so the lifter has nothing to speculate on. Every indirect call
site makes an out-of-line dispatch lookup plus three writes to globals that
every guest thread shares (the trace ring and the call counter). This is the
half of VirtualDispatchTraceFiles that icall_feedback.py did not copy.

The lifter now hands the runtime the call site (RECOMP_ICALL_SAFE_AT, which
is RECOMP_ICALL_SAFE plus a site argument). Under RECOMP_ICALL_FEEDBACK the
runtime keeps a per-site target set behind an inline (site, last target)
cache. A site that keeps calling the same target pays two loads and a
compare. The set is written as icall_sites.dump beside the targets dump.
`icall_feedback merge` unions it into a cumulative icall_sites.json, and a
site that ever overflowed the record stays saturated. tools.recomp reads that
file (--icall-sites). A site whose whole recorded set is at most four
translated, non-manual functions becomes guarded direct calls through
RECOMP_ABI_CALL, with RECOMP_ICALL_SAFE_AT as the fallback. An unseen target
is slow, never wrong. With no sites file the output is unchanged apart from
the site argument. The guard counters compile away unless
RECOMP_ICALL_FEEDBACK is defined.

_fixup_icall_esp_save learned the new spelling. Without that, the esp save
it plants is never closed and the function does not compile.

Tests:
- test_icall_guarded.py checks the emitted text: guarded, unrecorded, an
  undefined target, and too many targets.
- test_icall_sites_merge.py checks that merge is cumulative and saturation
  sticks.
- test_icall_guarded_runtime.py compiles a lifted guarded site against
  RECOMP_ICALL_SAFE_AT taken from the header. For recorded, unseen,
  unresolvable and non-code targets it checks the callee, the lookup count
  and the final esp. It also round-trips a dump from icall_feedback.c through
  the merge parser.

Negative controls:
- guards calling the wrong arm fail the runtime test on "recorded B";
- the old translator fails 5 tests (the unclosed brace);
- the old lifter fails 3;
- the old icall_feedback.c fails the round trip.

On a retail title, one 150 s recorded run gave 1,379 sites, 1,327 of them
with a single target, and the next generation guarded 1,243. In the run
after that, the guards took all 13,174,538 calls at guarded sites with zero
misses and no faults, and the frame was unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017QvivF8psGzR3Ws8tv21Ut
2026-09-26 09:51:43 +01:00
andeecollard 26de405194 fix(disasm): the returning-body probe follows a switch through a measured table
probes_as_returning_body ended at any unconditional jmp that was not an
immediate forward branch, so a `jmp [reg*4 + table]` switch dispatch looked
like a tail call. The imm-ref pass relies on that probe, which left one shape
of function undiscoverable: reached only as an immediate (`mov eax, offset
fn`), no prologue, packed straight after the previous function's ret, and
opening with the dispatch. The cc-boundary pass needs int3 padding and the
gap-prologue pass needs a prologue, so nothing found it.

The cost is larger than one missing function. The switch it owns is
translated nowhere, and the gap-prologue pass then starts "functions" at its
later arms (`mov eax, 1 ; ret` probes as a constant stub) and at the npad in
front of the table, which decodes the table as code. A recompiled title
calls garbage from those and carries their bytes as untranslated
instructions.

resync_jump_tables has already measured the table by the time the imm-ref
pass asks, so the probe now continues at the first arm past the dispatch,
keeping its instruction budget. A dispatch through a table the engine has not
measured is still tail-call shaped: nothing says where its arms are.

test_returning_body_switch.py pins the probe on both sides and runs
detect_all over a caller plus such a function. Against the old probe the
end-to-end test finds three false functions (two arms and the table) and not
the real one; with the fix it finds the function and none of its arms.
Found on a retail title, where two such functions owned 21 switch tables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017QvivF8psGzR3Ws8tv21Ut
2026-09-26 09:45:45 +01:00
andeecollard 880cda202d fix(lifter): frndint rounds under the guest's x87 control word
frndint was lifted as rint(fp_top()), which rounds in the HOST's mode --
always nearest, because the guest's fldcw only ever reaches
g_fp_control_word. MSVC's CRT floor() and ceil() are _ctrlfp(RC=down/up)
around _frnd, a bare frndint, so both of them rounded to nearest: floor(2.7)
was 3 and ceil(2.3) was 2.

recomp_frndint(value, control) applies the RC bits (nearest-even, down, up,
toward zero), passes NaN and the infinities through, and keeps the sign of a
zero result. The lifter emits it for frndint, and recomp_fist (#47) now
rounds through it rather than carrying its own copy of the switch.

test_lifter_frndint.py checks the emitted statement and compiles and runs the
helper against each rounding mode; test_lifter_fpu_reverse.py's expected
frndint spelling moves with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017QvivF8psGzR3Ws8tv21Ut
2026-09-25 09:19:06 +01:00
Antigravity b72674f704 fix(lifter): read rcl/rcr carry-out in jc and setc 2026-09-24 23:46:07 -04:00
andeecollard 0d074a80c9 fix(lifter): REPE CMPS/SCAS produce CF, and keep the incoming ZF at a zero count
The rep-compare lift wrote ZF into `_flags` and nothing else. That answers
je/jne/sete, which is most of what a title asks of a rep compare, but MSVC's
std::string::compare (and memcmp) finish differently:

  xor eax, eax
  repe cmpsb              ; ecx = the shorter length
  je   equal
  sbb  eax, eax           ; CF -> -1 / 0
  sbb  eax, -1            ;    -> -1 / +1

Two defects, both found in Jet Set Radio Future's string compare:

CF. Nothing wrote _cf, so the sbb read the xor's CF of 0 and every mismatch
came back +1. "Less" was unreachable, so any ordering built on compare -- a
sorted container, a binary search -- got a one-sided answer. Only the byte
forms were tracked as flag setters at all, so after `repe cmpsd` the xor was
still the tracked setter.

A zero count. x86 leaves EFLAGS alone, so the je sees the xor's ZF=1 and
reports equal. `_flags` kept its last value, 0 on entry, so "" against ""
or against anything took the mismatch arm.

_rep_compare now writes `_cf = (first < second)`, unsigned at the element
width, in functions that declare _cf; the translator declares it when a
carry condition follows a rep compare, and _make_condition answers
jb/jae/jbe/ja from _cf and _flags. Every width (b/w/d) of cmps and scas is
tracked as a flag setter. A zero count keeps _cf for free (only an
iteration writes it); for ZF, lift_basic_block loads `_flags` from the
tracked setter before the loop.

test_lifter_rep_compare_flags.py compiles the lifted code and checks it
against x86 semantics for every count and ordering, across byte and dword
forms, including the sbb/sbb idiom. On main it fails 2 of 4; with this
change 4 of 4, and `pytest tools/` is 390 passed, 2 skipped.
2026-09-24 08:34:16 +01:00
fearkov d85dc71766 A join reached by test X, X and by cmp X, 0 inherits no flags, so its branch is dead
`_merge_flag_states` requires one operation across every predecessor of a
join. `test eax, eax` and `cmp [x], 0` are different operations by that
rule, so a block reached by one on each edge inherits no flag state at all
and its jcc falls back to `_flags` -- a variable nothing ever assigns. The
branch compiles as never taken, silently.

They are not actually different comparisons. Both compute X: ZF, SF and PF
come out identical, and both leave CF and OF clear. All that differs is how
the snapshot reconstructs the condition -- a cmp answers `je` with
`_fa == _fb`, a test with `(_fa & _fb) == 0` -- which is a property of this
lifter, not of the instructions.

So normalise `test X, X` onto `cmp X, 0` where the snapshot is taken, and at
the three sites that record the live flag setter so the state agrees with
what was emitted. The merge then succeeds because the predecessors really
are the same operation on the same width, rather than being forced through.

CF is unchanged: an unsigned value is never below zero, so a compare against
zero cannot borrow, which is the same `_cf = 0` the test path already
emitted.

Found in Shin Megami Tensei: Nine's video decoder. At 0x002C4EDC a `je` is
reached by `cmp [ebp-0x14], 0` falling through and by `test eax, eax`
jumping in from 0x002C4E3C. It sits in sub_002C4D63, which returns the mask
saying which coefficient groups carry AC terms; the IDCT it feeds has a
DC-only shortcut that writes a row of one repeated value for every group
whose bit is clear. With the branch dead the mask came back wrong, every
group took the shortcut, and each block came out flat across its rows with
its columns intact -- an intro video smeared horizontally with its vertical
structure still legible. Regenerating that function and sub_002C4372, which
has the same shape, renders it correctly.

Six tests cover the normalisation, the emitted snapshot, CF, and the merge
that used to return None. The negative control is `test eax, ebx`: a real
bitwise and, left alone, and still refused by the merge.
2026-09-23 22:56:28 -04:00
Antigravity 4492e16617 merge: carry alignment padding fix up from #111 2026-09-23 10:14:02 -07:00
Antigravity cebf48b6bd merge: carry alignment padding fix up from #111
# Conflicts:
#	tools/recomp/test_function_coalescence.py
2026-09-23 10:13:58 -07:00
Antigravity 8f66fe523a merge: carry alignment padding fix up from #111 2026-09-23 10:13:52 -07:00
Antigravity 9798409255 merge: carry alignment padding fix up from #111
# Conflicts:
#	tools/recomp/test_function_coalescence.py
2026-09-23 10:13:48 -07:00
Antigravity 8417351f5e merge: carry alignment padding fix up from #111
# Conflicts:
#	tools/recomp/test_function_coalescence.py
2026-09-23 10:13:43 -07:00
Antigravity 82fc2c3087 fix(recomp): treat a register self-move as alignment padding
MSVC pads a two-byte gap before an aligned branch target with
mov edi, edi. Coalescence accepted only nop and lea reg, [reg] as
padding, so a gap holding the self-move failed as an unowned CFG gap
and the repair was rejected. A mov with a different source is still
live code and still rejected.
2026-09-23 10:07:53 -07:00
Antigravity 6fd2f10c34 fix(lifter): read result sign at the operand width
After an 8- or 16-bit add, sub, adc, sbb, and, or, xor, neg or shift,
the sign conditions (js/jns and the jl/jg family that reduce to SF)
tested (int32_t)_fa. _fa is the result snapshot zero-extended, so it
is never negative below 32 bits: js was never taken and jns always
was. inc and dec already read _fas, the same snapshot sign-extended;
the rest of the family now does too.

test_result_sign_width translates op/js at al, ah, ax and eax and runs
it; conformance gains four sub-dword sets cases (360 vectors, 116
mismatches before). The clobber test's negative control now swaps
_fas back to the live destination as well.
2026-09-23 10:05:28 -07:00
andeecollard ee4eb97ce1 An untranslated instruction says so at runtime instead of vanishing into a comment
The lifter's answer to an instruction it cannot translate was a bare C
comment, and a comment is a no-op: the instruction vanished, the guest
carried on, and nothing at runtime could say the site had been reached.
Wreckless lost a bring-up to `bsf` that way and Half-Life 2 painted its
intro red to `cvtpi2ps`; each was found by working backwards from a
subsystem failure that had nothing to do with the cause. Eleven exits in
the lifter did this, three of them recording the site in the translator's
end-of-run tally and eight not.

All eleven now go through one helper, Lifter._unimplemented, which records
the site and emits RECOMP_UNIMPL(text, va) beside the comment. The
instruction is still a no-op -- nothing about generated behaviour changes
-- but the runtime logs the guest address and hit count of every
untranslated site it reaches (the first fifty), and under
RECOMP_UNIMPL_TRAP=1 aborts at the first one, at the cause. A healthy run
reaches none of these and prints nothing. Deliberately ignored
instructions (the cache hints) carry no marker, because a marker on a
decision would make the report count decisions as omissions; the unit
test pins that as its negative control, beside the three positive ones.

`grep TODO:` over a gen tree still enumerates every site, reached or not;
the comment stays beside the call.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XhigT6Yf5Xpp8sVmN8Vdi3
2026-09-21 19:11:15 +01:00
Antigravity cdb94c5f3a fix(lifter): lift LOOP/LOOPE/LOOPNE instead of branching on _flags
The LOOP family reached the generic jcc path, which only knows about
EFLAGS. That path dropped the ECX decrement entirely and emitted the
`_flags` fallback, a variable nothing ever assigns, so the back edge
compiled as never taken: a counted loop ran its body once with its
counter untouched.

    cmp  eax, 5
    loop L          ->  if (_flags /* loop: loop */) goto L;
    jge  done           if (_flags /* jge */) goto done;

Both branches above are dead. LOOP also preserves EFLAGS, so clearing
the tracked comparison discarded the flags the following jge needed.

Lift the counter directly and keep the flag state:

    ecx -= 1;
    if (ecx != 0) goto L;
    if (CMP_GE(_fas, _fbs)) goto done;

LOOPE/LOOPNE combine the counter with the tracked ZF condition. Without
a tracked setter they keep the existing fallback rather than inventing a
termination condition, but still count ECX down.
2026-09-21 08:37:01 -07:00
Antigravity 0d1edf1340 Reject unsupported interrupt returns 2026-09-21 08:28:46 -07:00
Antigravity 0e563acbcc Close callback and interrupt recovery gaps 2026-09-21 08:28:46 -07:00
Antigravity 8899d79e3c fix(recomp): close coalescence recovery evidence gaps
Rescan static callback tables against recovered instructions, reject
jump tables whose SIB scale is not a dword stride, carry pop-restored
constants through the indirect-target proof, and protect explicitly
configured SEH prolog/epilog helpers before coalescing.

Upstream already settles block-entry flag provenance to a fixed point,
so the equivalent solver from the original change is omitted; its
backward-computed-edge regression test passes against the existing
implementation.
2026-09-21 08:28:24 -07:00
fearkov 4dd267a89c rcl and rcr were never lifted, which silently breaks every 64-bit divide
Both are in _EFLAGS_SETTERS but neither had a lift, so they came out as
`/* TODO: rcr ebx, 1 */`. A comment is a silent no-op: the destination keeps
its previous value and nothing says the instruction was dropped.

Where they appear is not a corner. It is the divide MSVC emits for every
`long long` division:

    shr ecx, 1
    rcr ebx, 1        ; carries ecx's bit 0 into ebx's bit 31
    shr edx, 1
    rcr eax, 1
    or  ecx, ecx
    jnz loop

That loop normalises a 128-bit pair down to something a 32-bit divide can
take. With the rcr dropped the low halves never move, so both the divisor
and the dividend go into the divide wrong. Seventeen sites in the one title
I have to hand (Shin Megami Tensei: Nine), all of this shape, e.g. at
0x0023B4CF.

## The fix

`RC_ROT(val, n, &cf, width, left)` in the runtime header. The carry is a bit
sitting one place above the operand's top bit, so the rotation is over
width+1 places -- which is also why the count is reduced modulo width+1 for
the narrow forms and not modulo the width. A byte rotates through nine
positions, not eight. The 32-bit form takes the count masked to five bits
and no further, which is already inside 33.

`rcl`/`rcr` also join the direct-CF-reader tuple in
`translator.py::_function_needs_cf`. Without that `_cf` is not declared for
a function whose only carry user is an rcr, and the generated C does not
compile.

## Tests

`tools/recomp/test_lifter_rotate_carry.py`, six cases. Against main every
emission case fails and the semantics cases pass, because those check a
reference implementation rather than the lifter -- so the file is its own
negative control. 133 tests pass on the branch; the nine failures on main
are all from this file and nothing else.

The semantics test reconstructs the 64-bit shift the divide loop performs
and compares it against `value >> 1` for a handful of values including
0xFFFFFFFFFFFFFFFF, rather than asserting a table someone has to trust.

## Not in scope

`rol`/`ror` write no CF at all and are absent from CF_TRACKED, so a `jb`
after one takes the fallback path rather than reading a wrong carry. That is
a separate change: adding them shifts branch lifting across the whole image.
2026-09-20 22:52:04 -04:00
fearkov 37a21c8c95 --force-return: answer a function with a constant, instead of editing the C
Bring-up keeps arriving at the same shape: a title waits on a service the
runtime does not implement yet, the function that reports "is it finished"
answers no for ever, and everything past it is unreachable and therefore
untestable. Shin Megami Tensei: Nine's title screen asks whether the intro
movie has ended and its XMV decoder never reaches end of stream.

The alternative is editing the generated C by hand, which buries the
shortcut in hundreds of megabytes of output where nothing names it, it does
not survive a regeneration, and nobody else can reproduce the run.

The value is assigned at the ret rather than by skipping the body, which is
the whole design: the epilogue still runs, so esp is adjusted by the
function's own ret -- 4 for a cdecl, 4+n for a stdcall -- and nothing has to
infer the calling convention. Tail calls leave through their target rather
than a ret, so they carry the assignment after the call; the DDS9 function
above ends exactly that way, so handling only ret would have missed the case
that motivated the option.

g_force_return is read once at startup from RECOMP_FORCE_RETURN, so a build
carrying forced functions behaves normally until it is set.

258 tests pass against 254 on main.
2026-09-20 17:30:44 -04:00
Antigravity ab7f912d61 Limit segmented table rejection to FS 2026-09-20 07:40:41 -07:00
Antigravity 042c9a7792 Close indirect recovery proof gaps 2026-09-20 07:40:41 -07:00
Antigravity 1d089fb82a Harden recovery proof state 2026-09-20 07:40:41 -07:00
Antigravity 07bcf55ad0 Harden indirect call coalescence proof 2026-09-20 07:40:41 -07:00
Antigravity 6e1ad8bc52 Keep recovered CFG evidence authoritative 2026-09-20 07:40:41 -07:00
Antigravity 9a2895df4c Unify coalescence CFG evidence 2026-09-20 07:40:41 -07:00
Antigravity f9610dae3f Refresh computed edges after resync 2026-09-20 07:40:41 -07:00
Antigravity caecddc741 Track absolute LEA continuation targets 2026-09-20 07:40:41 -07:00
Antigravity 2ab796a98b Preserve coalescence owner and jump edges 2026-09-20 07:40:41 -07:00
Antigravity f9e6f1b47e Address continuation and manual-entry review 2026-09-20 07:40:41 -07:00