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

@@ -1577,15 +1577,45 @@
15771577 g_hoist_len_var = hbuf; g_hoist_len_recv = hn;
15781578 }
15791579 }
1580- emit_indent(b, indent);
1581- buf_puts(b, "while (");
1582- if (is_until) buf_puts(b, "!(");
1583- emit_cond(c, pred, b);
1584- if (is_until) buf_puts(b, ")");
1585- buf_puts(b, ") {\n");
1586- emit_loop_body(c, body, b, indent + 1);
1587- emit_indent(b, indent);
1588- buf_puts(b, "}\n");
1580+ /* Capture the predicate and any expression preludes it needs (method-call
1581+ temps etc.) into local buffers. A loop condition like
1582+ `advance while ident_continue_byte?(byte)` evaluates a method call
1583+ (`byte`, which reads mutable state) every iteration -- but emit_cond
1584+ routes that call's setup through g_pre, which is normally flushed ONCE
1585+ before the statement. For a loop that hoists the call out of the loop, so
1586+ the condition never re-evaluates (infinite loop / stuck position). Re-emit
1587+ the preludes INSIDE the loop and break on the (negated) condition so the
1588+ condition is recomputed each iteration. Conditions with no prelude keep
1589+ the plain `while (cond)` form. */
1590+ Buf cpre; memset(&cpre, 0, sizeof cpre);
1591+ Buf ccond; memset(&ccond, 0, sizeof ccond);
1592+ Buf *sv_pre = g_pre; int sv_ind = g_indent;
1593+ g_pre = &cpre; g_indent = indent + 1;
1594+ emit_cond(c, pred, &ccond);
1595+ g_pre = sv_pre; g_indent = sv_ind;
1596+ if (cpre.p && cpre.p[0]) {
1597+ emit_indent(b, indent); buf_puts(b, "while (1) {\n");
1598+ buf_puts(b, cpre.p);
1599+ emit_indent(b, indent + 1);
1600+ buf_puts(b, "if (");
1601+ if (!is_until) buf_puts(b, "!(");
1602+ buf_puts(b, ccond.p ? ccond.p : "0");
1603+ if (!is_until) buf_puts(b, ")");
1604+ buf_puts(b, ") break;\n");
1605+ emit_loop_body(c, body, b, indent + 1);
1606+ emit_indent(b, indent); buf_puts(b, "}\n");
1607+ } else {
1608+ emit_indent(b, indent);
1609+ buf_puts(b, "while (");
1610+ if (is_until) buf_puts(b, "!(");
1611+ buf_puts(b, ccond.p ? ccond.p : "");
1612+ if (is_until) buf_puts(b, ")");
1613+ buf_puts(b, ") {\n");
1614+ emit_loop_body(c, body, b, indent + 1);
1615+ emit_indent(b, indent);
1616+ buf_puts(b, "}\n");
1617+ }
1618+ free(cpre.p); free(ccond.p);
15891619 g_hoist_len_var = sv_hvar; g_hoist_len_recv = sv_hrecv;
15901620 }
15911621