Commit 2317220e

2317220e354eMerged

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

Description

First-class Proc: string return values via mrb_int-slot laundering

A proc returning a string now round-trips: the proc fn launders the pointer out through (mrb_int)(uintptr_t) and the .call site casts it back, mirroring the legacy generator's spinel -c output. String/heap-pointer arguments and params launder the same way, so a proc can take and return pointer values over the mrb_int[] ABI.

Scoped to string returns: array/hash/object returns construct via the statement prelude, which the manual return path doesn't provide (it would null-deref g_pre -- previously a spinelc segfault, now a clean defer). Float/poly/range don't fit the slot and also defer. Param-type inference from .call arguments is a separate later slice, so string-parameter procs still defer.

make test: 166 pass / 5 fail / 673 error (was 165 / 5 / 674); lambda_arrow_string_return green, no regressions.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
2317220e354e
Parent
d6f319863881

src/codegen.cmodifiedView file

@@ -254,6 +254,8 @@
254254 static void emit_boxed(Compiler *c, int node, Buf *b);
255255 static void emit_boxed_text(Compiler *c, TyKind t, const char *expr, Buf *b);
256256 static void emit_proc_literal(Compiler *c, int create, Buf *b);
257+static int proc_slot_is_direct(TyKind t);
258+static int proc_slot_is_ptr(TyKind t);
257259
258260 /* Strip ParenthesesNode wrappers to reach the inner expression. */
259261 static int unwrap_parens(Compiler *c, int id) {
@@ -560,11 +562,20 @@
560562 receiver name matches g_block_param_name; this is the escaped-value case.) */
561563 if (recv >= 0 && comp_ntype(c, recv) == TY_PROC &&
562564 (!strcmp(name, "call") || !strcmp(name, "()") || !strcmp(name, "[]"))) {
565+ TyKind rty = comp_ntype(c, id); /* the call's result = proc's body return */
566+ int unbox_ptr = proc_slot_is_ptr(rty);
567+ if (unbox_ptr) { buf_puts(b, "("); emit_ctype(c, rty, b); buf_puts(b, ")(uintptr_t)("); }
563568 buf_puts(b, "sp_proc_call(");
564569 emit_expr(c, recv, b);
565570 buf_puts(b, ", (mrb_int[16]){");
566- for (int k = 0; k < argc; k++) { if (k) buf_puts(b, ", "); emit_expr(c, argv[k], b); }
571+ for (int k = 0; k < argc; k++) {
572+ if (k) buf_puts(b, ", ");
573+ /* a heap-pointer argument is laundered into the mrb_int slot */
574+ if (proc_slot_is_ptr(comp_ntype(c, argv[k]))) { buf_puts(b, "(mrb_int)(uintptr_t)("); emit_expr(c, argv[k], b); buf_puts(b, ")"); }
575+ else emit_expr(c, argv[k], b);
576+ }
567577 buf_puts(b, "})");
578+ if (unbox_ptr) buf_puts(b, ")");
568579 return;
569580 }
570581
@@ -3832,6 +3843,13 @@
38323843 return block >= 0 ? nt_ref(c->nt, block, "body") : -1;
38333844 }
38343845
3846+/* Proc args + return ride the mrb_int slot of sp_proc_call. A value that fits
3847+ an mrb_int directly (int/bool/symbol/nil) needs no conversion; a heap pointer
3848+ (string/array/hash/object) is laundered through (mrb_int)(uintptr_t). Other
3849+ shapes (float, poly, range, time) don't fit the slot and defer. */
3850+static int proc_slot_is_direct(TyKind t) { return t == TY_INT || t == TY_BOOL || t == TY_SYMBOL || t == TY_NIL || t == TY_UNKNOWN; }
3851+static int proc_slot_is_ptr(TyKind t) { return t == TY_STRING || ty_is_array(t) || ty_is_hash(t) || ty_is_object(t); }
3852+
38353853 /* Lower a `proc {}` / `lambda {}` / `Proc.new {}` / `->(){}` literal: emit a
38363854 standalone `static mrb_int _proc_N(void *cap, mrb_int *args)` (sp_proc_call's
38373855 ABI) into g_procs, and emit the boxing `sp_proc_new_meta(...)` value into `b`. */
@@ -3884,11 +3902,14 @@
38843902 TyKind ret = TY_NIL;
38853903 { int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
38863904 if (bn > 0) ret = comp_ntype(c, bb[bn - 1]); }
3887- /* The proc fn returns mrb_int (the ABI). Until pointer/poly return
3888- laundering lands (later slice), only mrb_int-shaped returns are safe. */
3889- if (!(ret == TY_INT || ret == TY_BOOL || ret == TY_NIL)) {
3905+ /* The proc fn returns mrb_int (the ABI); a string return is laundered through
3906+ (mrb_int)(uintptr_t). Array/hash/object returns construct via the statement
3907+ prelude (not available in this manual return path) and defer for now, as do
3908+ float/poly/range/time, which don't fit the slot. */
3909+ int ret_ptr = (ret == TY_STRING);
3910+ if (!proc_slot_is_direct(ret) && !ret_ptr) {
38903911 free(params.v); free(used.v); free(locals.v); free(caps.v);
3891- unsupported(c, create, "proc with non-integer return (later slice)");
3912+ unsupported(c, create, "proc with array/hash/object/float/poly return (later slice)");
38923913 return;
38933914 }
38943915
@@ -3932,17 +3953,29 @@
39323953 const char *p = proc_param_name(c, create, k);
39333954 LocalVar *lv = scope_local(bs, p);
39343955 TyKind pt = lv ? lv->type : TY_INT;
3935- buf_puts(pb, " "); emit_ctype(c, pt, pb);
3936- /* params are int-shaped here; args[k] is already mrb_int */
3937- buf_printf(pb, " lv_%s = args[%d];\n", p, k);
3956+ buf_puts(pb, " "); emit_ctype(c, pt, pb); buf_printf(pb, " lv_%s = ", p);
3957+ /* a heap-pointer param is laundered back from the mrb_int slot */
3958+ if (proc_slot_is_ptr(pt)) { buf_puts(pb, "("); emit_ctype(c, pt, pb); buf_printf(pb, ")(uintptr_t)args[%d];\n", k); }
3959+ else buf_printf(pb, "args[%d];\n", k);
39383960 }
39393961 for (int i = 0; i < locals.n; i++) {
39403962 LocalVar *lv = scope_local(bs, locals.v[i]);
39413963 /* a celled local is a captured var (accessed via _cap), not a fn-local */
39423964 if (lv && !lv->is_block_param && !lv->is_cell) declare_local(c, pb, lv, 0);
39433965 }
3944- emit_stmts_tail(c, body, pb, 1);
3945- buf_puts(pb, " return 0;\n");
3966+ if (ret_ptr) {
3967+ /* launder a heap-pointer return through the mrb_int slot: emit the body's
3968+ leading statements, then `return (mrb_int)(uintptr_t)(<value>)`. */
3969+ int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
3970+ for (int k = 0; k < bn - 1; k++) emit_stmt(c, bb[k], pb, 1);
3971+ buf_puts(pb, " return (mrb_int)(uintptr_t)(");
3972+ if (bn > 0) emit_expr(c, bb[bn - 1], pb); else buf_puts(pb, "0");
3973+ buf_puts(pb, ");\n");
3974+ }
3975+ else {
3976+ emit_stmts_tail(c, body, pb, 1);
3977+ buf_puts(pb, " return 0;\n");
3978+ }
39463979 buf_puts(pb, "}\n");
39473980
39483981 g_pre = sv_pre; g_indent = sv_indent; g_nren = sv_nren; g_block_id = sv_block;