Skip to content

[AUTOMATED] Print a function-tail if wrapped or as a guard clause by the size of its tail (option tailform) - #1902

Merged
mahaloz merged 19 commits into
mainfrom
feat/tailform
Nov 23, 2026
Merged

mahaloz merged 19 commits into
mainfrom
feat/tailform

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

At the end of a function, if (c) { S } return K; and if (!c) return K; S; return K; are the same program. kuna printed whichever one the compiler's return layout implied, and that layout usually points the other way from the source: a short body ending in a call gets its own exit (a tail jump), so it prints as a guard clause, while a long body shares the epilogue and prints wrapped.

cat > tail.c <<'EOF'
int hits;
__attribute__((noipa)) void note(void) { hits++; }
__attribute__((noipa)) void note_if_zero(int a) { if (a == 0) note(); }
__attribute__((noipa)) int note_twice(int a) { if (a) { note(); note(); } return 0; }
int main(int c, char **v) { note_if_zero(c); return note_twice(c); }
EOF
gcc -O2 -o tail tail.c && kuna decompile tail note_if_zero && kuna decompile tail note_twice

Before:

void note_if_zero(int a0)
{
  if (a0)
    return;
  note(); // tail-call
}
unsigned long note_twice(int a0)
{
  if (!a0)
    return 0;
  note();
  note();
  return 0;
}

After:

void note_if_zero(int a0)
{
  if (!a0)
    note(); // tail-call
}
unsigned long note_twice(int a0)
{
  if (a0) {
    note();
    note();
  }
  return 0;
}

A long body goes the other way: six statements wrapped in if (p) { ... } before a shared epilogue now print behind if (!p) return;.

  • New P8 print-tree pass kuna_tailform, option tailform off|on|<n>|<void>,<value> (default on, <n> = 4). It works on the function's top-level statements. A guard if (c) return K; whose remaining tail prints at most <n> lines becomes if (!c) { tail } return K;. A one-arm if before the final bare return whose body prints more than <n> lines becomes a guard clause. In a valued function, an arm of that last if that ends in the function's own return K; drops it.
  • Both returns must give the same value: void, equal constants, or one SSA value. The condition is negated in place the way FlowBlock::negateCondition does it. Statements only move, and no op is cloned, reordered or removed. Tails that end in a decode-failure or halt truncation are left alone.
  • When the final return K; shares a basic block with the last statement (shrink-wrapped call; xor eax,eax; ret), the leaf prints without its return inside the if, and a return-only copy of the leaf prints after it. Two leaf flags and three small hooks in printc.rs do this.
  • kuna_tailform::decide holds the whole policy (current form, void or valued, printed lines), so another decision function can replace it.
  • A guard that looptest drops in front of a loop stays looptest's: the pass computes that plan on the tree it edits and leaves such an if (and any tail or body holding one) alone.
  • rettriangle stays as it is. It decides before structuring and still matters for joins nested inside other constructs.

On the decbench O2 (no inlining) corpus, 549 more functions match the source's control flow exactly (gained 755, lost 206), and 461 more at O0 (gained 717, lost 256). The threshold was fitted on half the projects and checked on the other half. The lost functions are sources that wrote the other form.

Tests: tests/stages/kuna-tailform.xml decompiles a gcc -O2 fixture with the option off and on (11 assertions, two of them on a loop looptest owns). kuna-cli/tests/tailform_round_trip.rs compiles the printed C of the -O2 and -O0 builds beside the source with gcc and clang at -O0/-O2, and checks every function returns, counts and stores what the source does. There are also unit tests for decide and the option value. Five vendored datatests, thirteen stage tests and three kuna-cli tests that pin the binary's return layout now set option tailform off, and one CLI probe accepts either polarity of a re-spelled if. docs/baseline.json is unchanged.

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU

mahaloz and others added 19 commits November 23, 2026 10:37
…ion tailform)

At the end of a function `if (c) { S } return K;` and
`if (!c) return K; S; return K;` are the same program, and kuna printed
whichever one the binary's return layout implied. The new P8 print-tree
pass kuna_tailform wraps a guard whose remaining tail prints at most 4
lines, turns a one-arm if before the final return whose body prints more
into a guard clause, and drops an arm's return that repeats the
function's final return. Returns must be void, equal constants or one
SSA value; conditions are negated in place; no op is cloned, reordered
or removed. Option `tailform off|on|<n>|<void>,<value>`, default on (4).

Vendored datatests and stage tests that pin the binary's own return
layout set `option tailform off`.

[AUTOMATED]

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
…keep the source helpers

The printed record() indexes the int array as bytes, a typing defect outside
this feature, so the round trip links the re-spelled functions against the
source's note() and record().

[AUTOMATED]

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
…ng call's if

tailform wraps the one-statement tail, so the test prints negated.

[AUTOMATED]

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
The looptest plan prints a rotated loop top-tested and drops the guard in
front of it. tailform now computes that plan on the tree it edits and
does not merge, wrap or guard a claimed if, nor move a tail or body that
holds one, so while (p) { ... } is not turned back into
if (!p) return; do { ... } while (p);. The stage test gains a list walk
that pins both plans, and the counters are re-derived after the merge.

[AUTOMATED]

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
…urn layout

compact_switch_cli checks the switch's nesting, rettriangle_round_trip the
split it compares against, and the warning-style test the line each warning
lands on; all three print the binary's own function-tail form.

[AUTOMATED]

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
…ing main

The looptest plan now takes the printer's declared counter types; the pass
plans before they exist and passes none, so counters keep their high's own
type. kuna-looptest-order pins the binary's return layout like
kuna-looptest, and the corpus count is re-derived.

[AUTOMATED]

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWVABMswm9QwjnywGWU
@mahaloz
mahaloz merged commit 8d781e7 into main Nov 23, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/tailform branch November 23, 2026 14:08
mahaloz added a commit to sjkim1127/kuna that referenced this pull request Nov 23, 2026
Resolve the stage-corpus count (885 on main after Noelo-Lab#1902, +1 here = 886) and
synthesize docs/baseline-stages.json from both sides (main 5198 + 5 = 5203).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWTdjVzTz11WxPZmJ9b
mahaloz added a commit that referenced this pull request Nov 23, 2026
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