Tco self recursion#67
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.