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_PROCcallarm 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.methodcall when every user class that definesmethodreturns 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 + 12172621726
end2172721727
# 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. The2173121731
# 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. Each2174621746
# 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 > 02175121751
a0 = arg_compiled[0]@@ -22013,14 +22013,28 @@
2201322013
if ck > 02201422014
ca = ca + ", "2201522015
end22016-
ca = ca + arg_compiled[ck]22016+
# proc ABI args are mrb_int — unbox bigint args so the array22017+
# init holds plain ints.22018+
av_pc = arg_compiled[ck]22019+
if ck < arg_types.length && arg_types[ck] == "bigint"22020+
@needs_bigint = 122021+
av_pc = "sp_bigint_to_int((sp_Bigint *)" + av_pc + ")"22022+
end22023+
ca = ca + av_pc2201722024
ck = ck + 12201822025
end2201922026
if ca == ""2202022027
ca = "0"2202122028
end2202222029
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_c22030+
if is_poly_ret == 122031+
proc_rhs = "sp_box_int(" + proc_c + ")"22032+
elsif base_type(result_t) == "bigint"22033+
@needs_bigint = 122034+
proc_rhs = "sp_bigint_new_int(" + proc_c + ")"22035+
else22036+
proc_rhs = proc_c22037+
end2202422038
emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_PROC) " + result_tmp + " = " + proc_rhs + ";")2202522039
end2202622040
if mname == "[]" && arg_compiled.length >= 1 && a0_is_int@@ -36673,6 +36687,35 @@
3667336687
end3667436688
end3667536689
end36690+
# Generic poly.method: when every user class defining `mname`36691+
# returns bigint, the dispatcher result is bigint. Walk the36692+
# whole class set instead of relying on the narrow-set ivar36693+
# 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 = 036698+
any_defines_pm = 036699+
cic_pm = 036700+
while cic_pm < @cls_names.length36701+
if cls_find_method(cic_pm, ml_ret) >= 036702+
if any_defines_pm == 036703+
all_bigint_pm = 136704+
end36705+
any_defines_pm = 136706+
rt_pm = cls_method_return(cic_pm, ml_ret)36707+
if base_type(rt_pm) != "bigint"36708+
all_bigint_pm = 036709+
cic_pm = @cls_names.length36710+
end36711+
end36712+
cic_pm = cic_pm + 136713+
end36714+
if any_defines_pm == 1 && all_bigint_pm == 136715+
expr_type = "bigint"36716+
end36717+
end36718+
end3667636719
end3667736720
if expr_type == "lambda"3667836721
if return_type == "int"