Commit 4cc82c69

4cc82c694163Merged

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

Description

feat(codegen): poly value into bigint slot unbox + rebox

Phase 1 continuation. Three sites where poly bitop helpers (sp_poly_band / _bor / _bxor / _shl / _shr) returning sp_RbVal with SP_TAG_INT were leaking into bigint-promoted slots:

  • IVW (@ivar = poly_expr where ivar's slot is bigint): added sp_bigint_new_int((val).v.i) arm to the existing poly-rhs unbox dispatch (mirrors the int/string/float arms).
  • compile_expr_for_expected_type (typed call args / return): added bigint expected_base case that unboxes via .v.i and re-boxes via sp_bigint_new_int.
  • compile_body_return_inner: explicit expr_type == "poly" && return_type == "bigint" branch before the maybe_bigint_coerce fallback. Coerce never handled poly source.

Optcarrot --int-overflow=promote errors: 12 -> 7 (-42%). Default raise mode: 631/0/0. Optcarrot wrap mode checksum 59662 unchanged.

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

Details

Commit
4cc82c694163
Parent
b1dc3ad75bd6

spinel_codegen.rbmodifiedView file

@@ -5800,6 +5800,15 @@
58005800 @needs_rb_value = 1
58015801 return "(" + val + ").v.sym"
58025802 end
5803+ if expected_base == "bigint"
5804+ # `--int-overflow=promote` widens slots to bigint; a poly recv
5805+ # whose underlying value is an int (sp_poly_bxor / poly arith
5806+ # returning SP_TAG_INT-tagged sp_RbVal) needs to land in a
5807+ # sp_Bigint * slot. Unbox via .v.i then re-box.
5808+ @needs_rb_value = 1
5809+ @needs_bigint = 1
5810+ return "sp_bigint_new_int((" + val + ").v.i)"
5811+ end
58035812 if is_obj_type(expected_base) == 1
58045813 @needs_rb_value = 1
58055814 cname = expected_base[4, expected_base.length - 4]
@@ -26435,6 +26444,13 @@
2643526444 val = "(" + val + ").v.s"
2643626445 elsif ivt_b == "float"
2643726446 val = "(" + val + ").v.f"
26447+ elsif ivt_b == "bigint"
26448+ # poly bitop helpers (sp_poly_band / _bor / _bxor / _shl / _shr)
26449+ # return sp_RbVal with SP_TAG_INT (Ruby bitops produce ints).
26450+ # Unbox via .v.i and rebox into sp_Bigint * to match the
26451+ # promote-mode bigint ivar slot.
26452+ @needs_bigint = 1
26453+ val = "sp_bigint_new_int((" + val + ").v.i)"
2643826454 elsif is_obj_type(ivt_b) == 1 || is_array_type(ivt_b) == 1 || is_hash_type(ivt_b) == 1 || is_ptr_array_type(ivt_b) == 1 || ivt_b == "proc"
2643926455 val = "(" + c_type(ivt) + ")(" + val + ").v.p"
2644026456 end
@@ -37429,6 +37445,12 @@
3742937445 emit(" return " + val + ";")
3743037446 end
3743137447 end
37448+ elsif expr_type == "poly" && base_type(return_type) == "bigint"
37449+ # poly bitop helpers / dispatchers can return sp_RbVal with
37450+ # SP_TAG_INT into a bigint-promoted return slot. Unbox + rebox.
37451+ @needs_rb_value = 1
37452+ @needs_bigint = 1
37453+ emit(" return sp_bigint_new_int((" + val + ").v.i);")
3743237454 else
3743337455 # `--int-overflow=promote` promotes int slots (incl. return
3743437456 # type) to bigint at analyze time, so a literal/int-typed