Commit f23a36e0
f23a36e09ea5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #241 from mame/pr/ptr-array-set
feat(codegen): ptr_array support for [nil] * N + heterogeneous obj writes
Details
- Commit
- f23a36e09ea5
- Parent
- 31f8a0b858c1
- Merge parent
- 631e29b0589e
spinel_codegen.rbmodifiedView file
@@ -4267,6 +4267,40 @@
42674267
042684268
end42694269
4270+
# `[nil] * N` / `[0] * N` is a sized empty default — the elements4271+
# come from `nil`/`0`, so the array's effective element type is the4272+
# same as `[]`'s default (int_array). Used by writer-scan and the4273+
# expected-type-aware compile path so a later typed `arr[i] = obj`4274+
# write can promote / direct-allocate the right kind of container.4275+
def is_sized_empty_array_default(nid)4276+
if nid < 04277+
return 04278+
end4279+
if @nd_type[nid] != "CallNode" || @nd_name[nid] != "*"4280+
return 04281+
end4282+
arr_recv = @nd_receiver[nid]4283+
if arr_recv < 0 || @nd_type[arr_recv] != "ArrayNode"4284+
return 04285+
end4286+
recv_elems = parse_id_list(@nd_elements[arr_recv])4287+
if recv_elems.length == 04288+
return 04289+
end4290+
ei = 04291+
while ei < recv_elems.length4292+
ti = recv_elems[ei]4293+
if ti < 04294+
return 04295+
end4296+
if @nd_type[ti] != "NilNode" && @nd_type[ti] != "IntegerNode"4297+
return 04298+
end4299+
ei = ei + 14300+
end4301+
14302+
end4303+
42704304
def base_type(t)42714305
if t.length > 1 && t[t.length - 1] == "?"42724306
return t[0, t.length - 1]@@ -6243,6 +6277,15 @@
62436277
@cls_ivar_types[ci] = types.join(";")62446278
elsif old != new_type && old != "poly"62456279
if is_array_type(old) == 1 && is_array_type(new_type) == 16280+
# Don't widen a typed `<obj>_ptr_array` slot back to6281+
# poly when the disagreement is just `int_array`. That6282+
# disagreement comes from the `[nil] * N` empty default6283+
# — the writer-scan `[]=` / `<<` widening is more6284+
# specific and should win.6285+
if is_ptr_array_type(old) == 1 && new_type == "int_array"6286+
k = k + 16287+
next6288+
end62466289
types[k] = "poly_array"62476290
@needs_rb_value = 162486291
@cls_ivar_types[ci] = types.join(";")@@ -7395,6 +7438,25 @@
73957438
return val73967439
end73977440
end7441+
# `[nil] * N` / `[0] * N` against a pointer-array LHS: build a7442+
# fresh ptr_array pre-filled with N NULLs so subsequent7443+
# `arr[i] = obj` writes via sp_PtrArray_set land in valid slots.7444+
# Without this, the default `*` codegen produces an int_array7445+
# which doesn't match the LHS storage.7446+
if (is_ptr_array_type(expected_base) == 1 || expected_base == "poly_array") && is_sized_empty_array_default(nid) == 17447+
@needs_gc = 17448+
cnt_e = compile_arg0(nid)7449+
tmp = new_temp7450+
if expected_base == "poly_array"7451+
@needs_rb_value = 17452+
emit(" sp_PolyArray *" + tmp + " = sp_PolyArray_new();")7453+
emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp + ", sp_box_nil()); }")7454+
else7455+
emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")7456+
emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp + ", NULL); }")7457+
end7458+
return tmp7459+
end73987460
val = compile_expr(nid)73997461
at = infer_type(nid)74007462
if expected_base == "poly" && at != "poly"@@ -8554,8 +8616,11 @@
85548616
return "poly_array"85558617
end85568618
if is_obj_type(elem_acc[0]) == 18557-
@needs_rb_value = 18558-
return "poly_array"8619+
# Homogeneous obj array — use a typed `<obj>_ptr_array` so reads8620+
# return a typed pointer (no sp_RbVal unbox needed at the call8621+
# site). Falls back to poly_array via normal widening when a8622+
# later mismatched-type write happens.8623+
return base_type(elem_acc[0]) + "_ptr_array"85598624
end85608625
""85618626
end@@ -9003,6 +9068,39 @@
90039068
end90049069
end90059070
end9071+
# `@arr[i] = v` against an ivar typed `int_array` (the9072+
# `[nil] * N` empty default) should widen to match the value9073+
# type the same way `<<` / `push` does. Without this, optcarrot's9074+
# `@fetch[addr] = method(:peek_X)` writes a Method into an9075+
# int_array slot — the read side then can't recover and `[].call`9076+
# dispatches against int.9077+
if mname == "[]=" && @current_class_idx >= 0 && recv >= 0 && @nd_type[recv] == "InstanceVariableReadNode"9078+
iname = @nd_name[recv]9079+
cur_t = cls_ivar_type(@current_class_idx, iname)9080+
if cur_t == "int_array"9081+
args_id = @nd_arguments[nid]9082+
if args_id >= 09083+
ai = get_args(args_id)9084+
if ai.length >= 29085+
vt = infer_type(ai[ai.length - 1])9086+
promoted = empty_array_promotion_for([vt])9087+
if promoted != "" && promoted != cur_t9088+
replace_ivar_type(@current_class_idx, iname, promoted)9089+
if promoted == "str_array"9090+
@needs_str_array = 19091+
elsif promoted == "float_array"9092+
@needs_float_array = 19093+
elsif promoted == "sym_array"9094+
@needs_int_array = 19095+
elsif promoted == "poly_array"9096+
@needs_rb_value = 19097+
@needs_gc = 19098+
end9099+
end9100+
end9101+
end9102+
end9103+
end90069104
end90079105
# Recurse via the centralized child walker (push_child_ids covers90089106
# the full set of AST slots — visiting a few extra slots is a@@ -14193,6 +14291,17 @@
1419314291
if iv_ctor != ""1419414292
@needs_gc = 11419514293
emit_raw(" " + self_arrow + ivar + " = " + iv_ctor + ";")14294+
elsif is_ptr_array_type(ivt) == 1 && is_sized_empty_array_default(expr_id_iv) == 114295+
# `@arr = [nil] * N` with @arr already widened to14296+
# ptr_array via writer-scan. The default `*` codegen14297+
# produces an sp_IntArray that mismatches the slot14298+
# type; emit a sized PtrArray of NULLs inline.14299+
@needs_gc = 114300+
cnt_e_iv = compile_arg0(expr_id_iv)14301+
tmp_iv = new_temp14302+
emit_raw(" sp_PtrArray *" + tmp_iv + " = sp_PtrArray_new();")14303+
emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_iv + ", NULL); }")14304+
emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";")1419614305
else1419714306
# Issue #130: same poly-slot boxing as the general1419814307
# InstanceVariableWriteNode emit path (compile_expr branch).@@ -23705,6 +23814,20 @@
2370523814
return2370623815
end2370723816
end23817+
# `@arr = [nil] * N` where @arr is a ptr_array slot. The23818+
# default `*` codegen produces an sp_IntArray, which mismatches23819+
# the slot type and corrupts later sp_PtrArray_set calls. Emit23820+
# a sized PtrArray pre-filled with NULLs inline so the slot23821+
# holds the matching storage.23822+
if is_ptr_array_type(ivt) == 1 && is_sized_empty_array_default(expr_id) == 123823+
@needs_gc = 123824+
cnt_e = compile_arg0(expr_id)23825+
tmp_arr = new_temp23826+
emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")23827+
emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_arr + ", NULL); }")23828+
emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")23829+
return23830+
end2370823831
# Issue #130: poly slot — every concrete-typed RHS must be boxed2370923832
# to sp_RbVal. Without the box, C compiler sees `sp_RbVal = mrb_int`2371023833
# (or const char *, etc.) and either rejects the assignment or@@ -27189,6 +27312,10 @@
2718927312
emit(" sp_StrArray_set(" + rc + ", " + idx + ", " + val + ");")2719027313
return2719127314
end27315+
if is_ptr_array_type(rt) == 127316+
emit(" sp_PtrArray_set(" + rc + ", " + idx + ", (void *)" + val + ");")27317+
return27318+
end2719227319
if rt == "str_int_hash"2719327320
emit(" sp_StrIntHash_set(" + rc + ", " + idx + ", " + val + ");")2719427321
returntest/ptr_array_set_widening.rbaddedView file
Loading diff…
test/ptr_array_set_widening.rb.expectedaddedView file
Loading diff…