Commit 6677f043

6677f043517cMerged

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

Description

fix(codegen): Method fn_ptr ABI switches to sp_Bigint* under promote mode

bm.call(x) / bm[x] on a Method instance casts the iv_fn_ptr to a uniform C signature. Under SPINEL_INT_OVERFLOW=promote that signature widens from mrb_int(*)(void*, mrb_int...) to sp_Bigint*(*)(void*, sp_Bigint*...) since every int slot widened.

Both Method call paths -- the obj_Method-typed receiver path (compile_method_or_aref_call) and the poly_array Method-dispatch arm (emit_poly_builtin_dispatch) -- now emit:

  • sp_Bigint* return type cast (instead of mrb_int)
  • sp_Bigint* arg type cast
  • sp_bigint_new_int() wrap of int / poly.v.i args at the boundary
  • sp_bigint_to_int() unwrap of the call result when caller wants int
  • sp_bigint_new_int(0) null-fn_ptr default (the typed dispatch needed a non-zero default; mrb_int 0 won't cast cleanly to a pointer type)

Also extend expr_emit_is_bigint to detect this Method call shape so compile_expr_for_expected_type doesn't double-wrap when the result feeds a bigint slot.

Picks up: bound_method_*, method, proc on a Method recv.

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

Details

Commit
6677f043517c
Parent
6f2af8ea3e38

spinel_codegen.rbmodifiedView file

@@ -13899,25 +13899,37 @@
1389913899 emit(" sp_Method *" + bm_tmp + " = " + rc + ";")
1390013900 args_id = @nd_arguments[nid]
1390113901 joined = ""
13902+ # In promote mode all int slots widened to bigint, so the
13903+ # uniform Method ABI's int args / int return become sp_Bigint*.
13904+ # The fn_ptr is cast accordingly; downstream consumers expecting
13905+ # mrb_int unbox via sp_bigint_to_int.
13906+ bm_promote = (@int_overflow_mode == "promote") ? 1 : 0
13907+ ret_ct_bm = bm_promote == 1 ? "sp_Bigint *" : "mrb_int"
13908+ arg_ct_bm = bm_promote == 1 ? "sp_Bigint *" : "mrb_int"
1390213909 sig_args = "void *"
1390313910 if args_id >= 0
1390413911 aargs = get_args(args_id)
1390513912 k = 0
1390613913 while k < aargs.length
13907- # Method's bound C function has the fixed `(void *self,
13908- # mrb_int...)` ABI; promote-widened bigint args need
13909- # unbox before being passed through.
1391013914 arg_v_bm = compile_expr(aargs[k])
13911- if infer_type(aargs[k]) == "bigint"
13915+ at_bm = infer_type(aargs[k])
13916+ if bm_promote == 1
1391213917 @needs_bigint = 1
13913- arg_v_bm = "sp_bigint_to_int((sp_Bigint *)" + arg_v_bm + ")"
13918+ if base_type(at_bm) == "int"
13919+ arg_v_bm = "sp_bigint_new_int(" + arg_v_bm + ")"
13920+ end
13921+ else
13922+ if at_bm == "bigint"
13923+ @needs_bigint = 1
13924+ arg_v_bm = "sp_bigint_to_int((sp_Bigint *)" + arg_v_bm + ")"
13925+ end
1391413926 end
1391513927 joined = joined + ", " + arg_v_bm
13916- sig_args = sig_args + ", mrb_int"
13928+ sig_args = sig_args + ", " + arg_ct_bm
1391713929 k = k + 1
1391813930 end
1391913931 end
13920- return "((mrb_int (*)(" + sig_args + "))(uintptr_t)" + bm_tmp + "->iv_fn_ptr)((void *)" + bm_tmp + "->iv_self_obj" + joined + ")"
13932+ return "((" + ret_ct_bm + " (*)(" + sig_args + "))(uintptr_t)" + bm_tmp + "->iv_fn_ptr)((void *)" + bm_tmp + "->iv_self_obj" + joined + ")"
1392113933 end
1392213934 end
1392313935
@@ -15708,6 +15720,17 @@
1570815720 return 0
1570915721 end
1571015722 mn_eb = @nd_name[nid]
15723+ # `bm.call(x)` / `bm[x]` on a Method recv in promote mode: the
15724+ # fn_ptr cast returns sp_Bigint * (uniform ABI widening). The
15725+ # cached at/return for this CallNode lags reality so the
15726+ # caller's int-expected slot path mis-wraps with
15727+ # sp_bigint_new_int. Detect directly.
15728+ if (mn_eb == "call" || mn_eb == "[]") && @int_overflow_mode == "promote"
15729+ rcv_bm_eb = @nd_receiver[nid]
15730+ if rcv_bm_eb >= 0 && base_type(infer_type(rcv_bm_eb)) == "obj_Method"
15731+ return 1
15732+ end
15733+ end
1571115734 # CallNode dispatch to a user method whose return slot has been
1571215735 # promoted to bigint: infer_type can be stale (the cls_method
1571315736 # _return / @meth_return_types tables get rewritten by
@@ -22931,30 +22954,48 @@
2293122954 # any that came in as poly.
2293222955 method_idx = find_class_idx("Method")
2293322956 if method_idx >= 0 && @needs_method == 1
22957+ bm2_promote = (@int_overflow_mode == "promote") ? 1 : 0
22958+ ret_ct_bm2 = bm2_promote == 1 ? "sp_Bigint *" : "mrb_int"
22959+ arg_ct_bm2 = bm2_promote == 1 ? "sp_Bigint *" : "mrb_int"
22960+ null_default_bm2 = bm2_promote == 1 ? "sp_bigint_new_int(0)" : "0"
2293422961 mp = "((sp_Method *)" + recv_tmp + ".v.p)"
2293522962 sig_args = "void *"
2293622963 call_args = "(void *)" + mp + "->iv_self_obj"
2293722964 mi = 0
2293822965 while mi < arg_compiled.length
22939- sig_args = sig_args + ", mrb_int"
22966+ sig_args = sig_args + ", " + arg_ct_bm2
2294022967 ai_val = arg_compiled[mi]
22941- if mi < arg_types.length && arg_types[mi] == "poly"
22942- ai_val = "(" + ai_val + ").v.i"
22943- elsif mi < arg_types.length && arg_types[mi] == "bigint"
22968+ if bm2_promote == 1
2294422969 @needs_bigint = 1
22945- ai_val = "sp_bigint_to_int((sp_Bigint *)" + ai_val + ")"
22946- elsif ai_val.start_with?("sp_bigint_") || ai_val.start_with?("(sp_Bigint *)")
22970+ if mi < arg_types.length && arg_types[mi] == "poly"
22971+ @needs_rb_value = 1
22972+ ai_val = "sp_bigint_new_int((" + ai_val + ").v.i)"
22973+ elsif mi < arg_types.length && base_type(arg_types[mi]) == "int"
22974+ ai_val = "sp_bigint_new_int(" + ai_val + ")"
22975+ end
22976+ else
22977+ if mi < arg_types.length && arg_types[mi] == "poly"
22978+ ai_val = "(" + ai_val + ").v.i"
22979+ elsif mi < arg_types.length && arg_types[mi] == "bigint"
22980+ @needs_bigint = 1
22981+ ai_val = "sp_bigint_to_int((sp_Bigint *)" + ai_val + ")"
22982+ elsif ai_val.start_with?("sp_bigint_") || ai_val.start_with?("(sp_Bigint *)")
2294722983 # Stale-int cache hiding a bigint emit (canonical arith /
2294822984 # bitop CallNode whose operands include a promote-widened
2294922985 # local). Method's fn_ptr ABI is fixed mrb_int — unbox.
22950- @needs_bigint = 1
22951- ai_val = "sp_bigint_to_int((sp_Bigint *)" + ai_val + ")"
22986+ @needs_bigint = 1
22987+ ai_val = "sp_bigint_to_int((sp_Bigint *)" + ai_val + ")"
22988+ end
2295222989 end
2295322990 call_args = call_args + ", " + ai_val
2295422991 mi = mi + 1
2295522992 end
22956- mc = "(" + mp + " && " + mp + "->iv_fn_ptr ? ((mrb_int (*)(" + sig_args + "))(uintptr_t)" + mp + "->iv_fn_ptr)(" + call_args + ") : 0)"
22957- mrhs = is_poly_ret == 1 ? "sp_box_int(" + mc + ")" : mc
22993+ mc = "(" + mp + " && " + mp + "->iv_fn_ptr ? ((" + ret_ct_bm2 + " (*)(" + sig_args + "))(uintptr_t)" + mp + "->iv_fn_ptr)(" + call_args + ") : " + null_default_bm2 + ")"
22994+ if bm2_promote == 1
22995+ mrhs = is_poly_ret == 1 ? "sp_box_int(sp_bigint_to_int((sp_Bigint *)" + mc + "))" : "sp_bigint_to_int((sp_Bigint *)" + mc + ")"
22996+ else
22997+ mrhs = is_poly_ret == 1 ? "sp_box_int(" + mc + ")" : mc
22998+ end
2295822999 emit(" if (" + recv_tmp + ".cls_id == " + cls_id_for_user_internal(method_idx).to_s + ") " + result_tmp + " = " + mrhs + ";")
2295923000 end
2296023001 end