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
end31763176
3177+
def is_splat_with_target(nid)3178+
if nid < 03179+
return 03180+
end3181+
if @nd_type[nid] != "SplatNode"3182+
return 03183+
end3184+
if @nd_expression[nid] < 03185+
return 03186+
end3187+
13188+
end3189+
31773190
def type_is_pointer(t)31783191
if is_nullable_type(t) == 131793192
t = base_type(t)@@ -5145,13 +5158,40 @@
51455158
ek = ek + 151465159
end51475160
else5148-
at = infer_type(arg_ids[ak])5149-
if ak < ptypes.length5150-
if ptypes[ak] == "int"5151-
if at != "int"5152-
ptypes[ak] = at5161+
# SplatNode: treat the splat source's element type as5162+
# contributing to *every* fixed param from `ak` up to the5163+
# last non-rest one. So `foo(*strs)` correctly infers a5164+
# str-typed first param even though the call site has5165+
# 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 >= 05169+
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 = ak5173+
while pi3 < ptypes.length5174+
# Don't clobber the trailing rest int_array param.5175+
if pi3 == ptypes.length - 1 && ptypes[pi3] == "int_array"5176+
pi3 = pi3 + 15177+
next5178+
end5179+
if ptypes[pi3] == "int"5180+
ptypes[pi3] = elem_t_for_inf5181+
end5182+
pi3 = pi3 + 15183+
end51535184
end51545185
end5186+
else5187+
at = infer_type(arg_ids[ak])5188+
if ak < ptypes.length5189+
if ptypes[ak] == "int"5190+
if at != "int"5191+
ptypes[ak] = at5192+
end5193+
end5194+
end51555195
end51565196
end51575197
ak = ak + 1@@ -17063,6 +17103,255 @@
1706317103
"sp_box_int(" + val + ")"1706417104
end1706517105
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 destination17108+
# int_array variable `dst`. Used when expanding `*args` into a rest17109+
# 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 = 117114+
@needs_gc = 117115+
if src_t == "int_array" || src_t == "sym_array"17116+
i = new_temp17117+
emit(" for (mrb_int " + i + " = 0; " + i + " < sp_IntArray_length(" + src_v + "); " + i + "++) sp_IntArray_push(" + dst + ", sp_IntArray_get(" + src_v + ", " + i + "));")17118+
return17119+
end17120+
if src_t == "str_array"17121+
@needs_str_array = 117122+
i = new_temp17123+
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+
return17125+
end17126+
if src_t == "float_array"17127+
@needs_float_array = 117128+
i = new_temp17129+
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+
return17131+
end17132+
if is_ptr_array_type(src_t) == 117133+
@needs_ptr_array = 117134+
i = new_temp17135+
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+
return17137+
end17138+
if src_t == "poly_array"17139+
@needs_poly_array = 117140+
i = new_temp17141+
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+
return17143+
end17144+
# Fallback: treat the single value as one element.17145+
emit(" sp_IntArray_push(" + dst + ", (mrb_int)" + src_v + ");")17146+
end17147+
17148+
# Read an element of a typed array as an mrb_int (so it fits int param17149+
# 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+
end17154+
if src_t == "str_array"17155+
return "(mrb_int)sp_StrArray_get(" + src_v + ", " + idx_expr + ")"17156+
end17157+
if src_t == "float_array"17158+
return "(mrb_int)sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"17159+
end17160+
if is_ptr_array_type(src_t) == 117161+
return "(mrb_int)(intptr_t)sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"17162+
end17163+
if src_t == "poly_array"17164+
# Pull the int channel out of the tagged union. Lossy for non-int17165+
# tags — Spinel's *rest can only hold mrb_int today, so any non-int17166+
# element splatted into a rest param prints as raw bits.17167+
return "sp_PolyArray_get(" + src_v + ", " + idx_expr + ").v.i"17168+
end17169+
"0"17170+
end17171+
17172+
# Same as array_get_as_int_expr but returns the element in its native17173+
# 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+
end17178+
if src_t == "str_array"17179+
return "sp_StrArray_get(" + src_v + ", " + idx_expr + ")"17180+
end17181+
if src_t == "float_array"17182+
return "sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"17183+
end17184+
if is_ptr_array_type(src_t) == 117185+
return "sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"17186+
end17187+
"0"17188+
end17189+
17190+
# Splat-aware companion to compile_call_args_with_defaults. Handles a17191+
# single SplatNode in positional args. The conceptual positional list17192+
# is (prefix... ++ splat_array ++ suffix...); fixed params eat from the17193+
# 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_idx17198+
suffix_count = positional_ids.length - splat_idx - 117199+
17200+
# Pre-evaluate the splat source so we can index it twice (length and17201+
# element access) without re-evaluating side effects.17202+
src_t = "int_array"17203+
src_v = "0"17204+
if splat_src_id >= 017205+
src_t = infer_type(splat_src_id)17206+
@needs_gc = 117207+
src_tmp = new_temp17208+
emit(" " + c_type(src_t) + " " + src_tmp + " = " + compile_expr(splat_src_id) + ";")17209+
if type_is_pointer(src_t) == 117210+
emit(" SP_GC_ROOT(" + src_tmp + ");")17211+
end17212+
src_v = src_tmp17213+
end17214+
src_len_expr = length_c_expr(src_t, src_v)17215+
if src_len_expr == ""17216+
src_len_expr = "0"17217+
end17218+
17219+
# Identify if the last param is a rest int_array.17220+
method_has_rest = 017221+
if pnames.length > 017222+
if ptypes[pnames.length - 1] == "int_array"17223+
method_has_rest = 117224+
end17225+
end17226+
n_fixed = pnames.length17227+
if method_has_rest == 117228+
n_fixed = pnames.length - 117229+
end17230+
17231+
result = ""17232+
k = 017233+
while k < pnames.length17234+
if k > 017235+
result = result + ", "17236+
end17237+
17238+
# Keyword args take priority (matches non-splat path).17239+
kw_found = 017240+
ki = 017241+
while ki < kw_names.length17242+
if kw_names[ki] == pnames[k]17243+
kw_found = 117244+
if k < ptypes.length && ptypes[k] == "poly"17245+
result = result + "sp_box_str(" + kw_vals[ki] + ")"17246+
else17247+
result = result + kw_vals[ki]17248+
end17249+
end17250+
ki = ki + 117251+
end17252+
if kw_found == 117253+
k = k + 117254+
next17255+
end17256+
17257+
# Rest param: bundle leftover splat elements + suffix positionals.17258+
if k == pnames.length - 1 && method_has_rest == 117259+
@needs_int_array = 117260+
@needs_gc = 117261+
rest_tmp = new_temp17262+
emit(" sp_IntArray *" + rest_tmp + " = sp_IntArray_new();")17263+
# Prefix positionals beyond n_fixed overflow into the rest before17264+
# any splat content (e.g. take(1, 2, *xs) where take has only a17265+
# *rest param: the literals 1 and 2 must lead the bundle).17266+
po_start = n_fixed17267+
if po_start < 017268+
po_start = 017269+
end17270+
if po_start < prefix_count17271+
poi = po_start17272+
while poi < prefix_count17273+
emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(positional_ids[poi]) + ");")17274+
poi = poi + 117275+
end17276+
end17277+
consumed = n_fixed - prefix_count17278+
if consumed < 017279+
consumed = 017280+
end17281+
i_loop = new_temp17282+
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 = 017284+
while si < suffix_count17285+
pid = positional_ids[splat_idx + 1 + si]17286+
emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(pid) + ");")17287+
si = si + 117288+
end17289+
result = result + rest_tmp17290+
k = k + 117291+
next17292+
end17293+
17294+
# Fixed param. Determine which conceptual positional it consumes.17295+
pt = "int"17296+
if k < ptypes.length17297+
pt = ptypes[k]17298+
end17299+
if k < prefix_count17300+
if pt == "poly"17301+
result = result + box_expr_to_poly(positional_ids[k])17302+
else17303+
result = result + compile_expr(positional_ids[k])17304+
end17305+
k = k + 117306+
next17307+
end17308+
# Index into the splat source.17309+
idx_in_splat = k - prefix_count17310+
# Unconsumed splat elements available for fixed params:17311+
# src_len - (positional slots after the splat that need to come17312+
# from the splat to feed remaining fixed params)17313+
# We don't know src_len statically, so we trust the caller has17314+
# provided enough — a runtime over-read returns 0/NULL via the17315+
# array's bounds clamp.17316+
slots_left_for_splat = n_fixed - prefix_count17317+
if idx_in_splat < slots_left_for_splat17318+
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 + ge17322+
else17323+
result = result + ge17324+
end17325+
k = k + 117326+
next17327+
end17328+
# Comes from a suffix positional (after the splat).17329+
suffix_offset = idx_in_splat - slots_left_for_splat17330+
pid_idx = splat_idx + 1 + suffix_offset17331+
if pid_idx < positional_ids.length17332+
if pt == "poly"17333+
result = result + box_expr_to_poly(positional_ids[pid_idx])17334+
else17335+
result = result + compile_expr(positional_ids[pid_idx])17336+
end17337+
else17338+
# Fall back to default if defined.17339+
if k < defaults.length17340+
def_id = defaults[k].to_i17341+
if def_id >= 017342+
result = result + compile_expr(def_id)17343+
else17344+
result = result + "0"17345+
end17346+
else17347+
result = result + "0"17348+
end17349+
end17350+
k = k + 117351+
end17352+
result17353+
end17354+
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 = -117370+
splat_count_local = 01708017371
ak = 01708117372
while ak < arg_ids.length1708217373
if @nd_type[arg_ids[ak]] == "KeywordHashNode"@@ -17097,11 +17388,21 @@
1709717388
ek = ek + 11709817389
end1709917390
else17391+
if @nd_type[arg_ids[ak]] == "SplatNode"17392+
if splat_idx < 017393+
splat_idx = positional_ids.length17394+
end17395+
splat_count_local = splat_count_local + 117396+
end1710017397
positional_ids.push(arg_ids[ak])1710117398
end1710217399
ak = ak + 11710317400
end1710417401
17402+
if splat_count_local == 117403+
return compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, positional_ids, splat_idx)17404+
end17405+
1710517406
result = ""1710617407
k = 01710717408
while k < pnames.length@@ -17131,16 +17432,61 @@
1713117432
if kw_found == 01713217433
if k < ptypes.length1713317434
if ptypes[k] == "int_array"17134-
# Only splat if there are more positional args than total params17135-
# (i.e., this is a rest/splat parameter, not a regular array param)17136-
if positional_ids.length > pnames.length17435+
# Rest parameter (splat). Trigger when caller passes more17436+
# positional args than the method has params, OR when any17437+
# positional arg is itself a SplatNode that we have to expand.17438+
has_splat_arg = 017439+
si = k17440+
while si < positional_ids.length17441+
if @nd_type[positional_ids[si]] == "SplatNode"17442+
has_splat_arg = 117443+
end17444+
si = si + 117445+
end17446+
# Treat the last param as a rest target when it's the trailing17447+
# int_array slot. This covers three cases:17448+
# - extra positional args spilling in (the original heuristic)17449+
# - a SplatNode somewhere in the args17450+
# - no args supplied at all (rest-only method called bare)17451+
is_last_param = 017452+
if k == pnames.length - 117453+
is_last_param = 117454+
end17455+
treat_as_rest = 017456+
if positional_ids.length > pnames.length || has_splat_arg == 117457+
treat_as_rest = 117458+
end17459+
if is_last_param == 1 && positional_ids.length <= k17460+
treat_as_rest = 117461+
end17462+
if treat_as_rest == 117463+
# Fast path: the only positional is a splat whose source is17464+
# 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 >= 017468+
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 + 117472+
next17473+
end17474+
end17475+
end1713717476
@needs_int_array = 11713817477
@needs_gc = 11713917478
tmp = new_temp1714017479
emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")17141-
pi = 017480+
pi = k1714217481
while pi < positional_ids.length17143-
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 >= 017485+
emit_splat_into_int_array(tmp, src_expr2)17486+
end17487+
else17488+
emit(" sp_IntArray_push(" + tmp + ", (mrb_int)" + compile_expr(positional_ids[pi]) + ");")17489+
end1714417490
pi = pi + 11714517491
end1714617492
result = result + tmp@@ -18211,6 +18557,9 @@
1821118557
if is_ptr_array_type(rt) == 11821218558
return "sp_PtrArray_length(" + rc + ")"1821318559
end18560+
if rt == "poly_array"18561+
return "sp_PolyArray_length(" + rc + ")"18562+
end1821418563
if rt == "str_int_hash"1821518564
return "sp_StrIntHash_length(" + rc + ")"1821618565
endtest/splat_call.rbaddedView file
Loading diff…