Commit 2b83f7c1

2b83f7c183e4Merged

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

Description

codegen: call a statically-known object-bound method through its real ABI

recv.method(:m).call(args) cast the fn pointer to the legacy mrb_int()(void, mrb_int...) ABI. When m's int params/return widened to poly under promote, that truncated the boxed args and return to garbage (the call returned 0). When the target method is statically resolved, cast through its actual return and parameter C types and coerce each arg via emit_arg_or_default. Falls back to the raw ABI for an unresolved target; identical code in default mode (no widening). Fixes bound_method_single_eval.

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

Details

Commit
2b83f7c183e4
Parent
4c1c7c91f155

src/codegen_call.cmodifiedView file

@@ -4051,15 +4051,31 @@
40514051 buf_puts(b, ")");
40524052 return;
40534053 }
4054- /* object-bound: ((mrb_int(*)(void*, mrb_int...))rc->fn)((void*)rc->self, args) */
4054+ /* object-bound: cast fn through its real signature and call it once.
4055+ When the target method is statically known (`recv.method(:m)`), use its
4056+ actual return and parameter C types so a promote-widened poly method --
4057+ \`sp_RbVal (*)(void*, sp_RbVal)\` -- is not invoked through the legacy
4058+ mrb_int ABI (which truncates the boxed args and return to garbage).
4059+ Falls back to the raw mrb_int ABI when the target is unresolved. */
40554060 int tr = ++g_tmp;
4061+ Scope *tm = target >= 0 ? &c->scopes[target] : NULL;
4062+ TyKind tret = tm ? (TyKind)tm->ret : TY_INT;
4063+ if (!is_scalar_ret(tret)) tret = TY_INT; /* aggregate ret: raw carrier */
40564064 buf_printf(b, "({ sp_BoundMethod *_t%d = ", tr); emit_expr(c, recv, b); buf_puts(b, "; ");
4057- buf_printf(b, "((mrb_int (*)(void *");
4058- for (int k = 0; k < argc; k++) buf_puts(b, ", mrb_int");
4065+ buf_puts(b, "(("); emit_ctype(c, tret, b); buf_puts(b, " (*)(void *");
4066+ for (int k = 0; k < argc; k++) {
4067+ buf_puts(b, ", ");
4068+ if (tm && k < tm->nparams) {
4069+ LocalVar *pp = scope_local(tm, tm->pnames[k]);
4070+ emit_ctype(c, pp ? pp->type : TY_INT, b);
4071+ }
4072+ else buf_puts(b, "mrb_int");
4073+ }
40594074 buf_printf(b, "))(uintptr_t)_t%d->fn)((void *)_t%d->self", tr, tr);
40604075 for (int k = 0; k < argc; k++) {
40614076 buf_puts(b, ", ");
4062- 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, ")"); }
4077+ if (tm && k < tm->nparams) emit_arg_or_default(c, tm, k, argv[k], b);
4078+ else 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, ")"); }
40634079 else emit_expr(c, argv[k], b);
40644080 }
40654081 buf_printf(b, "); })");