Commit 20f730c0

20f730c00446Merged

Richard Ramsden <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat: support splat at the call site

Lower SplatNode in positional args to the appropriate runtime expansion. Pure splat, prefix+splat+suffix, multi-splat, and splat into methods with fixed prefix params are all handled, and prefix-param type inference flows from the splat source's element type so head_str(*strs) types its fixed param as string. Also fixes a latent crash where def foo(*rest); foo() end emitted 0 (NULL) for the empty rest slot — bare calls to *rest methods now construct an empty sp_IntArray.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
20f730c00446
Parent
65250c0ad119

spinel_codegen.rbmodifiedView file

@@ -3174,6 +3174,19 @@
31743174 "int"
31753175 end
31763176
3177+ def is_splat_with_target(nid)
3178+ if nid < 0
3179+ return 0
3180+ end
3181+ if @nd_type[nid] != "SplatNode"
3182+ return 0
3183+ end
3184+ if @nd_expression[nid] < 0
3185+ return 0
3186+ end
3187+ 1
3188+ end
3189+
31773190 def type_is_pointer(t)
31783191 if is_nullable_type(t) == 1
31793192 t = base_type(t)
@@ -5145,13 +5158,40 @@
51455158 ek = ek + 1
51465159 end
51475160 else
5148- at = infer_type(arg_ids[ak])
5149- if ak < ptypes.length
5150- if ptypes[ak] == "int"
5151- if at != "int"
5152- ptypes[ak] = at
5161+ # SplatNode: treat the splat source's element type as
5162+ # contributing to *every* fixed param from `ak` up to the
5163+ # last non-rest one. So `foo(*strs)` correctly infers a
5164+ # str-typed first param even though the call site has
5165+ # only a single SplatNode arg.
5166+ if @nd_type[arg_ids[ak]] == "SplatNode"
5167+ splat_src_for_inf = @nd_expression[arg_ids[ak]]
5168+ if splat_src_for_inf >= 0
5169+ splat_t_for_inf = infer_type(splat_src_for_inf)
5170+ elem_t_for_inf = elem_type_of_array(splat_t_for_inf)
5171+ if elem_t_for_inf != "int" && elem_t_for_inf != ""
5172+ pi3 = ak
5173+ while pi3 < ptypes.length
5174+ # Don't clobber the trailing rest int_array param.
5175+ if pi3 == ptypes.length - 1 && ptypes[pi3] == "int_array"
5176+ pi3 = pi3 + 1
5177+ next
5178+ end
5179+ if ptypes[pi3] == "int"
5180+ ptypes[pi3] = elem_t_for_inf
5181+ end
5182+ pi3 = pi3 + 1
5183+ end
51535184 end
51545185 end
5186+ else
5187+ at = infer_type(arg_ids[ak])
5188+ if ak < ptypes.length
5189+ if ptypes[ak] == "int"
5190+ if at != "int"
5191+ ptypes[ak] = at
5192+ end
5193+ end
5194+ end
51555195 end
51565196 end
51575197 ak = ak + 1
@@ -17063,6 +17103,255 @@
1706317103 "sp_box_int(" + val + ")"
1706417104 end
1706517105
17106+ # Emit a runtime loop that pushes every element of the array `src_expr`
17107+ # (a node id whose value is some typed array) onto the destination
17108+ # int_array variable `dst`. Used when expanding `*args` into a rest
17109+ # parameter that will be received as sp_IntArray *.
17110+ def emit_splat_into_int_array(dst, src_expr)
17111+ src_t = infer_type(src_expr)
17112+ src_v = compile_expr(src_expr)
17113+ @needs_int_array = 1
17114+ @needs_gc = 1
17115+ if src_t == "int_array" || src_t == "sym_array"
17116+ i = new_temp
17117+ emit(" for (mrb_int " + i + " = 0; " + i + " < sp_IntArray_length(" + src_v + "); " + i + "++) sp_IntArray_push(" + dst + ", sp_IntArray_get(" + src_v + ", " + i + "));")
17118+ return
17119+ end
17120+ if src_t == "str_array"
17121+ @needs_str_array = 1
17122+ i = new_temp
17123+ emit(" for (mrb_int " + i + " = 0; " + i + " < sp_StrArray_length(" + src_v + "); " + i + "++) sp_IntArray_push(" + dst + ", (mrb_int)sp_StrArray_get(" + src_v + ", " + i + "));")
17124+ return
17125+ end
17126+ if src_t == "float_array"
17127+ @needs_float_array = 1
17128+ i = new_temp
17129+ emit(" for (mrb_int " + i + " = 0; " + i + " < sp_FloatArray_length(" + src_v + "); " + i + "++) sp_IntArray_push(" + dst + ", (mrb_int)sp_FloatArray_get(" + src_v + ", " + i + "));")
17130+ return
17131+ end
17132+ if is_ptr_array_type(src_t) == 1
17133+ @needs_ptr_array = 1
17134+ i = new_temp
17135+ emit(" for (mrb_int " + i + " = 0; " + i + " < sp_PtrArray_length(" + src_v + "); " + i + "++) sp_IntArray_push(" + dst + ", (mrb_int)(intptr_t)sp_PtrArray_get(" + src_v + ", " + i + "));")
17136+ return
17137+ end
17138+ if src_t == "poly_array"
17139+ @needs_poly_array = 1
17140+ i = new_temp
17141+ emit(" for (mrb_int " + i + " = 0; " + i + " < sp_PolyArray_length(" + src_v + "); " + i + "++) sp_IntArray_push(" + dst + ", sp_PolyArray_get(" + src_v + ", " + i + ").v.i);")
17142+ return
17143+ end
17144+ # Fallback: treat the single value as one element.
17145+ emit(" sp_IntArray_push(" + dst + ", (mrb_int)" + src_v + ");")
17146+ end
17147+
17148+ # Read an element of a typed array as an mrb_int (so it fits int param
17149+ # slots and the int_array rest bundle uniformly).
17150+ def array_get_as_int_expr(src_t, src_v, idx_expr)
17151+ if src_t == "int_array" || src_t == "sym_array"
17152+ return "sp_IntArray_get(" + src_v + ", " + idx_expr + ")"
17153+ end
17154+ if src_t == "str_array"
17155+ return "(mrb_int)sp_StrArray_get(" + src_v + ", " + idx_expr + ")"
17156+ end
17157+ if src_t == "float_array"
17158+ return "(mrb_int)sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"
17159+ end
17160+ if is_ptr_array_type(src_t) == 1
17161+ return "(mrb_int)(intptr_t)sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"
17162+ end
17163+ if src_t == "poly_array"
17164+ # Pull the int channel out of the tagged union. Lossy for non-int
17165+ # tags — Spinel's *rest can only hold mrb_int today, so any non-int
17166+ # element splatted into a rest param prints as raw bits.
17167+ return "sp_PolyArray_get(" + src_v + ", " + idx_expr + ").v.i"
17168+ end
17169+ "0"
17170+ end
17171+
17172+ # Same as array_get_as_int_expr but returns the element in its native
17173+ # C type (used when the param slot is typed, e.g. const char *).
17174+ def array_get_native_expr(src_t, src_v, idx_expr)
17175+ if src_t == "int_array" || src_t == "sym_array"
17176+ return "sp_IntArray_get(" + src_v + ", " + idx_expr + ")"
17177+ end
17178+ if src_t == "str_array"
17179+ return "sp_StrArray_get(" + src_v + ", " + idx_expr + ")"
17180+ end
17181+ if src_t == "float_array"
17182+ return "sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"
17183+ end
17184+ if is_ptr_array_type(src_t) == 1
17185+ return "sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"
17186+ end
17187+ "0"
17188+ end
17189+
17190+ # Splat-aware companion to compile_call_args_with_defaults. Handles a
17191+ # single SplatNode in positional args. The conceptual positional list
17192+ # is (prefix... ++ splat_array ++ suffix...); fixed params eat from the
17193+ # left; the rest param (if any) gets the remainder.
17194+ def compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, positional_ids, splat_idx)
17195+ splat_node = positional_ids[splat_idx]
17196+ splat_src_id = @nd_expression[splat_node]
17197+ prefix_count = splat_idx
17198+ suffix_count = positional_ids.length - splat_idx - 1
17199+
17200+ # Pre-evaluate the splat source so we can index it twice (length and
17201+ # element access) without re-evaluating side effects.
17202+ src_t = "int_array"
17203+ src_v = "0"
17204+ if splat_src_id >= 0
17205+ src_t = infer_type(splat_src_id)
17206+ @needs_gc = 1
17207+ src_tmp = new_temp
17208+ emit(" " + c_type(src_t) + " " + src_tmp + " = " + compile_expr(splat_src_id) + ";")
17209+ if type_is_pointer(src_t) == 1
17210+ emit(" SP_GC_ROOT(" + src_tmp + ");")
17211+ end
17212+ src_v = src_tmp
17213+ end
17214+ src_len_expr = length_c_expr(src_t, src_v)
17215+ if src_len_expr == ""
17216+ src_len_expr = "0"
17217+ end
17218+
17219+ # Identify if the last param is a rest int_array.
17220+ method_has_rest = 0
17221+ if pnames.length > 0
17222+ if ptypes[pnames.length - 1] == "int_array"
17223+ method_has_rest = 1
17224+ end
17225+ end
17226+ n_fixed = pnames.length
17227+ if method_has_rest == 1
17228+ n_fixed = pnames.length - 1
17229+ end
17230+
17231+ result = ""
17232+ k = 0
17233+ while k < pnames.length
17234+ if k > 0
17235+ result = result + ", "
17236+ end
17237+
17238+ # Keyword args take priority (matches non-splat path).
17239+ kw_found = 0
17240+ ki = 0
17241+ while ki < kw_names.length
17242+ if kw_names[ki] == pnames[k]
17243+ kw_found = 1
17244+ if k < ptypes.length && ptypes[k] == "poly"
17245+ result = result + "sp_box_str(" + kw_vals[ki] + ")"
17246+ else
17247+ result = result + kw_vals[ki]
17248+ end
17249+ end
17250+ ki = ki + 1
17251+ end
17252+ if kw_found == 1
17253+ k = k + 1
17254+ next
17255+ end
17256+
17257+ # Rest param: bundle leftover splat elements + suffix positionals.
17258+ if k == pnames.length - 1 && method_has_rest == 1
17259+ @needs_int_array = 1
17260+ @needs_gc = 1
17261+ rest_tmp = new_temp
17262+ emit(" sp_IntArray *" + rest_tmp + " = sp_IntArray_new();")
17263+ # Prefix positionals beyond n_fixed overflow into the rest before
17264+ # any splat content (e.g. take(1, 2, *xs) where take has only a
17265+ # *rest param: the literals 1 and 2 must lead the bundle).
17266+ po_start = n_fixed
17267+ if po_start < 0
17268+ po_start = 0
17269+ end
17270+ if po_start < prefix_count
17271+ poi = po_start
17272+ while poi < prefix_count
17273+ emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(positional_ids[poi]) + ");")
17274+ poi = poi + 1
17275+ end
17276+ end
17277+ consumed = n_fixed - prefix_count
17278+ if consumed < 0
17279+ consumed = 0
17280+ end
17281+ i_loop = new_temp
17282+ emit(" for (mrb_int " + i_loop + " = " + consumed.to_s + "; " + i_loop + " < " + src_len_expr + "; " + i_loop + "++) sp_IntArray_push(" + rest_tmp + ", " + array_get_as_int_expr(src_t, src_v, i_loop) + ");")
17283+ si = 0
17284+ while si < suffix_count
17285+ pid = positional_ids[splat_idx + 1 + si]
17286+ emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(pid) + ");")
17287+ si = si + 1
17288+ end
17289+ result = result + rest_tmp
17290+ k = k + 1
17291+ next
17292+ end
17293+
17294+ # Fixed param. Determine which conceptual positional it consumes.
17295+ pt = "int"
17296+ if k < ptypes.length
17297+ pt = ptypes[k]
17298+ end
17299+ if k < prefix_count
17300+ if pt == "poly"
17301+ result = result + box_expr_to_poly(positional_ids[k])
17302+ else
17303+ result = result + compile_expr(positional_ids[k])
17304+ end
17305+ k = k + 1
17306+ next
17307+ end
17308+ # Index into the splat source.
17309+ idx_in_splat = k - prefix_count
17310+ # Unconsumed splat elements available for fixed params:
17311+ # src_len - (positional slots after the splat that need to come
17312+ # from the splat to feed remaining fixed params)
17313+ # We don't know src_len statically, so we trust the caller has
17314+ # provided enough — a runtime over-read returns 0/NULL via the
17315+ # array's bounds clamp.
17316+ slots_left_for_splat = n_fixed - prefix_count
17317+ if idx_in_splat < slots_left_for_splat
17318+ ge = array_get_native_expr(src_t, src_v, idx_in_splat.to_s)
17319+ if pt == "poly"
17320+ # The splat element itself isn't a node id, so wrap manually.
17321+ result = result + ge
17322+ else
17323+ result = result + ge
17324+ end
17325+ k = k + 1
17326+ next
17327+ end
17328+ # Comes from a suffix positional (after the splat).
17329+ suffix_offset = idx_in_splat - slots_left_for_splat
17330+ pid_idx = splat_idx + 1 + suffix_offset
17331+ if pid_idx < positional_ids.length
17332+ if pt == "poly"
17333+ result = result + box_expr_to_poly(positional_ids[pid_idx])
17334+ else
17335+ result = result + compile_expr(positional_ids[pid_idx])
17336+ end
17337+ else
17338+ # Fall back to default if defined.
17339+ if k < defaults.length
17340+ def_id = defaults[k].to_i
17341+ if def_id >= 0
17342+ result = result + compile_expr(def_id)
17343+ else
17344+ result = result + "0"
17345+ end
17346+ else
17347+ result = result + "0"
17348+ end
17349+ end
17350+ k = k + 1
17351+ end
17352+ result
17353+ end
17354+
1706617355 def compile_call_args_with_defaults(nid, mi)
1706717356 args_id = @nd_arguments[nid]
1706817357 arg_ids = []
@@ -17077,6 +17366,8 @@
1707717366 kw_names = "".split(",")
1707817367 kw_vals = "".split(",")
1707917368 positional_ids = []
17369+ splat_idx = -1
17370+ splat_count_local = 0
1708017371 ak = 0
1708117372 while ak < arg_ids.length
1708217373 if @nd_type[arg_ids[ak]] == "KeywordHashNode"
@@ -17097,11 +17388,21 @@
1709717388 ek = ek + 1
1709817389 end
1709917390 else
17391+ if @nd_type[arg_ids[ak]] == "SplatNode"
17392+ if splat_idx < 0
17393+ splat_idx = positional_ids.length
17394+ end
17395+ splat_count_local = splat_count_local + 1
17396+ end
1710017397 positional_ids.push(arg_ids[ak])
1710117398 end
1710217399 ak = ak + 1
1710317400 end
1710417401
17402+ if splat_count_local == 1
17403+ return compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, positional_ids, splat_idx)
17404+ end
17405+
1710517406 result = ""
1710617407 k = 0
1710717408 while k < pnames.length
@@ -17131,16 +17432,61 @@
1713117432 if kw_found == 0
1713217433 if k < ptypes.length
1713317434 if ptypes[k] == "int_array"
17134- # Only splat if there are more positional args than total params
17135- # (i.e., this is a rest/splat parameter, not a regular array param)
17136- if positional_ids.length > pnames.length
17435+ # Rest parameter (splat). Trigger when caller passes more
17436+ # positional args than the method has params, OR when any
17437+ # positional arg is itself a SplatNode that we have to expand.
17438+ has_splat_arg = 0
17439+ si = k
17440+ while si < positional_ids.length
17441+ if @nd_type[positional_ids[si]] == "SplatNode"
17442+ has_splat_arg = 1
17443+ end
17444+ si = si + 1
17445+ end
17446+ # Treat the last param as a rest target when it's the trailing
17447+ # int_array slot. This covers three cases:
17448+ # - extra positional args spilling in (the original heuristic)
17449+ # - a SplatNode somewhere in the args
17450+ # - no args supplied at all (rest-only method called bare)
17451+ is_last_param = 0
17452+ if k == pnames.length - 1
17453+ is_last_param = 1
17454+ end
17455+ treat_as_rest = 0
17456+ if positional_ids.length > pnames.length || has_splat_arg == 1
17457+ treat_as_rest = 1
17458+ end
17459+ if is_last_param == 1 && positional_ids.length <= k
17460+ treat_as_rest = 1
17461+ end
17462+ if treat_as_rest == 1
17463+ # Fast path: the only positional is a splat whose source is
17464+ # already an int_array. Pass it directly without copying.
17465+ if has_splat_arg == 1 && positional_ids.length == k + 1 && @nd_type[positional_ids[k]] == "SplatNode"
17466+ src_expr = @nd_expression[positional_ids[k]]
17467+ if src_expr >= 0
17468+ src_t = infer_type(src_expr)
17469+ if src_t == "int_array" || src_t == "sym_array"
17470+ result = result + compile_expr(src_expr)
17471+ k = k + 1
17472+ next
17473+ end
17474+ end
17475+ end
1713717476 @needs_int_array = 1
1713817477 @needs_gc = 1
1713917478 tmp = new_temp
1714017479 emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
17141- pi = 0
17480+ pi = k
1714217481 while pi < positional_ids.length
17143- emit(" sp_IntArray_push(" + tmp + ", " + compile_expr(positional_ids[pi]) + ");")
17482+ if @nd_type[positional_ids[pi]] == "SplatNode"
17483+ src_expr2 = @nd_expression[positional_ids[pi]]
17484+ if src_expr2 >= 0
17485+ emit_splat_into_int_array(tmp, src_expr2)
17486+ end
17487+ else
17488+ emit(" sp_IntArray_push(" + tmp + ", (mrb_int)" + compile_expr(positional_ids[pi]) + ");")
17489+ end
1714417490 pi = pi + 1
1714517491 end
1714617492 result = result + tmp
@@ -18211,6 +18557,9 @@
1821118557 if is_ptr_array_type(rt) == 1
1821218558 return "sp_PtrArray_length(" + rc + ")"
1821318559 end
18560+ if rt == "poly_array"
18561+ return "sp_PolyArray_length(" + rc + ")"
18562+ end
1821418563 if rt == "str_int_hash"
1821518564 return "sp_StrIntHash_length(" + rc + ")"
1821618565 end