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 0
42684268 end
42694269
4270+ # `[nil] * N` / `[0] * N` is a sized empty default — the elements
4271+ # come from `nil`/`0`, so the array's effective element type is the
4272+ # same as `[]`'s default (int_array). Used by writer-scan and the
4273+ # 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 < 0
4277+ return 0
4278+ end
4279+ if @nd_type[nid] != "CallNode" || @nd_name[nid] != "*"
4280+ return 0
4281+ end
4282+ arr_recv = @nd_receiver[nid]
4283+ if arr_recv < 0 || @nd_type[arr_recv] != "ArrayNode"
4284+ return 0
4285+ end
4286+ recv_elems = parse_id_list(@nd_elements[arr_recv])
4287+ if recv_elems.length == 0
4288+ return 0
4289+ end
4290+ ei = 0
4291+ while ei < recv_elems.length
4292+ ti = recv_elems[ei]
4293+ if ti < 0
4294+ return 0
4295+ end
4296+ if @nd_type[ti] != "NilNode" && @nd_type[ti] != "IntegerNode"
4297+ return 0
4298+ end
4299+ ei = ei + 1
4300+ end
4301+ 1
4302+ end
4303+
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) == 1
6280+ # Don't widen a typed `<obj>_ptr_array` slot back to
6281+ # poly when the disagreement is just `int_array`. That
6282+ # disagreement comes from the `[nil] * N` empty default
6283+ # — the writer-scan `[]=` / `<<` widening is more
6284+ # specific and should win.
6285+ if is_ptr_array_type(old) == 1 && new_type == "int_array"
6286+ k = k + 1
6287+ next
6288+ end
62466289 types[k] = "poly_array"
62476290 @needs_rb_value = 1
62486291 @cls_ivar_types[ci] = types.join(";")
@@ -7395,6 +7438,25 @@
73957438 return val
73967439 end
73977440 end
7441+ # `[nil] * N` / `[0] * N` against a pointer-array LHS: build a
7442+ # fresh ptr_array pre-filled with N NULLs so subsequent
7443+ # `arr[i] = obj` writes via sp_PtrArray_set land in valid slots.
7444+ # Without this, the default `*` codegen produces an int_array
7445+ # 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) == 1
7447+ @needs_gc = 1
7448+ cnt_e = compile_arg0(nid)
7449+ tmp = new_temp
7450+ if expected_base == "poly_array"
7451+ @needs_rb_value = 1
7452+ 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+ else
7455+ 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+ end
7458+ return tmp
7459+ end
73987460 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 end
85568618 if is_obj_type(elem_acc[0]) == 1
8557- @needs_rb_value = 1
8558- return "poly_array"
8619+ # Homogeneous obj array — use a typed `<obj>_ptr_array` so reads
8620+ # return a typed pointer (no sp_RbVal unbox needed at the call
8621+ # site). Falls back to poly_array via normal widening when a
8622+ # later mismatched-type write happens.
8623+ return base_type(elem_acc[0]) + "_ptr_array"
85598624 end
85608625 ""
85618626 end
@@ -9003,6 +9068,39 @@
90039068 end
90049069 end
90059070 end
9071+ # `@arr[i] = v` against an ivar typed `int_array` (the
9072+ # `[nil] * N` empty default) should widen to match the value
9073+ # type the same way `<<` / `push` does. Without this, optcarrot's
9074+ # `@fetch[addr] = method(:peek_X)` writes a Method into an
9075+ # 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 >= 0
9083+ ai = get_args(args_id)
9084+ if ai.length >= 2
9085+ vt = infer_type(ai[ai.length - 1])
9086+ promoted = empty_array_promotion_for([vt])
9087+ if promoted != "" && promoted != cur_t
9088+ replace_ivar_type(@current_class_idx, iname, promoted)
9089+ if promoted == "str_array"
9090+ @needs_str_array = 1
9091+ elsif promoted == "float_array"
9092+ @needs_float_array = 1
9093+ elsif promoted == "sym_array"
9094+ @needs_int_array = 1
9095+ elsif promoted == "poly_array"
9096+ @needs_rb_value = 1
9097+ @needs_gc = 1
9098+ end
9099+ end
9100+ end
9101+ end
9102+ end
9103+ end
90069104 end
90079105 # Recurse via the centralized child walker (push_child_ids covers
90089106 # the full set of AST slots — visiting a few extra slots is a
@@ -14193,6 +14291,17 @@
1419314291 if iv_ctor != ""
1419414292 @needs_gc = 1
1419514293 emit_raw(" " + self_arrow + ivar + " = " + iv_ctor + ";")
14294+ elsif is_ptr_array_type(ivt) == 1 && is_sized_empty_array_default(expr_id_iv) == 1
14295+ # `@arr = [nil] * N` with @arr already widened to
14296+ # ptr_array via writer-scan. The default `*` codegen
14297+ # produces an sp_IntArray that mismatches the slot
14298+ # type; emit a sized PtrArray of NULLs inline.
14299+ @needs_gc = 1
14300+ cnt_e_iv = compile_arg0(expr_id_iv)
14301+ tmp_iv = new_temp
14302+ 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 else
1419714306 # Issue #130: same poly-slot boxing as the general
1419814307 # InstanceVariableWriteNode emit path (compile_expr branch).
@@ -23705,6 +23814,20 @@
2370523814 return
2370623815 end
2370723816 end
23817+ # `@arr = [nil] * N` where @arr is a ptr_array slot. The
23818+ # default `*` codegen produces an sp_IntArray, which mismatches
23819+ # the slot type and corrupts later sp_PtrArray_set calls. Emit
23820+ # a sized PtrArray pre-filled with NULLs inline so the slot
23821+ # holds the matching storage.
23822+ if is_ptr_array_type(ivt) == 1 && is_sized_empty_array_default(expr_id) == 1
23823+ @needs_gc = 1
23824+ cnt_e = compile_arg0(expr_id)
23825+ tmp_arr = new_temp
23826+ 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+ return
23830+ end
2370823831 # Issue #130: poly slot — every concrete-typed RHS must be boxed
2370923832 # 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 return
2719127314 end
27315+ if is_ptr_array_type(rt) == 1
27316+ emit(" sp_PtrArray_set(" + rc + ", " + idx + ", (void *)" + val + ");")
27317+ return
27318+ end
2719227319 if rt == "str_int_hash"
2719327320 emit(" sp_StrIntHash_set(" + rc + ", " + idx + ", " + val + ");")
2719427321 return