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:
-
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. -
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 inif (...)("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
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); } for (int j = 0; j < bn2 - 1; j++) emit_stmt(c, bb2[j], g_pre, g_indent + 1); int saveI = g_indent; g_indent = g_indent + 1; Buf vb2; memset(&vb2, 0, sizeof vb2); emit_expr(c, bb2[bn2 - 1], &vb2); /* The block value is a condition: route through emit_cond so a poly / nil / scalar predicate becomes a valid C truthiness test (e.g. `count(&:alive)` where the element method is poly-dispatched would otherwise emit `if (sp_RbVal)` -- a struct in scalar position). */ Buf vb2; memset(&vb2, 0, sizeof vb2); emit_cond(c, bb2[bn2 - 1], &vb2); g_indent = saveI; emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "if (%s) _t%d++;\n", vb2.p ? vb2.p : "0", tcnt); free(vb2.p); 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); } for (int j = 0; j < bn2 - 1; j++) emit_stmt(c, bb2[j], g_pre, g_indent + 1); int saveI = g_indent; g_indent = g_indent + 1; Buf vb2; memset(&vb2, 0, sizeof vb2); emit_expr(c, bb2[bn2 - 1], &vb2); /* The block value is a condition: route through emit_cond so a poly / nil / scalar predicate becomes a valid C truthiness test (e.g. `count(&:alive)` where the element method is poly-dispatched would otherwise emit `if (sp_RbVal)` -- a struct in scalar position). */ Buf vb2; memset(&vb2, 0, sizeof vb2); emit_cond(c, bb2[bn2 - 1], &vb2); g_indent = saveI; emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "if (%s) _t%d++;\n", vb2.p ? vb2.p : "0", tcnt); free(vb2.p); emit_indent(g_pre, g_indent); buf_printf(g_pre, "sp_PolyArray *_t%d = sp_PolyArray_new();\n", ta); for (int ai = 1; ai < ac; ai++) { emit_indent(g_pre, g_indent); buf_printf(g_pre, "sp_PolyArray_push(_t%d, ", ta); /* Emit the boxed arg into a local buffer first: an arg that is itself a call rooting its operands pushes those decls to g_pre, which must land as whole statements before this push line, not inside its arg list (#1498 / #1508). */ Buf ab; memset(&ab, 0, sizeof ab); emit_boxed(c, av[ai], &ab); buf_printf(g_pre, "%s);\n", ab.p ? ab.p : "sp_box_nil()"); emit_indent(g_pre, g_indent); buf_printf(g_pre, "sp_PolyArray_push(_t%d, %s);\n", ta, ab.p ? ab.p : "sp_box_nil()"); free(ab.p); } buf_printf(b, "sp_str_format_polyarr(_t%d, _t%d)", tf, ta);test/count_block_poly_pred.rbaddedView file
Loading diff…
test/count_block_poly_pred.rb.expectedaddedView file
Loading diff…
test/format_arg_rooting.rbaddedView file
Loading diff…
test/format_arg_rooting.rb.expectedaddedView file
Loading diff…