Commit bf15cf9a

bf15cf9a4ed9Merged

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

Description

fix(codegen): poly dispatch + return tail bigint detection

Two related promote-mode tweaks for poly-recv method dispatch:

  • emit_poly_builtin_dispatch's SP_BUILTIN_PROC call arm packed call args into (mrb_int[]){...} raw. Promote-widened bigint args need sp_bigint_to_int unbox at the array init.
  • The same arm assigns result_tmp = sp_proc_call(...) directly, but result_tmp's slot may be bigint when the dispatcher's static return type is bigint (all user-class arms return bigint). Add a result_t arg to emit_poly_builtin_dispatch and wrap with sp_bigint_new_int when bigint.
  • compile_body_return's tail-arith peek extends to a poly.method call when every user class that defines method returns bigint — the dispatcher's actual emit lands a bigint into the result temp.

Promote-mode test count: 548 pass / 49 fail / 21 error -> 549 pass / 49 fail / 20 error. Default raise mode 618 / 0 / 0.

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

Details

Commit
bf15cf9a4ed9
Parent
1e3e8f15f6f1

spinel_codegen.rbmodifiedView file

@@ -21725,7 +21725,7 @@
2172521725 i = i + 1
2172621726 end
2172721727 # Built-in type dispatch (cls_id < 0).
21728- emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, arg_types, tmp, is_poly_ret, narrowed_int_idx)
21728+ emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, arg_types, tmp, is_poly_ret, narrowed_int_idx, ret_type)
2172921729 emit(" }")
2173021730 # Warn when the user-class dispatch loop emitted zero arms. The
2173121731 # result temp keeps its default (0) and downstream code consuming
@@ -21745,7 +21745,7 @@
2174521745 # Emit branches for the built-in (negative cls_id) entries. Each
2174621746 # entry maps a (SP_BUILTIN_*, method) pair to a C expression.
2174721747 # Adding a new built-in type means one more `if` branch here.
21748- def emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, arg_types, result_tmp, is_poly_ret, narrowed_int = 0)
21748+ def emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, arg_types, result_tmp, is_poly_ret, narrowed_int = 0, result_t = "")
2174921749 a0 = ""
2175021750 if arg_compiled.length > 0
2175121751 a0 = arg_compiled[0]
@@ -22013,14 +22013,28 @@
2201322013 if ck > 0
2201422014 ca = ca + ", "
2201522015 end
22016- ca = ca + arg_compiled[ck]
22016+ # proc ABI args are mrb_int — unbox bigint args so the array
22017+ # init holds plain ints.
22018+ av_pc = arg_compiled[ck]
22019+ if ck < arg_types.length && arg_types[ck] == "bigint"
22020+ @needs_bigint = 1
22021+ av_pc = "sp_bigint_to_int((sp_Bigint *)" + av_pc + ")"
22022+ end
22023+ ca = ca + av_pc
2201722024 ck = ck + 1
2201822025 end
2201922026 if ca == ""
2202022027 ca = "0"
2202122028 end
2202222029 proc_c = "sp_proc_call((sp_Proc *)" + recv_tmp + ".v.p, (mrb_int[]){" + ca + "})"
22023- proc_rhs = is_poly_ret == 1 ? "sp_box_int(" + proc_c + ")" : proc_c
22030+ if is_poly_ret == 1
22031+ proc_rhs = "sp_box_int(" + proc_c + ")"
22032+ elsif base_type(result_t) == "bigint"
22033+ @needs_bigint = 1
22034+ proc_rhs = "sp_bigint_new_int(" + proc_c + ")"
22035+ else
22036+ proc_rhs = proc_c
22037+ end
2202422038 emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_PROC) " + result_tmp + " = " + proc_rhs + ";")
2202522039 end
2202622040 if mname == "[]" && arg_compiled.length >= 1 && a0_is_int
@@ -36673,6 +36687,35 @@
3667336687 end
3667436688 end
3667536689 end
36690+ # Generic poly.method: when every user class defining `mname`
36691+ # returns bigint, the dispatcher result is bigint. Walk the
36692+ # whole class set instead of relying on the narrow-set ivar
36693+ # heuristic (which requires the recv to be an ivar read).
36694+ if expr_type != "bigint"
36695+ ml_recv2 = @nd_receiver[last]
36696+ if ml_recv2 >= 0 && infer_type(ml_recv2) == "poly"
36697+ all_bigint_pm = 0
36698+ any_defines_pm = 0
36699+ cic_pm = 0
36700+ while cic_pm < @cls_names.length
36701+ if cls_find_method(cic_pm, ml_ret) >= 0
36702+ if any_defines_pm == 0
36703+ all_bigint_pm = 1
36704+ end
36705+ any_defines_pm = 1
36706+ rt_pm = cls_method_return(cic_pm, ml_ret)
36707+ if base_type(rt_pm) != "bigint"
36708+ all_bigint_pm = 0
36709+ cic_pm = @cls_names.length
36710+ end
36711+ end
36712+ cic_pm = cic_pm + 1
36713+ end
36714+ if any_defines_pm == 1 && all_bigint_pm == 1
36715+ expr_type = "bigint"
36716+ end
36717+ end
36718+ end
3667636719 end
3667736720 if expr_type == "lambda"
3667836721 if return_type == "int"