Commit 41a4dd16

41a4dd16e4a3Merged

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

Description

fix(codegen): index op-assign / multi-write target unbox bigint index

compile_index_op_assign, compile_index_and_assign, compile_index_or_assign, and the IndexTargetNode arm in emit_multi_write_target all declared mrb_int <ti> = <idx>; as part of their typed-array set/get fast paths. With promote mode widening the source index expression to bigint, the init needed an sp_bigint_to_int unbox.

Snapshot the unboxed index into a local idx_int_* variable at the top of each function so the typed-array arms (float_array, int_array) use the unboxed value, while the poly-recv arm further down (which needs the boxed sp_RbVal for tag dispatch) keeps using the raw idx. Also extend compile_expr_as_int itself so other call sites get the same bigint unbox when the arg expression is bigint-typed.

Promote-mode test count: 499 pass / 43 fail / 76 error -> 501 pass / 43 fail / 74 error. Default raise mode unchanged at 618 / 0 / 0.

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

Details

Commit
41a4dd16e4a3
Parent
2d46808b2d42

spinel_codegen.rbmodifiedView file

@@ -12437,6 +12437,15 @@
1243712437 if infer_type(nid) == "poly"
1243812438 return "(" + ce + ").v.i"
1243912439 end
12440+ # `--int-overflow=promote` widens int slots to bigint; many
12441+ # int-expecting C call sites (array indices, hash keys, range
12442+ # endpoints, ...) still need a raw mrb_int. Mirror compile_arg0
12443+ # _as_int's bigint arm so promote mode flows the unbox through
12444+ # the broader expr path.
12445+ if infer_type(nid) == "bigint"
12446+ @needs_bigint = 1
12447+ return "sp_bigint_to_int((sp_Bigint *)" + ce + ")"
12448+ end
1244012449 ce
1244112450 end
1244212451
@@ -25734,11 +25743,18 @@
2573425743 rt = infer_type(tgt_recv)
2573525744 rc = compile_expr_gc_rooted(tgt_recv)
2573625745 idx = compile_expr(tgt_arg_ids[0])
25746+ # promote-mode-widened index: unbox to mrb_int for typed-array /
25747+ # int-keyed-hash recv sites that expect an int key.
25748+ idx_int_mt = idx
25749+ if infer_type(tgt_arg_ids[0]) == "bigint"
25750+ @needs_bigint = 1
25751+ idx_int_mt = "sp_bigint_to_int((sp_Bigint *)" + idx + ")"
25752+ end
2573725753 if rt == "float_array" || rt == "int_array"
2573825754 pfx = array_c_prefix(rt)
2573925755 tt = new_temp
2574025756 ti = new_temp
25741- emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +
25757+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx_int_mt +
2574225758 "; sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")
2574325759 end
2574425760 if rt == "str_int_hash"
@@ -30268,12 +30284,17 @@
3026830284 rc = compile_expr_gc_rooted(recv)
3026930285 idx = compile_expr(arg_ids[0])
3027030286 val = compile_expr(@nd_expression[nid])
30287+ idx_int_io = idx
30288+ if infer_type(arg_ids[0]) == "bigint"
30289+ @needs_bigint = 1
30290+ idx_int_io = "sp_bigint_to_int((sp_Bigint *)" + idx + ")"
30291+ end
3027130292
3027230293 if rt == "float_array" || rt == "int_array"
3027330294 pfx = array_c_prefix(rt)
3027430295 tt = new_temp
3027530296 ti = new_temp
30276- emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +
30297+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx_int_io +
3027730298 "; sp_" + pfx + "_set(" + tt + ", " + ti +
3027830299 ", sp_" + pfx + "_get(" + tt + ", " + ti + ") " + op + " (" + val + ")); }")
3027930300 return
@@ -30441,6 +30462,11 @@
3044130462 rt = infer_type(recv)
3044230463 rc = compile_expr_gc_rooted(recv)
3044330464 idx = compile_expr(arg_ids[0])
30465+ idx_int_ia = idx
30466+ if infer_type(arg_ids[0]) == "bigint"
30467+ @needs_bigint = 1
30468+ idx_int_ia = "sp_bigint_to_int((sp_Bigint *)" + idx + ")"
30469+ end
3044430470 # Note: defer compile_expr(@nd_expression[nid]) to inside each
3044530471 # if-branch so RHS side effects only fire when the get is truthy
3044630472 # (Ruby `&&=` short-circuit semantics).
@@ -30449,7 +30475,7 @@
3044930475 pfx = array_c_prefix(rt)
3045030476 tt = new_temp
3045130477 ti = new_temp
30452- emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")
30478+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx_int_ia + ";")
3045330479 emit(" if (sp_" + pfx + "_get(" + tt + ", " + ti + ")) {")
3045430480 val = compile_expr(@nd_expression[nid])
3045530481 emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));")
@@ -30506,6 +30532,11 @@
3050630532 rt = infer_type(recv)
3050730533 rc = compile_expr_gc_rooted(recv)
3050830534 idx = compile_expr(arg_ids[0])
30535+ idx_int_or = idx
30536+ if infer_type(arg_ids[0]) == "bigint"
30537+ @needs_bigint = 1
30538+ idx_int_or = "sp_bigint_to_int((sp_Bigint *)" + idx + ")"
30539+ end
3050930540 # Note: defer compile_expr(@nd_expression[nid]) to inside each
3051030541 # if-branch so RHS side effects only fire when the get is falsy
3051130542 # (Ruby `||=` short-circuit semantics).
@@ -30514,7 +30545,7 @@
3051430545 pfx = array_c_prefix(rt)
3051530546 tt = new_temp
3051630547 ti = new_temp
30517- emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")
30548+ emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx_int_or + ";")
3051830549 emit(" if (!sp_" + pfx + "_get(" + tt + ", " + ti + ")) {")
3051930550 val = compile_expr(@nd_expression[nid])
3052030551 emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));")