Commit 2270a7d2

2270a7d25500Merged

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

Description

fix(codegen): for-in/times/downto loop var wrap to bigint when slot promoted

ForNode (both range and array variants), compile_times_block, and compile_downto_block emitted lv_<bp> = <counter> directly, ignoring promote mode's widening of the outer-scope LV. Mirror compile_upto_block's already-correct pattern:

  • Probe find_var_type(bp) before the loop.
  • If the slot is bigint, allocate a fresh mrb_int counter, run the loop on it, and box each iteration into the LV via sp_bigint_new_int(counter) (for range/upto/downto/times) or sp_bigint_new_int(sp_<arr>_get(...)) (for for-in array).
  • Re-declare the block-scope LV with the actual slot type so the body's arithmetic dispatches through bigint helpers.

times and downto also unbox a bigint receiver/limit through sp_bigint_to_int so the C loop bound stays mrb_int.

Promote-mode test count: 457 pass / 43 fail / 118 error -> 460 pass / 44 fail / 114 error. Default raise mode unchanged at 618 / 0 / 0.

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

Details

Commit
2270a7d25500
Parent
cdd2dc95fac9

spinel_codegen.rbmodifiedView file

@@ -26408,11 +26408,22 @@
2640826408 vname = @nd_name[tgt]
2640926409 end
2641026410 end
26411+ slot_t_for = find_var_type(vname)
2641126412 if @nd_type[coll] == "RangeNode"
2641226413 left = compile_expr(@nd_left[coll])
2641326414 right = compile_expr(@nd_right[coll])
2641426415 cmp = range_excl_end(coll) == 1 ? "<" : "<="
26415- emit(" for (lv_" + vname + " = " + left + "; lv_" + vname + " " + cmp + " " + right + "; lv_" + vname + "++) {")
26416+ if slot_t_for == "bigint"
26417+ # promote-mode-widened loop var: use a fresh mrb_int counter
26418+ # and wrap into the bigint slot at each iteration so the body
26419+ # sees a bigint LV but `++` still works on the C counter.
26420+ @needs_bigint = 1
26421+ ftmp = new_temp
26422+ emit(" for (mrb_int " + ftmp + " = " + left + "; " + ftmp + " " + cmp + " " + right + "; " + ftmp + "++) {")
26423+ emit(" lv_" + vname + " = sp_bigint_new_int(" + ftmp + ");")
26424+ else
26425+ emit(" for (lv_" + vname + " = " + left + "; lv_" + vname + " " + cmp + " " + right + "; lv_" + vname + "++) {")
26426+ end
2641626427 @indent = @indent + 1
2641726428 redo_label = push_redo_label
2641826429 emit_redo_label(redo_label)
@@ -26429,7 +26440,13 @@
2642926440 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")
2643026441 # Loop-var assignment stays OUTSIDE the redo label so `redo` re-runs
2643126442 # the body with the same value (matches MRI: `redo` does not advance).
26432- emit(" lv_" + vname + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
26443+ et_for = elem_type_of_array(ct)
26444+ if slot_t_for == "bigint" && et_for == "int"
26445+ @needs_bigint = 1
26446+ emit(" lv_" + vname + " = sp_bigint_new_int(sp_" + pfx + "_get(" + rc + ", " + tmp + "));")
26447+ else
26448+ emit(" lv_" + vname + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
26449+ end
2643326450
2643426451 @indent = @indent + 1
2643526452 redo_label = push_redo_label
@@ -31647,17 +31664,30 @@
3164731664 def compile_times_block(nid)
3164831665 old = @in_loop
3164931666 @in_loop = 1
31650- rc = compile_expr_gc_rooted(@nd_receiver[nid])
31667+ rc_recv = @nd_receiver[nid]
31668+ rc = compile_expr_gc_rooted(rc_recv)
3165131669 bp1 = get_block_param(nid, 0)
3165231670 tmp = new_temp
31671+ # With promote mode the receiver of `times` is itself sp_Bigint *,
31672+ # so the loop bound needs to be unboxed to mrb_int.
31673+ if rc_recv >= 0 && infer_type(rc_recv) == "bigint"
31674+ @needs_bigint = 1
31675+ rc = "sp_bigint_to_int((sp_Bigint *)" + rc + ")"
31676+ end
3165331677 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < " + rc + "; " + tmp + "++) {")
31678+ bp_t_tb = bp1 != "" ? find_var_type(bp1) : ""
3165431679 if bp1 != ""
31655- emit(" lv_" + bp1 + " = " + tmp + ";")
31680+ if bp_t_tb == "bigint"
31681+ @needs_bigint = 1
31682+ emit(" lv_" + bp1 + " = sp_bigint_new_int(" + tmp + ");")
31683+ else
31684+ emit(" lv_" + bp1 + " = " + tmp + ";")
31685+ end
3165631686 end
3165731687 @indent = @indent + 1
3165831688 push_scope
3165931689 if bp1 != ""
31660- declare_var(bp1, "int")
31690+ declare_var(bp1, bp_t_tb == "bigint" ? "bigint" : "int")
3166131691 end
3166231692 redo_label = push_redo_label
3166331693 emit_redo_label(redo_label)
@@ -31733,18 +31763,29 @@
3173331763 def compile_downto_block(nid)
3173431764 old = @in_loop
3173531765 @in_loop = 1
31736- rc = compile_expr_gc_rooted(@nd_receiver[nid])
31737- lim = compile_arg0(nid)
31766+ rc_recv_d = @nd_receiver[nid]
31767+ rc = compile_expr_gc_rooted(rc_recv_d)
31768+ if rc_recv_d >= 0 && infer_type(rc_recv_d) == "bigint"
31769+ @needs_bigint = 1
31770+ rc = "sp_bigint_to_int((sp_Bigint *)" + rc + ")"
31771+ end
31772+ lim = compile_arg0_as_int(nid)
3173831773 bp1 = get_block_param(nid, 0)
3173931774 tmp = new_temp
3174031775 emit(" for (mrb_int " + tmp + " = " + rc + "; " + tmp + " >= " + lim + "; " + tmp + "--) {")
31776+ bp_t_dd = bp1 != "" ? find_var_type(bp1) : ""
3174131777 if bp1 != ""
31742- emit(" lv_" + bp1 + " = " + tmp + ";")
31778+ if bp_t_dd == "bigint"
31779+ @needs_bigint = 1
31780+ emit(" lv_" + bp1 + " = sp_bigint_new_int(" + tmp + ");")
31781+ else
31782+ emit(" lv_" + bp1 + " = " + tmp + ";")
31783+ end
3174331784 end
3174431785 @indent = @indent + 1
3174531786 push_scope
3174631787 if bp1 != ""
31747- declare_var(bp1, "int")
31788+ declare_var(bp1, bp_t_dd == "bigint" ? "bigint" : "int")
3174831789 end
3174931790 redo_label = push_redo_label
3175031791 emit_redo_label(redo_label)