Commit 4c5bff52

4c5bff524c14Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Mark heap globals during GC; write print(n.chr) as a raw byte

Two fixes finishing bm_ao_render and bm_so_mandelbrot:

  • Heap objects reachable only through a file-scope static — a constant (RAND = Rand.new), a global variable, a module/class-level ivar, or a singleton-reader slot — were invisible to the collector: the generated TU never extended sp_gc_mark_globals_hook beyond the runtime's own sp_re_mark_globals. The first collection swept them and recycled the memory (ao_render's PRNG turned to garbage mid-render; with different GC timing it segfaulted in Isect_scan). Emit sp_mark_user_globals covering every heap-typed gv_/cst_/civ_/sg_ slot, chaining to sp_re_mark_globals, and install it in sp_re_init.

  • print n.chr went through the C-string path, so a NUL byte vanished from the output (the P4/P6 image benchmarks emit them). Emit putchar(n & 0xff) directly.

bm_so_mandelbrot and bm_ao_render now pass byte-for-byte. make test stays green (845 pass).

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

Details

Commit
4c5bff524c14
Parent
508a32b696a8

src/codegen.cmodifiedView file

@@ -1289,6 +1289,10 @@
12891289 buf_puts(b, "static void sp_re_init(void) {\n");
12901290 buf_puts(b, " sp_sym_name_fn = sp_sym_to_s;\n");
12911291 buf_puts(b, " sp_user_exc_parent_fn = sp_user_exc_parent;\n");
1292+ /* Replace the runtime's hook with the superset that also marks this
1293+ program's heap-typed globals/constants/class-ivars (it chains to
1294+ sp_re_mark_globals itself). */
1295+ buf_puts(b, " sp_gc_mark_globals_hook = sp_mark_user_globals;\n");
12921296 if (g_re_count > 0) {
12931297 buf_puts(b, " sp_re_set_error_handler(sp_re_startup_error_handler);\n");
12941298 for (int i = 0; i < g_re_count; i++) {
@@ -1776,6 +1780,42 @@
17761780 }
17771781 if (c->ngvars || c->nconsts) buf_puts(&b, "\n");
17781782
1783+ /* GC marking for the file-scope statics above: heap objects reachable
1784+ only through a global/constant/class-ivar slot would otherwise be
1785+ swept (RAND = Rand.new lost its PRNG mid-render). Chained ahead of
1786+ the runtime's own sp_re_mark_globals via the hook in sp_re_init. */
1787+ {
1788+ buf_puts(&b, "static void sp_mark_user_globals(void) {\n");
1789+ buf_puts(&b, " sp_re_mark_globals();\n");
1790+ for (int i = 0; i < c->ngvars; i++) {
1791+ LocalVar *lv = &c->gvars[i];
1792+ if (!is_scalar_ret(lv->type)) continue;
1793+ if (lv->type == TY_STRING) buf_printf(&b, " sp_mark_string(gv_%s);\n", lv->name);
1794+ else if (lv->type == TY_POLY) buf_printf(&b, " sp_mark_rbval(gv_%s);\n", lv->name);
1795+ else if (needs_root(lv->type)) buf_printf(&b, " if (gv_%s) sp_gc_mark((void *)gv_%s);\n", lv->name, lv->name);
1796+ }
1797+ for (int i = 0; i < c->nconsts; i++) {
1798+ LocalVar *lv = &c->consts[i];
1799+ if (!is_scalar_ret(lv->type)) continue;
1800+ if (lv->type == TY_STRING) buf_printf(&b, " sp_mark_string(cst_%s);\n", lv->name);
1801+ else if (lv->type == TY_POLY) buf_printf(&b, " sp_mark_rbval(cst_%s);\n", lv->name);
1802+ else if (needs_root(lv->type)) buf_printf(&b, " if (cst_%s) sp_gc_mark((void *)cst_%s);\n", lv->name, lv->name);
1803+ }
1804+ for (int i = 0; i < c->nclasses; i++) {
1805+ ClassInfo *ci = &c->classes[i];
1806+ for (int j = 0; j < ci->nivars; j++) {
1807+ TyKind t = ci->ivar_types[j] == TY_UNKNOWN ? TY_INT : ci->ivar_types[j];
1808+ const char *iv = ci->ivars[j] + 1;
1809+ if (t == TY_STRING) buf_printf(&b, " sp_mark_string(civ_%s_%s);\n", ci->name, iv);
1810+ else if (t == TY_POLY) buf_printf(&b, " sp_mark_rbval(civ_%s_%s);\n", ci->name, iv);
1811+ else if (needs_root(t)) buf_printf(&b, " if (civ_%s_%s) sp_gc_mark((void *)civ_%s_%s);\n", ci->name, iv, ci->name, iv);
1812+ }
1813+ for (int j = 0; j < ci->nsg_readers; j++)
1814+ buf_printf(&b, " sp_mark_rbval(sg_%s_%s);\n", ci->name, ci->sg_readers[j]);
1815+ }
1816+ buf_puts(&b, "}\n\n");
1817+ }
1818+
17791819 /* Constructor defs, method defs, and main go into a separate buffer. Any
17801820 proc literals they contain accumulate static functions into g_procs /
17811821 g_proc_protos; we splice those in ahead of these bodies, since a proc