Commit d5d96178

d5d961783af1Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +4 added · ~1 modified

Description

Fix two codegen errors compiling Conway's Game of Life (#1508)

Two unrelated bugs surfaced by the gist:

  1. A format()/sprintf() argument that is itself a call rooting its own operands emitted that rooting decl into the PolyArray_push argument position -- sp_PolyArray_push(_t, sp_RbVal _t2 = ...; SP_GC_ROOT... (a declaration where an expression is required, the #1498 g_pre-leak class). The push line was written to g_pre mid-statement and the arg's sub-emission appended its prelude into it. Emit the boxed arg into a local buffer first, so its preludes flush to g_pre as whole statements before the push line.

  2. count { |x| <expr> } on a poly array used the block value directly as a C condition. A poly (sp_RbVal) predicate is a struct, invalid in if (...) ("used struct type value where scalar is required"). Route the predicate through emit_cond so any type becomes a valid truthiness test (poly -> sp_poly_truthy, nil -> false, scalar -> direct).

Test 984 (gcc+clang), optcarrot 59662, bench 57. (The gist still needs a separate ivar-array-widening fix -- an external attr-writer assignment of a poly array does not widen the empty-[] ivar -- tracked separately.)

Refs #1508.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
d5d961783af1
Parent
47191ea99a5b

src/codegen_call.cmodifiedView file

@@ -965,7 +965,11 @@
965965 if (bp) { emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "lv_%s = sp_%sArray_get(_t%d, _t%d);\n", bp, k, trecv, ti); }
966966 for (int j = 0; j < bn2 - 1; j++) emit_stmt(c, bb2[j], g_pre, g_indent + 1);
967967 int saveI = g_indent; g_indent = g_indent + 1;
968- Buf vb2; memset(&vb2, 0, sizeof vb2); emit_expr(c, bb2[bn2 - 1], &vb2);
968+ /* The block value is a condition: route through emit_cond so a poly /
969+ nil / scalar predicate becomes a valid C truthiness test (e.g.
970+ `count(&:alive)` where the element method is poly-dispatched would
971+ otherwise emit `if (sp_RbVal)` -- a struct in scalar position). */
972+ Buf vb2; memset(&vb2, 0, sizeof vb2); emit_cond(c, bb2[bn2 - 1], &vb2);
969973 g_indent = saveI;
970974 emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "if (%s) _t%d++;\n", vb2.p ? vb2.p : "0", tcnt);
971975 free(vb2.p);
@@ -1203,7 +1207,11 @@
12031207 if (bp) { emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "lv_%s = sp_PolyArray_get(_t%d, _t%d);\n", bp, trecv, ti); }
12041208 for (int j = 0; j < bn2 - 1; j++) emit_stmt(c, bb2[j], g_pre, g_indent + 1);
12051209 int saveI = g_indent; g_indent = g_indent + 1;
1206- Buf vb2; memset(&vb2, 0, sizeof vb2); emit_expr(c, bb2[bn2 - 1], &vb2);
1210+ /* The block value is a condition: route through emit_cond so a poly /
1211+ nil / scalar predicate becomes a valid C truthiness test (e.g.
1212+ `count(&:alive)` where the element method is poly-dispatched would
1213+ otherwise emit `if (sp_RbVal)` -- a struct in scalar position). */
1214+ Buf vb2; memset(&vb2, 0, sizeof vb2); emit_cond(c, bb2[bn2 - 1], &vb2);
12071215 g_indent = saveI;
12081216 emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "if (%s) _t%d++;\n", vb2.p ? vb2.p : "0", tcnt);
12091217 free(vb2.p);
@@ -4445,11 +4453,14 @@
44454453 emit_indent(g_pre, g_indent);
44464454 buf_printf(g_pre, "sp_PolyArray *_t%d = sp_PolyArray_new();\n", ta);
44474455 for (int ai = 1; ai < ac; ai++) {
4448- emit_indent(g_pre, g_indent);
4449- buf_printf(g_pre, "sp_PolyArray_push(_t%d, ", ta);
4456+ /* Emit the boxed arg into a local buffer first: an arg that is itself a
4457+ call rooting its operands pushes those decls to g_pre, which must land
4458+ as whole statements before this push line, not inside its arg list
4459+ (#1498 / #1508). */
44504460 Buf ab; memset(&ab, 0, sizeof ab);
44514461 emit_boxed(c, av[ai], &ab);
4452- buf_printf(g_pre, "%s);\n", ab.p ? ab.p : "sp_box_nil()");
4462+ emit_indent(g_pre, g_indent);
4463+ buf_printf(g_pre, "sp_PolyArray_push(_t%d, %s);\n", ta, ab.p ? ab.p : "sp_box_nil()");
44534464 free(ab.p);
44544465 }
44554466 buf_printf(b, "sp_str_format_polyarr(_t%d, _t%d)", tf, ta);