Skip to content

Tco self recursion#67

Merged
mdbergmann merged 3 commits into
masterfrom
tco-self-recursion
Feb 11, 2026
Merged

Tco self recursion#67
mdbergmann merged 3 commits into
masterfrom
tco-self-recursion

Conversation

@mdbergmann
Copy link
Copy Markdown
Owner

No description provided.

mdbergmann and others added 3 commits February 11, 2026 19:05
Add TCO pass to the optimizer that transforms self-recursive tail calls
into jumps, eliminating stack growth. The optimizer detects jsr _SUB_X
in tail position (only labels/NOPs/return-value push-pop between the
call and unlk/rts), then remaps param moves from sp-relative to
a5-relative offsets and replaces jsr with jmp to a _tcr re-entry label
placed after the SUB prologue.

Scope: normal SUBs with numeric params only (excludes CALLBACK,
INVOKABLE, module, and string-param SUBs). Non-tail calls (factorial,
fibonacci) are correctly left unchanged.

Tested on emulator: GCD(1000000,3) requiring ~333K iterations runs
with constant stack usage. All existing recursion tests pass unchanged.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Uses in-source OPTION O+ directive so tests enable optimization
themselves, allowing deep recursion assertions to work in the
standard test runner without any runner changes.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Add "Tail-Call Optimisation" section to docs/ace.txt explaining
  eligible patterns, accumulator technique, and TCO requirements
- Mention TCO in the -O switch description
- Add OPTION O+ to TCO test files so they enable optimization
  themselves, with deep recursion assertions uncommented

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@mdbergmann mdbergmann merged commit 14c2b7f into master Feb 11, 2026
@mdbergmann mdbergmann deleted the tco-self-recursion branch February 19, 2026 19:56
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