Files
BFM-decomp/cookbook/C0277.md
T

2.1 KiB

§256 — GOTOS IN THE TARGET'S BLOCK ORDER REPRODUCE SWITCH PLACEMENT WITHOUT SWITCH'S SIDE EFFECTS (P31 S58b)

Three cards found the same escape from opposite directions.

THE PROBLEM. For a small dispatch, switch and if/else-if are not the only two spellings, and both can perturb things outside the dispatch. func_80181F78 (ov_SC06_000, 244 ins): switch nested the bodies inside the compare region and rotated the registers of an unrelated upstream copy loop; if/else-if emitted a bne staircase. The actual source shape is a flat two-target dispatch written as explicit gotos in the target's own block order:

if (t == 0) goto body0;
if (t == 1) goto body1;
goto end;
body0: …; goto end;
body1: …;
end:  ;

which emits beqz v1,.Lbody0 / beq v1,v0,.Lbody1 / j .Lend with both bodies after the tests and an unconditional j over body0 — the target layout — while leaving the loop's allocation untouched.

THE DISCRIMINATOR vs a switch's inline median tree (func_8017FB98, ov_SC01_009): both carry slti. What separates them is whether the arm bodies sit inline after their tests (switch's inline-tree emission) or after the whole dispatch, reached by forward jumps with delay-slot-filled compare constants (goto-dispatch). Inline-tree emission never produces the delay-slot-filled compares.

AND THE THIRD CARD. func_801F23D0 (md_SC03_076): a goto-ladder with one shared join is required for the tail shape — an if/else-if ladder with per-arm returns duplicated the lw/store in every arm. Zero-byte __asm__("") barriers between the chain tests were what kept gcc from threading the goto joins back into an else-if ladder; without them every variant collapsed to the 32-ins duplicated-tail form regardless of goto-vs-return spelling. The t=0/t=1 flag then materialised through the branch slots on its own.

BOUNDARY. Reach for gotos only when the dispatch has already been proven not to be a switch (no jtbl, no median tree, or a switch measurably perturbs code outside it). §225's warning stands: gotos landing mid-flow are for shapes no structured spelling reaches, not a first resort.