Commit 83914908

83914908c1ffMerged

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

Description

codegen: unbox poly sources into scalar block params at yield and instance_exec binds

With block params no longer widened to poly, a poly source bound to a scalar block param is the reverse coercion of the boxing arms added earlier: a poly yield value into an int block param (emit_block_invoke), and a poly instance_exec arg -- a widened ivar or trampoline temp -- into an int block param. Unbox with emit_unbox_text down to the slot type when the source is poly and the slot is a concrete scalar. Unblocks the yield / block-forward family (yield, forward_args_block, block_forward_recv_typed/self_call, inlined_yield_self_locals, instance_exec_mixed_args).

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

Details

Commit
83914908c1ff
Parent
9c94f5400377

src/codegen_call.cmodifiedView file

@@ -5314,6 +5314,9 @@
53145314 if (!pn) continue;
53155315 LocalVar *plv = scope_local(comp_scope_of(c, id), pn);
53165316 int ppoly = plv && plv->type == TY_POLY; /* widened slot needs a boxed rvalue */
5317+ /* a scalar slot (e.g. an int block param, which is NOT widened) fed a
5318+ poly arg needs the reverse: unbox the poly down to the slot type. */
5319+ int pscalar = plv && plv->type != TY_POLY && plv->type != TY_UNKNOWN;
53175320 emit_indent(g_pre, g_indent);
53185321 buf_printf(g_pre, "lv_%s = ", rename_local(pn));
53195322 if (as_kind) {
@@ -5332,10 +5335,20 @@
53325335 Buf eb; memset(&eb, 0, sizeof eb);
53335336 if (an >= 0) { if (ppoly) emit_boxed(c, an, &eb); else emit_expr(c, an, &eb); }
53345337 else buf_puts(&eb, "0");
5335- buf_puts(g_pre, eb.p ? eb.p : "0"); free(eb.p);
5338+ if (an >= 0 && pscalar && comp_ntype(c, an) == TY_POLY)
5339+ emit_unbox_text(c, plv->type, eb.p ? eb.p : "", g_pre);
5340+ else buf_puts(g_pre, eb.p ? eb.p : "0");
5341+ free(eb.p);
53365342 }
53375343 else if (is_exec) {
5338- if (p < iac) { if (ppoly) emit_boxed(c, iav[p], g_pre); else emit_expr(c, iav[p], g_pre); }
5344+ if (p < iac) {
5345+ if (ppoly) emit_boxed(c, iav[p], g_pre);
5346+ else if (pscalar && comp_ntype(c, iav[p]) == TY_POLY) {
5347+ Buf eb; memset(&eb, 0, sizeof eb); emit_expr(c, iav[p], &eb);
5348+ emit_unbox_text(c, plv->type, eb.p ? eb.p : "", g_pre); free(eb.p);
5349+ }
5350+ else emit_expr(c, iav[p], g_pre);
5351+ }
53395352 else emit_ie_param_default(c, plv ? plv->type : TY_POLY, g_pre);
53405353 }
53415354 else buf_printf(g_pre, "_t%d", tr); /* instance_eval yields self */
@@ -9961,6 +9974,12 @@
99619974 TyKind at = comp_ntype(c, yargs[k]);
99629975 if (bt == TY_POLY && at != TY_POLY && at != TY_UNKNOWN)
99639976 emit_boxed(c, yargs[k], b);
9977+ else if (at == TY_POLY && bt != TY_POLY && bt != TY_UNKNOWN) {
9978+ /* a poly yield value into a scalar (e.g. int, non-widened) block param:
9979+ unbox down to the slot type (the reverse of the box arm above). */
9980+ Buf yb; memset(&yb, 0, sizeof yb); emit_expr(c, yargs[k], &yb);
9981+ emit_unbox_text(c, bt, yb.p ? yb.p : "", b); free(yb.p);
9982+ }
99649983 else
99659984 emit_expr(c, yargs[k], b);
99669985 }