Commit 3b5109d7

3b5109d7c695Merged

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

Description

Emit proc poly-return via _sp_proc_poly_ret global slot

Proc/lambda bodies returning TY_POLY (case-expression union of nil and a value type, etc.) can't fit in the mrb_int ABI slot. Add a mechanism:

  • A file-static sp_RbVal _sp_proc_poly_ret is emitted when any proc in the file has a poly return type.
  • The proc body assigns the boxed result to _sp_proc_poly_ret via the g_result_var/g_result_poly path (tail-return stores into the slot).
  • The call site reads it back: ((void)sp_proc_call(...), _sp_proc_poly_ret).

Also extend ret_ptr (heap-pointer returns) to cover all proc_slot_is_ptr types (arrays, hashes, objects) in addition to strings, and fix a prelude-buffer bug in the ret_ptr path where the last expression could trash g_pre (array literals need g_pre for temporaries).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
3b5109d7c695
Parent
895ac581a530

src/codegen.cmodifiedView file

@@ -116,6 +116,7 @@
116116 static Buf g_procs;
117117 static Buf g_proc_protos;
118118 static int g_proc_counter = 0;
119+static int g_needs_proc_poly_retslot = 0; /* any proc returns TY_POLY via _sp_proc_poly_ret */
119120
120121 /* Static regex-literal table: each distinct (source, flags) pair compiles once
121122 to an sp_re_pat_<i> global initialized in sp_re_init(). */
@@ -3805,9 +3806,10 @@
38053806 (!strcmp(name, "call") || !strcmp(name, "()") || !strcmp(name, "[]"))) {
38063807 TyKind rty = comp_ntype(c, id); /* the call's result = proc's body return */
38073808 int unbox_ptr = proc_slot_is_ptr(rty);
3808- int unbox_poly = (rty == TY_POLY); /* sp_proc_call → mrb_int; box into sp_RbVal */
3809+ int unbox_poly = (rty == TY_POLY);
38093810 if (unbox_ptr) { buf_puts(b, "("); emit_ctype(c, rty, b); buf_puts(b, ")(uintptr_t)("); }
3810- if (unbox_poly) buf_puts(b, "sp_box_int(");
3811+ /* poly return: proc stores result in _sp_proc_poly_ret; read it back after the call */
3812+ if (unbox_poly) buf_puts(b, "((void)");
38113813 buf_puts(b, "sp_proc_call(");
38123814 emit_expr(c, recv, b);
38133815 buf_puts(b, ", (mrb_int[16]){");
@@ -3819,7 +3821,7 @@
38193821 }
38203822 buf_puts(b, "})");
38213823 if (unbox_ptr) buf_puts(b, ")");
3822- if (unbox_poly) buf_puts(b, ")");
3824+ if (unbox_poly) buf_puts(b, ", _sp_proc_poly_ret)");
38233825 return;
38243826 }
38253827
@@ -15223,15 +15225,17 @@
1522315225 TyKind ret = TY_NIL;
1522415226 { int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
1522515227 if (bn > 0) ret = comp_ntype(c, bb[bn - 1]); }
15226- /* The proc fn returns mrb_int (the ABI); a string return is laundered through
15227- (mrb_int)(uintptr_t). Array/hash/object returns construct via the statement
15228- prelude (not available in this manual return path) and defer for now, as do
15229- float/poly/range/time, which don't fit the slot. */
15230- int ret_ptr = (ret == TY_STRING);
15228+ /* The proc fn returns mrb_int (the ABI); heap-pointer values (strings,
15229+ arrays, hashes, objects) are laundered through (mrb_int)(uintptr_t).
15230+ TY_POLY values are stored in _sp_proc_poly_ret (file-static sp_RbVal)
15231+ before return; the call site reads it back.
15232+ Float/range/time don't fit the slot and defer. */
15233+ int ret_ptr = proc_slot_is_ptr(ret);
1523115234 int ret_void = (ret == TY_VOID); /* body's last expr has no value (e.g. puts) */
15232- if (!proc_slot_is_direct(ret) && !ret_ptr && !ret_void) {
15235+ int ret_poly = (ret == TY_POLY);
15236+ if (!proc_slot_is_direct(ret) && !ret_ptr && !ret_void && !ret_poly) {
1523315237 free(params.v); free(used.v); free(locals.v); free(caps.v);
15234- unsupported(c, create, "proc with array/hash/object/float/poly return (later slice)");
15238+ unsupported(c, create, "proc with float/range/time return (later slice)");
1523515239 return;
1523615240 }
1523715241
@@ -15272,11 +15276,11 @@
1527215276 /* Save every emission global: the proc body is a fresh function context. */
1527315277 Buf *sv_pre = g_pre; int sv_indent = g_indent, sv_nren = g_nren, sv_block = g_block_id;
1527415278 const char *sv_bpn = g_block_param_name, *sv_self = g_self, *sv_rv = g_result_var;
15275- TyKind sv_rt = g_ret_type;
15279+ TyKind sv_rt = g_ret_type; int sv_rp = g_result_poly;
1527615280 const char *sv_cap_struct = g_cap_struct; NameSet *sv_cap_names = g_cap_names;
1527715281 int sv_ensure_depth = g_ensure_depth;
1527815282 g_pre = NULL; g_indent = 0; g_nren = 0; g_block_id = -1; g_block_param_name = NULL;
15279- g_self = "self"; g_result_var = NULL; g_ret_type = ret; g_ensure_depth = 0;
15283+ g_self = "self"; g_result_var = NULL; g_ret_type = ret; g_ensure_depth = 0; g_result_poly = 0;
1528015284 char cap_struct_name[32] = "";
1528115285 if (ncap > 0) { snprintf(cap_struct_name, sizeof cap_struct_name, "_proc_cap_%d", pid); g_cap_struct = cap_struct_name; g_cap_names = &caps; }
1528215286 else { g_cap_struct = NULL; g_cap_names = NULL; }
@@ -15303,13 +15307,37 @@
1530315307 }
1530415308 if (ret_ptr) {
1530515309 /* launder a heap-pointer return through the mrb_int slot: emit the body's
15306- leading statements, then `return (mrb_int)(uintptr_t)(<value>)`. */
15310+ leading statements, then a prelude-wrapped `return (mrb_int)(uintptr_t)(<value>)`.
15311+ The last expression may itself need a prelude (e.g. array allocation), so wrap
15312+ emit_expr in a temporary prelude buffer that drains before the return line. */
1530715313 int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
1530815314 for (int k = 0; k < bn - 1; k++) emit_stmt(c, bb[k], pb, 1);
15309- buf_puts(pb, " return (mrb_int)(uintptr_t)(");
15310- if (bn > 0) emit_expr(c, bb[bn - 1], pb); else buf_puts(pb, "0");
15311- buf_puts(pb, ");\n");
15315+ if (bn > 0) {
15316+ Buf rpre = {0}, rval = {0};
15317+ Buf *sv_rpre = g_pre; int sv_rind = g_indent;
15318+ g_pre = &rpre; g_indent = 1;
15319+ emit_expr(c, bb[bn - 1], &rval);
15320+ g_pre = sv_rpre; g_indent = sv_rind;
15321+ if (rpre.p) buf_puts(pb, rpre.p);
15322+ buf_puts(pb, " return (mrb_int)(uintptr_t)(");
15323+ if (rval.p) buf_puts(pb, rval.p);
15324+ buf_puts(pb, ");\n");
15325+ free(rpre.p); free(rval.p);
15326+ }
15327+ else buf_puts(pb, " return 0;\n");
1531215328 }
15329+ else if (ret_poly) {
15330+ /* Store the sp_RbVal result in the file-static _sp_proc_poly_ret slot;
15331+ the call site reads it back after sp_proc_call returns. */
15332+ if (!g_needs_proc_poly_retslot) {
15333+ g_needs_proc_poly_retslot = 1;
15334+ buf_puts(&g_proc_protos, "static sp_RbVal _sp_proc_poly_ret;\n");
15335+ }
15336+ g_result_var = "_sp_proc_poly_ret"; g_result_poly = 1;
15337+ emit_stmts_tail(c, body, pb, 1);
15338+ g_result_var = NULL; g_result_poly = 0;
15339+ buf_puts(pb, " return 0;\n");
15340+ }
1531315341 else if (ret_void) {
1531415342 /* no usable value: run the body as plain statements, return nil (0) */
1531515343 emit_stmts(c, body, pb, 1);
@@ -15324,6 +15352,7 @@
1532415352 g_pre = sv_pre; g_indent = sv_indent; g_nren = sv_nren; g_block_id = sv_block;
1532515353 g_block_param_name = sv_bpn; g_self = sv_self; g_result_var = sv_rv; g_ret_type = sv_rt;
1532615354 g_cap_struct = sv_cap_struct; g_cap_names = sv_cap_names; g_ensure_depth = sv_ensure_depth;
15355+ g_result_poly = sv_rp;
1532715356
1532815357 if (ncap == 0) {
1532915358 buf_printf(b, "sp_proc_new_meta((void *)_proc_%d, NULL, NULL, %d, %s, %d, %s)",
@@ -15993,6 +16022,7 @@
1599316022 free(g_procs.p); free(g_proc_protos.p);
1599416023 memset(&g_procs, 0, sizeof g_procs);
1599516024 memset(&g_proc_protos, 0, sizeof g_proc_protos);
16025+ g_needs_proc_poly_retslot = 0;
1599616026
1599716027 comp_free(c);
1599816028 return b.p;