Commit 44718b9f

44718b9fb969Merged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

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

Two related codegen pieces that together let Array.new(N) { [].dup } plus a later @a[i][j] << X push (of any kind: tuple, IntArray, Method, …) survive without silently truncating the pointer to mrb_int.

  • Array.new(N) { [].dup } (and { [] }) infer/emit each inner as a fresh sp_PolyArray. Pushes go through sp_PolyArray_push (or via sp_poly_shl's cls_id dispatch depending on the static type), so the runtime keeps the real cls_id for every pushed kind. Three call sites need the same logic (infer_type, infer_ivar_init_type, compile_constructor_expr) so the ivar widening pipeline stays consistent.

    Concretely, when the block tail is statically empty (ArrayNode [] or [].dup) the constructor returns poly_array and the emit allocates a fresh sp_box_poly_array(sp_PolyArray_new()) per inner — no need to compile the empty-array tail at all.

  • compile_multi_write extended for poly RHS: when the value expression's static type is poly (e.g. @attr_lut[i] where @attr_lut is poly_array of inner arrays — each element is a boxed sp_PolyArray with cls_id POLY_ARRAY), unbox the cls_id-POLY_ARRAY value to sp_PolyArray * and destructure via sp_PolyArray_get. Mirror of the existing val_t_local == poly_array branch.

    emit_multi_write_target gains the inverse direction: when value_type is poly but the consumer slot is concrete (mrb_int / sp_IntArray * / const char * / …), unbox via the new unbox_poly_to(at, val) helper. The helper matches box_value_to_poly's coverage in reverse — any concrete slot type the boxer handles, the unboxer recovers.

Together these unblock optcarrot's @attr_lut = (0..0x7fff).map do |i| entries[[io_addr, attr_shift]] ||= [io_addr, TILE_LUT[…], attr_shift] @lut_update_attr[bank][idx] << entries[key] entries[key] end pattern (the heaviest single workaround in optcarrot_build.sh — removed in a separate sweep, total ~80 lines of patches).

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
44718b9fb969
Parent
59f48e594a9d

spinel_codegen.rbmodifiedView file

@@ -4472,6 +4472,17 @@
44724472 if body_an >= 0
44734473 stmts_an = get_stmts(body_an)
44744474 if stmts_an.length > 0
4475+ # `[]` / `[].dup` block tail: the inner element type
4476+ # is statically ambiguous. Use poly_array (a
4477+ # PolyArray-of-PolyArray) for the result so later
4478+ # pushes of pointer-typed values (3-tuples,
4479+ # IntArrays, etc.) survive — the sp_poly_shl runtime
4480+ # dispatch handles any push kind via cls_id.
4481+ if is_empty_array_or_dup(stmts_an.last) == 1
4482+ @needs_rb_value = 1
4483+ @needs_gc = 1
4484+ return "poly_array"
4485+ end
44754486 bret = infer_type(stmts_an.last)
44764487 if bret == "string"
44774488 return "str_array"
@@ -5284,6 +5295,35 @@
52845295 0
52855296 end
52865297
5298+ # `[]` / `[].dup` — an empty array whose static element type is
5299+ # ambiguous. When this is the tail of a block whose result becomes
5300+ # the inner storage of a nested array (`Array.new(N) { [].dup }`,
5301+ # `(0..N).map { [].dup }`), defaulting to `int_array` is unsafe:
5302+ # later pushes of pointer-typed values (3-tuples, IntArrays, etc.)
5303+ # silently truncate the pointer to mrb_int. Use `poly_array` for
5304+ # the inner container instead so push goes through sp_PolyArray
5305+ # (with sp_RbVal slots) and the runtime cls_id dispatch in
5306+ # sp_poly_shl handles any pushed kind correctly.
5307+ def is_empty_array_or_dup(nid)
5308+ if is_empty_array_literal(nid) == 1
5309+ return 1
5310+ end
5311+ if nid < 0
5312+ return 0
5313+ end
5314+ if @nd_type[nid] != "CallNode"
5315+ return 0
5316+ end
5317+ if @nd_name[nid] != "dup"
5318+ return 0
5319+ end
5320+ recv = @nd_receiver[nid]
5321+ if recv < 0
5322+ return 0
5323+ end
5324+ is_empty_array_literal(recv)
5325+ end
5326+
52875327 # `[nil] * N` / `[0] * N` is a sized empty default — the elements
52885328 # come from `nil`/`0`, so the array's effective element type is the
52895329 # same as `[]`'s default (int_array). Used by writer-scan and the
@@ -8279,6 +8319,13 @@
82798319 if body_an2 >= 0
82808320 stmts_an2 = get_stmts(body_an2)
82818321 if stmts_an2.length > 0
8322+ # `[]` / `[].dup` block tail → poly_array (see
8323+ # infer_constructor_type for rationale).
8324+ if is_empty_array_or_dup(stmts_an2.last) == 1
8325+ @needs_rb_value = 1
8326+ @needs_gc = 1
8327+ return "poly_array"
8328+ end
82828329 bret2 = infer_type(stmts_an2.last)
82838330 if bret2 == "string"
82848331 return "str_array"
@@ -23754,10 +23801,21 @@
2375423801 arrnew_count = compile_expr(arrnew_aargs.first)
2375523802 arrnew_bp = get_block_param(nid, 0)
2375623803 block_ret_an = "int"
23804+ arrnew_empty_inner = 0
2375723805 if arrnew_body >= 0
2375823806 arrnew_pre_stmts = get_stmts(arrnew_body)
2375923807 if arrnew_pre_stmts.length > 0
23760- block_ret_an = infer_type(arrnew_pre_stmts.last)
23808+ # `[]` / `[].dup` block tail: emit each inner as a fresh
23809+ # poly_array so future pushes (with sp_poly_shl runtime
23810+ # cls_id dispatch) handle any pushed kind. This is the
23811+ # "deferred element type" path — the static type is
23812+ # `poly_array_ptr_array`.
23813+ if is_empty_array_or_dup(arrnew_pre_stmts.last) == 1
23814+ arrnew_empty_inner = 1
23815+ block_ret_an = "poly_array"
23816+ else
23817+ block_ret_an = infer_type(arrnew_pre_stmts.last)
23818+ end
2376123819 end
2376223820 end
2376323821 an_ret_is_arr = (block_ret_an == "int_array" || block_ret_an == "float_array" || block_ret_an == "str_array" || block_ret_an == "sym_array")
@@ -23800,19 +23858,27 @@
2380023858 compile_stmt(arrnew_stmts2[arrnew_k])
2380123859 arrnew_k = arrnew_k + 1
2380223860 end
23803- arrnew_lastv = compile_expr(arrnew_stmts2.last)
23804- if block_ret_an == "string"
23805- emit(" sp_StrArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23806- elsif block_ret_an == "float"
23807- emit(" sp_FloatArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23808- elsif block_ret_an == "poly"
23809- emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23810- elsif an_ret_is_deep_arr
23811- emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + box_value_to_poly(block_ret_an, arrnew_lastv) + ");")
23812- elsif an_ret_is_arr
23813- emit(" sp_PtrArray_push(" + arrnew_tmp + ", (void *)(" + arrnew_lastv + "));")
23861+ if arrnew_empty_inner == 1
23862+ # Skip compiling the `[]` / `[].dup` tail entirely —
23863+ # we know it's an empty array; emit a fresh
23864+ # sp_PolyArray instead so each inner is allocated as
23865+ # the deferred-type container.
23866+ emit(" sp_PolyArray_push(" + arrnew_tmp + ", sp_box_poly_array(sp_PolyArray_new()));")
2381423867 else
23815- emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23868+ arrnew_lastv = compile_expr(arrnew_stmts2.last)
23869+ if block_ret_an == "string"
23870+ emit(" sp_StrArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23871+ elsif block_ret_an == "float"
23872+ emit(" sp_FloatArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23873+ elsif block_ret_an == "poly"
23874+ emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23875+ elsif an_ret_is_deep_arr
23876+ emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + box_value_to_poly(block_ret_an, arrnew_lastv) + ");")
23877+ elsif an_ret_is_arr
23878+ emit(" sp_PtrArray_push(" + arrnew_tmp + ", (void *)(" + arrnew_lastv + "));")
23879+ else
23880+ emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")
23881+ end
2381623882 end
2381723883 end
2381823884 end
@@ -27676,6 +27742,52 @@
2767627742 # Box an already-compiled value of static type `at` into an sp_RbVal.
2767727743 # Mirrors box_expr_to_poly but operates on a raw (type, value) pair so
2767827744 # callers that already have temps don't have to re-emit the expr.
27745+ # Unbox an sp_RbVal expression `val` into the C representation
27746+ # of `at`. Used at sites where the destructure / cls_id-aware
27747+ # dispatch produced an sp_RbVal but the consumer slot is a
27748+ # concrete C type (mrb_int, sp_IntArray *, const char *, ...).
27749+ # No runtime cls_id check — caller has already narrowed via
27750+ # static type / wrapping cls_id dispatch.
27751+ def unbox_poly_to(at, val)
27752+ base = is_nullable_type(at) == 1 ? base_type(at) : at
27753+ if base == "int" || base == "bool" || base == "nil"
27754+ return "(" + val + ").v.i"
27755+ end
27756+ if base == "float"
27757+ return "(" + val + ").v.f"
27758+ end
27759+ if base == "string"
27760+ return "(const char *)(" + val + ").v.p"
27761+ end
27762+ if base == "symbol"
27763+ return "(sp_sym)(" + val + ").v.i"
27764+ end
27765+ if base == "int_array"
27766+ return "(sp_IntArray *)(" + val + ").v.p"
27767+ end
27768+ if base == "float_array"
27769+ return "(sp_FloatArray *)(" + val + ").v.p"
27770+ end
27771+ if base == "str_array"
27772+ return "(sp_StrArray *)(" + val + ").v.p"
27773+ end
27774+ if base == "sym_array"
27775+ return "(sp_IntArray *)(" + val + ").v.p"
27776+ end
27777+ if base == "poly_array"
27778+ return "(sp_PolyArray *)(" + val + ").v.p"
27779+ end
27780+ if is_ptr_array_type(base) == 1
27781+ return "(sp_PtrArray *)(" + val + ").v.p"
27782+ end
27783+ if is_obj_type(base) == 1
27784+ cname = base[4, base.length - 4]
27785+ return "(sp_" + cname + " *)(" + val + ").v.p"
27786+ end
27787+ # Fallback: cast generic pointer.
27788+ "(void *)(" + val + ").v.p"
27789+ end
27790+
2767927791 def box_value_to_poly(at, val)
2768027792 nullable = is_nullable_type(at)
2768127793 raw_at = at
@@ -30129,6 +30241,12 @@
3012930241 if vt == "poly" && value_type != "" && value_type != "poly"
3013030242 v = box_value_to_poly(value_type, value_expr)
3013130243 end
30244+ # Reverse direction: poly RHS into a concrete-typed local
30245+ # slot. Happens when destructuring a poly_array element
30246+ # (each slot was sp_PolyArray_get, returning sp_RbVal).
30247+ if value_type == "poly" && vt != "" && vt != "poly"
30248+ v = unbox_poly_to(vt, value_expr)
30249+ end
3013230250 emit(" " + fiber_var_ref(lname) + " = " + v + ";")
3013330251 return
3013430252 end
@@ -30157,6 +30275,9 @@
3015730275 if it == "poly" && value_type != "" && value_type != "poly"
3015830276 v = box_value_to_poly(value_type, value_expr)
3015930277 end
30278+ if value_type == "poly" && it != "" && it != "poly"
30279+ v = unbox_poly_to(it, value_expr)
30280+ end
3016030281 end
3016130282 emit(" " + self_arrow + sanitize_ivar(iname) + " = " + v + ";")
3016230283 end
@@ -30509,6 +30630,16 @@
3050930630 if val_t_local == "poly_array"
3051030631 @needs_rb_value = 1
3051130632 emit(" sp_PolyArray *" + tmp + " = " + compile_expr(val_id) + ";")
30633+ elsif val_t_local == "poly"
30634+ # RHS is a poly value (e.g. `@attr_lut[i]` where @attr_lut
30635+ # is a poly_array of inner arrays — each element is a
30636+ # boxed sp_PolyArray with cls_id POLY_ARRAY). Unbox to
30637+ # sp_PolyArray * and destructure via sp_PolyArray_get.
30638+ # Same shape as the static poly_array branch above.
30639+ @needs_rb_value = 1
30640+ rb_tmp_mw = new_temp
30641+ emit(" sp_RbVal " + rb_tmp_mw + " = " + compile_expr(val_id) + ";")
30642+ emit(" sp_PolyArray *" + tmp + " = (" + rb_tmp_mw + ".cls_id == SP_BUILTIN_POLY_ARRAY) ? (sp_PolyArray *)" + rb_tmp_mw + ".v.p : NULL;")
3051230643 else
3051330644 @needs_int_array = 1
3051430645 emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";")
@@ -30517,7 +30648,7 @@
3051730648 k = 0
3051830649 while k < targets.length
3051930650 tid = targets[k]
30520- if val_t_local == "poly_array"
30651+ if val_t_local == "poly_array" || val_t_local == "poly"
3052130652 rhs = "sp_PolyArray_get(" + tmp + ", " + k.to_s + ")"
3052230653 val_t_for_target = "poly"
3052330654 else