Commit 35ab107f

35ab107f4c5bMerged

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

Description

feat(codegen): nested array literals (3D+) via PolyArray-of-PolyArray

3D array literals [[[a, b], [c, d]], [[e, f], [g, h]]] infer the second-level [[a, b], [c, d]] as int_array_ptr_array, but the outer level used to box each element via sp_box_ptr_array — losing the elem-type info, so the next [] read returned an unknown-cls_id obj that fell through every dispatch arm to sp_box_nil().

Fix: when an array literal's element is itself a typed <X>_ptr_array (or already poly_array), recompile that inner literal as poly_array instead. Each level boxes via sp_box_poly_array / sp_box_int_array / etc., so the cls_id chain stays tagged and arr[i][j][k] recurses through three SP_BUILTIN_POLY_ARRAY / SP_BUILTIN_INT_ARRAY dispatch arms.

Adds compile_array_literal_as_poly (recursive helper) and wires it into all three IVW-with-ArrayNode-RHS paths (compile_array_literal's poly_array branch, emit_constructor's IVW path, and compile_stmt's general IVW path).

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

Details

Commit
35ab107f4c5b
Parent
0fc98c358452

spinel_codegen.rbmodifiedView file

@@ -2116,6 +2116,18 @@
21162116 end
21172117 return "poly_array"
21182118 end
2119+ # Nested-deeper case: elements are themselves a typed
2120+ # ptr_array (`int_array_ptr_array`, etc.) or already
2121+ # poly_array. Spinel doesn't have a typed
2122+ # `<X>_ptr_array_ptr_array` slot, so box each level via
2123+ # poly_array — sp_box_*_array on each push, and the
2124+ # poly-builtin dispatch on `[]` recurses into the next
2125+ # level.
2126+ if is_ptr_array_type(et) == 1 || et == "poly_array"
2127+ @needs_gc = 1
2128+ @needs_rb_value = 1
2129+ return "poly_array"
2130+ end
21192131 # Check if elements have mixed types
21202132 k = 1
21212133 while k < elems.length
@@ -15401,9 +15413,17 @@
1540115413 while ek < elems_iv.length
1540215414 eid = elems_iv[ek]
1540315415 et = infer_type(eid)
15404- ev = compile_expr(eid)
15405- ebox = et == "poly" ? ev : box_value_to_poly(et, ev)
15406- emit_raw(" sp_PolyArray_push(" + tmp_iv + ", " + ebox + ");")
15416+ # 3D+ shape: nested ArrayNode with ptr_array elem
15417+ # type → recompile as poly_array so cls_id chain
15418+ # stays tagged through every level.
15419+ if @nd_type[eid] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array")
15420+ inner_iv = compile_array_literal_as_poly(eid)
15421+ emit_raw(" sp_PolyArray_push(" + tmp_iv + ", sp_box_poly_array(" + inner_iv + "));")
15422+ else
15423+ ev = compile_expr(eid)
15424+ ebox = et == "poly" ? ev : box_value_to_poly(et, ev)
15425+ emit_raw(" sp_PolyArray_push(" + tmp_iv + ", " + ebox + ");")
15426+ end
1540715427 ek = ek + 1
1540815428 end
1540915429 emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";")
@@ -24632,6 +24652,34 @@
2463224652 "(!" + cond + " ? " + then_val + " : 0)"
2463324653 end
2463424654
24655+ # Compile an `ArrayNode` literal as `sp_PolyArray *`, regardless
24656+ # of the inferred elem type. Used by the nested-array path in
24657+ # the poly_array branch so 3D-and-deeper arrays preserve cls_id
24658+ # tags through every level (e.g. an inner `[1,2,3]` ends up
24659+ # boxed via `sp_box_int_array` so the dispatch can still
24660+ # `sp_IntArray_get` on it).
24661+ def compile_array_literal_as_poly(nid)
24662+ @needs_gc = 1
24663+ @needs_rb_value = 1
24664+ elems = parse_id_list(@nd_elements[nid])
24665+ tmp = new_temp
24666+ emit(" sp_PolyArray *" + tmp + " = sp_PolyArray_new();")
24667+ k = 0
24668+ while k < elems.length
24669+ eid = elems[k]
24670+ et = infer_type(eid)
24671+ if @nd_type[eid] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array")
24672+ inner = compile_array_literal_as_poly(eid)
24673+ emit(" sp_PolyArray_push(" + tmp + ", sp_box_poly_array(" + inner + "));")
24674+ else
24675+ val = compile_expr(eid)
24676+ emit(" sp_PolyArray_push(" + tmp + ", " + box_value_to_poly(et, val) + ");")
24677+ end
24678+ k = k + 1
24679+ end
24680+ tmp
24681+ end
24682+
2463524683 def compile_array_literal(nid)
2463624684 @needs_gc = 1
2463724685 elems = parse_id_list(@nd_elements[nid])
@@ -24670,8 +24718,21 @@
2467024718 k = 0
2467124719 while k < elems.length
2467224720 et = infer_type(elems[k])
24673- val = compile_expr(elems[k])
24674- emit(" sp_PolyArray_push(" + tmp + ", " + box_value_to_poly(et, val) + ");")
24721+ # 3D+ shape: when the element is itself a typed
24722+ # `<X>_ptr_array` that came from a nested ArrayNode, the
24723+ # default `compile_array_literal` for that element would
24724+ # emit a typed sp_PtrArray and `sp_box_ptr_array` would
24725+ # erase the elem type (cls_id PTR_ARRAY). Recompile the
24726+ # nested literal as poly_array so the next-level dispatch
24727+ # sees `cls_id == POLY_ARRAY` and recurses with each
24728+ # innermost typed array still tagged correctly.
24729+ if @nd_type[elems[k]] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array")
24730+ val = compile_array_literal_as_poly(elems[k])
24731+ emit(" sp_PolyArray_push(" + tmp + ", sp_box_poly_array(" + val + "));")
24732+ else
24733+ val = compile_expr(elems[k])
24734+ emit(" sp_PolyArray_push(" + tmp + ", " + box_value_to_poly(et, val) + ");")
24735+ end
2467524736 k = k + 1
2467624737 end
2467724738 return tmp
@@ -25265,9 +25326,17 @@
2526525326 while ek < elems_pa.length
2526625327 eid = elems_pa[ek]
2526725328 et = infer_type(eid)
25268- ev = compile_expr(eid)
25269- ebox = et == "poly" ? ev : box_value_to_poly(et, ev)
25270- emit(" sp_PolyArray_push(" + tmp_arr + ", " + ebox + ");")
25329+ # 3D+ shape: nested ArrayNode with ptr_array element
25330+ # type — recompile as poly_array so cls_id chain stays
25331+ # tagged through every level.
25332+ if @nd_type[eid] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array")
25333+ inner = compile_array_literal_as_poly(eid)
25334+ emit(" sp_PolyArray_push(" + tmp_arr + ", sp_box_poly_array(" + inner + "));")
25335+ else
25336+ ev = compile_expr(eid)
25337+ ebox = et == "poly" ? ev : box_value_to_poly(et, ev)
25338+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + ebox + ");")
25339+ end
2527125340 ek = ek + 1
2527225341 end
2527325342 emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")