Commit 240333ca

240333ca4623Merged

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

Description

feat(codegen): Class#superclass / #ancestors / <, <=, > (#404 Phase 3 Tier 1)

Phase 3 of the Class-object work lands user-class hierarchy introspection on the sp_Class value: precomputed parents + flat ancestors table emitted per program, runtime helpers that walk those tables, and codegen arms that lower the Class-recv hierarchy methods.

What works after this commit:

  • Foo.superclass returns a sp_Class for the parent (or the {-1} sentinel for layout-root classes, which stringifies to "").
  • Foo.ancestors returns a sp_PolyArray whose elements are boxed sp_Class values (new SP_TAG_CLASS poly tag + sp_box_class helper). Iteration via each |klass|; klass.to_s; end lands on the program-emitted sp_class_to_s via a new SP_TAG_CLASS arm in sp_poly_to_s.
  • Foo < Bar, Foo <= Bar, Foo > Bar, Foo >= Bar -- proper / non-proper subclass checks via sp_class_lt / sp_class_le over the flat ancestors table.

Out of scope for this tier (deferred follow-ups on #404):

  • Module / include integration. @cls_kinds and @cls_includes are not yet collected; ancestors currently walk parent chains only. Mixing in a module shows up as "module is unknown to the class table" -- the module hierarchy stays in the parallel @module_names space until the next tier unifies cls_id numbering.
  • Dynamic is_a?(C) against a poly receiver -- needs the same ancestors-table lookup but routed through sp_class_for_poly to recover the runtime cls_id of a poly value.
  • case obj when Klass1, Klass2 on a Class subject -- same sp_class_le machinery, threaded through case-when's WhenNode condition compile.

Emission policy: Phase 1 / 2's "always emit when @cls_names > 0, let attribute((unused)) + link-time DCE drop the dead helpers" policy carries over. emit_class_runtime runs before compile_expr toggles per-feature flags, so flag-gated emission would always miss; the residual cost is a few hundred bytes of read-only data per program with classes (Optcarrot self-host: checksum 59662 unchanged, fps within noise).

To keep the SP_TAG_CLASS arm in sp_poly_to_s (lib/sp_runtime.h) linkable for programs that never declare a user class, the class names table + sp_class_to_s now emit even when @cls_names is empty (sentinel 1-slot array; ISO C forbids a zero-length brace initializer). Programs without classes never construct a SP_TAG_CLASS value, so the arm stays cold but the forward decl resolves.

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

Details

Commit
240333ca4623
Parent
5d13c0299186

lib/sp_runtime.hmodifiedView file

@@ -908,6 +908,11 @@
908908 #define SP_TAG_NIL 4
909909 #define SP_TAG_OBJ 5
910910 #define SP_TAG_SYM 6
911+/* Issue #404 Phase 3: Class values boxed into a poly slot (e.g.
912+ as ancestors-array elements). cls_id of the sp_RbVal carries
913+ the boxed sp_Class's cls_id directly so unboxing is just a
914+ field read. */
915+#define SP_TAG_CLASS 7
911916 /* Negative cls_id values let SP_TAG_OBJ also carry built-in pointer
912917 types (IntArray, FloatArray, ...) — avoids minting a new SP_TAG_*
913918 per type. Non-negative cls_id stays an index into the user-class
@@ -941,6 +946,8 @@
941946 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; }
942947 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; }
943948 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; }
949+/* Issue #404 Phase 3: box a sp_Class into a poly slot. */
950+static sp_RbVal sp_box_class(sp_Class c) { sp_RbVal r; r.tag = SP_TAG_CLASS; r.cls_id = (int)c.cls_id; r.v.i = c.cls_id; return r; }
944951 static sp_RbVal sp_box_nullable_str(const char *v) { return v ? sp_box_str(v) : sp_box_nil(); }
945952 static sp_RbVal sp_box_nullable_obj(void *p, int cls_id) { return p ? sp_box_obj(p, cls_id) : sp_box_nil(); }
946953 /* Built-in pointer boxes — share SP_TAG_OBJ with a reserved negative
@@ -1021,6 +1028,11 @@
10211028 }
10221029 static mrb_bool sp_poly_nil_p(sp_RbVal v) { return v.tag == SP_TAG_NIL; }
10231030 static mrb_bool sp_poly_truthy(sp_RbVal v) { return !(v.tag == SP_TAG_NIL || (v.tag == SP_TAG_BOOL && !v.v.b)); }
1031+/* Issue #404 Phase 3: forward-declare the program-emitted class
1032+ name lookup so sp_poly_to_s's SP_TAG_CLASS arm resolves. The
1033+ codegen emits sp_class_to_s unconditionally (even for programs
1034+ with no user classes), so the link is always satisfied. */
1035+static const char *sp_class_to_s(sp_Class c);
10241036 static const char *sp_poly_to_s(sp_RbVal v) {
10251037 switch (v.tag) {
10261038 case SP_TAG_INT: return sp_int_to_s(v.v.i);
@@ -1029,6 +1041,7 @@
10291041 case SP_TAG_BOOL: return v.v.b ? SPL("true") : SPL("false");
10301042 case SP_TAG_NIL: return sp_str_empty;
10311043 case SP_TAG_SYM: return sp_sym_to_s((sp_sym)v.v.i);
1044+ case SP_TAG_CLASS: { sp_Class c = {v.v.i}; return sp_class_to_s(c); }
10321045 case SP_TAG_OBJ:
10331046 switch (v.cls_id) {
10341047 case SP_BUILTIN_INT_ARRAY: return sp_IntArray_inspect((sp_IntArray *)v.v.p);