Commit b1dc3ad7

b1dc3ad75bd6Merged

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

Description

feat(codegen): unary ~ on bigint + bigint-aware emit walker for callees

Phase 1 continuation. Three companion fixes for the bit-op path:

  • ~bigint (unary bit-complement). compile_call_expr's ~ arm fell through to (~<expr>), which C rejects on sp_Bigint *. Route through the new sp_bigint_not helper when the operand's emit is bigint.
  • expr_emit_is_bigint: extend the walker to recognize user method calls whose cls_method_return resolves to bigint. The inference cache infer_type lags behind promote_int_to_bigint _globally's rewrites, so a call to obj.f whose return slot has been promoted still infers as "int" through annotate_all _node_types' fill. Cross-checking via the post-promote table catches this. Also added ~ and -@ to the walker's list.
  • compile_arg0_as_int / compile_bigint_arg / compile_body_return _inner: all now use expr_emit_is_bigint as the primary check. Mirrors the lt-widening in compile_operator_expr. Without these, int_array.push(method_returning_bigint), bigint helper args, and implicit-return arith chains all leaked bigint into int slots / wrapped bigint with another sp_bigint_new_int.
  • compile_return_stmt's maybe_bigint_coerce path: when rt is "int" but the expression's actual emit is bigint, treat rt as bigint so the wrap is skipped.

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

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

Details

Commit
b1dc3ad75bd6
Parent
dfa16a99feae

spinel_codegen.rbmodifiedView file

@@ -12889,12 +12889,15 @@
1288912889 # array indices, sp_*_get / _set, and other int-expecting C
1289012890 # helpers still need an `mrb_int` here. Unbox through
1289112891 # sp_bigint_to_int so the index is a native int even when the
12892- # source expression came from a promoted slot.
12893- if infer_type(arg_ids[0]) == "bigint"
12892+ # source expression came from a promoted slot. Cover both
12893+ # direct-type (infer_type) and stale-cache cases (a method call
12894+ # returning bigint that infer_type still reports as "int").
12895+ at_a0i = infer_type(arg_ids[0])
12896+ if at_a0i == "bigint" || expr_emit_is_bigint(arg_ids[0]) == 1
1289412897 @needs_bigint = 1
1289512898 return "sp_bigint_to_int((sp_Bigint *)" + ce + ")"
1289612899 end
12897- if infer_type(arg_ids[0]) == "poly"
12900+ if at_a0i == "poly"
1289812901 return "(" + ce + ").v.i"
1289912902 end
1290012903 return ce
@@ -15361,6 +15364,11 @@
1536115364 # method call whose return slot is now promoted). Peek operands
1536215365 # / consult the post-promote method-return tables so we don't
1536315366 # wrap an already-bigint expression with sp_bigint_new_int.
15367+ # The unified `expr_emit_is_bigint` walker covers arith, bitop,
15368+ # unary, paren, and user-method-return cases in one pass.
15369+ if at != "bigint" && expr_emit_is_bigint(arg_ids[0]) == 1
15370+ at = "bigint"
15371+ end
1536415372 if at != "bigint" && @nd_type[arg_ids[0]] == "CallNode"
1536515373 ar_mn_bg = @nd_name[arg_ids[0]]
1536615374 if ar_mn_bg == "+" || ar_mn_bg == "-" || ar_mn_bg == "*" || ar_mn_bg == "/" || ar_mn_bg == "%" || ar_mn_bg == "**"
@@ -15517,8 +15525,29 @@
1551715525 return 0
1551815526 end
1551915527 mn_eb = @nd_name[nid]
15528+ # CallNode dispatch to a user method whose return slot has been
15529+ # promoted to bigint: infer_type can be stale (the cls_method
15530+ # _return / @meth_return_types tables get rewritten by
15531+ # promote_int_to_bigint_globally after annotate_all_node_types
15532+ # populates @nd_inferred_type). Resolve the call directly to
15533+ # cross-check.
15534+ rcv_eb_user = @nd_receiver[nid]
15535+ if rcv_eb_user >= 0
15536+ rcv_t_eb_user = base_type(infer_type(rcv_eb_user))
15537+ if is_obj_type(rcv_t_eb_user) == 1
15538+ cls_eb = rcv_t_eb_user[4, rcv_t_eb_user.length - 4]
15539+ ci_eb_user = find_class_idx(cls_eb)
15540+ if ci_eb_user >= 0
15541+ ret_eb_user = cls_method_return(ci_eb_user, mn_eb)
15542+ if base_type(ret_eb_user) == "bigint"
15543+ return 1
15544+ end
15545+ end
15546+ end
15547+ end
1552015548 if mn_eb != "+" && mn_eb != "-" && mn_eb != "*" && mn_eb != "/" && mn_eb != "%" && mn_eb != "**" &&
15521- mn_eb != "&" && mn_eb != "|" && mn_eb != "^" && mn_eb != "<<" && mn_eb != ">>"
15549+ mn_eb != "&" && mn_eb != "|" && mn_eb != "^" && mn_eb != "<<" && mn_eb != ">>" &&
15550+ mn_eb != "~" && mn_eb != "-@"
1552215551 return 0
1552315552 end
1552415553 rcv_eb = @nd_receiver[nid]
@@ -15539,12 +15568,14 @@
1553915568 # Bigint operators
1554015569 lt = infer_type(recv)
1554115570 if lt != "bigint"
15542- # Check if argument is bigint
15571+ # Check if argument is bigint (direct type or via emit walker).
1554315572 args_id = @nd_arguments[nid]
1554415573 if args_id >= 0
1554515574 aargs = get_args(args_id)
15546- if aargs.length > 0 && infer_type(aargs[0]) == "bigint"
15547- lt = "bigint"
15575+ if aargs.length > 0
15576+ if infer_type(aargs[0]) == "bigint" || expr_emit_is_bigint(aargs[0]) == 1
15577+ lt = "bigint"
15578+ end
1554815579 end
1554915580 end
1555015581 end
@@ -16235,6 +16266,13 @@
1623516266 return "(" + compile_expr(recv) + " ^ " + compile_arg0_as_int(nid) + ")"
1623616267 end
1623716268 if mname == "~"
16269+ # Unary bit-complement. Promote-mode widens int recvs to bigint,
16270+ # so route through sp_bigint_not when the C emit is bigint.
16271+ if base_type(infer_type(recv)) == "bigint" || expr_emit_is_bigint(recv) == 1
16272+ @needs_bigint = 1
16273+ rc_not = compile_expr(recv)
16274+ return "sp_bigint_not((sp_Bigint *)" + rc_not + ")"
16275+ end
1623816276 return "(~" + compile_expr(recv) + ")"
1623916277 end
1624016278 if mname == "-@"
@@ -28243,9 +28281,16 @@
2824328281 ret_ctype = c_type(@current_method_return)
2824428282 # `--int-overflow=promote` widens return slots to bigint; an
2824528283 # explicit `return <int_expr>` needs sp_bigint_new_int wrap.
28246- # Reverse covers callers that still want mrb_int.
28284+ # Reverse covers callers that still want mrb_int. Use the
28285+ # stale-cache walker so a bigint-emitting arith/bitop chain
28286+ # (`return a | b | c` with bigint operands) doesn't get
28287+ # double-wrapped just because infer_type still reports "int".
2824728288 elsif (base_type(@current_method_return) == "bigint" && rt == "int") || (base_type(@current_method_return) == "int" && rt == "bigint")
28248- ret_val = maybe_bigint_coerce(ret_val, rt, @current_method_return)
28289+ rt_eff = rt
28290+ if rt == "int" && expr_emit_is_bigint(arg_ids[0]) == 1
28291+ rt_eff = "bigint"
28292+ end
28293+ ret_val = maybe_bigint_coerce(ret_val, rt_eff, @current_method_return)
2824928294 ret_ctype = c_type(@current_method_return)
2825028295 end
2825128296 if @in_gc_scope == 1
@@ -37319,10 +37364,15 @@
3731937364 end
3732037365 val = compile_expr(last)
3732137366 expr_type = infer_type(last)
37322- # Peek arith operands to upgrade a stale "int" expr_type into
37323- # bigint when the actual emit produces bigint — mirrors the
37324- # other call sites (compile_bigint_arg, compile_operator_expr,
37325- # LV op-write).
37367+ # Peek arith/bitop operands to upgrade a stale "int" expr_type
37368+ # into bigint when the actual emit produces bigint — mirrors
37369+ # the other call sites (compile_bigint_arg, compile_operator
37370+ # _expr, LV op-write). The unified `expr_emit_is_bigint` walker
37371+ # covers arith, bitop, unary `~` / `-@`, parens, and user
37372+ # method calls whose return slot was promoted.
37373+ if expr_type != "bigint" && expr_emit_is_bigint(last) == 1
37374+ expr_type = "bigint"
37375+ end
3732637376 if expr_type != "bigint" && @nd_type[last] == "CallNode"
3732737377 ml_ret = @nd_name[last]
3732837378 if ml_ret == "+" || ml_ret == "-" || ml_ret == "*" || ml_ret == "/" || ml_ret == "%" || ml_ret == "**"