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 end
32133213
3214+ def is_splat_with_target(nid)
3215+ if nid < 0
3216+ return 0
3217+ end
3218+ if @nd_type[nid] != "SplatNode"
3219+ return 0
3220+ end
3221+ if @nd_expression[nid] < 0
3222+ return 0
3223+ end
3224+ 1
3225+ end
3226+
32143227 def type_is_pointer(t)
32153228 if is_nullable_type(t) == 1
32163229 t = base_type(t)
@@ -5181,11 +5194,38 @@
51815194 ek = ek + 1
51825195 end
51835196 else
5184- at = infer_type(arg_ids[ak])
5185- if ak < ptypes.length
5186- if ptypes[ak] == "int"
5187- if at != "int"
5188- ptypes[ak] = at
5197+ # SplatNode: treat the splat source's element type as
5198+ # contributing to *every* fixed param from `ak` up to the
5199+ # last non-rest one. So `foo(*strs)` correctly infers a
5200+ # str-typed first param even though the call site has
5201+ # 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 >= 0
5205+ 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 = ak
5209+ while pi3 < ptypes.length
5210+ # Don't clobber the trailing rest int_array param.
5211+ if pi3 == ptypes.length - 1 && ptypes[pi3] == "int_array"
5212+ pi3 = pi3 + 1
5213+ next
5214+ end
5215+ if ptypes[pi3] == "int"
5216+ ptypes[pi3] = elem_t_for_inf
5217+ end
5218+ pi3 = pi3 + 1
5219+ end
5220+ end
5221+ end
5222+ else
5223+ at = infer_type(arg_ids[ak])
5224+ if ak < ptypes.length
5225+ if ptypes[ak] == "int"
5226+ if at != "int"
5227+ ptypes[ak] = at
5228+ end
51895229 end
51905230 end
51915231 end
@@ -16852,6 +16892,253 @@
1685216892 "sp_box_int(" + val + ")"
1685316893 end
1685416894
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 destination
16897+ # int_array variable `dst`. Used when expanding `*args` into a rest
16898+ # 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 = 1
16903+ @needs_gc = 1
16904+ if src_t == "int_array" || src_t == "sym_array"
16905+ i = new_temp
16906+ emit(" for (mrb_int " + i + " = 0; " + i + " < sp_IntArray_length(" + src_v + "); " + i + "++) sp_IntArray_push(" + dst + ", sp_IntArray_get(" + src_v + ", " + i + "));")
16907+ return
16908+ end
16909+ if src_t == "str_array"
16910+ @needs_str_array = 1
16911+ i = new_temp
16912+ 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+ return
16914+ end
16915+ if src_t == "float_array"
16916+ @needs_float_array = 1
16917+ i = new_temp
16918+ 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+ return
16920+ end
16921+ if is_ptr_array_type(src_t) == 1
16922+ i = new_temp
16923+ 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+ return
16925+ end
16926+ if src_t == "poly_array"
16927+ i = new_temp
16928+ 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+ return
16930+ end
16931+ # Fallback: treat the single value as one element.
16932+ emit(" sp_IntArray_push(" + dst + ", (mrb_int)" + src_v + ");")
16933+ end
16934+
16935+ # Read an element of a typed array as an mrb_int (so it fits int param
16936+ # 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+ end
16941+ if src_t == "str_array"
16942+ return "(mrb_int)sp_StrArray_get(" + src_v + ", " + idx_expr + ")"
16943+ end
16944+ if src_t == "float_array"
16945+ return "(mrb_int)sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"
16946+ end
16947+ if is_ptr_array_type(src_t) == 1
16948+ return "(mrb_int)(intptr_t)sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"
16949+ end
16950+ if src_t == "poly_array"
16951+ # Pull the int channel out of the tagged union. Lossy for non-int
16952+ # tags — Spinel's *rest can only hold mrb_int today, so any non-int
16953+ # element splatted into a rest param prints as raw bits.
16954+ return "sp_PolyArray_get(" + src_v + ", " + idx_expr + ").v.i"
16955+ end
16956+ "0"
16957+ end
16958+
16959+ # Same as array_get_as_int_expr but returns the element in its native
16960+ # 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+ end
16965+ if src_t == "str_array"
16966+ return "sp_StrArray_get(" + src_v + ", " + idx_expr + ")"
16967+ end
16968+ if src_t == "float_array"
16969+ return "sp_FloatArray_get(" + src_v + ", " + idx_expr + ")"
16970+ end
16971+ if is_ptr_array_type(src_t) == 1
16972+ return "sp_PtrArray_get(" + src_v + ", " + idx_expr + ")"
16973+ end
16974+ "0"
16975+ end
16976+
16977+ # Splat-aware companion to compile_call_args_with_defaults. Handles a
16978+ # single SplatNode in positional args. The conceptual positional list
16979+ # is (prefix... ++ splat_array ++ suffix...); fixed params eat from the
16980+ # 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_idx
16985+ suffix_count = positional_ids.length - splat_idx - 1
16986+
16987+ # Pre-evaluate the splat source so we can index it twice (length and
16988+ # element access) without re-evaluating side effects.
16989+ src_t = "int_array"
16990+ src_v = "0"
16991+ if splat_src_id >= 0
16992+ src_t = infer_type(splat_src_id)
16993+ @needs_gc = 1
16994+ src_tmp = new_temp
16995+ emit(" " + c_type(src_t) + " " + src_tmp + " = " + compile_expr(splat_src_id) + ";")
16996+ if type_is_pointer(src_t) == 1
16997+ emit(" SP_GC_ROOT(" + src_tmp + ");")
16998+ end
16999+ src_v = src_tmp
17000+ end
17001+ src_len_expr = length_c_expr(src_t, src_v)
17002+ if src_len_expr == ""
17003+ src_len_expr = "0"
17004+ end
17005+
17006+ # Identify if the last param is a rest int_array.
17007+ method_has_rest = 0
17008+ if pnames.length > 0
17009+ if ptypes[pnames.length - 1] == "int_array"
17010+ method_has_rest = 1
17011+ end
17012+ end
17013+ n_fixed = pnames.length
17014+ if method_has_rest == 1
17015+ n_fixed = pnames.length - 1
17016+ end
17017+
17018+ result = ""
17019+ k = 0
17020+ while k < pnames.length
17021+ if k > 0
17022+ result = result + ", "
17023+ end
17024+
17025+ # Keyword args take priority (matches non-splat path).
17026+ kw_found = 0
17027+ ki = 0
17028+ while ki < kw_names.length
17029+ if kw_names[ki] == pnames[k]
17030+ kw_found = 1
17031+ if k < ptypes.length && ptypes[k] == "poly"
17032+ result = result + "sp_box_str(" + kw_vals[ki] + ")"
17033+ else
17034+ result = result + kw_vals[ki]
17035+ end
17036+ end
17037+ ki = ki + 1
17038+ end
17039+ if kw_found == 1
17040+ k = k + 1
17041+ next
17042+ end
17043+
17044+ # Rest param: bundle leftover splat elements + suffix positionals.
17045+ if k == pnames.length - 1 && method_has_rest == 1
17046+ @needs_int_array = 1
17047+ @needs_gc = 1
17048+ rest_tmp = new_temp
17049+ emit(" sp_IntArray *" + rest_tmp + " = sp_IntArray_new();")
17050+ # Prefix positionals beyond n_fixed overflow into the rest before
17051+ # any splat content (e.g. take(1, 2, *xs) where take has only a
17052+ # *rest param: the literals 1 and 2 must lead the bundle).
17053+ po_start = n_fixed
17054+ if po_start < 0
17055+ po_start = 0
17056+ end
17057+ if po_start < prefix_count
17058+ poi = po_start
17059+ while poi < prefix_count
17060+ emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(positional_ids[poi]) + ");")
17061+ poi = poi + 1
17062+ end
17063+ end
17064+ consumed = n_fixed - prefix_count
17065+ if consumed < 0
17066+ consumed = 0
17067+ end
17068+ i_loop = new_temp
17069+ 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 = 0
17071+ while si < suffix_count
17072+ pid = positional_ids[splat_idx + 1 + si]
17073+ emit(" sp_IntArray_push(" + rest_tmp + ", (mrb_int)" + compile_expr(pid) + ");")
17074+ si = si + 1
17075+ end
17076+ result = result + rest_tmp
17077+ k = k + 1
17078+ next
17079+ end
17080+
17081+ # Fixed param. Determine which conceptual positional it consumes.
17082+ pt = "int"
17083+ if k < ptypes.length
17084+ pt = ptypes[k]
17085+ end
17086+ if k < prefix_count
17087+ if pt == "poly"
17088+ result = result + box_expr_to_poly(positional_ids[k])
17089+ else
17090+ result = result + compile_expr(positional_ids[k])
17091+ end
17092+ k = k + 1
17093+ next
17094+ end
17095+ # Index into the splat source.
17096+ idx_in_splat = k - prefix_count
17097+ # Unconsumed splat elements available for fixed params:
17098+ # src_len - (positional slots after the splat that need to come
17099+ # from the splat to feed remaining fixed params)
17100+ # We don't know src_len statically, so we trust the caller has
17101+ # provided enough — a runtime over-read returns 0/NULL via the
17102+ # array's bounds clamp.
17103+ slots_left_for_splat = n_fixed - prefix_count
17104+ if idx_in_splat < slots_left_for_splat
17105+ 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 + ge
17109+ else
17110+ result = result + ge
17111+ end
17112+ k = k + 1
17113+ next
17114+ end
17115+ # Comes from a suffix positional (after the splat).
17116+ suffix_offset = idx_in_splat - slots_left_for_splat
17117+ pid_idx = splat_idx + 1 + suffix_offset
17118+ if pid_idx < positional_ids.length
17119+ if pt == "poly"
17120+ result = result + box_expr_to_poly(positional_ids[pid_idx])
17121+ else
17122+ result = result + compile_expr(positional_ids[pid_idx])
17123+ end
17124+ else
17125+ # Fall back to default if defined.
17126+ if k < defaults.length
17127+ def_id = defaults[k].to_i
17128+ if def_id >= 0
17129+ result = result + compile_expr(def_id)
17130+ else
17131+ result = result + "0"
17132+ end
17133+ else
17134+ result = result + "0"
17135+ end
17136+ end
17137+ k = k + 1
17138+ end
17139+ result
17140+ end
17141+
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 = -1
17157+ splat_count_local = 0
1686917158 ak = 0
1687017159 while ak < arg_ids.length
1687117160 if @nd_type[arg_ids[ak]] == "KeywordHashNode"
@@ -16886,11 +17175,21 @@
1688617175 ek = ek + 1
1688717176 end
1688817177 else
17178+ if @nd_type[arg_ids[ak]] == "SplatNode"
17179+ if splat_idx < 0
17180+ splat_idx = positional_ids.length
17181+ end
17182+ splat_count_local = splat_count_local + 1
17183+ end
1688917184 positional_ids.push(arg_ids[ak])
1689017185 end
1689117186 ak = ak + 1
1689217187 end
1689317188
17189+ if splat_count_local == 1
17190+ return compile_call_args_splat(nid, mi, pnames, ptypes, defaults, kw_names, kw_vals, positional_ids, splat_idx)
17191+ end
17192+
1689417193 result = ""
1689517194 k = 0
1689617195 while k < pnames.length
@@ -16920,16 +17219,61 @@
1692017219 if kw_found == 0
1692117220 if k < ptypes.length
1692217221 if ptypes[k] == "int_array"
16923- # Only splat if there are more positional args than total params
16924- # (i.e., this is a rest/splat parameter, not a regular array param)
16925- if positional_ids.length > pnames.length
17222+ # Rest parameter (splat). Trigger when caller passes more
17223+ # positional args than the method has params, OR when any
17224+ # positional arg is itself a SplatNode that we have to expand.
17225+ has_splat_arg = 0
17226+ si = k
17227+ while si < positional_ids.length
17228+ if @nd_type[positional_ids[si]] == "SplatNode"
17229+ has_splat_arg = 1
17230+ end
17231+ si = si + 1
17232+ end
17233+ # Treat the last param as a rest target when it's the trailing
17234+ # int_array slot. This covers three cases:
17235+ # - extra positional args spilling in (the original heuristic)
17236+ # - a SplatNode somewhere in the args
17237+ # - no args supplied at all (rest-only method called bare)
17238+ is_last_param = 0
17239+ if k == pnames.length - 1
17240+ is_last_param = 1
17241+ end
17242+ treat_as_rest = 0
17243+ if positional_ids.length > pnames.length || has_splat_arg == 1
17244+ treat_as_rest = 1
17245+ end
17246+ if is_last_param == 1 && positional_ids.length <= k
17247+ treat_as_rest = 1
17248+ end
17249+ if treat_as_rest == 1
17250+ # Fast path: the only positional is a splat whose source is
17251+ # 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 >= 0
17255+ 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 + 1
17259+ next
17260+ end
17261+ end
17262+ end
1692617263 @needs_int_array = 1
1692717264 @needs_gc = 1
1692817265 tmp = new_temp
1692917266 emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
16930- pi = 0
17267+ pi = k
1693117268 while pi < positional_ids.length
16932- 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 >= 0
17272+ emit_splat_into_int_array(tmp, src_expr2)
17273+ end
17274+ else
17275+ emit(" sp_IntArray_push(" + tmp + ", (mrb_int)" + compile_expr(positional_ids[pi]) + ");")
17276+ end
1693317277 pi = pi + 1
1693417278 end
1693517279 result = result + tmp
@@ -18017,6 +18361,9 @@
1801718361 if is_ptr_array_type(rt) == 1
1801818362 return "sp_PtrArray_length(" + rc + ")"
1801918363 end
18364+ if rt == "poly_array"
18365+ return "sp_PolyArray_length(" + rc + ")"
18366+ end
1802018367 if rt == "str_int_hash"
1802118368 return "sp_StrIntHash_length(" + rc + ")"
1802218369 end