Commit 8a93d3dd
8a93d3dda44aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Boxing for loop counter block params when unified to poly
When block param names collide across multiple blocks (e.g. |i| in 3.times and |i| in array.each), the param gets widened to TY_POLY. Assign the concrete int/float loop counter via sp_box_int/sp_box_float when the destination slot is poly.
Add emit_iter_param_assign helper for times/step loops.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 8a93d3dda44a
- Parent
- 053918baaa56
src/codegen.cmodifiedView file
@@ -5661,6 +5661,25 @@
56615661
56625662
/* Block iteration lowered to an inline C for-loop. Handles n.times,56635663
array.each, range.each, n.upto/downto. Returns 1 if handled. */5664+
/* Emit `lv_<p0> = <expr_src>` boxing if p0 is poly and src is concrete. */5665+
static void emit_iter_param_assign(Compiler *c, int block, const char *p0_orig,5666+
const char *p0_ren, TyKind src_type,5667+
const char *src_expr, Buf *b, int indent) {5668+
Scope *sc = comp_scope_of(c, block);5669+
LocalVar *lv = sc ? scope_local(sc, p0_orig) : NULL;5670+
TyKind pt = lv ? lv->type : src_type;5671+
emit_indent(b, indent);5672+
if (pt == TY_POLY && src_type != TY_POLY) {5673+
Buf bx; memset(&bx, 0, sizeof bx);5674+
emit_boxed_text(c, src_type, src_expr, &bx);5675+
buf_printf(b, "lv_%s = %s;\n", p0_ren, bx.p ? bx.p : src_expr);5676+
free(bx.p);5677+
}5678+
else {5679+
buf_printf(b, "lv_%s = %s;\n", p0_ren, src_expr);5680+
}5681+
}5682+
56645683
static int emit_iteration_stmt(Compiler *c, int id, Buf *b, int indent) {56655684
const NodeTable *nt = c->nt;56665685
int block = nt_ref(nt, id, "block");@@ -5680,7 +5699,8 @@
56805699
56815700
if (recv < 0) return 0;56825701
int body = nt_ref(nt, block, "body");5683-
const char *p0 = block_param_name(c, block, 0); if (p0) p0 = rename_local(p0);5702+
const char *p0_orig = block_param_name(c, block, 0);5703+
const char *p0 = p0_orig ? rename_local(p0_orig) : NULL;56845704
TyKind rt = comp_ntype(c, recv);56855705
56865706
/* n.times { |i| ... } */@@ -5691,7 +5711,7 @@
56915711
emit_indent(b, indent);56925712
buf_printf(b, "for (mrb_int _t%d = 0; _t%d < ", t, t);56935713
buf_puts(b, rb.p); buf_printf(b, "; _t%d++) {\n", t);5694-
if (p0) { emit_indent(b, indent + 1); buf_printf(b, "lv_%s = _t%d;\n", p0, t); }5714+
if (p0) { char ts[32]; snprintf(ts, sizeof ts, "_t%d", t); emit_iter_param_assign(c, block, p0_orig, p0, TY_INT, ts, b, indent + 1); }56955715
emit_stmts(c, body, b, indent + 1);56965716
emit_indent(b, indent); buf_puts(b, "}\n");56975717
free(rb.p);@@ -5718,7 +5738,7 @@
57185738
buf_printf(b, "for (mrb_int _t%d = ", t); emit_expr(c, recv, b);57195739
buf_printf(b, "; _t%d >= 0 ? _t%d <= _t%d : _t%d >= _t%d; _t%d += _t%d) {\n",57205740
ts, t, tl, t, tl, t, ts);5721-
if (p0) { emit_indent(b, indent + 1); buf_printf(b, "lv_%s = _t%d;\n", p0, t); }5741+
if (p0) { char ts2[32]; snprintf(ts2, sizeof ts2, "_t%d", t); emit_iter_param_assign(c, block, p0_orig, p0, TY_INT, ts2, b, indent + 1); }57225742
emit_stmts(c, body, b, indent + 1);57235743
emit_indent(b, indent); buf_puts(b, "}\n");57245744
return 1;@@ -5739,7 +5759,7 @@
57395759
buf_printf(b, "mrb_int _t%d = (mrb_int)floor((_t%d-_t%d)/_t%d + _t%d_e);\n", tn, tl, tb, ts, tn);57405760
emit_indent(b, indent);57415761
buf_printf(b, "for (mrb_int _t%d = 0; _t%d <= _t%d; _t%d++) {\n", ti, ti, tn, ti);5742-
if (p0) { emit_indent(b, indent + 1); buf_printf(b, "lv_%s = _t%d + _t%d * _t%d;\n", p0, tb, ti, ts); }5762+
if (p0) { char fp_expr[64]; snprintf(fp_expr, sizeof fp_expr, "_t%d + _t%d * _t%d", tb, ti, ts); emit_iter_param_assign(c, block, p0_orig, p0, TY_FLOAT, fp_expr, b, indent + 1); }57435763
emit_stmts(c, body, b, indent + 1);57445764
emit_indent(b, indent); buf_puts(b, "}\n");57455765
return 1;