Commit 22c47646

22c47646304aMerged

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

Description

codegen(promote): emit builtin-array bound-method adapters with the poly ABI

The <int_array>.method(:[]) / :[]= / :push trampolines were emitted with the legacy mrb_int ABI, but under promote a bound method is invoked through the poly ABI (sp_RbVal self/args/return). Calling the mrb_int adapter through the poly cast scrambled args and return.

Emit the adapters with sp_RbVal parameters/return under promote, unboxing the index/value with sp_poly_to_i / sp_poly_to_s and boxing the result.

Fixes a regression from "invoke bound methods through the poly ABI" (builtin_array_method_capture under promote).

Default test 992/0/0 and optcarrot checksum 59662 unchanged.

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

Details

Commit
22c47646304a
Parent
e6f13226c1e0

src/codegen_call.cmodifiedView file

@@ -3972,7 +3972,29 @@
39723972 bam_done[ki][oi] = 1;
39733973 const char *cast = (ki == 0) ? "" : "(mrb_int)(uintptr_t)";
39743974 const char *uncast = (ki == 0) ? "" : "(const char *)(uintptr_t)";
3975- if (oi == 0) {
3975+ if (g_promote_mode) {
3976+ /* promote: bound methods are invoked through the poly ABI, so the
3977+ adapter takes/returns sp_RbVal (boxing the int/string element). */
3978+ const char *boxret = (ki == 0) ? "sp_box_int_or_nil" : "sp_box_str";
3979+ const char *unbox = (ki == 0) ? "sp_poly_to_i" : "sp_poly_to_s";
3980+ const char *boxarr = (ki == 0) ? "sp_box_int_array" : "sp_box_str_array";
3981+ if (oi == 0) {
3982+ buf_printf(&g_proc_protos, "static sp_RbVal _bam_%sArray_get(void *a, sp_RbVal i);\n", bk);
3983+ buf_printf(&g_procs, "static sp_RbVal _bam_%sArray_get(void *a, sp_RbVal i) {\n"
3984+ " return %s(sp_%sArray_get((sp_%sArray *)a, sp_poly_to_i(i)));\n}\n", bk, boxret, bk, bk);
3985+ }
3986+ else if (oi == 1) {
3987+ buf_printf(&g_proc_protos, "static sp_RbVal _bam_%sArray_set(void *a, sp_RbVal i, sp_RbVal v);\n", bk);
3988+ buf_printf(&g_procs, "static sp_RbVal _bam_%sArray_set(void *a, sp_RbVal i, sp_RbVal v) {\n"
3989+ " sp_%sArray_set((sp_%sArray *)a, sp_poly_to_i(i), %s(v));\n return v;\n}\n", bk, bk, bk, unbox);
3990+ }
3991+ else {
3992+ buf_printf(&g_proc_protos, "static sp_RbVal _bam_%sArray_push(void *a, sp_RbVal v);\n", bk);
3993+ buf_printf(&g_procs, "static sp_RbVal _bam_%sArray_push(void *a, sp_RbVal v) {\n"
3994+ " sp_%sArray_push((sp_%sArray *)a, %s(v));\n return %s(a);\n}\n", bk, bk, bk, unbox, boxarr);
3995+ }
3996+ }
3997+ else if (oi == 0) {
39763998 buf_printf(&g_proc_protos, "static mrb_int _bam_%sArray_get(void *a, mrb_int i);\n", bk);
39773999 buf_printf(&g_procs, "static mrb_int _bam_%sArray_get(void *a, mrb_int i) {\n"
39784000 " return %ssp_%sArray_get((sp_%sArray *)a, i);\n}\n", bk, cast, bk, bk);