Commit 33c8219a

33c8219ac984Merged

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

Description

fix(codegen): range#each / Array.new(N){} / hash#each block-LV wrap to bigint

Add emit_block_lv_assign_int helper that emits lv_<bp> = <int_rhs> as either the raw assignment or sp_bigint_new_int(<int_rhs>) depending on the block param's outer-scope slot type. Apply it to:

  • compile_each_block's range arm (both literal-range and dynamic-range cases) — was emitting for (lv_x = lo; lv_x <= hi; lv_x++) directly into a promote-mode-widened sp_Bigint * slot.
  • compile_constructor_expr's Array.new(N){} block — was emitting lv_i = <counter>; straight.
  • compile_each_block's str_int_hash / int_str_hash / sym_int_hash arms — the int-typed key or value got assigned raw into a bigint-widened block param.

Promote-mode test count: 476 pass / 52 fail / 90 error -> 476 pass / 54 fail / 88 error. Default raise mode unchanged at 618 / 0 / 0.

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

Details

Commit
33c8219ac984
Parent
a38cc41d0948

spinel_codegen.rbmodifiedView file

@@ -12367,6 +12367,22 @@
1236712367 "sp_system_args(" + arg_ids.length.to_s + ", (const char*[]){" + argv + "})"
1236812368 end
1236912369
12370+ # Emit `lv_<bp> = <rhs>;` honoring promote-mode widening: if the
12371+ # block param's outer scope slot is bigint while the underlying
12372+ # iterator value is mrb_int, wrap through sp_bigint_new_int.
12373+ # Returns nothing — used for inline assignments inside emit_*
12374+ # block helpers where the LV is the function-level slot (not a
12375+ # block-scoped fresh decl).
12376+ def emit_block_lv_assign_int(bp, rhs_int_expr)
12377+ slot_t = find_var_type(bp)
12378+ if slot_t == "bigint"
12379+ @needs_bigint = 1
12380+ emit(" lv_" + bp + " = sp_bigint_new_int(" + rhs_int_expr + ");")
12381+ else
12382+ emit(" lv_" + bp + " = " + rhs_int_expr + ";")
12383+ end
12384+ end
12385+
1237012386 # Like compile_arg0, but unboxes the result to mrb_int when the
1237112387 # argument's static type is poly. Use at call sites that pass the
1237212388 # arg directly to a C function expecting `mrb_int` (sp_IntArray_get,
@@ -15666,7 +15682,13 @@
1566615682 emit(" for (mrb_int " + arrnew_iv + " = 0; " + arrnew_iv + " < " + arrnew_count + "; " + arrnew_iv + "++) {")
1566715683 @indent = @indent + 1
1566815684 if arrnew_bp != ""
15669- emit(" lv_" + arrnew_bp + " = " + arrnew_iv + ";")
15685+ arrnew_bp_t = find_var_type(arrnew_bp)
15686+ if arrnew_bp_t == "bigint"
15687+ @needs_bigint = 1
15688+ emit(" lv_" + arrnew_bp + " = sp_bigint_new_int(" + arrnew_iv + ");")
15689+ else
15690+ emit(" lv_" + arrnew_bp + " = " + arrnew_iv + ";")
15691+ end
1567015692 end
1567115693 if arrnew_body >= 0
1567215694 arrnew_stmts2 = get_stmts(arrnew_body)
@@ -31462,7 +31484,7 @@
3146231484 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < " + rc + "->len; " + tmp + "++) {")
3146331485 emit(" lv_" + bp1 + " = " + rc + "->order[" + tmp + "];")
3146431486 if bp2 != ""
31465- emit(" lv_" + bp2 + " = sp_StrIntHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
31487+ emit_block_lv_assign_int(bp2, "sp_StrIntHash_get(" + rc + ", " + rc + "->order[" + tmp + "])")
3146631488 end
3146731489 @indent = @indent + 1
3146831490 push_scope
@@ -31481,7 +31503,7 @@
3148131503 if rt == "int_str_hash"
3148231504 tmp = new_temp
3148331505 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < " + rc + "->len; " + tmp + "++) {")
31484- emit(" lv_" + bp1 + " = " + rc + "->order[" + tmp + "];")
31506+ emit_block_lv_assign_int(bp1, rc + "->order[" + tmp + "]")
3148531507 if bp2 != ""
3148631508 emit(" lv_" + bp2 + " = sp_IntStrHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
3148731509 end
@@ -31525,7 +31547,7 @@
3152531547 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < " + rc + "->len; " + tmp + "++) {")
3152631548 emit(" lv_" + bp1 + " = " + rc + "->order[" + tmp + "];")
3152731549 if bp2 != ""
31528- emit(" lv_" + bp2 + " = sp_SymIntHash_get(" + rc + ", " + rc + "->order[" + tmp + "]);")
31550+ emit_block_lv_assign_int(bp2, "sp_SymIntHash_get(" + rc + ", " + rc + "->order[" + tmp + "])")
3152931551 end
3153031552 @indent = @indent + 1
3153131553 push_scope
@@ -31626,15 +31648,23 @@
3162631648 end
3162731649 end
3162831650 end
31651+ bp_t_re = has_bp == 1 ? find_var_type(bp1) : ""
3162931652 if lit_range >= 0
3163031653 cmp = range_excl_end(lit_range) == 1 ? "<" : "<="
3163131654 lo = compile_expr(@nd_left[lit_range])
3163231655 hi = compile_expr(@nd_right[lit_range])
31633- emit(" for (lv_" + bp1 + " = " + lo + "; lv_" + bp1 + " " + cmp + " " + hi + "; lv_" + bp1 + "++) {")
31656+ if bp_t_re == "bigint"
31657+ @needs_bigint = 1
31658+ rtmp_re = new_temp
31659+ emit(" for (mrb_int " + rtmp_re + " = " + lo + "; " + rtmp_re + " " + cmp + " " + hi + "; " + rtmp_re + "++) {")
31660+ emit(" lv_" + bp1 + " = sp_bigint_new_int(" + rtmp_re + ");")
31661+ else
31662+ emit(" for (lv_" + bp1 + " = " + lo + "; lv_" + bp1 + " " + cmp + " " + hi + "; lv_" + bp1 + "++) {")
31663+ end
3163431664 @indent = @indent + 1
3163531665 push_scope
3163631666 if has_bp == 1
31637- declare_var(bp1, "int")
31667+ declare_var(bp1, bp_t_re == "bigint" ? "bigint" : "int")
3163831668 end
3163931669 redo_label = push_redo_label
3164031670 emit_redo_label(redo_label)
@@ -31647,11 +31677,18 @@
3164731677 tmp = new_temp
3164831678 tmp2 = new_temp
3164931679 emit(" sp_Range " + tmp2 + " = " + rc + ";")
31650- emit(" for (lv_" + bp1 + " = " + tmp2 + ".first; lv_" + bp1 + " <= " + tmp2 + ".last; lv_" + bp1 + "++) {")
31680+ if bp_t_re == "bigint"
31681+ @needs_bigint = 1
31682+ rtmp_re2 = new_temp
31683+ emit(" for (mrb_int " + rtmp_re2 + " = " + tmp2 + ".first; " + rtmp_re2 + " <= " + tmp2 + ".last; " + rtmp_re2 + "++) {")
31684+ emit(" lv_" + bp1 + " = sp_bigint_new_int(" + rtmp_re2 + ");")
31685+ else
31686+ emit(" for (lv_" + bp1 + " = " + tmp2 + ".first; lv_" + bp1 + " <= " + tmp2 + ".last; lv_" + bp1 + "++) {")
31687+ end
3165131688 @indent = @indent + 1
3165231689 push_scope
3165331690 if has_bp == 1
31654- declare_var(bp1, "int")
31691+ declare_var(bp1, bp_t_re == "bigint" ? "bigint" : "int")
3165531692 end
3165631693 redo_label = push_redo_label
3165731694 emit_redo_label(redo_label)