Commit 7b272468

7b272468b4adMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Push every argument of a multi-arg Array#push / #append

Array#push and its alias #append take any number of arguments, but both the expression- and statement-form handlers compiled only the first (compile_arg0), so ["a"].append("b", "c") dropped "c". The expression-form int/float/ptr/poly pushes also returned 0 instead of the array, so a chained arr.push(x) == other compared an int.

Add compile_multi_push / emit_multi_push (with a shared push_arg_expr that coerces each argument to the element-slot type — int/lambda/bigint/ poly, const-char* snapshot for str_array, boxed for poly_array) and route all five typed-array push handlers and the statement-form arms through them. Each push now emits per argument and yields the array.

Fixes #1323

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

Details

Commit
7b272468b4ad
Parent
854e2cb8fa12

spinel_codegen.rbmodifiedView file

@@ -16547,6 +16547,72 @@
1654716547 "0"
1654816548 end
1654916549
16550+ # Coerce one push/append argument to the array's element-slot type.
16551+ def push_arg_expr(aid, pfx)
16552+ if pfx == "IntArray"
16553+ # A lambda value stored in an int_array slot round-trips through the
16554+ # integer slot; compile_expr_as_int already unboxes poly/bigint.
16555+ if infer_type(aid) == "lambda"
16556+ return "(mrb_int)" + compile_expr(aid)
16557+ end
16558+ # promote-mode: a slot whose emit produced a sp_Bigint * but whose
16559+ # infer_type still reports "int" needs the explicit unbox that
16560+ # compile_expr_as_int's infer_type check would miss.
16561+ if infer_type(aid) != "bigint" && expr_emit_is_bigint(aid) == 1
16562+ @needs_bigint = 1
16563+ return "sp_bigint_to_int((sp_Bigint *)(" + compile_expr(aid) + "))"
16564+ end
16565+ return compile_expr_as_int(aid)
16566+ end
16567+ if pfx == "StrArray"
16568+ return compile_expr_for_expected_type(aid, "string")
16569+ end
16570+ if pfx == "PolyArray"
16571+ return box_expr_to_poly(aid)
16572+ end
16573+ compile_expr(aid)
16574+ end
16575+
16576+ # Statement-context `arr.push(a, b, ...)`: emit one push per arg.
16577+ def emit_multi_push(nid, rc, pfx)
16578+ args_id = @nd_arguments[nid]
16579+ if args_id < 0
16580+ return
16581+ end
16582+ arg_ids = get_args(args_id)
16583+ k = 0
16584+ while k < arg_ids.length
16585+ emit(" sp_" + pfx + "_push(" + rc + ", " + push_arg_expr(arg_ids[k], pfx) + ");")
16586+ k = k + 1
16587+ end
16588+ end
16589+
16590+ # `arr.push(a, b, ...)` / `arr.append(...)` -- Array#push takes any
16591+ # number of arguments and returns the array. Emit one push per arg as
16592+ # a comma-expression and yield `rc`. `rc` must be a stable lvalue (a
16593+ # temp); array-literal receivers are already lifted to one by
16594+ # compile_expr_gc_rooted. Empty arg list returns the array unchanged.
16595+ def compile_multi_push(nid, rc, pfx)
16596+ args_id = @nd_arguments[nid]
16597+ if args_id < 0
16598+ return rc
16599+ end
16600+ # get_args returns an int array of node ids; do not seed arg_ids with
16601+ # a str_array (`"".split`) first -- unifying get_args' result with
16602+ # str_array widens get_args to poly across the whole self-host build.
16603+ arg_ids = get_args(args_id)
16604+ if arg_ids.length == 0
16605+ return rc
16606+ end
16607+ parts = "("
16608+ k = 0
16609+ while k < arg_ids.length
16610+ parts = parts + "sp_" + pfx + "_push(" + rc + ", " + push_arg_expr(arg_ids[k], pfx) + "), "
16611+ k = k + 1
16612+ end
16613+ parts + rc + ")"
16614+ end
16615+
1655016616 # Returns 1 if `nid` is a CallNode whose receiver AND first argument
1655116617 # are both ArrayNode literals (`[...]` or `%i[...]` / `%w[...]`).
1655216618 # Used by the int_array-vs-sym_array equality guard so that
@@ -25039,7 +25105,7 @@
2503925105 return "sp_IntArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2504025106 end
2504125107 if mname == "push"
25042- return "(sp_IntArray_push(" + rc + ", " + compile_arg0_as_int(nid) + "), 0)"
25108+ return compile_multi_push(nid, rc, "IntArray")
2504325109 end
2504425110 if mname == "pop"
2504525111 return "sp_IntArray_pop(" + rc + ")"
@@ -25516,7 +25582,7 @@
2551625582 return "sp_FloatArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2551725583 end
2551825584 if mname == "push"
25519- return "(sp_FloatArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)"
25585+ return compile_multi_push(nid, rc, "FloatArray")
2552025586 end
2552125587 if mname == "pop"
2552225588 return "sp_FloatArray_pop(" + rc + ")"
@@ -25603,7 +25669,7 @@
2560325669 return "((" + ct + ")sp_PtrArray_get(" + rc + ", sp_PtrArray_length(" + rc + ") - 1))"
2560425670 end
2560525671 if mname == "push"
25606- return "(sp_PtrArray_push(" + rc + ", " + compile_arg0(nid) + "), 0)"
25672+ return compile_multi_push(nid, rc, pfx)
2560725673 end
2560825674 if mname == "pop"
2560925675 # Array#pop on `<X>_ptr_array`. Returns NULL on empty, cast
@@ -25848,7 +25914,7 @@
2584825914 # ...) compares like an array, not like int 0. Issue #619 puzzle 9.
2584925915 # The `<<` operator path further up already does this for its
2585025916 # str_array branch.
25851- return "(sp_StrArray_push(" + rc + ", " + compile_arg0_as_cstr(nid) + "), " + rc + ")"
25917+ return compile_multi_push(nid, rc, "StrArray")
2585225918 end
2585325919 if mname == "pop"
2585425920 return "sp_StrArray_pop(" + rc + ")"
@@ -26117,12 +26183,7 @@
2611726183 return "sp_PolyArray_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
2611826184 end
2611926185 if mname == "push"
26120- arg_id = -1
26121- aargs = get_args(@nd_arguments[nid])
26122- if aargs.length > 0
26123- arg_id = aargs[0]
26124- end
26125- return "(sp_PolyArray_push(" + rc + ", " + box_expr_to_poly(arg_id) + "), 0)"
26186+ return compile_multi_push(nid, rc, "PolyArray")
2612626187 end
2612726188 # `clear` in expression position (e.g. `@x ||= @arr.clear`).
2612826189 # Mirrors the stmt-form arm in compile_*_stmt: zero the
@@ -39511,46 +39572,22 @@
3951139572 rt = infer_type(recv)
3951239573 if rt == "int_array" || rt == "sym_array"
3951339574 rc = compile_expr_gc_rooted(recv)
39514- av = compile_arg0(nid)
39515- # If pushing a lambda value, cast to mrb_int. With promote
39516- # mode, an int_array.push(bigint_lv) sees a sp_Bigint * arg
39517- # against an mrb_int param — unbox.
39518- a0id = -1
39519- args_id2 = @nd_arguments[nid]
39520- if args_id2 >= 0
39521- aargs2 = get_args(args_id2)
39522- if aargs2.length > 0
39523- a0id = aargs2[0]
39524- end
39525- end
39526- if a0id >= 0
39527- at_iaip = infer_type(a0id)
39528- if at_iaip == "lambda"
39529- av = "(mrb_int)" + av
39530- elsif at_iaip == "bigint" || expr_emit_is_bigint(a0id) == 1
39531- @needs_bigint = 1
39532- av = "sp_bigint_to_int((sp_Bigint *)" + av + ")"
39533- elsif at_iaip == "poly"
39534- @needs_rb_value = 1
39535- av = "(" + av + ").v.i"
39536- end
39537- end
39538- emit(" sp_IntArray_push(" + rc + ", " + av + ");")
39575+ emit_multi_push(nid, rc, "IntArray")
3953939576 return 1
3954039577 end
3954139578 if rt == "str_array"
3954239579 rc = compile_expr_gc_rooted(recv)
39543- emit(" sp_StrArray_push(" + rc + ", " + compile_arg0_as_cstr(nid) + ");")
39580+ emit_multi_push(nid, rc, "StrArray")
3954439581 return 1
3954539582 end
3954639583 if rt == "float_array"
3954739584 rc = compile_expr_gc_rooted(recv)
39548- emit(" sp_FloatArray_push(" + rc + ", " + compile_arg0(nid) + ");")
39585+ emit_multi_push(nid, rc, "FloatArray")
3954939586 return 1
3955039587 end
3955139588 if is_ptr_array_type(rt) == 1
3955239589 rc = compile_expr_gc_rooted(recv)
39553- emit(" sp_PtrArray_push(" + rc + ", " + compile_arg0(nid) + ");")
39590+ emit_multi_push(nid, rc, array_c_prefix(rt))
3955439591 return 1
3955539592 end
3955639593 end