Commit ab64a279

ab64a279d511Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #336 from mame/pr/str-poly-hash-widen-and-misc

fix(codegen): widen str_poly_hash for IndexOrWriteNode + map block re…

Details

Commit
ab64a279d511
Parent
de98a258cf3f
Merge parent
fd62cb0f0a02

spinel_codegen.rbmodifiedView file

@@ -3838,6 +3838,15 @@
38383838 if is_ptr_array_type(bret) == 1 || bret == "poly_array"
38393839 return "poly_array"
38403840 end
3841+ # Block returns a generic `poly` (e.g. `entries[key]`
3842+ # where `entries` is `str_poly_hash`) — the resulting
3843+ # array is heterogeneous, so encode as poly_array. The
3844+ # @needs_rb_value bookkeeping stays in scan-pass
3845+ # widening; this branch only types the .map result.
3846+ if bret == "poly"
3847+ @needs_rb_value = 1
3848+ return "poly_array"
3849+ end
38413850 # poly_array bret intentionally falls through. Returning
38423851 # poly_array_ptr_array would be more accurate, but ivars
38433852 # holding the result (and the corresponding `[nil] *
@@ -11318,6 +11327,49 @@
1131811327 end
1131911328 end
1132011329 end
11330+ # IndexOrWriteNode (`recv[idx] ||= val`) — scan widens the hash type
11331+ # the same way the regular `[]=` CallNode path does. Without this,
11332+ # optcarrots `entries = {}; entries[k] ||= [...]` leaves `entries`
11333+ # at the str_int_hash default, so reads return int (lost cls_id)
11334+ # and downstream `(0..N).map { entries[k] }` collapses to IntArray.
11335+ if @nd_type[nid] == "IndexOrWriteNode" || @nd_type[nid] == "IndexAndWriteNode" || @nd_type[nid] == "IndexOperatorWriteNode"
11336+ iow_recv = @nd_receiver[nid]
11337+ iow_args_id = @nd_arguments[nid]
11338+ iow_val = @nd_expression[nid]
11339+ if iow_recv >= 0 && iow_args_id >= 0 && iow_val >= 0
11340+ iow_args = get_args(iow_args_id)
11341+ if iow_args.length >= 1
11342+ iow_kt = infer_type(iow_args[0])
11343+ iow_vt = infer_type(iow_val)
11344+ # Widen the recv's hash type if it's still the empty-default
11345+ if @nd_type[iow_recv] == "InstanceVariableReadNode" && @current_class_idx >= 0
11346+ iow_iname = @nd_name[iow_recv]
11347+ iow_cur = cls_ivar_type(@current_class_idx, iow_iname)
11348+ if iow_cur == "str_int_hash"
11349+ iow_promoted = promote_empty_hash_for(iow_kt, iow_vt)
11350+ if iow_promoted != "" && iow_promoted != iow_cur
11351+ replace_ivar_type(@current_class_idx, iow_iname, iow_promoted)
11352+ if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash"
11353+ @needs_rb_value = 1
11354+ end
11355+ end
11356+ end
11357+ elsif @nd_type[iow_recv] == "LocalVariableReadNode"
11358+ iow_lname = @nd_name[iow_recv]
11359+ iow_cur = find_var_type(iow_lname)
11360+ if iow_cur == "str_int_hash"
11361+ iow_promoted = promote_empty_hash_for(iow_kt, iow_vt)
11362+ if iow_promoted != "" && iow_promoted != iow_cur
11363+ set_var_type(iow_lname, iow_promoted)
11364+ if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash"
11365+ @needs_rb_value = 1
11366+ end
11367+ end
11368+ end
11369+ end
11370+ end
11371+ end
11372+ end
1132111373 if @nd_type[nid] == "CallNode"
1132211374 mname = @nd_name[nid]
1132311375 recv = @nd_receiver[nid]
@@ -18271,6 +18323,64 @@
1827118323 # literal — concretely-typed hashes (`h = {"a" => 1}`) keep
1827218324 # their declared type even when later []= writes mix value
1827318325 # types.
18326+ # IndexOrWriteNode (`h[k] ||= v`), IndexAndWriteNode (`h[k] &&= v`),
18327+ # IndexOperatorWriteNode (`h[k] += v` etc.) — each implicitly does
18328+ # an `h[k] = ...` write, so widen the local hash type the same way
18329+ # as the regular `[]=` CallNode below. Without this, optcarrots
18330+ # `entries = {}; entries[key] ||= [...]` leaves `entries` at the
18331+ # str_int_hash default — `entries[key]` reads return mrb_int and
18332+ # the surrounding `.map { entries[key] }` collapses to IntArray,
18333+ # losing the cls_id chain on the value pointers.
18334+ if @nd_type[nid] == "IndexOrWriteNode" || @nd_type[nid] == "IndexAndWriteNode" || @nd_type[nid] == "IndexOperatorWriteNode"
18335+ iow_recv = @nd_receiver[nid]
18336+ iow_args_id = @nd_arguments[nid]
18337+ iow_val = @nd_expression[nid]
18338+ if iow_recv >= 0 && iow_args_id >= 0 && iow_val >= 0 && @nd_type[iow_recv] == "LocalVariableReadNode"
18339+ iow_hname = @nd_name[iow_recv]
18340+ iow_aargs = get_args(iow_args_id)
18341+ if iow_aargs.length >= 1
18342+ iow_ki = 0
18343+ while iow_ki < names.length
18344+ if names[iow_ki] == iow_hname
18345+ iow_cur = types[iow_ki]
18346+ iow_promotable = (iow_cur == "str_int_hash" ||
18347+ iow_cur == "str_str_hash" ||
18348+ iow_cur == "int_str_hash" ||
18349+ iow_cur == "sym_int_hash" ||
18350+ iow_cur == "sym_str_hash")
18351+ if iow_promotable && iow_ki < @scan_empty_hash_flags.length && @scan_empty_hash_flags[iow_ki] == "1"
18352+ iow_kt = scan_locals_arg_type(iow_aargs[0], names, types, params)
18353+ iow_vt = scan_locals_arg_type(iow_val, names, types, params)
18354+ iow_promoted = promote_empty_hash_for(iow_kt, iow_vt)
18355+ if iow_promoted != "" && iow_promoted != iow_cur
18356+ types[iow_ki] = iow_promoted
18357+ if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash"
18358+ @scan_empty_hash_flags[iow_ki] = ""
18359+ end
18360+ if iow_promoted == "str_str_hash"
18361+ @needs_str_str_hash = 1
18362+ elsif iow_promoted == "int_str_hash"
18363+ @needs_int_str_hash = 1
18364+ @needs_int_array = 1
18365+ elsif iow_promoted == "sym_int_hash"
18366+ @needs_sym_int_hash = 1
18367+ elsif iow_promoted == "sym_str_hash"
18368+ @needs_sym_str_hash = 1
18369+ elsif iow_promoted == "str_poly_hash"
18370+ @needs_str_poly_hash = 1
18371+ @needs_rb_value = 1
18372+ elsif iow_promoted == "sym_poly_hash"
18373+ @needs_sym_poly_hash = 1
18374+ @needs_rb_value = 1
18375+ end
18376+ end
18377+ end
18378+ end
18379+ iow_ki = iow_ki + 1
18380+ end
18381+ end
18382+ end
18383+ end
1827418384 if @nd_type[nid] == "CallNode"
1827518385 if @nd_name[nid] == "[]="
1827618386 recv = @nd_receiver[nid]
@@ -18365,6 +18475,9 @@
1836518475 end
1836618476 names.push(lname)
1836718477 types.push(elem_type)
18478+ @scan_literal_flags.push("")
18479+ @scan_empty_flags.push("")
18480+ @scan_empty_hash_flags.push("")
1836818481 end
1836918482 end
1837018483 end
@@ -18379,6 +18492,9 @@
1837918492 if not_in(lname, params) == 1
1838018493 names.push(lname)
1838118494 types.push("string")
18495+ @scan_literal_flags.push("")
18496+ @scan_empty_flags.push("")
18497+ @scan_empty_hash_flags.push("")
1838218498 end
1838318499 end
1838418500 end
@@ -18453,6 +18569,9 @@
1845318569 else
1845418570 types.push("int")
1845518571 end
18572+ @scan_literal_flags.push("")
18573+ @scan_empty_flags.push("")
18574+ @scan_empty_hash_flags.push("")
1845618575 end
1845718576 end
1845818577 nk = nk + 1
@@ -18468,6 +18587,9 @@
1846818587 if not_in(bname, names) == 1
1846918588 if not_in(bname, params) == 1
1847018589 names.push(bname)
18590+ @scan_literal_flags.push("")
18591+ @scan_empty_flags.push("")
18592+ @scan_empty_hash_flags.push("")
1847118593 # Infer type from receiver context
1847218594 recv_type = ""
1847318595 if @nd_receiver[nid] >= 0
@@ -31748,6 +31870,17 @@
3174831870 return
3174931871 end
3175031872 end
31873+ # Slice assignment with poly recv: `@bg_pixels[xfine, 8] =
31874+ # pattern_lut[bg_pattern]` against an iv typed as poly (because
31875+ # widening saw it as either an int_array or a poly_array at
31876+ # different points). Without this, the fall-through `[]=` path
31877+ # would treat arg_ids[1] (the slice length) as the value to
31878+ # store and silently drop the actual rhs. Dispatch by runtime
31879+ # cls_id and copy len elements from src to dest.
31880+ if arg_ids.length == 3 && rt == "poly"
31881+ compile_poly_slice_assign(nid, recv, rc, arg_ids)
31882+ return
31883+ end
3175131884 idx = "0"
3175231885 val = "0"
3175331886 if arg_ids.length >= 1
@@ -31859,6 +31992,48 @@
3185931992 end
3186031993 end
3186131994
31995+ # `arr[start, len] = src` where arr is a poly-typed slot (sp_RbVal).
31996+ # Dispatches by runtime cls_id of arr and src and emits a per-index
31997+ # copy. Same-length only — `src.length` must equal `len`. Falls
31998+ # back silently when src isn't an array (no copy emitted).
31999+ def compile_poly_slice_assign(nid, recv, rc, arg_ids)
32000+ @needs_rb_value = 1
32001+ start_id = arg_ids[0]
32002+ len_id = arg_ids[1]
32003+ src_id = arg_ids[2]
32004+ start_e = compile_expr(start_id)
32005+ len_e = compile_expr(len_id)
32006+ src_e = compile_expr_gc_rooted(src_id)
32007+ src_t = infer_type(src_id)
32008+ # Box src to poly so we can switch on cls_id at runtime even when
32009+ # spinel statically inferred a concrete array kind (e.g. when src
32010+ # is `pattern_lut[bg_pattern]` returning a poly value boxed from
32011+ # a typed array push).
32012+ if src_t != "poly"
32013+ src_e = box_value_to_poly(src_t, src_e)
32014+ end
32015+ rc_t = new_temp
32016+ src_t_v = new_temp
32017+ start_t = new_temp
32018+ len_t = new_temp
32019+ ii = new_temp
32020+ emit(" {")
32021+ emit(" sp_RbVal " + rc_t + " = " + rc + ";")
32022+ emit(" sp_RbVal " + src_t_v + " = " + src_e + ";")
32023+ emit(" mrb_int " + start_t + " = " + start_e + ";")
32024+ emit(" mrb_int " + len_t + " = " + len_e + ";")
32025+ emit(" if (" + rc_t + ".cls_id == SP_BUILTIN_INT_ARRAY && " + src_t_v + ".cls_id == SP_BUILTIN_INT_ARRAY) {")
32026+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < " + len_t + "; " + ii + "++) sp_IntArray_set((sp_IntArray *)" + rc_t + ".v.p, " + start_t + " + " + ii + ", sp_IntArray_get((sp_IntArray *)" + src_t_v + ".v.p, " + ii + "));")
32027+ emit(" } else if (" + rc_t + ".cls_id == SP_BUILTIN_POLY_ARRAY && " + src_t_v + ".cls_id == SP_BUILTIN_POLY_ARRAY) {")
32028+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < " + len_t + "; " + ii + "++) sp_PolyArray_set((sp_PolyArray *)" + rc_t + ".v.p, " + start_t + " + " + ii + ", sp_PolyArray_get((sp_PolyArray *)" + src_t_v + ".v.p, " + ii + "));")
32029+ emit(" } else if (" + rc_t + ".cls_id == SP_BUILTIN_INT_ARRAY && " + src_t_v + ".cls_id == SP_BUILTIN_POLY_ARRAY) {")
32030+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < " + len_t + "; " + ii + "++) sp_IntArray_set((sp_IntArray *)" + rc_t + ".v.p, " + start_t + " + " + ii + ", sp_PolyArray_get((sp_PolyArray *)" + src_t_v + ".v.p, " + ii + ").v.i);")
32031+ emit(" } else if (" + rc_t + ".cls_id == SP_BUILTIN_POLY_ARRAY && " + src_t_v + ".cls_id == SP_BUILTIN_INT_ARRAY) {")
32032+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < " + len_t + "; " + ii + "++) sp_PolyArray_set((sp_PolyArray *)" + rc_t + ".v.p, " + start_t + " + " + ii + ", sp_box_int(sp_IntArray_get((sp_IntArray *)" + src_t_v + ".v.p, " + ii + ")));")
32033+ emit(" }")
32034+ emit(" }")
32035+ end
32036+
3186232037 # Compile `arr[start, len] = src` slice assignment. Replaces
3186332038 # `len` elements of `arr` starting at `start` with the elements
3186432039 # of `src`. Same-length only — `src.length` must equal `len` at
@@ -33847,7 +34022,7 @@
3384734022 block_is_typed_array = (block_ret_r == "int_array" || block_ret_r == "str_array" ||
3384834023 block_ret_r == "float_array" || block_ret_r == "sym_array" ||
3384934024 is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array")
33850- if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float" && block_is_typed_array == false
34025+ if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float" && block_ret_r != "poly" && block_is_typed_array == false
3385134026 return "0"
3385234027 end
3385334028 @needs_int_array = 1
@@ -33859,6 +34034,12 @@
3385934034 elsif block_ret_r == "float"
3386034035 @needs_float_array = 1
3386134036 emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")
34037+ elsif block_ret_r == "poly"
34038+ # Block returns a generic poly value (e.g. `entries[key]`
34039+ # over a str_poly_hash) — accumulator must be PolyArray
34040+ # since IntArray would lose the cls_id tag on the value.
34041+ @needs_rb_value = 1
34042+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
3386234043 elsif block_is_typed_array
3386334044 # Array-of-<X>: 1D-block-return uses PtrArray (matches
3386434045 # the inferred `<bret>_ptr_array` type). Deeper nesting
@@ -33935,6 +34116,8 @@
3393534116 emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_r + ");")
3393634117 elsif block_ret_r == "float"
3393734118 emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_r + ");")
34119+ elsif block_ret_r == "poly"
34120+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + lastv_r + ");")
3393834121 elsif block_is_typed_array
3393934122 if is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array"
3394034123 emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret_r, lastv_r) + ");")