Commit bfbcb558

bfbcb5589663Merged

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

Description

Let a poly value carry a bigint (SP_TAG_BIGINT)

The poly value (sp_RbVal) had no bigint tag, so a bignum stored in a heterogeneous Array or Hash boxed as nil -- [1, "x", 2**70] lost its last element, {k: big} lost its value. Add SP_TAG_BIGINT (v.p is a GC-allocated sp_Bigint*) and make a boxed bigint a first-class poly element:

  • sp_box_bigint constructor + emit_boxed / emit_boxed_text / emit_unbox_text TY_BIGINT cases (the array/hash literal path has its own boxing switch, which is where the nil came from);
  • GC marks the boxed pointer (sp_mark_rbval);
  • the poly consumers grow a bigint arm: puts / to_s / inspect / class_name (Integer) / to_i / to_f / numeric_p, and the hash key helpers (sp_rbval_hash_key / _eql_key);
  • sp_poly_eq / sp_poly_cmp compare bigints (and mixed int/bigint) through sp_bigint_cmp rather than the truncating float path, via a total sp_poly_as_bigint coercion;
  • codegen's bigint comparison/arith operand emission goes through a new emit_bigint_operand helper so a poly operand (a retrieved container element) narrows with sp_poly_as_bigint instead of being miscompiled as sp_bigint_new_int(<sp_RbVal>).

Verified under clang+ASAN/UBSAN with GC stress (boxed bigints survive collection). This is additive and independent of --int-overflow=promote; it is also the representation the poly-based promote work will build on.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
bfbcb5589663
Parent
9e5a124ac71b

lib/sp_gc.hmodifiedView file

@@ -25,6 +25,7 @@
2525 #define SP_TAG_SYM 6
2626 #define SP_TAG_CLASS 7
2727 #define SP_TAG_ENCODING 8
28+#define SP_TAG_BIGINT 9 /* v.p is a GC-allocated sp_Bigint* */
2829 /* SP_TAG_OBJ cls_id sentinel for an opaque foreign/FFI pointer (e.g. a
2930 ffi_read_ptr / ffi func ptr return). It is NOT a sp_gc_alloc allocation, so
3031 the collector must not trace it -- sp_mark_rbval skips it. Kept here (not with
@@ -84,6 +85,7 @@
8485 static inline void sp_mark_rbval(sp_RbVal v) {
8586 if (v.tag == SP_TAG_STR) sp_mark_string(v.v.s);
8687 else if (v.tag == SP_TAG_OBJ && v.cls_id != SP_BUILTIN_FOREIGN_PTR) sp_gc_mark(v.v.p);
88+ else if (v.tag == SP_TAG_BIGINT) sp_gc_mark(v.v.p);
8789 }
8890 /* Closure-cell content markers. A captured non-int local is laundered into the
8991 pointer-sized mrb_int cell as (uintptr_t)<ptr>; the cell's GC scan marks the