Skip to content

bugc: fix multi-block function return values#200

Merged
gnidan merged 1 commit intocall-returnfrom
compiler-fix-multiblock-return
Apr 2, 2026
Merged

bugc: fix multi-block function return values#200
gnidan merged 1 commit intocall-returnfrom
compiler-fix-multiblock-return

Conversation

@gnidan
Copy link
Copy Markdown
Member

@gnidan gnidan commented Apr 2, 2026

Summary

  • Fix incorrect return values from functions with if/else branches returning from both sides
  • Branch condition leaked onto stack via loadValue DUP; return epilogue mistakenly treated stale condition as the return value
  • Add generateReturnEpilogue that loads return value, cleans stale stack via SWAP+POP, loads saved return PC, and jumps back (same imperative pattern as generateCallTerminator)
  • Add behavioral tests for multi-block returns

Functions with if/else branches returning from both sides
produced wrong return values. The branch terminator's
loadValue(condition) DUPs the condition onto the stack;
JUMPI consumes the dup but the original remains. Successor
blocks inherit this stale value, so the return epilogue
incorrectly treated it as the return value.

Add generateReturnEpilogue that loads the return value,
cleans stale stack entries via SWAP+POP, then loads the
saved return PC and jumps back. Uses the same imperative
state pattern as generateCallTerminator.

Also changes generateTerminator return type from
Transition<S, S> to Transition<S, Stack> to accommodate
the stack-cleaning epilogue.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-04-02 08:32 UTC

@gnidan gnidan merged commit 1a452be into call-return Apr 2, 2026
4 checks passed
@gnidan gnidan deleted the compiler-fix-multiblock-return branch April 2, 2026 08:28
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