Commit fc0f3a34

fc0f3a34b344Merged

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

Description

fix(codegen): chained [idx] ||= lowers to a value-producing temp for poly_array / poly recv

PR #377 covered the *_poly_hash arms of the IndexOrWriteNode expression-form. Indexed access against poly-element receivers — poly_array and poly (an sp_RbVal carrying a poly_array at runtime) — still fell through to the int default, so a chain like (arr[i] ||= []) << v collapsed to 0 << ... and failed the C compile.

Two new helpers mirror the *_poly_hash shape:

  • compile_typed_array_index_or_assign_to_temp — direct poly_array recv. SP_TAG_NIL miss probe with an in-bounds guard before _get (sp_PolyArray_get reads data[i] unchecked) and pad-to-i with _push(sp_box_nil()) before _set (sp_PolyArray_set has no auto-grow). Mirrors CRuby's arr[i] = v auto-grow when the slot is past len.

  • compile_poly_value_array_index_or_assign_to_temp — poly recv. Same body wrapped with a cls_id == SP_BUILTIN_POLY_ARRAY cast guard so the runtime falls through cleanly when the poly carries a non-array shape.

Both are wired into compile_expr's IndexOrWriteNode branch and into compile_index_or_assign (the stmt form), and infer_type returns "poly" for IndexOrWriteNode over a poly / poly_array recv so chain consumers see an sp_RbVal slot.

Companion fix chained_or_assign_rhs_box: when the rhs is an ArrayNode literal ([], [nil, nil], …) and the slot is poly-element, lower it as a poly_array instead of the default int_array. Without this every chain level's cls_id == SP_BUILTIN_POLY_ARRAY check would fail at runtime (boxed int_array != poly_array) and the next level's back-set would skip silently.

This unblocks optcarrot's setup_lut:

(((@h[b] ||= [])[i] ||= [nil, nil])[0] ||= []) << v

so the wip(spinel-aot): replace chained ||= over @lut_update with reverse-index arrays workaround on the optcarrot for-spinel branch can be dropped (commit 5dc95a7 / f0ffd88 depending on worktree, restoring the original setup_lut shape with plain {} instead of compare_by_identity).

Details

Commit
fc0f3a34b344
Parent
9000016d8a3f

spinel_codegen.rbmodifiedView file

@@ -2387,6 +2387,13 @@
23872387 if rt_iow_t == "int_array" || rt_iow_t == "float_array" || rt_iow_t == "str_array" || rt_iow_t == "sym_array"
23882388 return elem_type_of_array(rt_iow_t)
23892389 end
2390+ # poly_array elements are sp_RbVal; chained IndexOrWriteNode
2391+ # over a poly_array recv (or a poly-typed recv that carries a
2392+ # poly_array at runtime) returns the element value as poly so
2393+ # the next chain link sees an sp_RbVal, not the int default.
2394+ if rt_iow_t == "poly_array" || rt_iow_t == "poly"
2395+ return "poly"
2396+ end
23902397 end
23912398 return "int"
23922399 end
@@ -21212,6 +21219,22 @@
2121221219 tg = compile_typed_poly_hash_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow, rt_iow)
2121321220 return tg
2121421221 end
21222+ if rt_iow == "poly_array"
21223+ rc_iow = compile_expr_gc_rooted(recv_iow)
21224+ arg_ids_iow = get_args(args_iow)
21225+ tg = compile_typed_array_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow, rt_iow)
21226+ return tg
21227+ end
21228+ if rt_iow == "poly"
21229+ # `<poly>[i] ||= v` — recv carries a poly_array at runtime
21230+ # (the typical mid-chain shape, e.g. `(@h[k] ||= [])[i] ||=
21231+ # ...`). Inline the cls_id guard + auto-grow set so the
21232+ # chain produces an sp_RbVal value even when the initial
21233+ # `[]` literal was empty.
21234+ rc_iow = compile_expr_gc_rooted(recv_iow)
21235+ tg = compile_poly_value_array_index_or_assign_to_temp(nid, rc_iow)
21236+ return tg
21237+ end
2121521238 end
2121621239 end
2121721240 if t == "ConstantReadNode"
@@ -35295,6 +35318,14 @@
3529535318 compile_typed_poly_hash_index_or_assign_to_temp(nid, rc, arg_ids, rt)
3529635319 return
3529735320 end
35321+ if rt == "poly_array"
35322+ compile_typed_array_index_or_assign_to_temp(nid, rc, arg_ids, rt)
35323+ return
35324+ end
35325+ if rt == "poly"
35326+ compile_poly_value_array_index_or_assign_to_temp(nid, rc)
35327+ return
35328+ end
3529835329 end
3529935330
3530035331 # Emit the get-then-set body for `recv[k] ||= v` against a
@@ -35309,7 +35340,7 @@
3530935340 emit(" sp_PolyPolyHash *" + tt + " = " + rc + ";")
3531035341 emit(" sp_RbVal " + tk + " = " + key_boxed + ";")
3531135342 emit(" sp_RbVal " + tg + " = sp_PolyPolyHash_get(" + tt + ", " + tk + ");")
35312- val_boxed = box_expr_to_poly(@nd_expression[nid])
35343+ val_boxed = chained_or_assign_rhs_box(@nd_expression[nid])
3531335344 emit(" if (" + tg + ".tag == SP_TAG_NIL) {")
3531435345 emit(" " + tg + " = " + val_boxed + ";")
3531535346 emit(" sp_PolyPolyHash_set(" + tt + ", " + tk + ", " + tg + ");")
@@ -35338,7 +35369,7 @@
3533835369 emit(" " + hash_c + " *" + tt + " = " + rc + ";")
3533935370 emit(" " + key_t + " " + tk + " = " + key_expr + ";")
3534035371 emit(" sp_RbVal " + tg + " = " + hash_c + "_get(" + tt + ", " + tk + ");")
35341- val_boxed = box_expr_to_poly(@nd_expression[nid])
35372+ val_boxed = chained_or_assign_rhs_box(@nd_expression[nid])
3534235373 emit(" if (" + tg + ".tag == SP_TAG_NIL) {")
3534335374 emit(" " + tg + " = " + val_boxed + ";")
3534435375 emit(" " + hash_c + "_set(" + tt + ", " + tk + ", " + tg + ");")
@@ -35346,6 +35377,95 @@
3534635377 tg
3534735378 end
3534835379
35380+ # Box the rhs of an `||=` whose lhs is a poly-element slot. For
35381+ # ArrayNode literals — `[]`, `[nil, nil]`, … — promote to
35382+ # poly_array (boxed) so the slot carries a uniformly poly shape;
35383+ # subsequent chain levels can then inspect the value's cls_id
35384+ # against `SP_BUILTIN_POLY_ARRAY` reliably. Without the promote,
35385+ # `[]` would lower to `sp_box_int_array(sp_IntArray_new())`, the
35386+ # next chain level's `cls_id == POLY_ARRAY` check would fail, and
35387+ # the back-set would skip silently. Non-ArrayNode rhs falls back
35388+ # to the existing `box_expr_to_poly` machinery.
35389+ def chained_or_assign_rhs_box(rhs_id)
35390+ if rhs_id >= 0 && @nd_type[rhs_id] == "ArrayNode"
35391+ @needs_rb_value = 1
35392+ @needs_gc = 1
35393+ return "sp_box_poly_array(" + compile_array_literal_as_poly(rhs_id) + ")"
35394+ end
35395+ box_expr_to_poly(rhs_id)
35396+ end
35397+
35398+ # `recv[i] ||= v` against a typed-array receiver whose element
35399+ # slot is a boxed sp_RbVal — i.e. poly_array. The miss probe is
35400+ # the same `tag == SP_TAG_NIL` pivot used by the *_poly_hash
35401+ # forms, but indexed access has two extra wrinkles:
35402+ #
35403+ # - the key must be in-bounds before `_get` is safe to call
35404+ # (sp_PolyArray_get reads `data[i]` without a length check),
35405+ # - on miss the slot may be beyond `len`, so before `_set` we
35406+ # pad the array with nil entries up to `i` inclusive.
35407+ #
35408+ # Mirrors CRuby's `arr[i] = v` auto-grow semantics for the gap.
35409+ # Scalar-element typed arrays (int_array, float_array, …) need a
35410+ # different probe (out-of-bounds check, no SP_TAG_NIL) and are
35411+ # not handled here; the caller falls back to the existing stmt
35412+ # path for those.
35413+ def compile_typed_array_index_or_assign_to_temp(nid, rc, arg_ids, recv_type)
35414+ tt = new_temp
35415+ ti = new_temp
35416+ tg = new_temp
35417+ arr_c = "sp_PolyArray"
35418+ emit(" " + arr_c + " *" + tt + " = " + rc + ";")
35419+ emit(" mrb_int " + ti + " = " + compile_expr(arg_ids[0]) + ";")
35420+ emit(" sp_RbVal " + tg + " = sp_box_nil();")
35421+ emit(" if (" + ti + " >= 0 && " + ti + " < sp_" + arr_c.sub("sp_", "") + "_length(" + tt + ")) {")
35422+ emit(" " + tg + " = " + arr_c + "_get(" + tt + ", " + ti + ");")
35423+ emit(" }")
35424+ val_boxed = chained_or_assign_rhs_box(@nd_expression[nid])
35425+ emit(" if (" + tg + ".tag == SP_TAG_NIL) {")
35426+ emit(" " + tg + " = " + val_boxed + ";")
35427+ emit(" while (sp_" + arr_c.sub("sp_", "") + "_length(" + tt + ") <= " + ti + ") { " + arr_c + "_push(" + tt + ", sp_box_nil()); }")
35428+ emit(" " + arr_c + "_set(" + tt + ", " + ti + ", " + tg + ");")
35429+ emit(" }")
35430+ tg
35431+ end
35432+
35433+ # `<poly>[i] ||= v` where the recv is an sp_RbVal carrying — at
35434+ # runtime — a poly_array. Common in chains like
35435+ # `(@h[k] ||= [])[i] ||= ...`: spinel types `(@h[k] ||= [])` as
35436+ # poly because the hash leaf type is poly, and the inner `||=`
35437+ # sees a poly recv even though the value is concretely a
35438+ # poly_array.
35439+ #
35440+ # The miss probe needs an extra `tag == SP_TAG_OBJ &&
35441+ # cls_id == SP_BUILTIN_POLY_ARRAY` guard so the runtime falls
35442+ # through cleanly when the poly carries a non-array shape. The
35443+ # body otherwise mirrors `compile_typed_array_index_or_assign_to_temp`.
35444+ def compile_poly_value_array_index_or_assign_to_temp(nid, rc)
35445+ tr = new_temp
35446+ tp = new_temp
35447+ ti = new_temp
35448+ tg = new_temp
35449+ args_id = @nd_arguments[nid]
35450+ arg0 = args_id >= 0 ? get_args(args_id)[0] : -1
35451+ emit(" sp_RbVal " + tr + " = " + rc + ";")
35452+ emit(" sp_PolyArray *" + tp + " = (" + tr + ".tag == SP_TAG_OBJ && " + tr + ".cls_id == SP_BUILTIN_POLY_ARRAY) ? (sp_PolyArray *)" + tr + ".v.p : NULL;")
35453+ emit(" mrb_int " + ti + " = " + compile_expr(arg0) + ";")
35454+ emit(" sp_RbVal " + tg + " = sp_box_nil();")
35455+ emit(" if (" + tp + " && " + ti + " >= 0 && " + ti + " < sp_PolyArray_length(" + tp + ")) {")
35456+ emit(" " + tg + " = sp_PolyArray_get(" + tp + ", " + ti + ");")
35457+ emit(" }")
35458+ val_boxed = chained_or_assign_rhs_box(@nd_expression[nid])
35459+ emit(" if (" + tg + ".tag == SP_TAG_NIL) {")
35460+ emit(" " + tg + " = " + val_boxed + ";")
35461+ emit(" if (" + tp + ") {")
35462+ emit(" while (sp_PolyArray_length(" + tp + ") <= " + ti + ") { sp_PolyArray_push(" + tp + ", sp_box_nil()); }")
35463+ emit(" sp_PolyArray_set(" + tp + ", " + ti + ", " + tg + ");")
35464+ emit(" }")
35465+ emit(" }")
35466+ tg
35467+ end
35468+
3534935469 # Return a C expression that evaluates to the inspected form of `val`
3535035470 # (a value of inferred Ruby type `at`), following Ruby's Object#inspect
3535135471 # contract. Returns "" when `at` has no inspect implementation yet, so