Commit fdeb639a
fdeb639a7db1Merged
Erik Peterson <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
codegen: re-evaluate loop-condition preludes each iteration (fix hoisted side-effecting while condition)
emit_while emitted the predicate via emit_cond, whose method-call preludes (temps) route through g_pre -- which is flushed ONCE before the statement. For a loop, that hoisted the condition's method calls OUT of the loop, so the condition was computed a single time and never re-checked.
Concretely, the Tungsten lexer's advance while ident_continue_byte?(byte)
compiled to:
mrb_int _t = byte(); // hoisted out of the loop
while (ident_continue_byte_p(_t)) advance();
so byte (which reads the mutable scan position that advance bumps) was read
once -> the scanner never advanced past the first identifier -> the parser
re-parsed the same token forever (O(infinity) -> tens of GB before OOM).
Fix: when the predicate needs preludes, emit while (1) { <preludes>; if (!cond) break; <body> } (and if (cond) break for until), so the condition
and its preludes are recomputed every iteration. Conditions with no prelude
keep the plain while (cond) form. The loop-invariant strlen hoist is
preserved.
Full test suite green: 894 pass / 0 fail / 0 error. With this, the migrated Tungsten stage0 lexer advances correctly: stage1 now runs in seconds with bounded memory (was OOMing past 128GB) and reaches a real parse error instead of looping.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- fdeb639a7db1
- Parent
- d40bc31b97a3
src/codegen_stmt.cmodifiedView file
g_hoist_len_var = hbuf; g_hoist_len_recv = hn; } } emit_indent(b, indent); buf_puts(b, "while ("); if (is_until) buf_puts(b, "!("); emit_cond(c, pred, b); if (is_until) buf_puts(b, ")"); buf_puts(b, ") {\n"); emit_loop_body(c, body, b, indent + 1); emit_indent(b, indent); buf_puts(b, "}\n"); /* Capture the predicate and any expression preludes it needs (method-call temps etc.) into local buffers. A loop condition like `advance while ident_continue_byte?(byte)` evaluates a method call (`byte`, which reads mutable state) every iteration -- but emit_cond routes that call's setup through g_pre, which is normally flushed ONCE before the statement. For a loop that hoists the call out of the loop, so the condition never re-evaluates (infinite loop / stuck position). Re-emit the preludes INSIDE the loop and break on the (negated) condition so the condition is recomputed each iteration. Conditions with no prelude keep the plain `while (cond)` form. */ Buf cpre; memset(&cpre, 0, sizeof cpre); Buf ccond; memset(&ccond, 0, sizeof ccond); Buf *sv_pre = g_pre; int sv_ind = g_indent; g_pre = &cpre; g_indent = indent + 1; emit_cond(c, pred, &ccond); g_pre = sv_pre; g_indent = sv_ind; if (cpre.p && cpre.p[0]) { emit_indent(b, indent); buf_puts(b, "while (1) {\n"); buf_puts(b, cpre.p); emit_indent(b, indent + 1); buf_puts(b, "if ("); if (!is_until) buf_puts(b, "!("); buf_puts(b, ccond.p ? ccond.p : "0"); if (!is_until) buf_puts(b, ")"); buf_puts(b, ") break;\n"); emit_loop_body(c, body, b, indent + 1); emit_indent(b, indent); buf_puts(b, "}\n"); } else { emit_indent(b, indent); buf_puts(b, "while ("); if (is_until) buf_puts(b, "!("); buf_puts(b, ccond.p ? ccond.p : ""); if (is_until) buf_puts(b, ")"); buf_puts(b, ") {\n"); emit_loop_body(c, body, b, indent + 1); emit_indent(b, indent); buf_puts(b, "}\n"); } free(cpre.p); free(ccond.p); g_hoist_len_var = sv_hvar; g_hoist_len_recv = sv_hrecv;}