Commit 02f605f1

02f605f199f0Merged

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

Description

Merge pull request #1492 from ryanseys/rs-g18-float-capture

Support float-typed closure captures

Details

Commit
02f605f199f0
Parent
b758ea15a55f
Merge parent
6dd154f7a585

src/codegen.cmodifiedView file

@@ -287,11 +287,23 @@
287287 else buf_printf(b, " *_cell_%s = 0;\n", lv->name);
288288 continue;
289289 }
290+ /* A float capture gets a native mrb_float cell rather than laundering the
291+ bits through the int slot: *_cell_x is then a real mrb_float lvalue, so
292+ the ordinary read / write / compound-assign paths work unchanged. The
293+ cell holds no GC pointer, so no cell scan is needed. */
294+ if (lv->type == TY_FLOAT) {
295+ buf_printf(b, " mrb_float *_cell_%s = (mrb_float *)sp_gc_alloc(sizeof(mrb_float), NULL, NULL);\n", lv->name);
296+ buf_printf(b, " SP_GC_ROOT(_cell_%s);\n", lv->name);
297+ if (lv->is_param) buf_printf(b, " *_cell_%s = lv_%s;\n", lv->name, lv->name);
298+ else buf_printf(b, " *_cell_%s = 0.0;\n", lv->name);
299+ continue;
300+ }
290301 /* A pointer (string / array / hash / heap object) capture is laundered
291302 through the int cell as (uintptr_t)<ptr>, with a cell scan that marks
292303 the referent. Int / bool stay direct. Float / poly / by-value structs
293304 don't fit a single int cell -- still deferred. */
294305 int ptr_cell = proc_slot_is_ptr(lv->type) && !comp_ty_value_obj(c, lv->type);
306+ /* TY_FLOAT is handled by the native mrb_float-cell branch above. */
295307 if (lv->type != TY_INT && lv->type != TY_BOOL && lv->type != TY_UNKNOWN && !ptr_cell)
296308 unsupported(c, s->def_node, "closure capturing a non-integer variable (later slice)");
297309 const char *cell_scan = !ptr_cell ? "NULL"
@@ -981,14 +993,24 @@
981993 for (int k = 0; k < arity; k++) nameset_add(&params, proc_param_name(c, create, k));
982994 proc_collect_used(c, body, &used);
983995 proc_collect_locals(c, body, &locals);
996+ /* A float proc parameter still rides the truncating mrb_int arg slot, so a
997+ proc that takes one can't safely also enable float captures: keep the clean
998+ reject rather than emit a silently-wrong double. (Float args through the
999+ proc slot are a separate, pre-existing gap.) */
1000+ int has_float_param = 0;
1001+ for (int k = 0; k < arity; k++) {
1002+ LocalVar *plv = scope_local(bs, proc_param_name(c, create, k));
1003+ if (plv && plv->type == TY_FLOAT) { has_float_param = 1; break; }
1004+ }
9841005 for (int u = 0; u < used.n; u++) {
9851006 const char *nm = used.v[u];
9861007 if (nameset_has(&params, nm)) continue;
9871008 LocalVar *lv = scope_local(bs, nm);
9881009 if (lv && lv->is_cell) {
9891010 int ptr_cell = proc_slot_is_ptr(lv->type) && !comp_ty_value_obj(c, lv->type);
1011+ int float_cell = lv->type == TY_FLOAT && !has_float_param;
9901012 if (lv->type != TY_INT && lv->type != TY_BOOL && lv->type != TY_UNKNOWN &&
991- lv->type != TY_PROC && !ptr_cell) {
1013+ lv->type != TY_PROC && !float_cell && !ptr_cell) {
9921014 free(params.v); free(used.v); free(locals.v); free(caps.v);
9931015 unsupported(c, create, "proc capturing a non-integer variable (later slice)");
9941016 return;
@@ -1105,7 +1127,12 @@
11051127 the cap struct unreachable and free it out from under the proc. */
11061128 if (ncap > 0 || cap_self) {
11071129 buf_printf(&g_procs, "typedef struct {");
1108- for (int i = 0; i < ncap; i++) buf_printf(&g_procs, " mrb_int *%s;", caps.v[i]);
1130+ for (int i = 0; i < ncap; i++) {
1131+ LocalVar *clv = scope_local(bs, caps.v[i]);
1132+ /* a float capture rides a native mrb_float cell (see emit_scope_decls). */
1133+ const char *cty = (clv && clv->type == TY_FLOAT) ? "mrb_float" : "mrb_int";
1134+ buf_printf(&g_procs, " %s *%s;", cty, caps.v[i]);
1135+ }
11091136 if (cap_self) buf_puts(&g_procs, " void *__self;");
11101137 buf_printf(&g_procs, " } _proc_cap_%d;\n", pid);
11111138 buf_printf(&g_procs, "static void _proc_cap_scan_%d(void *p) {\n", pid);