Commit 304bb73a

304bb73a0913Merged

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

Description

fix(codegen): inlined yield body arith / params honor bigint slot

The inlined yield-method body (compile_yield_call_stmt, compile_yield_method_call_stmt) declared the renamed-_yN params at the promote-widened bigint type but initialized them from raw mrb_int args; also compile_expr_remap's arith branch emitted raw C +, *, etc. against the bigint-typed remapped recv. Both tripped pointer/int conversion in the inlined block_given? else branch (x * 2 against a bigint lv_x_yN).

Three fixes:

  • Param init in compile_yield_call_stmt / compile_yield_method_call _stmt: route an int arg into a bigint slot via sp_bigint_new_int (and the reverse via sp_bigint_to_int).
  • compile_yield_call_stmt also declares the original param name in scope (mirroring compile_yield_method_call_stmt) so infer_type on unrenamed AST reads finds the bigint slot.
  • compile_expr_remap: when the recv is bigint, dispatch arithmetic / comparison through sp_bigint_add / sub / mul / div / mod / cmp, wrapping the rhs through sp_bigint_new_int when it's int.

Also extend compile_stmt_with_block's LocalVariableWriteNode arm with the same int<->bigint coerce its sibling main-body arm already has.

compile_yield_stmt args also unbox bigint to mrb_int before the _block ABI call (which takes mrb_int).

Promote-mode test count: 502 pass / 43 fail / 73 error -> 503 pass / 43 fail / 72 error. Default raise mode unchanged at 618 / 0 / 0.

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

Details

Commit
304bb73a0913
Parent
29edbb8a7aa4

spinel_codegen.rbmodifiedView file

@@ -24997,13 +24997,13 @@
2499724997 end
2499824998 end
2499924999 if vt == "bigint"
25000- rhs_t = infer_type(@nd_expression[nid])
25001- val = compile_expr(@nd_expression[nid])
25002- if rhs_t == "bigint"
25003- emit(" " + vref + " = " + val + ";")
25004- else
25005- emit(" " + vref + " = sp_bigint_new_int(" + val + ");")
25006- end
25000+ # Route through compile_expr_for_expected_type so the cache-
25001+ # override for arith CallNode on a bigint recv kicks in. This
25002+ # avoids the double-wrap `sp_bigint_new_int(sp_bigint_mul(...))`
25003+ # when the rhs is a bigint expression whose cached annotation
25004+ # is stale at "int".
25005+ val = compile_expr_for_expected_type(@nd_expression[nid], "bigint")
25006+ emit(" " + vref + " = " + val + ";")
2500725007 # Trigger GC after bigint statement (safe point - all results stored in vars)
2500825008 emit(" if(sp_gc_bytes>sp_gc_threshold){size_t _b=sp_gc_bytes;sp_gc_collect();size_t _f=_b-sp_gc_bytes;if(_f<_b/4)sp_gc_threshold=_b*2;else if(sp_gc_bytes>0){sp_gc_threshold=sp_gc_bytes*4;if(sp_gc_threshold<sp_gc_threshold_init)sp_gc_threshold=sp_gc_threshold_init;}else sp_gc_threshold=sp_gc_threshold_init;}")
2500925009 return
@@ -34183,7 +34183,14 @@
3418334183 aids = get_args(args_id)
3418434184 k = 0
3418534185 while k < aids.length
34186- emitted.push(compile_expr(aids[k]))
34186+ # The block's C ABI is `(mrb_int, ..., void *)`; promote-mode
34187+ # widened args are sp_Bigint * locally and need unboxing.
34188+ yarg_ev = compile_expr(aids[k])
34189+ if infer_type(aids[k]) == "bigint"
34190+ @needs_bigint = 1
34191+ yarg_ev = "sp_bigint_to_int((sp_Bigint *)" + yarg_ev + ")"
34192+ end
34193+ emitted.push(yarg_ev)
3418734194 k = k + 1
3418834195 end
3418934196 end
@@ -34249,14 +34256,26 @@
3424934256 pt = ptypes[k]
3425034257 end
3425134258 tname = pnames[k] + suffix
34252- val = "0"
34259+ val = c_default_val(pt)
3425334260 if k < arg_ids.length
3425434261 val = compile_expr(arg_ids[k])
34262+ arg_t_inline = infer_type(arg_ids[k])
34263+ if base_type(pt) == "bigint" && arg_t_inline == "int"
34264+ @needs_bigint = 1
34265+ val = "sp_bigint_new_int(" + val + ")"
34266+ elsif base_type(pt) == "int" && arg_t_inline == "bigint"
34267+ @needs_bigint = 1
34268+ val = "sp_bigint_to_int((sp_Bigint *)" + val + ")"
34269+ end
3425534270 end
3425634271 emit(" " + c_type(pt) + " lv_" + tname + " = " + val + ";")
3425734272 param_map_from.push(pnames[k])
3425834273 param_map_to.push(tname)
3425934274 declare_var(tname, pt)
34275+ # Also expose the original name so infer_type's scope lookup
34276+ # inside the inlined body (which still sees the AST's
34277+ # LocalVariableReadNode "x", not "x_y1") finds the right type.
34278+ declare_var(pnames[k], pt)
3426034279 k = k + 1
3426134280 end
3426234281
@@ -34343,9 +34362,17 @@
3434334362 pt_yc = ptypes[kp]
3434434363 end
3434534364 tname = pnames[kp] + suffix
34346- val_yc = "0"
34365+ val_yc = c_default_val(pt_yc)
3434734366 if kp < arg_ids_yc.length
3434834367 val_yc = compile_expr(arg_ids_yc[kp])
34368+ arg_t_yc = infer_type(arg_ids_yc[kp])
34369+ if base_type(pt_yc) == "bigint" && arg_t_yc == "int"
34370+ @needs_bigint = 1
34371+ val_yc = "sp_bigint_new_int(" + val_yc + ")"
34372+ elsif base_type(pt_yc) == "int" && arg_t_yc == "bigint"
34373+ @needs_bigint = 1
34374+ val_yc = "sp_bigint_to_int((sp_Bigint *)" + val_yc + ")"
34375+ end
3434934376 end
3435034377 emit(" " + c_type(pt_yc) + " lv_" + tname + " = " + val_yc + ";")
3435134378 param_map_from.push(pnames[kp])
@@ -34465,6 +34492,14 @@
3446534492 if vt_lv == "poly" && rhs_t_lv != "" && rhs_t_lv != "poly"
3446634493 val = box_value_to_poly(rhs_t_lv, val)
3446734494 end
34495+ if vt_lv == "bigint" && rhs_t_lv == "int"
34496+ @needs_bigint = 1
34497+ val = "sp_bigint_new_int(" + val + ")"
34498+ end
34499+ if vt_lv == "int" && rhs_t_lv == "bigint"
34500+ @needs_bigint = 1
34501+ val = "sp_bigint_to_int((sp_Bigint *)" + val + ")"
34502+ end
3446834503 emit(" lv_" + rname + " = " + val + ";")
3446934504 return
3447034505 end
@@ -34698,6 +34733,61 @@
3469834733 mname = @nd_name[nid]
3469934734 recv = @nd_receiver[nid]
3470034735 if recv >= 0
34736+ # promote-mode-widened recv: route arith / cmp through the
34737+ # sp_bigint_* helpers. Mirrors compile_operator_expr's bigint
34738+ # branch but for the remapped (inlined yield body) emit path.
34739+ recv_t_rm = base_type(infer_type(recv))
34740+ if recv_t_rm == "bigint" && (mname == "+" || mname == "-" || mname == "*" || mname == "/" || mname == "%" || mname == "<" || mname == ">" || mname == "<=" || mname == ">=" || mname == "==" || mname == "!=")
34741+ @needs_bigint = 1
34742+ rc_b_rm = "(sp_Bigint *)" + compile_expr_remap(recv, map_from, map_to)
34743+ args_id_rm = @nd_arguments[nid]
34744+ arg_b_rm = "sp_bigint_new_int(0)"
34745+ if args_id_rm >= 0
34746+ arg_ids_rm = get_args(args_id_rm)
34747+ if arg_ids_rm.length > 0
34748+ arg_raw_rm = compile_expr_remap(arg_ids_rm[0], map_from, map_to)
34749+ arg_t_rm = infer_type(arg_ids_rm[0])
34750+ if arg_t_rm == "bigint"
34751+ arg_b_rm = "(sp_Bigint *)" + arg_raw_rm
34752+ else
34753+ arg_b_rm = "sp_bigint_new_int(" + arg_raw_rm + ")"
34754+ end
34755+ end
34756+ end
34757+ if mname == "+"
34758+ return "sp_bigint_add(" + rc_b_rm + ", " + arg_b_rm + ")"
34759+ end
34760+ if mname == "-"
34761+ return "sp_bigint_sub(" + rc_b_rm + ", " + arg_b_rm + ")"
34762+ end
34763+ if mname == "*"
34764+ return "sp_bigint_mul(" + rc_b_rm + ", " + arg_b_rm + ")"
34765+ end
34766+ if mname == "/"
34767+ return "sp_bigint_div(" + rc_b_rm + ", " + arg_b_rm + ")"
34768+ end
34769+ if mname == "%"
34770+ return "sp_bigint_mod(" + rc_b_rm + ", " + arg_b_rm + ")"
34771+ end
34772+ if mname == "<"
34773+ return "(sp_bigint_cmp(" + rc_b_rm + ", " + arg_b_rm + ") < 0)"
34774+ end
34775+ if mname == ">"
34776+ return "(sp_bigint_cmp(" + rc_b_rm + ", " + arg_b_rm + ") > 0)"
34777+ end
34778+ if mname == "<="
34779+ return "(sp_bigint_cmp(" + rc_b_rm + ", " + arg_b_rm + ") <= 0)"
34780+ end
34781+ if mname == ">="
34782+ return "(sp_bigint_cmp(" + rc_b_rm + ", " + arg_b_rm + ") >= 0)"
34783+ end
34784+ if mname == "=="
34785+ return "(sp_bigint_cmp(" + rc_b_rm + ", " + arg_b_rm + ") == 0)"
34786+ end
34787+ if mname == "!="
34788+ return "(sp_bigint_cmp(" + rc_b_rm + ", " + arg_b_rm + ") != 0)"
34789+ end
34790+ end
3470134791 if mname == "+"
3470234792 return "(" + compile_expr_remap(recv, map_from, map_to) + " + " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
3470334793 end
@@ -34952,6 +35042,14 @@
3495235042 val = c_default_val(pt)
3495335043 if k < arg_ids.length
3495435044 val = compile_expr(arg_ids[k])
35045+ arg_t_ym = infer_type(arg_ids[k])
35046+ if base_type(pt) == "bigint" && arg_t_ym == "int"
35047+ @needs_bigint = 1
35048+ val = "sp_bigint_new_int(" + val + ")"
35049+ elsif base_type(pt) == "int" && arg_t_ym == "bigint"
35050+ @needs_bigint = 1
35051+ val = "sp_bigint_to_int((sp_Bigint *)" + val + ")"
35052+ end
3495535053 end
3495635054 emit(" " + c_type(pt) + " lv_" + tname + " = " + val + ";")
3495735055 map_from.push(pnames[k])