Commit cf6b313f

cf6b313fb6e8Merged

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

Description

fix(codegen): destructure poly RHS for multi-write and auto-splat poly recv each block

Two adjacent gaps in the post-#381 poly path produced the shared symptom — locals wired up as mrb_int with truncated contents, so subsequent length / to_i / << calls fell off poly dispatch and emitted literal 0:

  1. multi_write_target_type had a poly_array arm but no poly arm. When the RHS is poly (e.g. @h[k][i] where @h is a poly_poly_hash whose values are poly_arrays), each LHS local fell through to the int default and emit_multi_write_target truncated the compile_multi_write poly arm's sp_PolyArray_get result via unbox_poly_to("int", ...).

  2. compile_each_block's rt == "poly" arm only assigned bp1 and silently discarded bp2, even when the yielded element was itself an Array. CRuby auto-splats Array elements across multi-param blocks; mirror that at runtime by branching on val.cls_id for the two element shapes the destructuring naturally produces — SP_BUILTIN_POLY_ARRAY (heterogeneous element types) and SP_BUILTIN_INT_ARRAY (both elements ints, the common [i, fixed] shape).

Together these unblock optcarrot's setup_lut returning to its original 4-level chained-||= form on top of #381's collection support.

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

Details

Commit
cf6b313fb6e8
Parent
3ee4eed9c742

spinel_codegen.rbmodifiedView file

@@ -5187,6 +5187,15 @@
51875187 if rt == "poly_array"
51885188 return "poly"
51895189 end
5190+ if rt == "poly"
5191+ # RHS evaluates to sp_RbVal (e.g. `@h[k][i]` where @h is a
5192+ # poly_poly_hash whose values are poly_arrays of inner poly
5193+ # elements). Mirror compile_multi_write's `val_t_local == "poly"`
5194+ # arm: unbox to sp_PolyArray * at runtime, fetch each slot via
5195+ # sp_PolyArray_get returning sp_RbVal, so each target slot is
5196+ # typed `poly` and stays boxed for downstream poly dispatch.
5197+ return "poly"
5198+ end
51905199 "int"
51915200 end
51925201
@@ -36417,12 +36426,36 @@
3641736426 emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_PTR_ARRAY) " + val_tmp + " = sp_box_obj(sp_PtrArray_get((sp_PtrArray *)" + poly_tmp + ".v.p, " + idx_tmp + "), 0);")
3641836427 emit(" else if (" + poly_tmp + ".cls_id == SP_BUILTIN_POLY_ARRAY) " + val_tmp + " = sp_PolyArray_get((sp_PolyArray *)" + poly_tmp + ".v.p, " + idx_tmp + ");")
3641936428 if has_bp == 1
36420- emit(" sp_RbVal lv_" + bp1 + " = " + val_tmp + ";")
36429+ if bp2 != ""
36430+ # Multi-param block over a poly receiver: CRuby auto-splats
36431+ # when the yielded element is itself an Array. Mirror that:
36432+ # if the runtime element is a typed array, distribute its
36433+ # first two slots across bp1/bp2; otherwise bp1 takes the
36434+ # whole element and bp2 stays nil.
36435+ emit(" sp_RbVal lv_" + bp1 + " = sp_box_nil();")
36436+ emit(" sp_RbVal lv_" + bp2 + " = sp_box_nil();")
36437+ emit(" if (" + val_tmp + ".tag == SP_TAG_OBJ && " + val_tmp + ".cls_id == SP_BUILTIN_POLY_ARRAY) {")
36438+ emit(" sp_PolyArray *_pa = (sp_PolyArray *)" + val_tmp + ".v.p;")
36439+ emit(" if (sp_PolyArray_length(_pa) > 0) lv_" + bp1 + " = sp_PolyArray_get(_pa, 0);")
36440+ emit(" if (sp_PolyArray_length(_pa) > 1) lv_" + bp2 + " = sp_PolyArray_get(_pa, 1);")
36441+ emit(" } else if (" + val_tmp + ".tag == SP_TAG_OBJ && " + val_tmp + ".cls_id == SP_BUILTIN_INT_ARRAY) {")
36442+ emit(" sp_IntArray *_ia = (sp_IntArray *)" + val_tmp + ".v.p;")
36443+ emit(" if (sp_IntArray_length(_ia) > 0) lv_" + bp1 + " = sp_box_int(sp_IntArray_get(_ia, 0));")
36444+ emit(" if (sp_IntArray_length(_ia) > 1) lv_" + bp2 + " = sp_box_int(sp_IntArray_get(_ia, 1));")
36445+ emit(" } else {")
36446+ emit(" lv_" + bp1 + " = " + val_tmp + ";")
36447+ emit(" }")
36448+ else
36449+ emit(" sp_RbVal lv_" + bp1 + " = " + val_tmp + ";")
36450+ end
3642136451 end
3642236452 @indent = @indent + 2
3642336453 push_scope
3642436454 if has_bp == 1
3642536455 declare_var(bp1, "poly")
36456+ if bp2 != ""
36457+ declare_var(bp2, "poly")
36458+ end
3642636459 end
3642736460 redo_label = push_redo_label
3642836461 emit_redo_label(redo_label)