Commit 3ee4eed9

3ee4eed9c742Merged

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

Description

Merge pull request #381 from mame/pr/chained-or-assign-array

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

Details

Commit
3ee4eed9c742
Parent
c07d880fcf7a
Merge parent
fc0f3a34b344

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
@@ -21273,6 +21280,22 @@
2127321280 tg = compile_typed_poly_hash_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow, rt_iow)
2127421281 return tg
2127521282 end
21283+ if rt_iow == "poly_array"
21284+ rc_iow = compile_expr_gc_rooted(recv_iow)
21285+ arg_ids_iow = get_args(args_iow)
21286+ tg = compile_typed_array_index_or_assign_to_temp(nid, rc_iow, arg_ids_iow, rt_iow)
21287+ return tg
21288+ end
21289+ if rt_iow == "poly"
21290+ # `<poly>[i] ||= v` — recv carries a poly_array at runtime
21291+ # (the typical mid-chain shape, e.g. `(@h[k] ||= [])[i] ||=
21292+ # ...`). Inline the cls_id guard + auto-grow set so the
21293+ # chain produces an sp_RbVal value even when the initial
21294+ # `[]` literal was empty.
21295+ rc_iow = compile_expr_gc_rooted(recv_iow)
21296+ tg = compile_poly_value_array_index_or_assign_to_temp(nid, rc_iow)
21297+ return tg
21298+ end
2127621299 end
2127721300 end
2127821301 if t == "ConstantReadNode"
@@ -35356,6 +35379,14 @@
3535635379 compile_typed_poly_hash_index_or_assign_to_temp(nid, rc, arg_ids, rt)
3535735380 return
3535835381 end
35382+ if rt == "poly_array"
35383+ compile_typed_array_index_or_assign_to_temp(nid, rc, arg_ids, rt)
35384+ return
35385+ end
35386+ if rt == "poly"
35387+ compile_poly_value_array_index_or_assign_to_temp(nid, rc)
35388+ return
35389+ end
3535935390 end
3536035391
3536135392 # Emit the get-then-set body for `recv[k] ||= v` against a
@@ -35370,7 +35401,7 @@
3537035401 emit(" sp_PolyPolyHash *" + tt + " = " + rc + ";")
3537135402 emit(" sp_RbVal " + tk + " = " + key_boxed + ";")
3537235403 emit(" sp_RbVal " + tg + " = sp_PolyPolyHash_get(" + tt + ", " + tk + ");")
35373- val_boxed = box_expr_to_poly(@nd_expression[nid])
35404+ val_boxed = chained_or_assign_rhs_box(@nd_expression[nid])
3537435405 emit(" if (" + tg + ".tag == SP_TAG_NIL) {")
3537535406 emit(" " + tg + " = " + val_boxed + ";")
3537635407 emit(" sp_PolyPolyHash_set(" + tt + ", " + tk + ", " + tg + ");")
@@ -35399,7 +35430,7 @@
3539935430 emit(" " + hash_c + " *" + tt + " = " + rc + ";")
3540035431 emit(" " + key_t + " " + tk + " = " + key_expr + ";")
3540135432 emit(" sp_RbVal " + tg + " = " + hash_c + "_get(" + tt + ", " + tk + ");")
35402- val_boxed = box_expr_to_poly(@nd_expression[nid])
35433+ val_boxed = chained_or_assign_rhs_box(@nd_expression[nid])
3540335434 emit(" if (" + tg + ".tag == SP_TAG_NIL) {")
3540435435 emit(" " + tg + " = " + val_boxed + ";")
3540535436 emit(" " + hash_c + "_set(" + tt + ", " + tk + ", " + tg + ");")
@@ -35407,6 +35438,95 @@
3540735438 tg
3540835439 end
3540935440
35441+ # Box the rhs of an `||=` whose lhs is a poly-element slot. For
35442+ # ArrayNode literals — `[]`, `[nil, nil]`, … — promote to
35443+ # poly_array (boxed) so the slot carries a uniformly poly shape;
35444+ # subsequent chain levels can then inspect the value's cls_id
35445+ # against `SP_BUILTIN_POLY_ARRAY` reliably. Without the promote,
35446+ # `[]` would lower to `sp_box_int_array(sp_IntArray_new())`, the
35447+ # next chain level's `cls_id == POLY_ARRAY` check would fail, and
35448+ # the back-set would skip silently. Non-ArrayNode rhs falls back
35449+ # to the existing `box_expr_to_poly` machinery.
35450+ def chained_or_assign_rhs_box(rhs_id)
35451+ if rhs_id >= 0 && @nd_type[rhs_id] == "ArrayNode"
35452+ @needs_rb_value = 1
35453+ @needs_gc = 1
35454+ return "sp_box_poly_array(" + compile_array_literal_as_poly(rhs_id) + ")"
35455+ end
35456+ box_expr_to_poly(rhs_id)
35457+ end
35458+
35459+ # `recv[i] ||= v` against a typed-array receiver whose element
35460+ # slot is a boxed sp_RbVal — i.e. poly_array. The miss probe is
35461+ # the same `tag == SP_TAG_NIL` pivot used by the *_poly_hash
35462+ # forms, but indexed access has two extra wrinkles:
35463+ #
35464+ # - the key must be in-bounds before `_get` is safe to call
35465+ # (sp_PolyArray_get reads `data[i]` without a length check),
35466+ # - on miss the slot may be beyond `len`, so before `_set` we
35467+ # pad the array with nil entries up to `i` inclusive.
35468+ #
35469+ # Mirrors CRuby's `arr[i] = v` auto-grow semantics for the gap.
35470+ # Scalar-element typed arrays (int_array, float_array, …) need a
35471+ # different probe (out-of-bounds check, no SP_TAG_NIL) and are
35472+ # not handled here; the caller falls back to the existing stmt
35473+ # path for those.
35474+ def compile_typed_array_index_or_assign_to_temp(nid, rc, arg_ids, recv_type)
35475+ tt = new_temp
35476+ ti = new_temp
35477+ tg = new_temp
35478+ arr_c = "sp_PolyArray"
35479+ emit(" " + arr_c + " *" + tt + " = " + rc + ";")
35480+ emit(" mrb_int " + ti + " = " + compile_expr(arg_ids[0]) + ";")
35481+ emit(" sp_RbVal " + tg + " = sp_box_nil();")
35482+ emit(" if (" + ti + " >= 0 && " + ti + " < sp_" + arr_c.sub("sp_", "") + "_length(" + tt + ")) {")
35483+ emit(" " + tg + " = " + arr_c + "_get(" + tt + ", " + ti + ");")
35484+ emit(" }")
35485+ val_boxed = chained_or_assign_rhs_box(@nd_expression[nid])
35486+ emit(" if (" + tg + ".tag == SP_TAG_NIL) {")
35487+ emit(" " + tg + " = " + val_boxed + ";")
35488+ emit(" while (sp_" + arr_c.sub("sp_", "") + "_length(" + tt + ") <= " + ti + ") { " + arr_c + "_push(" + tt + ", sp_box_nil()); }")
35489+ emit(" " + arr_c + "_set(" + tt + ", " + ti + ", " + tg + ");")
35490+ emit(" }")
35491+ tg
35492+ end
35493+
35494+ # `<poly>[i] ||= v` where the recv is an sp_RbVal carrying — at
35495+ # runtime — a poly_array. Common in chains like
35496+ # `(@h[k] ||= [])[i] ||= ...`: spinel types `(@h[k] ||= [])` as
35497+ # poly because the hash leaf type is poly, and the inner `||=`
35498+ # sees a poly recv even though the value is concretely a
35499+ # poly_array.
35500+ #
35501+ # The miss probe needs an extra `tag == SP_TAG_OBJ &&
35502+ # cls_id == SP_BUILTIN_POLY_ARRAY` guard so the runtime falls
35503+ # through cleanly when the poly carries a non-array shape. The
35504+ # body otherwise mirrors `compile_typed_array_index_or_assign_to_temp`.
35505+ def compile_poly_value_array_index_or_assign_to_temp(nid, rc)
35506+ tr = new_temp
35507+ tp = new_temp
35508+ ti = new_temp
35509+ tg = new_temp
35510+ args_id = @nd_arguments[nid]
35511+ arg0 = args_id >= 0 ? get_args(args_id)[0] : -1
35512+ emit(" sp_RbVal " + tr + " = " + rc + ";")
35513+ emit(" sp_PolyArray *" + tp + " = (" + tr + ".tag == SP_TAG_OBJ && " + tr + ".cls_id == SP_BUILTIN_POLY_ARRAY) ? (sp_PolyArray *)" + tr + ".v.p : NULL;")
35514+ emit(" mrb_int " + ti + " = " + compile_expr(arg0) + ";")
35515+ emit(" sp_RbVal " + tg + " = sp_box_nil();")
35516+ emit(" if (" + tp + " && " + ti + " >= 0 && " + ti + " < sp_PolyArray_length(" + tp + ")) {")
35517+ emit(" " + tg + " = sp_PolyArray_get(" + tp + ", " + ti + ");")
35518+ emit(" }")
35519+ val_boxed = chained_or_assign_rhs_box(@nd_expression[nid])
35520+ emit(" if (" + tg + ".tag == SP_TAG_NIL) {")
35521+ emit(" " + tg + " = " + val_boxed + ";")
35522+ emit(" if (" + tp + ") {")
35523+ emit(" while (sp_PolyArray_length(" + tp + ") <= " + ti + ") { sp_PolyArray_push(" + tp + ", sp_box_nil()); }")
35524+ emit(" sp_PolyArray_set(" + tp + ", " + ti + ", " + tg + ");")
35525+ emit(" }")
35526+ emit(" }")
35527+ tg
35528+ end
35529+
3541035530 # Return a C expression that evaluates to the inspected form of `val`
3541135531 # (a value of inferred Ruby type `at`), following Ruby's Object#inspect
3541235532 # contract. Returns "" when `at` has no inspect implementation yet, so