Commit 53641c1f
53641c1fdb21Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #112 from rramsden/splat-call
feat(codegen): support splat at the call site
Details
- Commit
- 53641c1fdb21
- Parent
- 9a2b3b9764dc
- Merge parent
- 20f730c00446
spinel_codegen.rbmodifiedView file
@@ -3210,6 +3210,19 @@
32103210
"int"32113211
end32123212
3213+
def is_splat_with_target(nid)3214+
if nid < 03215+
return 03216+
end3217+
if @nd_type[nid] != "SplatNode"3218+
return 03219+
end3220+
if @nd_expression[nid] < 03221+
return 03222+
end3223+
13224+
end3225+
32133226
def type_is_pointer(t)32143227
if is_nullable_type(t) == 132153228
t = base_type(t)@@ -5180,13 +5193,40 @@
51805193
ek = ek + 151815194
end51825195
else5183-
at = infer_type(arg_ids[ak])5184-
if ak < ptypes.length5185-
if ptypes[ak] == "int"5186-
if at != "int"5187-
ptypes[ak] = at5196+
# SplatNode: treat the splat source's element type as5197+
# contributing to *every* fixed param from `ak` up to the5198+
# last non-rest one. So `foo(*strs)` correctly infers a5199+
# str-typed first param even though the call site has5200+
# only a single SplatNode arg.5201+
if @nd_type[arg_ids[ak]] == "SplatNode"5202+
splat_src_for_inf = @nd_expression[arg_ids[ak]]5203+
if splat_src_for_inf >= 05204+
splat_t_for_inf = infer_type(splat_src_for_inf)5205+
elem_t_for_inf = elem_type_of_array(splat_t_for_inf)5206+
if elem_t_for_inf != "int" && elem_t_for_inf != ""5207+
pi3 = ak5208+
while pi3 < ptypes.length5209+
# Don't clobber the trailing rest int_array param.5210+
if pi3 == ptypes.length - 1 && ptypes[pi3] == "int_array"5211+
pi3 = pi3 + 15212+
next5213+
end5214+
if ptypes[pi3] == "int"5215+
ptypes[pi3] = elem_t_for_inf5216+
end5217+
pi3 = pi3 + 15218+
end51885219
end51895220
end5221+
else5222+
at = infer_type(arg_ids[ak])5223+
if ak < ptypes.length5224+
if ptypes[ak] == "int"5225+
if at != "int"5226+
ptypes[ak] = at5227+
end5228+
end5229+
end51905230
end51915231
end51925232
ak = ak + 1@@ -16822,6 +16862,255 @@
1682216862
"sp_box_int(" + val + ")"1682316863
end1682416864
16865+
# Emit a runtime loop that pushes every element of the array `src_expr`16866+
# (a node id whose value is some typed array) onto the destination16867+
# int_array variable `dst`. Used when expanding `*args` into a rest16868+
# parameter that will be received as sp_IntArray *.16869+
def emit_splat_into_int_array(dst, src_expr)16870+
src_t = infer_type(src_expr)16871+
src_v = compile_expr(src_expr)16872+
@needs_int_array = 116873+
@needs_gc = 116874+
if src_t == "int_array" || src_t == "sym_array"16875+
i = new_temp16876+
emit(" for (mrb_int " + i + " = 0; " + i + " < sp_IntArray_length(" + src_v + "); " + i + "++) sp_IntArray_push(" + dst + ", sp_IntArray_get(" + src_v + ", " + i + "));")16877+
return16878+
end16879+
if src_t == "str_array"16880+
@needs_str_array = 116881+
i = new_temp16882+
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 + "));")16883+
return16884+
end16885+
if src_t == "float_array"16886+
@needs_float_array = 116887+
i = new_temp16888+
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 + "));")16889+
return16890+
end16891+
if is_ptr_array_type(src_t) == 116892+
@needs_ptr_array = 116893+
i = new_temp16894+
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 + "));")16895+
return16896+
end16897+
if src_t == "poly_array"16898+
@needs_poly_array = 116899+
i = new_temp16900+
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);")16901+
return16902+
end16903+
# Fallback: treat the single value as one element.16904+
emit(" sp_IntArray_push(" + dst + ", (mrb_int)" + src_v + ");")16905+
end16906+
16907+
# Read an element of a typed array as an mrb_int (so it fits int param16908+
# slots and the int_array rest bundle uniformly).16909+
def array_get_as_int_expr(src_t, src_v, idx_expr)16910+
if src_t == "int_array" || src_t == "sym_array"16911+
return "sp_IntArray_get(" + src_v + ", " + idx_expr + ")"16912+
end16913+
if src_t == "str_array"16914+
return "(mrb_int)sp_StrArray_get(" + src_v + ", " + idx_expr + ")"16915+
end16916+
if src_t == "float_array"16917+
return "(mrb_int)sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"16918+
end16919+
if is_ptr_array_type(src_t) == 116920+
return "(mrb_int)(intptr_t)sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"16921+
end16922+
if src_t == "poly_array"16923+
# Pull the int channel out of the tagged union. Lossy for non-int16924+
# tags — Spinel's *rest can only hold mrb_int today, so any non-int16925+
# element splatted into a rest param prints as raw bits.16926+
return "sp_PolyArray_get(" + src_v + ", " + idx_expr + ").v.i"16927+
end16928+
"0"16929+
end16930+
16931+
# Same as array_get_as_int_expr but returns the element in its native16932+
# C type (used when the param slot is typed, e.g. const char *).16933+
def array_get_native_expr(src_t, src_v, idx_expr)16934+
if src_t == "int_array" || src_t == "sym_array"16935+
return "sp_IntArray_get(" + src_v + ", " + idx_expr + ")"16936+
end16937+
if src_t == "str_array"16938+
return "sp_StrArray_get(" + src_v + ", " + idx_expr + ")"16939+
end16940+
if src_t == "float_array"16941+
return "sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"16942+
end16943+
if is_ptr_array_type(src_t) == 116944+
return "sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"16945+
end16946+
"0"16947+
end16948+
16949+
# Splat-aware companion to compile_call_args_with_defaults. Handles a16950+
# single SplatNode in positional args. The conceptual positional list16951+
# is (prefix... ++ splat_array ++ suffix...); fixed params eat from the16952+
# left; the rest param (if any) gets the remainder.16953+
def compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, positional_ids, splat_idx)16954+
splat_node = positional_ids[splat_idx]16955+
splat_src_id = @nd_expression[splat_node]16956+
prefix_count = splat_idx16957+
suffix_count = positional_ids.length - splat_idx - 116958+
16959+
# Pre-evaluate the splat source so we can index it twice (length and16960+
# element access) without re-evaluating side effects.16961+
src_t = "int_array"16962+
src_v = "0"16963+
if splat_src_id >= 016964+
src_t = infer_type(splat_src_id)16965+
@needs_gc = 116966+
src_tmp = new_temp16967+
emit(" " + c_type(src_t) + " " + src_tmp + " = " + compile_expr(splat_src_id) + ";")16968+
if type_is_pointer(src_t) == 116969+
emit(" SP_GC_ROOT(" + src_tmp + ");")16970+
end16971+
src_v = src_tmp16972+
end16973+
src_len_expr = length_c_expr(src_t, src_v)16974+
if src_len_expr == ""16975+
src_len_expr = "0"16976+
end16977+
16978+
# Identify if the last param is a rest int_array.16979+
method_has_rest = 016980+
if pnames.length > 016981+
if ptypes[pnames.length - 1] == "int_array"16982+
method_has_rest = 116983+
end16984+
end16985+
n_fixed = pnames.length16986+
if method_has_rest == 116987+
n_fixed = pnames.length - 116988+
end16989+
16990+
result = ""16991+
k = 016992+
while k < pnames.length16993+
if k > 016994+
result = result + ", "16995+
end16996+
16997+
# Keyword args take priority (matches non-splat path).16998+
kw_found = 016999+
ki = 017000+
while ki < kw_names.length17001+
if kw_names[ki] == pnames[k]17002+
kw_found = 117003+
if k < ptypes.length && ptypes[k] == "poly"17004+
result = result + "sp_box_str(" + kw_vals[ki] + ")"17005+
else17006+
result = result + kw_vals[ki]17007+
end17008+
end17009+
ki = ki + 117010+
end17011+
if kw_found == 117012+
k = k + 117013+
next17014+
end17015+
17016+
# Rest param: bundle leftover splat elements + suffix positionals.17017+
if k == pnames.length - 1 && method_has_rest == 117018+
@needs_int_array = 117019+
@needs_gc = 117020+
rest_tmp = new_temp17021+
emit(" sp_IntArray *" + rest_tmp + " = sp_IntArray_new();")17022+
# Prefix positionals beyond n_fixed overflow into the rest before17023+
# any splat content (e.g. take(1, 2, *xs) where take has only a17024+
# *rest param: the literals 1 and 2 must lead the bundle).17025+
po_start = n_fixed17026+
if po_start < 017027+
po_start = 017028+
end17029+
if po_start < prefix_count17030+
poi = po_start17031+
while poi < prefix_count17032+
emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(positional_ids[poi]) + ");")17033+
poi = poi + 117034+
end17035+
end17036+
consumed = n_fixed - prefix_count17037+
if consumed < 017038+
consumed = 017039+
end17040+
i_loop = new_temp17041+
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) + ");")17042+
si = 017043+
while si < suffix_count17044+
pid = positional_ids[splat_idx + 1 + si]17045+
emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(pid) + ");")17046+
si = si + 117047+
end17048+
result = result + rest_tmp17049+
k = k + 117050+
next17051+
end17052+
17053+
# Fixed param. Determine which conceptual positional it consumes.17054+
pt = "int"17055+
if k < ptypes.length17056+
pt = ptypes[k]17057+
end17058+
if k < prefix_count17059+
if pt == "poly"17060+
result = result + box_expr_to_poly(positional_ids[k])17061+
else17062+
result = result + compile_expr(positional_ids[k])17063+
end17064+
k = k + 117065+
next17066+
end17067+
# Index into the splat source.17068+
idx_in_splat = k - prefix_count17069+
# Unconsumed splat elements available for fixed params:17070+
# src_len - (positional slots after the splat that need to come17071+
# from the splat to feed remaining fixed params)17072+
# We don't know src_len statically, so we trust the caller has17073+
# provided enough — a runtime over-read returns 0/NULL via the17074+
# array's bounds clamp.17075+
slots_left_for_splat = n_fixed - prefix_count17076+
if idx_in_splat < slots_left_for_splat17077+
ge = array_get_native_expr(src_t, src_v, idx_in_splat.to_s)17078+
if pt == "poly"17079+
# The splat element itself isn't a node id, so wrap manually.17080+
result = result + ge17081+
else17082+
result = result + ge17083+
end17084+
k = k + 117085+
next17086+
end17087+
# Comes from a suffix positional (after the splat).17088+
suffix_offset = idx_in_splat - slots_left_for_splat17089+
pid_idx = splat_idx + 1 + suffix_offset17090+
if pid_idx < positional_ids.length17091+
if pt == "poly"17092+
result = result + box_expr_to_poly(positional_ids[pid_idx])17093+
else17094+
result = result + compile_expr(positional_ids[pid_idx])17095+
end17096+
else17097+
# Fall back to default if defined.17098+
if k < defaults.length17099+
def_id = defaults[k].to_i17100+
if def_id >= 017101+
result = result + compile_expr(def_id)17102+
else17103+
result = result + "0"17104+
end17105+
else17106+
result = result + "0"17107+
end17108+
end17109+
k = k + 117110+
end17111+
result17112+
end17113+
1682517114
def compile_call_args_with_defaults(nid, mi)1682617115
args_id = @nd_arguments[nid]1682717116
arg_ids = []@@ -16836,6 +17125,8 @@
1683617125
kw_names = "".split(",")1683717126
kw_vals = "".split(",")1683817127
positional_ids = []17128+
splat_idx = -117129+
splat_count_local = 01683917130
ak = 01684017131
while ak < arg_ids.length1684117132
if @nd_type[arg_ids[ak]] == "KeywordHashNode"@@ -16856,11 +17147,21 @@
1685617147
ek = ek + 11685717148
end1685817149
else17150+
if @nd_type[arg_ids[ak]] == "SplatNode"17151+
if splat_idx < 017152+
splat_idx = positional_ids.length17153+
end17154+
splat_count_local = splat_count_local + 117155+
end1685917156
positional_ids.push(arg_ids[ak])1686017157
end1686117158
ak = ak + 11686217159
end1686317160
17161+
if splat_count_local == 117162+
return compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, positional_ids, splat_idx)17163+
end17164+
1686417165
result = ""1686517166
k = 01686617167
while k < pnames.length@@ -16890,16 +17191,61 @@
1689017191
if kw_found == 01689117192
if k < ptypes.length1689217193
if ptypes[k] == "int_array"16893-
# Only splat if there are more positional args than total params16894-
# (i.e., this is a rest/splat parameter, not a regular array param)16895-
if positional_ids.length > pnames.length17194+
# Rest parameter (splat). Trigger when caller passes more17195+
# positional args than the method has params, OR when any17196+
# positional arg is itself a SplatNode that we have to expand.17197+
has_splat_arg = 017198+
si = k17199+
while si < positional_ids.length17200+
if @nd_type[positional_ids[si]] == "SplatNode"17201+
has_splat_arg = 117202+
end17203+
si = si + 117204+
end17205+
# Treat the last param as a rest target when it's the trailing17206+
# int_array slot. This covers three cases:17207+
# - extra positional args spilling in (the original heuristic)17208+
# - a SplatNode somewhere in the args17209+
# - no args supplied at all (rest-only method called bare)17210+
is_last_param = 017211+
if k == pnames.length - 117212+
is_last_param = 117213+
end17214+
treat_as_rest = 017215+
if positional_ids.length > pnames.length || has_splat_arg == 117216+
treat_as_rest = 117217+
end17218+
if is_last_param == 1 && positional_ids.length <= k17219+
treat_as_rest = 117220+
end17221+
if treat_as_rest == 117222+
# Fast path: the only positional is a splat whose source is17223+
# already an int_array. Pass it directly without copying.17224+
if has_splat_arg == 1 && positional_ids.length == k + 1 && @nd_type[positional_ids[k]] == "SplatNode"17225+
src_expr = @nd_expression[positional_ids[k]]17226+
if src_expr >= 017227+
src_t = infer_type(src_expr)17228+
if src_t == "int_array" || src_t == "sym_array"17229+
result = result + compile_expr(src_expr)17230+
k = k + 117231+
next17232+
end17233+
end17234+
end1689617235
@needs_int_array = 11689717236
@needs_gc = 11689817237
tmp = new_temp1689917238
emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")16900-
pi = 017239+
pi = k1690117240
while pi < positional_ids.length16902-
emit(" sp_IntArray_push(" + tmp + ", " + compile_expr(positional_ids[pi]) + ");")17241+
if @nd_type[positional_ids[pi]] == "SplatNode"17242+
src_expr2 = @nd_expression[positional_ids[pi]]17243+
if src_expr2 >= 017244+
emit_splat_into_int_array(tmp, src_expr2)17245+
end17246+
else17247+
emit(" sp_IntArray_push(" + tmp + ", (mrb_int)" + compile_expr(positional_ids[pi]) + ");")17248+
end1690317249
pi = pi + 11690417250
end1690517251
result = result + tmp@@ -17987,6 +18333,9 @@
1798718333
if is_ptr_array_type(rt) == 11798818334
return "sp_PtrArray_length(" + rc + ")"1798918335
end18336+
if rt == "poly_array"18337+
return "sp_PolyArray_length(" + rc + ")"18338+
end1799018339
if rt == "str_int_hash"1799118340
return "sp_StrIntHash_length(" + rc + ")"1799218341
endtest/splat_call.rbaddedView file
Loading diff…