Commit 75cb71cb
75cb71cb48e9Merged
Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge branch 'master' into rs-block-given-amp-block
Details
- Commit
- 75cb71cb48e9
- Parent
- d9f943427aa2
- Merge parent
- 9f101b63bdce
spinel_codegen.rbmodifiedView file
@@ -3211,6 +3211,19 @@
32113211
"int"32123212
end32133213
3214+
def is_splat_with_target(nid)3215+
if nid < 03216+
return 03217+
end3218+
if @nd_type[nid] != "SplatNode"3219+
return 03220+
end3221+
if @nd_expression[nid] < 03222+
return 03223+
end3224+
13225+
end3226+
32143227
def type_is_pointer(t)32153228
if is_nullable_type(t) == 132163229
t = base_type(t)@@ -5181,11 +5194,38 @@
51815194
ek = ek + 151825195
end51835196
else5184-
at = infer_type(arg_ids[ak])5185-
if ak < ptypes.length5186-
if ptypes[ak] == "int"5187-
if at != "int"5188-
ptypes[ak] = at5197+
# SplatNode: treat the splat source's element type as5198+
# contributing to *every* fixed param from `ak` up to the5199+
# last non-rest one. So `foo(*strs)` correctly infers a5200+
# str-typed first param even though the call site has5201+
# only a single SplatNode arg.5202+
if @nd_type[arg_ids[ak]] == "SplatNode"5203+
splat_src_for_inf = @nd_expression[arg_ids[ak]]5204+
if splat_src_for_inf >= 05205+
splat_t_for_inf = infer_type(splat_src_for_inf)5206+
elem_t_for_inf = elem_type_of_array(splat_t_for_inf)5207+
if elem_t_for_inf != "int" && elem_t_for_inf != ""5208+
pi3 = ak5209+
while pi3 < ptypes.length5210+
# Don't clobber the trailing rest int_array param.5211+
if pi3 == ptypes.length - 1 && ptypes[pi3] == "int_array"5212+
pi3 = pi3 + 15213+
next5214+
end5215+
if ptypes[pi3] == "int"5216+
ptypes[pi3] = elem_t_for_inf5217+
end5218+
pi3 = pi3 + 15219+
end5220+
end5221+
end5222+
else5223+
at = infer_type(arg_ids[ak])5224+
if ak < ptypes.length5225+
if ptypes[ak] == "int"5226+
if at != "int"5227+
ptypes[ak] = at5228+
end51895229
end51905230
end51915231
end@@ -16852,6 +16892,253 @@
1685216892
"sp_box_int(" + val + ")"1685316893
end1685416894
16895+
# Emit a runtime loop that pushes every element of the array `src_expr`16896+
# (a node id whose value is some typed array) onto the destination16897+
# int_array variable `dst`. Used when expanding `*args` into a rest16898+
# parameter that will be received as sp_IntArray *.16899+
def emit_splat_into_int_array(dst, src_expr)16900+
src_t = infer_type(src_expr)16901+
src_v = compile_expr(src_expr)16902+
@needs_int_array = 116903+
@needs_gc = 116904+
if src_t == "int_array" || src_t == "sym_array"16905+
i = new_temp16906+
emit(" for (mrb_int " + i + " = 0; " + i + " < sp_IntArray_length(" + src_v + "); " + i + "++) sp_IntArray_push(" + dst + ", sp_IntArray_get(" + src_v + ", " + i + "));")16907+
return16908+
end16909+
if src_t == "str_array"16910+
@needs_str_array = 116911+
i = new_temp16912+
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 + "));")16913+
return16914+
end16915+
if src_t == "float_array"16916+
@needs_float_array = 116917+
i = new_temp16918+
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 + "));")16919+
return16920+
end16921+
if is_ptr_array_type(src_t) == 116922+
i = new_temp16923+
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 + "));")16924+
return16925+
end16926+
if src_t == "poly_array"16927+
i = new_temp16928+
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);")16929+
return16930+
end16931+
# Fallback: treat the single value as one element.16932+
emit(" sp_IntArray_push(" + dst + ", (mrb_int)" + src_v + ");")16933+
end16934+
16935+
# Read an element of a typed array as an mrb_int (so it fits int param16936+
# slots and the int_array rest bundle uniformly).16937+
def array_get_as_int_expr(src_t, src_v, idx_expr)16938+
if src_t == "int_array" || src_t == "sym_array"16939+
return "sp_IntArray_get(" + src_v + ", " + idx_expr + ")"16940+
end16941+
if src_t == "str_array"16942+
return "(mrb_int)sp_StrArray_get(" + src_v + ", " + idx_expr + ")"16943+
end16944+
if src_t == "float_array"16945+
return "(mrb_int)sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"16946+
end16947+
if is_ptr_array_type(src_t) == 116948+
return "(mrb_int)(intptr_t)sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"16949+
end16950+
if src_t == "poly_array"16951+
# Pull the int channel out of the tagged union. Lossy for non-int16952+
# tags — Spinel's *rest can only hold mrb_int today, so any non-int16953+
# element splatted into a rest param prints as raw bits.16954+
return "sp_PolyArray_get(" + src_v + ", " + idx_expr + ").v.i"16955+
end16956+
"0"16957+
end16958+
16959+
# Same as array_get_as_int_expr but returns the element in its native16960+
# C type (used when the param slot is typed, e.g. const char *).16961+
def array_get_native_expr(src_t, src_v, idx_expr)16962+
if src_t == "int_array" || src_t == "sym_array"16963+
return "sp_IntArray_get(" + src_v + ", " + idx_expr + ")"16964+
end16965+
if src_t == "str_array"16966+
return "sp_StrArray_get(" + src_v + ", " + idx_expr + ")"16967+
end16968+
if src_t == "float_array"16969+
return "sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"16970+
end16971+
if is_ptr_array_type(src_t) == 116972+
return "sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"16973+
end16974+
"0"16975+
end16976+
16977+
# Splat-aware companion to compile_call_args_with_defaults. Handles a16978+
# single SplatNode in positional args. The conceptual positional list16979+
# is (prefix... ++ splat_array ++ suffix...); fixed params eat from the16980+
# left; the rest param (if any) gets the remainder.16981+
def compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, positional_ids, splat_idx)16982+
splat_node = positional_ids[splat_idx]16983+
splat_src_id = @nd_expression[splat_node]16984+
prefix_count = splat_idx16985+
suffix_count = positional_ids.length - splat_idx - 116986+
16987+
# Pre-evaluate the splat source so we can index it twice (length and16988+
# element access) without re-evaluating side effects.16989+
src_t = "int_array"16990+
src_v = "0"16991+
if splat_src_id >= 016992+
src_t = infer_type(splat_src_id)16993+
@needs_gc = 116994+
src_tmp = new_temp16995+
emit(" " + c_type(src_t) + " " + src_tmp + " = " + compile_expr(splat_src_id) + ";")16996+
if type_is_pointer(src_t) == 116997+
emit(" SP_GC_ROOT(" + src_tmp + ");")16998+
end16999+
src_v = src_tmp17000+
end17001+
src_len_expr = length_c_expr(src_t, src_v)17002+
if src_len_expr == ""17003+
src_len_expr = "0"17004+
end17005+
17006+
# Identify if the last param is a rest int_array.17007+
method_has_rest = 017008+
if pnames.length > 017009+
if ptypes[pnames.length - 1] == "int_array"17010+
method_has_rest = 117011+
end17012+
end17013+
n_fixed = pnames.length17014+
if method_has_rest == 117015+
n_fixed = pnames.length - 117016+
end17017+
17018+
result = ""17019+
k = 017020+
while k < pnames.length17021+
if k > 017022+
result = result + ", "17023+
end17024+
17025+
# Keyword args take priority (matches non-splat path).17026+
kw_found = 017027+
ki = 017028+
while ki < kw_names.length17029+
if kw_names[ki] == pnames[k]17030+
kw_found = 117031+
if k < ptypes.length && ptypes[k] == "poly"17032+
result = result + "sp_box_str(" + kw_vals[ki] + ")"17033+
else17034+
result = result + kw_vals[ki]17035+
end17036+
end17037+
ki = ki + 117038+
end17039+
if kw_found == 117040+
k = k + 117041+
next17042+
end17043+
17044+
# Rest param: bundle leftover splat elements + suffix positionals.17045+
if k == pnames.length - 1 && method_has_rest == 117046+
@needs_int_array = 117047+
@needs_gc = 117048+
rest_tmp = new_temp17049+
emit(" sp_IntArray *" + rest_tmp + " = sp_IntArray_new();")17050+
# Prefix positionals beyond n_fixed overflow into the rest before17051+
# any splat content (e.g. take(1, 2, *xs) where take has only a17052+
# *rest param: the literals 1 and 2 must lead the bundle).17053+
po_start = n_fixed17054+
if po_start < 017055+
po_start = 017056+
end17057+
if po_start < prefix_count17058+
poi = po_start17059+
while poi < prefix_count17060+
emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(positional_ids[poi]) + ");")17061+
poi = poi + 117062+
end17063+
end17064+
consumed = n_fixed - prefix_count17065+
if consumed < 017066+
consumed = 017067+
end17068+
i_loop = new_temp17069+
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) + ");")17070+
si = 017071+
while si < suffix_count17072+
pid = positional_ids[splat_idx + 1 + si]17073+
emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(pid) + ");")17074+
si = si + 117075+
end17076+
result = result + rest_tmp17077+
k = k + 117078+
next17079+
end17080+
17081+
# Fixed param. Determine which conceptual positional it consumes.17082+
pt = "int"17083+
if k < ptypes.length17084+
pt = ptypes[k]17085+
end17086+
if k < prefix_count17087+
if pt == "poly"17088+
result = result + box_expr_to_poly(positional_ids[k])17089+
else17090+
result = result + compile_expr(positional_ids[k])17091+
end17092+
k = k + 117093+
next17094+
end17095+
# Index into the splat source.17096+
idx_in_splat = k - prefix_count17097+
# Unconsumed splat elements available for fixed params:17098+
# src_len - (positional slots after the splat that need to come17099+
# from the splat to feed remaining fixed params)17100+
# We don't know src_len statically, so we trust the caller has17101+
# provided enough — a runtime over-read returns 0/NULL via the17102+
# array's bounds clamp.17103+
slots_left_for_splat = n_fixed - prefix_count17104+
if idx_in_splat < slots_left_for_splat17105+
ge = array_get_native_expr(src_t, src_v, idx_in_splat.to_s)17106+
if pt == "poly"17107+
# The splat element itself isn't a node id, so wrap manually.17108+
result = result + ge17109+
else17110+
result = result + ge17111+
end17112+
k = k + 117113+
next17114+
end17115+
# Comes from a suffix positional (after the splat).17116+
suffix_offset = idx_in_splat - slots_left_for_splat17117+
pid_idx = splat_idx + 1 + suffix_offset17118+
if pid_idx < positional_ids.length17119+
if pt == "poly"17120+
result = result + box_expr_to_poly(positional_ids[pid_idx])17121+
else17122+
result = result + compile_expr(positional_ids[pid_idx])17123+
end17124+
else17125+
# Fall back to default if defined.17126+
if k < defaults.length17127+
def_id = defaults[k].to_i17128+
if def_id >= 017129+
result = result + compile_expr(def_id)17130+
else17131+
result = result + "0"17132+
end17133+
else17134+
result = result + "0"17135+
end17136+
end17137+
k = k + 117138+
end17139+
result17140+
end17141+
1685517142
def compile_call_args_with_defaults(nid, mi)1685617143
args_id = @nd_arguments[nid]1685717144
arg_ids = []@@ -16866,6 +17153,8 @@
1686617153
kw_names = "".split(",")1686717154
kw_vals = "".split(",")1686817155
positional_ids = []17156+
splat_idx = -117157+
splat_count_local = 01686917158
ak = 01687017159
while ak < arg_ids.length1687117160
if @nd_type[arg_ids[ak]] == "KeywordHashNode"@@ -16886,11 +17175,21 @@
1688617175
ek = ek + 11688717176
end1688817177
else17178+
if @nd_type[arg_ids[ak]] == "SplatNode"17179+
if splat_idx < 017180+
splat_idx = positional_ids.length17181+
end17182+
splat_count_local = splat_count_local + 117183+
end1688917184
positional_ids.push(arg_ids[ak])1689017185
end1689117186
ak = ak + 11689217187
end1689317188
17189+
if splat_count_local == 117190+
return compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, positional_ids, splat_idx)17191+
end17192+
1689417193
result = ""1689517194
k = 01689617195
while k < pnames.length@@ -16920,16 +17219,61 @@
1692017219
if kw_found == 01692117220
if k < ptypes.length1692217221
if ptypes[k] == "int_array"16923-
# Only splat if there are more positional args than total params16924-
# (i.e., this is a rest/splat parameter, not a regular array param)16925-
if positional_ids.length > pnames.length17222+
# Rest parameter (splat). Trigger when caller passes more17223+
# positional args than the method has params, OR when any17224+
# positional arg is itself a SplatNode that we have to expand.17225+
has_splat_arg = 017226+
si = k17227+
while si < positional_ids.length17228+
if @nd_type[positional_ids[si]] == "SplatNode"17229+
has_splat_arg = 117230+
end17231+
si = si + 117232+
end17233+
# Treat the last param as a rest target when it's the trailing17234+
# int_array slot. This covers three cases:17235+
# - extra positional args spilling in (the original heuristic)17236+
# - a SplatNode somewhere in the args17237+
# - no args supplied at all (rest-only method called bare)17238+
is_last_param = 017239+
if k == pnames.length - 117240+
is_last_param = 117241+
end17242+
treat_as_rest = 017243+
if positional_ids.length > pnames.length || has_splat_arg == 117244+
treat_as_rest = 117245+
end17246+
if is_last_param == 1 && positional_ids.length <= k17247+
treat_as_rest = 117248+
end17249+
if treat_as_rest == 117250+
# Fast path: the only positional is a splat whose source is17251+
# already an int_array. Pass it directly without copying.17252+
if has_splat_arg == 1 && positional_ids.length == k + 1 && @nd_type[positional_ids[k]] == "SplatNode"17253+
src_expr = @nd_expression[positional_ids[k]]17254+
if src_expr >= 017255+
src_t = infer_type(src_expr)17256+
if src_t == "int_array" || src_t == "sym_array"17257+
result = result + compile_expr(src_expr)17258+
k = k + 117259+
next17260+
end17261+
end17262+
end1692617263
@needs_int_array = 11692717264
@needs_gc = 11692817265
tmp = new_temp1692917266
emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")16930-
pi = 017267+
pi = k1693117268
while pi < positional_ids.length16932-
emit(" sp_IntArray_push(" + tmp + ", " + compile_expr(positional_ids[pi]) + ");")17269+
if @nd_type[positional_ids[pi]] == "SplatNode"17270+
src_expr2 = @nd_expression[positional_ids[pi]]17271+
if src_expr2 >= 017272+
emit_splat_into_int_array(tmp, src_expr2)17273+
end17274+
else17275+
emit(" sp_IntArray_push(" + tmp + ", (mrb_int)" + compile_expr(positional_ids[pi]) + ");")17276+
end1693317277
pi = pi + 11693417278
end1693517279
result = result + tmp@@ -18017,6 +18361,9 @@
1801718361
if is_ptr_array_type(rt) == 11801818362
return "sp_PtrArray_length(" + rc + ")"1801918363
end18364+
if rt == "poly_array"18365+
return "sp_PolyArray_length(" + rc + ")"18366+
end1802018367
if rt == "str_int_hash"1802118368
return "sp_StrIntHash_length(" + rc + ")"1802218369
endtest/splat_call.rbaddedView file
Loading diff…