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.chrwent 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
buf_puts(b, "static void sp_re_init(void) {\n"); buf_puts(b, " sp_sym_name_fn = sp_sym_to_s;\n"); buf_puts(b, " sp_user_exc_parent_fn = sp_user_exc_parent;\n"); /* Replace the runtime's hook with the superset that also marks this program's heap-typed globals/constants/class-ivars (it chains to sp_re_mark_globals itself). */ buf_puts(b, " sp_gc_mark_globals_hook = sp_mark_user_globals;\n"); if (g_re_count > 0) { buf_puts(b, " sp_re_set_error_handler(sp_re_startup_error_handler);\n"); for (int i = 0; i < g_re_count; i++) { } if (c->ngvars || c->nconsts) buf_puts(&b, "\n"); /* GC marking for the file-scope statics above: heap objects reachable only through a global/constant/class-ivar slot would otherwise be swept (RAND = Rand.new lost its PRNG mid-render). Chained ahead of the runtime's own sp_re_mark_globals via the hook in sp_re_init. */ { buf_puts(&b, "static void sp_mark_user_globals(void) {\n"); buf_puts(&b, " sp_re_mark_globals();\n"); for (int i = 0; i < c->ngvars; i++) { LocalVar *lv = &c->gvars[i]; if (!is_scalar_ret(lv->type)) continue; if (lv->type == TY_STRING) buf_printf(&b, " sp_mark_string(gv_%s);\n", lv->name); else if (lv->type == TY_POLY) buf_printf(&b, " sp_mark_rbval(gv_%s);\n", lv->name); else if (needs_root(lv->type)) buf_printf(&b, " if (gv_%s) sp_gc_mark((void *)gv_%s);\n", lv->name, lv->name); } for (int i = 0; i < c->nconsts; i++) { LocalVar *lv = &c->consts[i]; if (!is_scalar_ret(lv->type)) continue; if (lv->type == TY_STRING) buf_printf(&b, " sp_mark_string(cst_%s);\n", lv->name); else if (lv->type == TY_POLY) buf_printf(&b, " sp_mark_rbval(cst_%s);\n", lv->name); else if (needs_root(lv->type)) buf_printf(&b, " if (cst_%s) sp_gc_mark((void *)cst_%s);\n", lv->name, lv->name); } for (int i = 0; i < c->nclasses; i++) { ClassInfo *ci = &c->classes[i]; for (int j = 0; j < ci->nivars; j++) { TyKind t = ci->ivar_types[j] == TY_UNKNOWN ? TY_INT : ci->ivar_types[j]; const char *iv = ci->ivars[j] + 1; if (t == TY_STRING) buf_printf(&b, " sp_mark_string(civ_%s_%s);\n", ci->name, iv); else if (t == TY_POLY) buf_printf(&b, " sp_mark_rbval(civ_%s_%s);\n", ci->name, iv); 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); } for (int j = 0; j < ci->nsg_readers; j++) buf_printf(&b, " sp_mark_rbval(sg_%s_%s);\n", ci->name, ci->sg_readers[j]); } buf_puts(&b, "}\n\n"); } /* Constructor defs, method defs, and main go into a separate buffer. Any proc literals they contain accumulate static functions into g_procs / g_proc_protos; we splice those in ahead of these bodies, since a procsrc/codegen_stmt.cmodifiedView file
Loading diff…