Commit abe0cdc1

abe0cdc196baMerged

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

Description

Merge pull request #300 from mame/pr/map-block-nested-array

feat(codegen): map block returning nested array preserves cls_id chain

Details

Commit
abe0cdc196ba
Parent
9f9082b5133b
Merge parent
705f5df8df2f

spinel_codegen.rbmodifiedView file

@@ -3608,21 +3608,22 @@
36083608 if is_obj_type(bret) == 1
36093609 return bret + "_ptr_array"
36103610 end
3611- # Block returns an array (e.g.
3612- # `[1, 6].map { (0..n).map { ... } }` — each outer
3613- # element is itself an int_array). The outer map's
3614- # result is then an array of arrays, encoded as
3615- # `<inner>_ptr_array`. Only fire this for array-shaped
3616- # recv (where the elements have a definite shape and
3617- # ivar typing reaches the ptr_array placeholder); for
3618- # range/int recv we fall through to the int_array
3619- # placeholder below so a follow-up int-typed assignment
3620- # to the same ivar still type-checks.
3611+ # Block returns a 1D array (e.g.
3612+ # `[1, 6].map { (0..n).map { i } }` or
3613+ # `(0..3).map { |i| (0..3).map { ... } }`) — each
3614+ # outer element is itself a 1D typed array. Encode
3615+ # as `<inner>_ptr_array` for the standard
3616+ # `arr[i][j]` dispatch shape.
36213617 if bret == "int_array" || bret == "float_array" || bret == "str_array" || bret == "sym_array"
3622- rt_recv_p = infer_type(recv)
3623- if rt_recv_p != "range" && rt_recv_p != "int"
3624- return bret + "_ptr_array"
3625- end
3618+ return bret + "_ptr_array"
3619+ end
3620+ # Block returns a deeper-nested array
3621+ # (`int_array_ptr_array`, `poly_array`). Encode as
3622+ # `poly_array` — cls_id tagging chain on the inner
3623+ # PolyArray pushes preserves elem type for
3624+ # `arr[i][j][k]` 3D dispatch.
3625+ if is_ptr_array_type(bret) == 1 || bret == "poly_array"
3626+ return "poly_array"
36263627 end
36273628 # poly_array bret intentionally falls through. Returning
36283629 # poly_array_ptr_array would be more accurate, but ivars
@@ -31513,13 +31514,15 @@
3151331514 end
3151431515 end
3151531516 end
31516- # Only handle scalar block returns inline; an array-returning
31517- # block (`(0..n).map { |i| [i, ...] }`) would make this an
31518- # array-of-arrays, which the int/str/float push helpers can't
31519- # express. Fall through to the "0" placeholder for those — the
31520- # generic int_array typing keeps any follow-up `@x = [...]`
31521- # assignment to the same slot type-checking.
31522- if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float"
31517+ # Scalar block returns push into the matching typed array.
31518+ # Array-returning blocks (`(0..n).map { |i| [...] }`) produce
31519+ # an array-of-arrays — store each block result as a pointer
31520+ # in `<X>_ptr_array`. Block return types we can't express
31521+ # fall through to a "0" placeholder.
31522+ block_is_typed_array = (block_ret_r == "int_array" || block_ret_r == "str_array" ||
31523+ block_ret_r == "float_array" || block_ret_r == "sym_array" ||
31524+ is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array")
31525+ if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float" && block_is_typed_array == false
3152331526 return "0"
3152431527 end
3152531528 @needs_int_array = 1
@@ -31531,6 +31534,19 @@
3153131534 elsif block_ret_r == "float"
3153231535 @needs_float_array = 1
3153331536 emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")
31537+ elsif block_is_typed_array
31538+ # Array-of-<X>: 1D-block-return uses PtrArray (matches
31539+ # the inferred `<bret>_ptr_array` type). Deeper nesting
31540+ # (block already returns ptr_array / poly_array) uses
31541+ # PolyArray with `box_value_to_poly` boxing — the
31542+ # cls_id chain stays tagged for `arr[i][j][k]...`
31543+ # dispatch.
31544+ if is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array"
31545+ @needs_rb_value = 1
31546+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
31547+ else
31548+ emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
31549+ end
3153431550 else
3153531551 emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
3153631552 end
@@ -31594,6 +31610,12 @@
3159431610 emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_r + ");")
3159531611 elsif block_ret_r == "float"
3159631612 emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_r + ");")
31613+ elsif block_is_typed_array
31614+ if is_ptr_array_type(block_ret_r) == 1 || block_ret_r == "poly_array"
31615+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret_r, lastv_r) + ");")
31616+ else
31617+ emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)" + lastv_r + ");")
31618+ end
3159731619 else
3159831620 emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_r + ");")
3159931621 end
@@ -31692,9 +31714,15 @@
3169231714 # `[1, 6].map { (0..n).map { ... } }`) — accumulator must
3169331715 # be a PtrArray so GC keeps each element alive (an IntArray
3169431716 # accumulator with pointer-payload mrb_ints would let the
31695- # inner arrays be collected).
31717+ # inner arrays be collected). Deeper nesting (block_ret is
31718+ # already a ptr_array / poly_array) uses PolyArray boxing
31719+ # so the cls_id chain stays tagged for `arr[i][j][k]...`.
3169631720 ret_is_arr2 = (block_ret == "int_array" || block_ret == "float_array" || block_ret == "str_array" || block_ret == "sym_array")
31697- if ret_is_arr2
31721+ ret_is_deep_arr2 = (is_ptr_array_type(block_ret) == 1 || block_ret == "poly_array")
31722+ if ret_is_deep_arr2
31723+ @needs_rb_value = 1
31724+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
31725+ elsif ret_is_arr2
3169831726 @needs_ptr_array = 1
3169931727 emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
3170031728 else
@@ -31715,7 +31743,28 @@
3171531743 if stmts3.length > 0
3171631744 last = stmts3[stmts3.length - 1]
3171731745 val = compile_expr(last)
31718- if ret_is_arr2
31746+ if ret_is_deep_arr2
31747+ # The block returns a typed `<X>_ptr_array` (e.g.
31748+ # `int_array_ptr_array`). The static type carries
31749+ # elem-type info but `sp_box_ptr_array(val)` would
31750+ # erase it (cls_id PTR_ARRAY only). Convert to
31751+ # PolyArray inline so each inner element gets its
31752+ # proper `sp_box_<elem>` tag — `arr[i][j][k]`
31753+ # dispatch then chains correctly.
31754+ if is_ptr_array_type(block_ret) == 1
31755+ inner_t = ptr_array_elem_type(block_ret)
31756+ conv = new_temp
31757+ ii = new_temp
31758+ emit(" sp_PolyArray *" + conv + " = sp_PolyArray_new();")
31759+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < sp_PtrArray_length(" + val + "); " + ii + "++) {")
31760+ inner_get = "(" + c_type(inner_t) + ")sp_PtrArray_get(" + val + ", " + ii + ")"
31761+ emit(" sp_PolyArray_push(" + conv + ", " + box_value_to_poly(inner_t, inner_get) + ");")
31762+ emit(" }")
31763+ emit(" sp_PolyArray_push(" + tmp_arr + ", sp_box_poly_array(" + conv + "));")
31764+ else
31765+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + box_value_to_poly(block_ret, val) + ");")
31766+ end
31767+ elsif ret_is_arr2
3171931768 emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)(" + val + "));")
3172031769 else
3172131770 emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")