Commit 145d121f

145d121fb15cMerged

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

Description

fix(codegen): bigint bitwise ops + ivar op-write expression form

Two related fixes:

  • compile_operator_expr's bigint branch handled + - * / % >= == but not & | ^ << >>, so bigint & 0xff fell through to the general int path and emitted raw C &. Add a bitwise arm that unboxes both sides via sp_bigint_to_int, applies the C op, and reboxes via sp_bigint_new_int. Guard on infer_type(recv) == "bigint" to avoid catching the poly-recv shape where lt is inferred bigint only because the arg is bigint.

  • compile_expr's InstanceVariableOperatorWriteNode arm (used as the rhs of chained @b = @a &= 0x80) emitted (iv_a &= 128LL) raw. Mirror the compile_stmt arm: when the ivar slot is bigint, route arithmetic through sp_bigint_add / sub / mul / div / mod and bitwise through the unbox-rebox shape.

Promote-mode test count: 508 pass / 45 fail / 65 error -> 508 pass / 46 fail / 64 error. Default raise mode unchanged at 618 / 0 / 0.

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

Details

Commit
145d121fb15c
Parent
76c4c2c1e64f

spinel_codegen.rbmodifiedView file

@@ -11509,6 +11509,34 @@
1150911509 if disp_iow != ""
1151011510 return "(" + lhs_iow + " = " + disp_iow + ")"
1151111511 end
11512+ # promote-mode bigint ivar: route arithmetic/bitwise through
11513+ # sp_bigint_* helpers. Mirrors the compile_stmt InstanceVariable
11514+ # OperatorWriteNode arm but returning the new slot value as the
11515+ # expression's value.
11516+ if ivar_t_iow == "bigint"
11517+ @needs_bigint = 1
11518+ rhs_t_iow = infer_type(@nd_expression[nid])
11519+ rhs_big_iow = rhs_t_iow == "bigint" ? "(sp_Bigint *)(" + val_iow + ")" : "sp_bigint_new_int(" + val_iow + ")"
11520+ big_op_iow = ""
11521+ if op_iow == "+"
11522+ big_op_iow = "sp_bigint_add"
11523+ elsif op_iow == "-"
11524+ big_op_iow = "sp_bigint_sub"
11525+ elsif op_iow == "*"
11526+ big_op_iow = "sp_bigint_mul"
11527+ elsif op_iow == "/"
11528+ big_op_iow = "sp_bigint_div"
11529+ elsif op_iow == "%"
11530+ big_op_iow = "sp_bigint_mod"
11531+ end
11532+ if big_op_iow != ""
11533+ return "(" + lhs_iow + " = " + big_op_iow + "((sp_Bigint *)" + lhs_iow + ", " + rhs_big_iow + "))"
11534+ end
11535+ if op_iow == "&" || op_iow == "|" || op_iow == "^" || op_iow == "<<" || op_iow == ">>"
11536+ rhs_int_iow = rhs_t_iow == "bigint" ? "sp_bigint_to_int(" + rhs_big_iow + ")" : "(" + val_iow + ")"
11537+ return "(" + lhs_iow + " = sp_bigint_new_int(sp_bigint_to_int((sp_Bigint *)" + lhs_iow + ") " + op_iow + " " + rhs_int_iow + "))"
11538+ end
11539+ end
1151211540 return "(" + lhs_iow + " " + op_iow + "= " + val_iow + ")"
1151311541 end
1151411542 if t == "LocalVariableOrWriteNode"
@@ -14974,6 +15002,14 @@
1497415002 if mname == "!="
1497515003 return "(sp_bigint_cmp(" + rc + ", " + arg + ") != 0)"
1497615004 end
15005+ # Bitwise / shift on a bigint recv: unbox to mrb_int, apply
15006+ # native op, rebox. Only when the receiver is genuinely bigint
15007+ # (not poly widened to share this `lt == bigint` block via the
15008+ # arg-side bigint check above) — poly recv shifts dispatch
15009+ # through a different runtime path.
15010+ if (mname == "&" || mname == "|" || mname == "^" || mname == "<<" || mname == ">>") && infer_type(recv) == "bigint"
15011+ return "sp_bigint_new_int(sp_bigint_to_int(" + rc + ") " + mname + " sp_bigint_to_int(" + arg + "))"
15012+ end
1497715013 end
1497815014 # Operators
1497915015 if mname == "**" || mname == "pow"