Commit 5ff1b0dd

5ff1b0dd76ffMerged

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

Description

feat(codegen): ivar op-assign bigint dispatch + push poly value

Two more promote-mode plug-fixes:

  • IVOpWrite (@ivar &= rhs, |=, ^=, <<=, >>=) on a bigint slot. Both expression-form and stmt-form emit sites routed sp_bigint_to_int(lhs) <op> sp_bigint_to_int(rhs) via the legacy inline shape; with a bigint-emitting rhs (a ^ chain in optcarrot's CPU code), rhs was sp_Bigint * and the inline & rejected. Dispatch through the sp_bigint_ helpers; use the stale-cache walker to upgrade rhs from "int" to "bigint" when the actual emit is bigint.
  • Stmt-level int_array << expr / int_array.push(expr) with a poly-typed rhs. Added a .v.i unbox arm next to the existing bigint-unbox. Matches the canonical optcarrot shape: @buffer << @mixer.sample while ... where Mixer#sample's return slot ends up poly from the complex method body.

Optcarrot --int-overflow=promote errors: 7 -> 4. Default raise: 631/0/0. Optcarrot wrap checksum 59662 unchanged.

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

Details

Commit
5ff1b0dd76ff
Parent
4cc82c694163

spinel_codegen.rbmodifiedView file

@@ -11964,8 +11964,21 @@
1196411964 return "(" + lhs_iow + " = " + big_op_iow + "((sp_Bigint *)" + lhs_iow + ", " + rhs_big_iow + "))"
1196511965 end
1196611966 if op_iow == "&" || op_iow == "|" || op_iow == "^" || op_iow == "<<" || op_iow == ">>"
11967- rhs_int_iow = rhs_t_iow == "bigint" ? "sp_bigint_to_int(" + rhs_big_iow + ")" : "(" + val_iow + ")"
11968- return "(" + lhs_iow + " = sp_bigint_new_int(sp_bigint_to_int((sp_Bigint *)" + lhs_iow + ") " + op_iow + " " + rhs_int_iow + "))"
11967+ # Promote-mode op-assign on a bigint ivar: route through the
11968+ # sp_bigint_<op> helpers so both `bigint &= int` and
11969+ # `bigint &= bigint_expr` shapes type-check. The legacy inline
11970+ # `sp_bigint_to_int(lhs) <op> sp_bigint_to_int(rhs)` only
11971+ # handled the int-rhs case cleanly; a bigint-emitting rhs (a
11972+ # `^` chain etc.) left the rhs as sp_Bigint * and the inline
11973+ # `&` rejected at C compile.
11974+ rhs_big_iow_arg = rhs_big_iow
11975+ if op_iow == "<<" || op_iow == ">>"
11976+ shamt_iow = rhs_t_iow == "bigint" ? "sp_bigint_to_int(" + rhs_big_iow + ")" : "(" + val_iow + ")"
11977+ helper_iow = op_iow == "<<" ? "sp_bigint_shl" : "sp_bigint_shr"
11978+ return "(" + lhs_iow + " = " + helper_iow + "((sp_Bigint *)" + lhs_iow + ", " + shamt_iow + "))"
11979+ end
11980+ big_bitop_iow = op_iow == "&" ? "sp_bigint_and" : (op_iow == "|" ? "sp_bigint_or" : "sp_bigint_xor")
11981+ return "(" + lhs_iow + " = " + big_bitop_iow + "((sp_Bigint *)" + lhs_iow + ", " + rhs_big_iow_arg + "))"
1196911982 end
1197011983 end
1197111984 return "(" + lhs_iow + " " + op_iow + "= " + val_iow + ")"
@@ -16473,14 +16486,8 @@
1647316486 # `Array.new(N) { i * 2 }` with bigint i caches "int" but
1647416487 # emits sp_bigint_mul.
1647516488 arrnew_tail_t = infer_type(arrnew_stmts2.last)
16476- if arrnew_tail_t != "bigint" && @nd_type[arrnew_stmts2.last] == "CallNode"
16477- an_mn = @nd_name[arrnew_stmts2.last]
16478- if an_mn == "+" || an_mn == "-" || an_mn == "*" || an_mn == "/" || an_mn == "%" || an_mn == "**"
16479- an_recv = @nd_receiver[arrnew_stmts2.last]
16480- if an_recv >= 0 && base_type(infer_type(an_recv)) == "bigint"
16481- arrnew_tail_t = "bigint"
16482- end
16483- end
16489+ if arrnew_tail_t != "bigint" && expr_emit_is_bigint(arrnew_stmts2.last) == 1
16490+ arrnew_tail_t = "bigint"
1648416491 end
1648516492 if arrnew_tail_t == "bigint"
1648616493 @needs_bigint = 1
@@ -26595,6 +26602,13 @@
2659526602 if ivar_t == "bigint"
2659626603 @needs_bigint = 1
2659726604 rhs_t_iov = infer_type(@nd_expression[nid])
26605+ # Stale "int" infer with a bigint-emitting rhs (a `^` or other
26606+ # bitop chain whose operands include bigint): the rhs is
26607+ # already sp_Bigint *, so wrapping it in sp_bigint_new_int is
26608+ # the wrong shape. Use the emit walker to upgrade.
26609+ if rhs_t_iov != "bigint" && expr_emit_is_bigint(@nd_expression[nid]) == 1
26610+ rhs_t_iov = "bigint"
26611+ end
2659826612 rhs_big_iov = rhs_t_iov == "bigint" ? "(sp_Bigint *)(" + val + ")" : "sp_bigint_new_int(" + val + ")"
2659926613 if op == "+"
2660026614 emit(" " + lhs + " = sp_bigint_add((sp_Bigint *)" + lhs + ", " + rhs_big_iov + ");")
@@ -26616,9 +26630,15 @@
2661626630 emit(" " + lhs + " = sp_bigint_mod((sp_Bigint *)" + lhs + ", " + rhs_big_iov + ");")
2661726631 return
2661826632 end
26619- if op == "&" || op == "|" || op == "^" || op == "<<" || op == ">>"
26620- rhs_int_iov = rhs_t_iov == "bigint" ? "sp_bigint_to_int(" + rhs_big_iov + ")" : "(" + val + ")"
26621- emit(" " + lhs + " = sp_bigint_new_int(sp_bigint_to_int((sp_Bigint *)" + lhs + ") " + op + " " + rhs_int_iov + ");")
26633+ if op == "<<" || op == ">>"
26634+ shamt_iov = rhs_t_iov == "bigint" ? "sp_bigint_to_int(" + rhs_big_iov + ")" : "(" + val + ")"
26635+ h_iov = op == "<<" ? "sp_bigint_shl" : "sp_bigint_shr"
26636+ emit(" " + lhs + " = " + h_iov + "((sp_Bigint *)" + lhs + ", " + shamt_iov + ");")
26637+ return
26638+ end
26639+ if op == "&" || op == "|" || op == "^"
26640+ h_bitop_iov = op == "&" ? "sp_bigint_and" : (op == "|" ? "sp_bigint_or" : "sp_bigint_xor")
26641+ emit(" " + lhs + " = " + h_bitop_iov + "((sp_Bigint *)" + lhs + ", " + rhs_big_iov + ");")
2662226642 return
2662326643 end
2662426644 end
@@ -28767,11 +28787,15 @@
2876728787 end
2876828788 end
2876928789 if a0id >= 0
28770- if infer_type(a0id) == "lambda"
28790+ at_iaip = infer_type(a0id)
28791+ if at_iaip == "lambda"
2877128792 av = "(mrb_int)" + av
28772- elsif infer_type(a0id) == "bigint"
28793+ elsif at_iaip == "bigint" || expr_emit_is_bigint(a0id) == 1
2877328794 @needs_bigint = 1
2877428795 av = "sp_bigint_to_int((sp_Bigint *)" + av + ")"
28796+ elsif at_iaip == "poly"
28797+ @needs_rb_value = 1
28798+ av = "(" + av + ").v.i"
2877528799 end
2877628800 end
2877728801 emit(" sp_IntArray_push(" + rc + ", " + av + ");")
@@ -28998,11 +29022,15 @@
2899829022 end
2899929023 end
2900029024 if a0id >= 0
29001- if infer_type(a0id) == "lambda"
29025+ at_iaip = infer_type(a0id)
29026+ if at_iaip == "lambda"
2900229027 av = "(mrb_int)" + av
29003- elsif infer_type(a0id) == "bigint"
29028+ elsif at_iaip == "bigint" || expr_emit_is_bigint(a0id) == 1
2900429029 @needs_bigint = 1
2900529030 av = "sp_bigint_to_int((sp_Bigint *)" + av + ")"
29031+ elsif at_iaip == "poly"
29032+ @needs_rb_value = 1
29033+ av = "(" + av + ").v.i"
2900629034 end
2900729035 end
2900829036 emit(" sp_IntArray_push(" + rc + ", " + av + ");")