Stop literal folds from consuming jump targets - #579
Merged
Conversation
The peephole literal folds (binary fold, unary fold, and the literal field-index fusion) merged a literal push with following operator tuples without checking whether those tuples are branch targets. A jump into the middle of the folded range — the join point of a ternary, for example — was remapped onto the folded replacement, so one branch of the conditional skipped the operation, produced the other branch's folded result, and leaked a value onto the operand stack: (v ? v : 24) * 2 with v = 60 yielded 48 and displaced later print operands. Each fold now requires that no consumed tuple other than the first be an address target, matching the guards the concat-run collapse and the ASSIGN+POP fusion already apply. A jump to the first tuple of the range stays safe: it lands on the replacement, which computes the same value. Fixes #578 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
Author
|
@codex please review |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Contributor
Author
|
@copilot resolve the merge conflicts in this pull request |
Co-authored-by: bertysentry <32521698+bertysentry@users.noreply.github.com>
Contributor
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.
Fixes #578
What
With the default tuple optimization,
jawk 'BEGIN { v = 60; print "size:", (v ? v : 24) * 2 }'printed60 48— the false branch's folded result, with the true-branch value leaking onto the operand stack and displacing the"size:"operand. It now printssize: 120, like gawk and like-sruns.How
(v ? v : 24) * 2compiles so that the false branch'sPUSH 24falls through to the join point (PUSH 2, MULTIPLY) that the true branch jumps to. The binary literal fold merged all three tuples intoPUSH 48, andremapAddressesthen landed the true branch'sGOTOon that folded literal, skipping the multiplication entirely.The three peephole literal folds — the binary fold, the unary fold, and the literal +
GET_INPUT_FIELDfusion — now require that no consumed tuple other than the first be an address target, exactly the guard that the concat-run collapse and the ASSIGN+POP fusion already apply. A jump to the first tuple of a folded range remains safe, because it lands on the replacement, which computes the same value.Why it matters
Any conditional expression whose result feeds a further operation on a literal was affected:
(cond ? a : b) * 2,-(cond ? a : b),$(cond ? 1 : 2). patsie75/awk-demo sizes its framebuffer withterminal["height"] = ($1 ? $1 : 24) * 2, which always produced 48 and made the demo abort withTerminal size (80x48) is smaller than program size (80x50)regardless of the real terminal size.Tests
AwkTupleOptimizationTestgains five cases: the binary, unary, and field-index join-point shapes (asserting both the runtime result and that the operator tuple survives folding), the assignment form, and the false-branch path of the guarded ternary.mvn verifypasses: all unit and compatibility tests, checkstyle, PMD, and SpotBugs clean. Behavior change documented underUnreleasedinbehavior-changes.md.🤖 Generated with Claude Code