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 routedsp_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.iunbox arm next to the existing bigint-unbox. Matches the canonical optcarrot shape:@buffer << @mixer.sample while ...where Mixer#sample's return slot ends uppolyfrom 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
end1196611966
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 the11968+
# sp_bigint_<op> helpers so both `bigint &= int` and11969+
# `bigint &= bigint_expr` shapes type-check. The legacy inline11970+
# `sp_bigint_to_int(lhs) <op> sp_bigint_to_int(rhs)` only11971+
# handled the int-rhs case cleanly; a bigint-emitting rhs (a11972+
# `^` chain etc.) left the rhs as sp_Bigint * and the inline11973+
# `&` rejected at C compile.11974+
rhs_big_iow_arg = rhs_big_iow11975+
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+
end11980+
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
end1197011983
end1197111984
return "(" + lhs_iow + " " + op_iow + "= " + val_iow + ")"@@ -16473,14 +16486,8 @@
1647316486
# `Array.new(N) { i * 2 }` with bigint i caches "int" but1647416487
# 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-
end16483-
end16489+
if arrnew_tail_t != "bigint" && expr_emit_is_bigint(arrnew_stmts2.last) == 116490+
arrnew_tail_t = "bigint"1648416491
end1648516492
if arrnew_tail_t == "bigint"1648616493
@needs_bigint = 1@@ -26595,6 +26602,13 @@
2659526602
if ivar_t == "bigint"2659626603
@needs_bigint = 12659726604
rhs_t_iov = infer_type(@nd_expression[nid])26605+
# Stale "int" infer with a bigint-emitting rhs (a `^` or other26606+
# bitop chain whose operands include bigint): the rhs is26607+
# already sp_Bigint *, so wrapping it in sp_bigint_new_int is26608+
# the wrong shape. Use the emit walker to upgrade.26609+
if rhs_t_iov != "bigint" && expr_emit_is_bigint(@nd_expression[nid]) == 126610+
rhs_t_iov = "bigint"26611+
end2659826612
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
return2661826632
end26619-
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+
return26638+
end26639+
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
return2662326643
end2662426644
end@@ -28767,11 +28787,15 @@
2876728787
end2876828788
end2876928789
if a0id >= 028770-
if infer_type(a0id) == "lambda"28790+
at_iaip = infer_type(a0id)28791+
if at_iaip == "lambda"2877128792
av = "(mrb_int)" + av28772-
elsif infer_type(a0id) == "bigint"28793+
elsif at_iaip == "bigint" || expr_emit_is_bigint(a0id) == 12877328794
@needs_bigint = 12877428795
av = "sp_bigint_to_int((sp_Bigint *)" + av + ")"28796+
elsif at_iaip == "poly"28797+
@needs_rb_value = 128798+
av = "(" + av + ").v.i"2877528799
end2877628800
end2877728801
emit(" sp_IntArray_push(" + rc + ", " + av + ");")@@ -28998,11 +29022,15 @@
2899829022
end2899929023
end2900029024
if a0id >= 029001-
if infer_type(a0id) == "lambda"29025+
at_iaip = infer_type(a0id)29026+
if at_iaip == "lambda"2900229027
av = "(mrb_int)" + av29003-
elsif infer_type(a0id) == "bigint"29028+
elsif at_iaip == "bigint" || expr_emit_is_bigint(a0id) == 12900429029
@needs_bigint = 12900529030
av = "sp_bigint_to_int((sp_Bigint *)" + av + ")"29031+
elsif at_iaip == "poly"29032+
@needs_rb_value = 129033+
av = "(" + av + ").v.i"2900629034
end2900729035
end2900829036
emit(" sp_IntArray_push(" + rc + ", " + av + ");")