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
end3841+
# Block returns a generic `poly` (e.g. `entries[key]`3842+
# where `entries` is `str_poly_hash`) — the resulting3843+
# array is heterogeneous, so encode as poly_array. The3844+
# @needs_rb_value bookkeeping stays in scan-pass3845+
# widening; this branch only types the .map result.3846+
if bret == "poly"3847+
@needs_rb_value = 13848+
return "poly_array"3849+
end38413850
# poly_array bret intentionally falls through. Returning38423851
# poly_array_ptr_array would be more accurate, but ivars38433852
# holding the result (and the corresponding `[nil] *@@ -11318,6 +11327,49 @@
1131811327
end1131911328
end1132011329
end11330+
# IndexOrWriteNode (`recv[idx] ||= val`) — scan widens the hash type11331+
# 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 >= 011340+
iow_args = get_args(iow_args_id)11341+
if iow_args.length >= 111342+
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-default11345+
if @nd_type[iow_recv] == "InstanceVariableReadNode" && @current_class_idx >= 011346+
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_cur11351+
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 = 111354+
end11355+
end11356+
end11357+
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_cur11363+
set_var_type(iow_lname, iow_promoted)11364+
if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash"11365+
@needs_rb_value = 111366+
end11367+
end11368+
end11369+
end11370+
end11371+
end11372+
end1132111373
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}`) keep1827218324
# their declared type even when later []= writes mix value1827318325
# types.18326+
# IndexOrWriteNode (`h[k] ||= v`), IndexAndWriteNode (`h[k] &&= v`),18327+
# IndexOperatorWriteNode (`h[k] += v` etc.) — each implicitly does18328+
# an `h[k] = ...` write, so widen the local hash type the same way18329+
# as the regular `[]=` CallNode below. Without this, optcarrots18330+
# `entries = {}; entries[key] ||= [...]` leaves `entries` at the18331+
# str_int_hash default — `entries[key]` reads return mrb_int and18332+
# 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 >= 118342+
iow_ki = 018343+
while iow_ki < names.length18344+
if names[iow_ki] == iow_hname18345+
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_cur18356+
types[iow_ki] = iow_promoted18357+
if iow_promoted == "str_poly_hash" || iow_promoted == "sym_poly_hash"18358+
@scan_empty_hash_flags[iow_ki] = ""18359+
end18360+
if iow_promoted == "str_str_hash"18361+
@needs_str_str_hash = 118362+
elsif iow_promoted == "int_str_hash"18363+
@needs_int_str_hash = 118364+
@needs_int_array = 118365+
elsif iow_promoted == "sym_int_hash"18366+
@needs_sym_int_hash = 118367+
elsif iow_promoted == "sym_str_hash"18368+
@needs_sym_str_hash = 118369+
elsif iow_promoted == "str_poly_hash"18370+
@needs_str_poly_hash = 118371+
@needs_rb_value = 118372+
elsif iow_promoted == "sym_poly_hash"18373+
@needs_sym_poly_hash = 118374+
@needs_rb_value = 118375+
end18376+
end18377+
end18378+
end18379+
iow_ki = iow_ki + 118380+
end18381+
end18382+
end18383+
end1827418384
if @nd_type[nid] == "CallNode"1827518385
if @nd_name[nid] == "[]="1827618386
recv = @nd_receiver[nid]@@ -18365,6 +18475,9 @@
1836518475
end1836618476
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
end1836918482
end1837018483
end@@ -18379,6 +18492,9 @@
1837918492
if not_in(lname, params) == 11838018493
names.push(lname)1838118494
types.push("string")18495+
@scan_literal_flags.push("")18496+
@scan_empty_flags.push("")18497+
@scan_empty_hash_flags.push("")1838218498
end1838318499
end1838418500
end@@ -18453,6 +18569,9 @@
1845318569
else1845418570
types.push("int")1845518571
end18572+
@scan_literal_flags.push("")18573+
@scan_empty_flags.push("")18574+
@scan_empty_hash_flags.push("")1845618575
end1845718576
end1845818577
nk = nk + 1@@ -18468,6 +18587,9 @@
1846818587
if not_in(bname, names) == 11846918588
if not_in(bname, params) == 11847018589
names.push(bname)18590+
@scan_literal_flags.push("")18591+
@scan_empty_flags.push("")18592+
@scan_empty_hash_flags.push("")1847118593
# Infer type from receiver context1847218594
recv_type = ""1847318595
if @nd_receiver[nid] >= 0@@ -31748,6 +31870,17 @@
3174831870
return3174931871
end3175031872
end31873+
# Slice assignment with poly recv: `@bg_pixels[xfine, 8] =31874+
# pattern_lut[bg_pattern]` against an iv typed as poly (because31875+
# widening saw it as either an int_array or a poly_array at31876+
# different points). Without this, the fall-through `[]=` path31877+
# would treat arg_ids[1] (the slice length) as the value to31878+
# store and silently drop the actual rhs. Dispatch by runtime31879+
# 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+
return31883+
end3175131884
idx = "0"3175231885
val = "0"3175331886
if arg_ids.length >= 1@@ -31859,6 +31992,48 @@
3185931992
end3186031993
end3186131994
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-index31997+
# copy. Same-length only — `src.length` must equal `len`. Falls31998+
# 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 = 132001+
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 when32009+
# spinel statically inferred a concrete array kind (e.g. when src32010+
# is `pattern_lut[bg_pattern]` returning a poly value boxed from32011+
# a typed array push).32012+
if src_t != "poly"32013+
src_e = box_value_to_poly(src_t, src_e)32014+
end32015+
rc_t = new_temp32016+
src_t_v = new_temp32017+
start_t = new_temp32018+
len_t = new_temp32019+
ii = new_temp32020+
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+
end32036+
3186232037
# Compile `arr[start, len] = src` slice assignment. Replaces3186332038
# `len` elements of `arr` starting at `start` with the elements3186432039
# 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 == false34025+
if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float" && block_ret_r != "poly" && block_is_typed_array == false3385134026
return "0"3385234027
end3385334028
@needs_int_array = 1@@ -33859,6 +34034,12 @@
3385934034
elsif block_ret_r == "float"3386034035
@needs_float_array = 13386134036
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 PolyArray34040+
# since IntArray would lose the cls_id tag on the value.34041+
@needs_rb_value = 134042+
emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")3386234043
elsif block_is_typed_array3386334044
# Array-of-<X>: 1D-block-return uses PtrArray (matches3386434045
# 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_array3393934122
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) + ");")test/poly_slice_assign.rbaddedView file
Loading diff…
test/poly_slice_assign.rb.expectedaddedView file
Loading diff…