Commit 0fc98c35

0fc98c358452Merged

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

Description

feat(codegen): SP_BUILTIN_POLY_ARRAY for poly-boxed sp_PolyArray

When a sp_PolyArray * value is passed where an sp_RbVal poly is expected (e.g. as the peek arg of add_mappings(addr, @ram, ...) in optcarrot's CPU mapping), box_value_to_poly previously fell through to the generic pointer path and stamped cls_id 0. The matching [] / length dispatch paths only checked for INT/FLT/ STR/SYM/PTR_ARRAY and Method (cls_id 24), so the cls_id-0 read was ignored and the dispatch returned nil — silently dropping every read on poly-array-backed memory ranges.

  • Reserve SP_BUILTIN_POLY_ARRAY (-10) and add a matching sp_box_poly_array helper alongside the other sp_box_*_array builders.
  • Box poly_array values via the new tag in both nullable and non-nullable branches of box_value_to_poly.
  • Dispatch the new cls_id in emit_poly_builtin_dispatch's [] arm (returns sp_RbVal directly when caller wants poly, else unboxes via .v.i) and in length / size.
  • emit_constructor: when an ivar slot is poly_array (or PtrArray) but the initializer is [nil] * N (a sized empty default), emit a sized container of sp_box_nil() / NULL placeholders so later PolyArray_set writes land on storage that matches the slot type.

Details

Commit
0fc98c358452
Parent
a0864f361051

lib/sp_runtime.hmodifiedView file

@@ -694,6 +694,7 @@
694694 #define SP_BUILTIN_PROC (-9) /* sp_Proc *, distinct from any tag-based id */
695695 #define SP_BUILTIN_RANGE (-10) /* sp_Range *, heap copy of stack-typed sp_Range when crossing into poly */
696696 #define SP_BUILTIN_TIME (-11) /* sp_Time *, heap copy of stack-typed sp_Time when crossing into poly */
697+#define SP_BUILTIN_POLY_ARRAY (-12) /* sp_PolyArray *, array of sp_RbVal */
697698 typedef struct { int tag; int cls_id; union { mrb_int i; const char *s; mrb_float f; mrb_bool b; void *p; } v; } sp_RbVal;
698699 static sp_RbVal sp_box_int(mrb_int v) { sp_RbVal r; r.tag = SP_TAG_INT; r.cls_id = 0; r.v.i = v; return r; }
699700 static sp_RbVal sp_box_str(const char *v) { sp_RbVal r; r.tag = SP_TAG_STR; r.cls_id = 0; r.v.s = v; return r; }
@@ -751,6 +752,7 @@
751752 }
752753 return buf;
753754 }
755+static sp_RbVal sp_box_poly_array(void *p) { return sp_box_obj(p, SP_BUILTIN_POLY_ARRAY); }
754756 static void sp_poly_puts(sp_RbVal v) {
755757 switch (v.tag) {
756758 case SP_TAG_INT: printf("%lld\n", (long long)v.v.i); break;