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 end
32123212
3213+ def is_splat_with_target(nid)
3214+ if nid < 0
3215+ return 0
3216+ end
3217+ if @nd_type[nid] != "SplatNode"
3218+ return 0
3219+ end
3220+ if @nd_expression[nid] < 0
3221+ return 0
3222+ end
3223+ 1
3224+ end
3225+
32133226 def type_is_pointer(t)
32143227 if is_nullable_type(t) == 1
32153228 t = base_type(t)
@@ -5180,13 +5193,40 @@
51805193 ek = ek + 1
51815194 end
51825195 else
5183- at = infer_type(arg_ids[ak])
5184- if ak < ptypes.length
5185- if ptypes[ak] == "int"
5186- if at != "int"
5187- ptypes[ak] = at
5196+ # SplatNode: treat the splat source's element type as
5197+ # contributing to *every* fixed param from `ak` up to the
5198+ # last non-rest one. So `foo(*strs)` correctly infers a
5199+ # str-typed first param even though the call site has
5200+ # 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 >= 0
5204+ 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 = ak
5208+ while pi3 < ptypes.length
5209+ # Don't clobber the trailing rest int_array param.
5210+ if pi3 == ptypes.length - 1 && ptypes[pi3] == "int_array"
5211+ pi3 = pi3 + 1
5212+ next
5213+ end
5214+ if ptypes[pi3] == "int"
5215+ ptypes[pi3] = elem_t_for_inf
5216+ end
5217+ pi3 = pi3 + 1
5218+ end
51885219 end
51895220 end
5221+ else
5222+ at = infer_type(arg_ids[ak])
5223+ if ak < ptypes.length
5224+ if ptypes[ak] == "int"
5225+ if at != "int"
5226+ ptypes[ak] = at
5227+ end
5228+ end
5229+ end
51905230 end
51915231 end
51925232 ak = ak + 1
@@ -16822,6 +16862,255 @@
1682216862 "sp_box_int(" + val + ")"
1682316863 end
1682416864
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 destination
16867+ # int_array variable `dst`. Used when expanding `*args` into a rest
16868+ # 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 = 1
16873+ @needs_gc = 1
16874+ if src_t == "int_array" || src_t == "sym_array"
16875+ i = new_temp
16876+ emit(" for (mrb_int " + i + " = 0; " + i + " < sp_IntArray_length(" + src_v + "); " + i + "++) sp_IntArray_push(" + dst + ", sp_IntArray_get(" + src_v + ", " + i + "));")
16877+ return
16878+ end
16879+ if src_t == "str_array"
16880+ @needs_str_array = 1
16881+ i = new_temp
16882+ 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+ return
16884+ end
16885+ if src_t == "float_array"
16886+ @needs_float_array = 1
16887+ i = new_temp
16888+ 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+ return
16890+ end
16891+ if is_ptr_array_type(src_t) == 1
16892+ @needs_ptr_array = 1
16893+ i = new_temp
16894+ 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+ return
16896+ end
16897+ if src_t == "poly_array"
16898+ @needs_poly_array = 1
16899+ i = new_temp
16900+ 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+ return
16902+ end
16903+ # Fallback: treat the single value as one element.
16904+ emit(" sp_IntArray_push(" + dst + ", (mrb_int)" + src_v + ");")
16905+ end
16906+
16907+ # Read an element of a typed array as an mrb_int (so it fits int param
16908+ # 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+ end
16913+ if src_t == "str_array"
16914+ return "(mrb_int)sp_StrArray_get(" + src_v + ", " + idx_expr + ")"
16915+ end
16916+ if src_t == "float_array"
16917+ return "(mrb_int)sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"
16918+ end
16919+ if is_ptr_array_type(src_t) == 1
16920+ return "(mrb_int)(intptr_t)sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"
16921+ end
16922+ if src_t == "poly_array"
16923+ # Pull the int channel out of the tagged union. Lossy for non-int
16924+ # tags — Spinel's *rest can only hold mrb_int today, so any non-int
16925+ # element splatted into a rest param prints as raw bits.
16926+ return "sp_PolyArray_get(" + src_v + ", " + idx_expr + ").v.i"
16927+ end
16928+ "0"
16929+ end
16930+
16931+ # Same as array_get_as_int_expr but returns the element in its native
16932+ # 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+ end
16937+ if src_t == "str_array"
16938+ return "sp_StrArray_get(" + src_v + ", " + idx_expr + ")"
16939+ end
16940+ if src_t == "float_array"
16941+ return "sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"
16942+ end
16943+ if is_ptr_array_type(src_t) == 1
16944+ return "sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"
16945+ end
16946+ "0"
16947+ end
16948+
16949+ # Splat-aware companion to compile_call_args_with_defaults. Handles a
16950+ # single SplatNode in positional args. The conceptual positional list
16951+ # is (prefix... ++ splat_array ++ suffix...); fixed params eat from the
16952+ # 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_idx
16957+ suffix_count = positional_ids.length - splat_idx - 1
16958+
16959+ # Pre-evaluate the splat source so we can index it twice (length and
16960+ # element access) without re-evaluating side effects.
16961+ src_t = "int_array"
16962+ src_v = "0"
16963+ if splat_src_id >= 0
16964+ src_t = infer_type(splat_src_id)
16965+ @needs_gc = 1
16966+ src_tmp = new_temp
16967+ emit(" " + c_type(src_t) + " " + src_tmp + " = " + compile_expr(splat_src_id) + ";")
16968+ if type_is_pointer(src_t) == 1
16969+ emit(" SP_GC_ROOT(" + src_tmp + ");")
16970+ end
16971+ src_v = src_tmp
16972+ end
16973+ src_len_expr = length_c_expr(src_t, src_v)
16974+ if src_len_expr == ""
16975+ src_len_expr = "0"
16976+ end
16977+
16978+ # Identify if the last param is a rest int_array.
16979+ method_has_rest = 0
16980+ if pnames.length > 0
16981+ if ptypes[pnames.length - 1] == "int_array"
16982+ method_has_rest = 1
16983+ end
16984+ end
16985+ n_fixed = pnames.length
16986+ if method_has_rest == 1
16987+ n_fixed = pnames.length - 1
16988+ end
16989+
16990+ result = ""
16991+ k = 0
16992+ while k < pnames.length
16993+ if k > 0
16994+ result = result + ", "
16995+ end
16996+
16997+ # Keyword args take priority (matches non-splat path).
16998+ kw_found = 0
16999+ ki = 0
17000+ while ki < kw_names.length
17001+ if kw_names[ki] == pnames[k]
17002+ kw_found = 1
17003+ if k < ptypes.length && ptypes[k] == "poly"
17004+ result = result + "sp_box_str(" + kw_vals[ki] + ")"
17005+ else
17006+ result = result + kw_vals[ki]
17007+ end
17008+ end
17009+ ki = ki + 1
17010+ end
17011+ if kw_found == 1
17012+ k = k + 1
17013+ next
17014+ end
17015+
17016+ # Rest param: bundle leftover splat elements + suffix positionals.
17017+ if k == pnames.length - 1 && method_has_rest == 1
17018+ @needs_int_array = 1
17019+ @needs_gc = 1
17020+ rest_tmp = new_temp
17021+ emit(" sp_IntArray *" + rest_tmp + " = sp_IntArray_new();")
17022+ # Prefix positionals beyond n_fixed overflow into the rest before
17023+ # any splat content (e.g. take(1, 2, *xs) where take has only a
17024+ # *rest param: the literals 1 and 2 must lead the bundle).
17025+ po_start = n_fixed
17026+ if po_start < 0
17027+ po_start = 0
17028+ end
17029+ if po_start < prefix_count
17030+ poi = po_start
17031+ while poi < prefix_count
17032+ emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(positional_ids[poi]) + ");")
17033+ poi = poi + 1
17034+ end
17035+ end
17036+ consumed = n_fixed - prefix_count
17037+ if consumed < 0
17038+ consumed = 0
17039+ end
17040+ i_loop = new_temp
17041+ 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 = 0
17043+ while si < suffix_count
17044+ pid = positional_ids[splat_idx + 1 + si]
17045+ emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(pid) + ");")
17046+ si = si + 1
17047+ end
17048+ result = result + rest_tmp
17049+ k = k + 1
17050+ next
17051+ end
17052+
17053+ # Fixed param. Determine which conceptual positional it consumes.
17054+ pt = "int"
17055+ if k < ptypes.length
17056+ pt = ptypes[k]
17057+ end
17058+ if k < prefix_count
17059+ if pt == "poly"
17060+ result = result + box_expr_to_poly(positional_ids[k])
17061+ else
17062+ result = result + compile_expr(positional_ids[k])
17063+ end
17064+ k = k + 1
17065+ next
17066+ end
17067+ # Index into the splat source.
17068+ idx_in_splat = k - prefix_count
17069+ # Unconsumed splat elements available for fixed params:
17070+ # src_len - (positional slots after the splat that need to come
17071+ # from the splat to feed remaining fixed params)
17072+ # We don't know src_len statically, so we trust the caller has
17073+ # provided enough — a runtime over-read returns 0/NULL via the
17074+ # array's bounds clamp.
17075+ slots_left_for_splat = n_fixed - prefix_count
17076+ if idx_in_splat < slots_left_for_splat
17077+ 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 + ge
17081+ else
17082+ result = result + ge
17083+ end
17084+ k = k + 1
17085+ next
17086+ end
17087+ # Comes from a suffix positional (after the splat).
17088+ suffix_offset = idx_in_splat - slots_left_for_splat
17089+ pid_idx = splat_idx + 1 + suffix_offset
17090+ if pid_idx < positional_ids.length
17091+ if pt == "poly"
17092+ result = result + box_expr_to_poly(positional_ids[pid_idx])
17093+ else
17094+ result = result + compile_expr(positional_ids[pid_idx])
17095+ end
17096+ else
17097+ # Fall back to default if defined.
17098+ if k < defaults.length
17099+ def_id = defaults[k].to_i
17100+ if def_id >= 0
17101+ result = result + compile_expr(def_id)
17102+ else
17103+ result = result + "0"
17104+ end
17105+ else
17106+ result = result + "0"
17107+ end
17108+ end
17109+ k = k + 1
17110+ end
17111+ result
17112+ end
17113+
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 = -1
17129+ splat_count_local = 0
1683917130 ak = 0
1684017131 while ak < arg_ids.length
1684117132 if @nd_type[arg_ids[ak]] == "KeywordHashNode"
@@ -16856,11 +17147,21 @@
1685617147 ek = ek + 1
1685717148 end
1685817149 else
17150+ if @nd_type[arg_ids[ak]] == "SplatNode"
17151+ if splat_idx < 0
17152+ splat_idx = positional_ids.length
17153+ end
17154+ splat_count_local = splat_count_local + 1
17155+ end
1685917156 positional_ids.push(arg_ids[ak])
1686017157 end
1686117158 ak = ak + 1
1686217159 end
1686317160
17161+ if splat_count_local == 1
17162+ return compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, positional_ids, splat_idx)
17163+ end
17164+
1686417165 result = ""
1686517166 k = 0
1686617167 while k < pnames.length
@@ -16890,16 +17191,61 @@
1689017191 if kw_found == 0
1689117192 if k < ptypes.length
1689217193 if ptypes[k] == "int_array"
16893- # Only splat if there are more positional args than total params
16894- # (i.e., this is a rest/splat parameter, not a regular array param)
16895- if positional_ids.length > pnames.length
17194+ # Rest parameter (splat). Trigger when caller passes more
17195+ # positional args than the method has params, OR when any
17196+ # positional arg is itself a SplatNode that we have to expand.
17197+ has_splat_arg = 0
17198+ si = k
17199+ while si < positional_ids.length
17200+ if @nd_type[positional_ids[si]] == "SplatNode"
17201+ has_splat_arg = 1
17202+ end
17203+ si = si + 1
17204+ end
17205+ # Treat the last param as a rest target when it's the trailing
17206+ # int_array slot. This covers three cases:
17207+ # - extra positional args spilling in (the original heuristic)
17208+ # - a SplatNode somewhere in the args
17209+ # - no args supplied at all (rest-only method called bare)
17210+ is_last_param = 0
17211+ if k == pnames.length - 1
17212+ is_last_param = 1
17213+ end
17214+ treat_as_rest = 0
17215+ if positional_ids.length > pnames.length || has_splat_arg == 1
17216+ treat_as_rest = 1
17217+ end
17218+ if is_last_param == 1 && positional_ids.length <= k
17219+ treat_as_rest = 1
17220+ end
17221+ if treat_as_rest == 1
17222+ # Fast path: the only positional is a splat whose source is
17223+ # 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 >= 0
17227+ 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 + 1
17231+ next
17232+ end
17233+ end
17234+ end
1689617235 @needs_int_array = 1
1689717236 @needs_gc = 1
1689817237 tmp = new_temp
1689917238 emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
16900- pi = 0
17239+ pi = k
1690117240 while pi < positional_ids.length
16902- 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 >= 0
17244+ emit_splat_into_int_array(tmp, src_expr2)
17245+ end
17246+ else
17247+ emit(" sp_IntArray_push(" + tmp + ", (mrb_int)" + compile_expr(positional_ids[pi]) + ");")
17248+ end
1690317249 pi = pi + 1
1690417250 end
1690517251 result = result + tmp
@@ -17987,6 +18333,9 @@
1798718333 if is_ptr_array_type(rt) == 1
1798818334 return "sp_PtrArray_length(" + rc + ")"
1798918335 end
18336+ if rt == "poly_array"
18337+ return "sp_PolyArray_length(" + rc + ")"
18338+ end
1799018339 if rt == "str_int_hash"
1799118340 return "sp_StrIntHash_length(" + rc + ")"
1799218341 end