Commit 78396742

7839674216edMerged

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

Description

feat(codegen): box built-in pointer types into poly via reserved cls_id

box_expr_to_poly only knew how to box scalars and obj_<UserClass> values. Anything else fell through to sp_box_int(val), which silently miscompiled when val was a pointer — passing a sp_IntArray * where sp_box_int wants mrb_int is a C type error, so this program didn't compile:

def kind_of(a); a.nil? ? "nil" : "something"; end puts kind_of(Object.new) puts kind_of([1, 2, 3])

Rather than introduce one new SP_TAG_* per built-in pointer type (which doesn't scale — Array<Float>, Array<Array<int>> etc. all need their own slot), reserve a small set of negative cls_id constants under the existing SP_TAG_OBJ:

SP_BUILTIN_INT_ARRAY (-1) SP_BUILTIN_FLOAT_ARRAY (-2) SP_BUILTIN_STR_ARRAY (-3) SP_BUILTIN_SYM_ARRAY (-4) SP_BUILTIN_PTR_ARRAY (-5)

with thin sp_box_int_array(p) etc. wrappers that delegate to sp_box_obj(p, SP_BUILTIN_*). box_expr_to_poly now picks the matching wrapper for these types. Adding more built-ins is one new constant + one wrapper.

This commit lets a poly-typed parameter accept built-in array values; dispatching methods on a poly value whose tag is a built-in is the follow-up.

Adds test/poly_box_int_array.rb as a small reproducer.

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

Details

Commit
7839674216ed
Parent
3936afd5b0ed

lib/sp_runtime.hmodifiedView file

@@ -544,6 +544,17 @@
544544 #define SP_TAG_NIL 4
545545 #define SP_TAG_OBJ 5
546546 #define SP_TAG_SYM 6
547+/* Negative cls_id values let SP_TAG_OBJ also carry built-in pointer
548+ types (IntArray, FloatArray, ...) — avoids minting a new SP_TAG_*
549+ per type. Non-negative cls_id stays an index into the user-class
550+ table as before. The element-type tag and the array cls_id are
551+ paired by `array_cls_id = -element_tag - 1`. */
552+#define SP_BUILTIN_ARRAY_OF(tag) (-(tag) - 1)
553+#define SP_BUILTIN_INT_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_INT) /* -1 */
554+#define SP_BUILTIN_STR_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_STR) /* -2 */
555+#define SP_BUILTIN_FLT_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_FLT) /* -3 */
556+#define SP_BUILTIN_PTR_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_OBJ) /* -6 */
557+#define SP_BUILTIN_SYM_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_SYM) /* -7 */
547558 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;
548559 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; }
549560 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; }
@@ -552,6 +563,13 @@
552563 static sp_RbVal sp_box_nil(void) { sp_RbVal r; r.tag = SP_TAG_NIL; r.cls_id = 0; r.v.i = 0; return r; }
553564 static sp_RbVal sp_box_obj(void *p, int cls_id) { sp_RbVal r; r.tag = SP_TAG_OBJ; r.cls_id = cls_id; r.v.p = p; return r; }
554565 static sp_RbVal sp_box_sym(sp_sym v) { sp_RbVal r; r.tag = SP_TAG_SYM; r.cls_id = 0; r.v.i = (mrb_int)v; return r; }
566+/* Built-in pointer boxes — share SP_TAG_OBJ with a reserved negative
567+ cls_id so the dispatch path is uniform. */
568+static sp_RbVal sp_box_int_array(void *p) { return sp_box_obj(p, SP_BUILTIN_INT_ARRAY); }
569+static sp_RbVal sp_box_float_array(void *p) { return sp_box_obj(p, SP_BUILTIN_FLT_ARRAY); }
570+static sp_RbVal sp_box_str_array(void *p) { return sp_box_obj(p, SP_BUILTIN_STR_ARRAY); }
571+static sp_RbVal sp_box_sym_array(void *p) { return sp_box_obj(p, SP_BUILTIN_SYM_ARRAY); }
572+static sp_RbVal sp_box_ptr_array(void *p) { return sp_box_obj(p, SP_BUILTIN_PTR_ARRAY); }
555573 static void sp_poly_puts(sp_RbVal v) {
556574 switch (v.tag) {
557575 case SP_TAG_INT: printf("%lld\n", (long long)v.v.i); break;