Commit 9153a62f

9153a62f417cMerged

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

Description

Merge pull request #366 from mame/pr/empty-array-deferred-elem

fix(codegen): empty-array deferred element type + poly RHS multi-write destructure

Details

Commit
9153a62f417c
Parent
36f1c6d4f7e9
Merge parent
44718b9fb969

spinel_codegen.rbmodifiedView file

@@ -4519,6 +4519,17 @@
45194519 if body_an >= 0
45204520 stmts_an = get_stmts(body_an)
45214521 if stmts_an.length > 0
4522+ # `[]` / `[].dup` block tail: the inner element type
4523+ # is statically ambiguous. Use poly_array (a
4524+ # PolyArray-of-PolyArray) for the result so later
4525+ # pushes of pointer-typed values (3-tuples,
4526+ # IntArrays, etc.) survive — the sp_poly_shl runtime
4527+ # dispatch handles any push kind via cls_id.
4528+ if is_empty_array_or_dup(stmts_an.last) == 1
4529+ @needs_rb_value = 1
4530+ @needs_gc = 1
4531+ return "poly_array"
4532+ end
45224533 bret = infer_type(stmts_an.last)
45234534 if bret == "string"
45244535 return "str_array"
@@ -5340,6 +5351,35 @@
53405351 0
53415352 end
53425353
5354+ # `[]` / `[].dup` — an empty array whose static element type is
5355+ # ambiguous. When this is the tail of a block whose result becomes
5356+ # the inner storage of a nested array (`Array.new(N) { [].dup }`,
5357+ # `(0..N).map { [].dup }`), defaulting to `int_array` is unsafe:
5358+ # later pushes of pointer-typed values (3-tuples, IntArrays, etc.)
5359+ # silently truncate the pointer to mrb_int. Use `poly_array` for
5360+ # the inner container instead so push goes through sp_PolyArray
5361+ # (with sp_RbVal slots) and the runtime cls_id dispatch in
5362+ # sp_poly_shl handles any pushed kind correctly.
5363+ def is_empty_array_or_dup(nid)
5364+ if is_empty_array_literal(nid) == 1
5365+ return 1
5366+ end
5367+ if nid < 0
5368+ return 0
5369+ end
5370+ if @nd_type[nid] != "CallNode"
5371+ return 0
5372+ end
5373+ if @nd_name[nid] != "dup"
5374+ return 0
5375+ end
5376+ recv = @nd_receiver[nid]
5377+ if recv < 0
5378+ return 0
5379+ end
5380+ is_empty_array_literal(recv)
5381+ end
5382+
53435383 # `[nil] * N` / `[0] * N` is a sized empty default — the elements
53445384 # come from `nil`/`0`, so the array's effective element type is the
53455385 # same as `[]`'s default (int_array). Used by writer-scan and the
@@ -8337,6 +8377,13 @@
83378377 if body_an2 >= 0
83388378 stmts_an2 = get_stmts(body_an2)
83398379 if stmts_an2.length > 0
8380+ # `[]` / `[].dup` block tail → poly_array (see
8381+ # infer_constructor_type for rationale).
8382+ if is_empty_array_or_dup(stmts_an2.last) == 1
8383+ @needs_rb_value = 1
8384+ @needs_gc = 1
8385+ return "poly_array"
8386+ end
83408387 bret2 = infer_type(stmts_an2.last)
83418388 if bret2 == "string"
83428389 return "str_array"
@@ -23844,10 +23891,21 @@
2384423891 arrnew_count = compile_expr(arrnew_aargs.first)
2384523892 arrnew_bp = get_block_param(nid, 0)
2384623893 block_ret_an = "int"
23894+ arrnew_empty_inner = 0
2384723895 if arrnew_body >= 0
2384823896 arrnew_pre_stmts = get_stmts(arrnew_body)
2384923897 if arrnew_pre_stmts.length > 0
23850- block_ret_an = infer_type(arrnew_pre_stmts.last)
23898+ # `[]` / `[].dup` block tail: emit each inner as a fresh
23899+ # poly_array so future pushes (with sp_poly_shl runtime
23900+ # cls_id dispatch) handle any pushed kind. This is the
23901+ # "deferred element type" path — the static type is
23902+ # `poly_array_ptr_array`.
23903+ if is_empty_array_or_dup(arrnew_pre_stmts.last) == 1
23904+ arrnew_empty_inner = 1
23905+ block_ret_an = "poly_array"
23906+ else
23907+ block_ret_an = infer_type(arrnew_pre_stmts.last)
23908+ end
2385123909 end
2385223910 end
2385323911 an_ret_is_arr = (block_ret_an == "int_array" || block_ret_an == "float_array" || block_ret_an == "str_array" || block_ret_an == "sym_array")
@@ -23890,19 +23948,27 @@
2389023948 compile_stmt(arrnew_stmts2[arrnew_k])
2389123949 arrnew_k = arrnew_k + 1
2389223950 end
23893- arrnew_lastv = compile_expr(arrnew_stmts2.last)
23894- if block_ret_an == "string"
23895- emit(" sp_StrArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23896- elsif block_ret_an == "float"
23897- emit(" sp_FloatArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23898- elsif block_ret_an == "poly"
23899- emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23900- elsif an_ret_is_deep_arr
23901- emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + box_value_to_poly(block_ret_an, arrnew_lastv) + ");")
23902- elsif an_ret_is_arr
23903- emit(" sp_PtrArray_push(" + arrnew_tmp + ", (void *)(" + arrnew_lastv + "));")
23951+ if arrnew_empty_inner == 1
23952+ # Skip compiling the `[]` / `[].dup` tail entirely —
23953+ # we know it's an empty array; emit a fresh
23954+ # sp_PolyArray instead so each inner is allocated as
23955+ # the deferred-type container.
23956+ emit(" sp_PolyArray_push(" + arrnew_tmp + ", sp_box_poly_array(sp_PolyArray_new()));")
2390423957 else
23905- emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23958+ arrnew_lastv = compile_expr(arrnew_stmts2.last)
23959+ if block_ret_an == "string"
23960+ emit(" sp_StrArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23961+ elsif block_ret_an == "float"
23962+ emit(" sp_FloatArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23963+ elsif block_ret_an == "poly"
23964+ emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23965+ elsif an_ret_is_deep_arr
23966+ emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + box_value_to_poly(block_ret_an, arrnew_lastv) + ");")
23967+ elsif an_ret_is_arr
23968+ emit(" sp_PtrArray_push(" + arrnew_tmp + ", (void *)(" + arrnew_lastv + "));")
23969+ else
23970+ emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23971+ end
2390623972 end
2390723973 end
2390823974 end
@@ -27984,6 +28050,52 @@
2798428050 # Box an already-compiled value of static type `at` into an sp_RbVal.
2798528051 # Mirrors box_expr_to_poly but operates on a raw (type, value) pair so
2798628052 # callers that already have temps don't have to re-emit the expr.
28053+ # Unbox an sp_RbVal expression `val` into the C representation
28054+ # of `at`. Used at sites where the destructure / cls_id-aware
28055+ # dispatch produced an sp_RbVal but the consumer slot is a
28056+ # concrete C type (mrb_int, sp_IntArray *, const char *, ...).
28057+ # No runtime cls_id check — caller has already narrowed via
28058+ # static type / wrapping cls_id dispatch.
28059+ def unbox_poly_to(at, val)
28060+ base = is_nullable_type(at) == 1 ? base_type(at) : at
28061+ if base == "int" || base == "bool" || base == "nil"
28062+ return "(" + val + ").v.i"
28063+ end
28064+ if base == "float"
28065+ return "(" + val + ").v.f"
28066+ end
28067+ if base == "string"
28068+ return "(const char *)(" + val + ").v.p"
28069+ end
28070+ if base == "symbol"
28071+ return "(sp_sym)(" + val + ").v.i"
28072+ end
28073+ if base == "int_array"
28074+ return "(sp_IntArray *)(" + val + ").v.p"
28075+ end
28076+ if base == "float_array"
28077+ return "(sp_FloatArray *)(" + val + ").v.p"
28078+ end
28079+ if base == "str_array"
28080+ return "(sp_StrArray *)(" + val + ").v.p"
28081+ end
28082+ if base == "sym_array"
28083+ return "(sp_IntArray *)(" + val + ").v.p"
28084+ end
28085+ if base == "poly_array"
28086+ return "(sp_PolyArray *)(" + val + ").v.p"
28087+ end
28088+ if is_ptr_array_type(base) == 1
28089+ return "(sp_PtrArray *)(" + val + ").v.p"
28090+ end
28091+ if is_obj_type(base) == 1
28092+ cname = base[4, base.length - 4]
28093+ return "(sp_" + cname + " *)(" + val + ").v.p"
28094+ end
28095+ # Fallback: cast generic pointer.
28096+ "(void *)(" + val + ").v.p"
28097+ end
28098+
2798728099 def box_value_to_poly(at, val)
2798828100 nullable = is_nullable_type(at)
2798928101 raw_at = at
@@ -30447,6 +30559,12 @@
3044730559 if vt == "poly" && value_type != "" && value_type != "poly"
3044830560 v = box_value_to_poly(value_type, value_expr)
3044930561 end
30562+ # Reverse direction: poly RHS into a concrete-typed local
30563+ # slot. Happens when destructuring a poly_array element
30564+ # (each slot was sp_PolyArray_get, returning sp_RbVal).
30565+ if value_type == "poly" && vt != "" && vt != "poly"
30566+ v = unbox_poly_to(vt, value_expr)
30567+ end
3045030568 emit(" " + fiber_var_ref(lname) + " = " + v + ";")
3045130569 return
3045230570 end
@@ -30475,6 +30593,9 @@
3047530593 if it == "poly" && value_type != "" && value_type != "poly"
3047630594 v = box_value_to_poly(value_type, value_expr)
3047730595 end
30596+ if value_type == "poly" && it != "" && it != "poly"
30597+ v = unbox_poly_to(it, value_expr)
30598+ end
3047830599 end
3047930600 emit(" " + self_arrow + sanitize_ivar(iname) + " = " + v + ";")
3048030601 end
@@ -30827,6 +30948,16 @@
3082730948 if val_t_local == "poly_array"
3082830949 @needs_rb_value = 1
3082930950 emit(" sp_PolyArray *" + tmp + " = " + compile_expr(val_id) + ";")
30951+ elsif val_t_local == "poly"
30952+ # RHS is a poly value (e.g. `@attr_lut[i]` where @attr_lut
30953+ # is a poly_array of inner arrays — each element is a
30954+ # boxed sp_PolyArray with cls_id POLY_ARRAY). Unbox to
30955+ # sp_PolyArray * and destructure via sp_PolyArray_get.
30956+ # Same shape as the static poly_array branch above.
30957+ @needs_rb_value = 1
30958+ rb_tmp_mw = new_temp
30959+ emit(" sp_RbVal " + rb_tmp_mw + " = " + compile_expr(val_id) + ";")
30960+ emit(" sp_PolyArray *" + tmp + " = (" + rb_tmp_mw + ".cls_id == SP_BUILTIN_POLY_ARRAY) ? (sp_PolyArray *)" + rb_tmp_mw + ".v.p : NULL;")
3083030961 else
3083130962 @needs_int_array = 1
3083230963 emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";")
@@ -30835,7 +30966,7 @@
3083530966 k = 0
3083630967 while k < targets.length
3083730968 tid = targets[k]
30838- if val_t_local == "poly_array"
30969+ if val_t_local == "poly_array" || val_t_local == "poly"
3083930970 rhs = "sp_PolyArray_get(" + tmp + ", " + k.to_s + ")"
3084030971 val_t_for_target = "poly"
3084130972 else