Skip to content

Peephole cleanup - #129

Merged
dmsc merged 11 commits into
masterfrom
peephole-cleanup
Oct 21, 2026
Merged

dmsc merged 11 commits into
masterfrom
peephole-cleanup

Conversation

@dmsc

@dmsc dmsc commented Oct 21, 2026

Copy link
Copy Markdown
Owner

This rewrites the peephole pass using simpler pattern matching rules instead of the explicit code.

dmsc added 11 commits October 20, 2026 23:51
Functions called from within a PRINT expression must only compute and
return a value; nested output or I/O-channel changes invalidate the
statement-local print state.

Aided by Opencode / DeepSeek V4.1 Flash 1017
A label may be reached by a jump, so the tracked print color from the
previous linear block is no longer valid.  Reset it together with the
top-of-stack tracking when a label is seen.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Add a reusable token-pattern matcher with typed captures and replacement
templates.  Rules are compiled once into tables and dispatched by their
first token.  FASTBASIC_PEEPHOLE_DEBUG=1 prints parsed rules, matches and
captures.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Replace the arithmetic, memory, I/O, POKE, constant, variable, update,
boolean, branch and print-join checks with entries in the compiled rule
table.  Structural passes that need to inspect whole ranges still run
procedurally.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Move number normalization, PUSH expansion and folding, SADDR folding,
label-target rewriting, short print strings and I/O channel tracking to
compiled rule tables.  This removes the remaining procedural matchers and
simplifies the optimizer loop.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Use an expression span to handle any value expression after a constant
byte POKE address.  Reject spans that cross source lines, labels, or
statement terminators so same-line statements and DPOKE sequences stay
separate.

The generic rule replaces the two redundant simple numeric-address POKE
rules.  Add a complex-value regression test, including same-line and
DPOKE boundary cases.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Use the expression-span rule for constant word addresses, emitting
NUM_POKE with the value expression.  Remove the redundant simple
word-address POKE rules and cover word-address values and statement
boundaries in the regression test.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Generalize symbolic byte-address POKE to match arbitrary value
expressions, replacing the two simple-value rules.  Add a cross-only
regression test for the imported byte symbol.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Fold the address/SADDR sequence around INPUT into a variable store.  This
removes the DPOKE address setup when the INPUT target is a word variable.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Add arithmetic, boolean, conditional-return, address increment and
byte-symbol PUT tests, and extend the boolean test to cover comparison
normalization.  Each test checks the optimized program's output, not only
that the compiler accepts it.

Aided by Opencode / DeepSeek V4.1 Flash 1017
Tag the optimizer debug output with the phase and rule number, and add a
development target that compiles the test sources with both targets and
fails if any peephole rule is not reached.  The check uses test sources
only, so an uncovered rule cannot be hidden by the compiler or IDE.

Remove the rules that the coverage report proved unreachable, and keep the
generated coverage files under build/ so make clean and make distclean
remove them.

Aided by Opencode / DeepSeek V4.1 Flash 1017
@dmsc
dmsc merged commit e1414a8 into master Oct 21, 2026
6 checks passed
@dmsc
dmsc deleted the peephole-cleanup branch October 22, 2026 12:12
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