Skip to content

Explain and reproduce ps2eeas's nops before div.s (opt-in @divpad) - #204

Merged
llesieur99 merged 4 commits into
OpenRAC:mainfrom
JeremyL71:tools/ps2eeas-div-padding
Dec 3, 2026
Merged

llesieur99 merged 4 commits into
OpenRAC:mainfrom
JeremyL71:tools/ps2eeas-div-padding

Conversation

@JeremyL71

@JeremyL71 JeremyL71 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explains the nops retail has before div.s (the "Not explained yet" paragraph of docs/build/toolchain.md) and reproduces them for files that opt in with /* cflags: @divpad */. No existing file changes: with this branch's tools, all 9358 objects of a full build have the same .text as with main's tools.

What ps2eeas does

Measured on Ps2EeAs.exe 1.9.25 (SN 3.01 mirror), on an x86 machine, with -Wdivbug-on, which prints a warning for every pad:

  • It pads the divide-unit ops div.s, sqrt.s, rsqrt.s, vdiv, vsqrt and vrsqrt. It does not pad div, divu, div1, mult, mul.s or madd.s.
  • An op 0 or 1 instruction after a label ("possible branch destination", a function's entry included) or after the delay slot of a B-type branch (b, beq…, bal, bc1*, the likely forms; not j, jr, jal, jalr) gets 2 minus that distance nops. The kind of instructions in between does not matter.
  • There is no option to turn this on or off: the padding is always on. -divbug-on is rejected as an unknown option; the bare divbug-on string in the binary is probably what the parser compares after -W.
  • The "recursion without end" noted in tools/ps2eeas_nops.py is probably a stack overflow. The PE header reserves 1 MiB of stack, and a copy with a 256 MiB reserve assembles the inputs that crashed it under Wine.

Where the labels come from: -g

Most of retail's padded div.s have no label or branch near them in the compiler's output. They do with -g: GCC then writes a line label ($LM<n>:) at the start of every source line. Retail was compiled with -g there and stripped afterwards, so a div.s that starts a source line has two nops, and one inside a line has none.

This also explains the old statistics (the nops look as if they depended on what writes the operands): what decides is whether the div.s starts a source line.

-g does not change the code: a full build with and without it has the same .text in all 8687 compared objects.

Changes

  • tools/ps2eeas_nops.py:
    • rule 4 (--div), measured in the first-pass object like rules 1–3. cc.sh now assembles that object with -Wa,-L, so the compiler's local labels are in its symbol table; the output object is unchanged.
    • rabbitizer's R5900 table has no sqrt.s (it decodes 0x46000104 as a bare c1), so rule 4 decodes such a word again with the plain MIPS table. Without this, a file whose source has a sqrt.s was refused ("object has 1 DIV ops, source has 2").
    • Rule 3 no longer loses an mtc1 hazard when a .stabn line, or a loop label's .p2align (a form retail has: li.s; .p2align 3; $Lloop: <reader>), sits between the two instructions. Before, such a file was refused ("refusing to guess"). If the alignment does pad, the object has no adjacent pair and the tool still refuses.
  • tools/ps2eeas_stabs.py (new): drops -g's .stabs/.stabn/.stabd directives and keeps the labels, so objects carry no debug sections.
  • tools/cc.sh: the @divpad cflags token compiles with -g, runs ps2eeas_stabs.py and passes --div. It works like @lit4.
  • tools/test_ps2eeas_nops.py: five tests: label then div.s gives 2 nops, branch and one instruction then div.s gives 1, further away gives 0; the nops already inserted count (label, div.s, div.s pads only the first, as ps2eeas does and as retail's 20 nop nop div.s div.s show); the rule is opt-in; stabs lines do not hide an mtc1 hazard. A fifth covers a sqrt.s after a label.
  • docs/build/toolchain.md: the paragraph now explains the nops and how to use @divpad.

Why opt-in

Retail is not uniform. func_L00_00590698 starts with a bare div.s, which ps2eeas would have padded whatever the source layout. func_004A9CE0's matched C would get two nops it does not have, because its division is on its own line. Applied to every file, the rule would break at least these two of the 58 objects that contain a DIV op.

Checks

  • python -m unittest test_ps2eeas_nops (from tools/): 12 tests pass.
  • Full tools/build.sh on x86: 9358 objects, .text identical to main's tools, no refusing to guess. Rechecked after the .p2align change on a tree with my match PR's files and the isolated profiles installed: 9471 objects, same .text.
  • With @divpad, four PAL near misses match in check_func.py --version pal: func_00502B10, func_L00_006D57A0, func_L00_0077FA58, and func_L01_004C3660 with its division on its own line. They are in my match PR, which builds on this one.
  • Not checked: the US disc. I only have the PAL disc, and match_map.py --write needs the US dump, so there was no map-wide comparison. The object-level .text comparison above covers the "no change without @divpad" claim.

🤖 Generated with Claude Code

Retail's nops before div.s come from ps2eeas's divide-unit erratum padding,
measured on ps2eeas 1.9.25 with -Wdivbug-on: div.s, sqrt.s, rsqrt.s, vdiv,
vsqrt and vrsqrt 0 or 1 instruction after a label or after a B-type
branch's delay slot get 2 minus that distance nops. The labels are mostly
-g's line labels: retail was compiled with -g there and stripped, so a
div.s that starts a source line has two nops.

- ps2eeas_nops.py: rule 4 (--div), measured in the first-pass object, whose
  labels cc.sh now keeps with -Wa,-L; stabs lines no longer hide an mtc1
  hazard from rule 3.
- ps2eeas_stabs.py: drops -g's stabs directives and keeps its labels.
- cc.sh: the @divpad cflags token compiles with -g and runs rule 4.
  Opt-in: some retail objects have no such nops (func_L00_00590698).

-g leaves the code unchanged: a full build has the same .text in all 8687
compared objects. With these tools and no file using @divpad, the 9358
objects of a full build have the same .text as with the old tools.
With @divpad, func_00502B10, func_L00_006D57A0, func_L00_0077FA58 and
func_L01_004C3660 (division moved to its own line) match on PAL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ps2eeas measures the distance to the barrier after its own padding:
retail has `nop nop div.s div.s` 20 times and `nop nop div.s nop div.s`
never, and on ps2eeas a label followed by two div.s pads only the first.
Rule 4 now adds the nops it gave earlier ops between the barrier and the
current one to the distance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
JeremyL71 and others added 2 commits December 1, 2026 20:45
move_sites() dropped the pending mtc1/li.s register at any directive, so
`li.s; .p2align 3; $Lloop: <reader>` -- a form retail has -- counted one
site fewer in the source than in the object and the tool refused the file.
Alignment emits nothing there; when it does pad, the object has no adjacent
pair and the counts still disagree, so the tool keeps refusing rather than
guessing. A full build gives the same .text in all 9471 objects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rabbitizer's R5900 table has no sqrt.s: it decodes a word such as
0x46000104 (sqrt.s $f4,$f0) as a bare `c1`. The object side of rule 4 then
saw one DIV op fewer than the source and refused the file ("object has 1 DIV
ops, source has 2"). Such a word is now decoded again with the plain MIPS
table. A test covers a sqrt.s after a label; it fails without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bytematchdev

bytematchdev Bot commented Dec 3, 2026

Copy link
Copy Markdown

ByteMatch progress report

SCUS_974.65

Code matched 18.78% · 1.09 MB of 5.78 MB
Fuzzy match 19.78%
Functions 9,511 / 18,172
Units complete 5,278 / 5,337

No function changes.

SCES_532.85

Code matched 20.09% · 1.17 MB of 5.81 MB
Fuzzy match 21.03%
Functions 9,849 / 18,136
Units complete 5,301 / 5,359

No function changes.

Commit 15cca03 · Ratchet: Deadlocked on ByteMatch

@llesieur99
llesieur99 merged commit 808dfb9 into OpenRAC:main Dec 3, 2026
1 check passed
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