Commit f934eea0
f934eea0a9ddMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
feat(codegen): built-in class prefix + primitive is_a? (#404 Phase 3 Tier 4)
Tier 4 reserves cls_ids 0..20 for Ruby primitive classes per
docs/CLASS-OBJECT.md and shifts user-class entries to the
[BC .. BC+N-1] range, with modules at [BC+N .. BC+N+M-1]. The
runtime sp_class_for_poly helper now maps every poly tag to
its built-in cls_id, unlocking 5.is_a?(Integer) and friends
on a poly primitive recv with a dynamic class arg.
Built-in entries (cls_ids 0..20): BasicObject, Object, Kernel, Comparable, Enumerable, NilClass, TrueClass, FalseClass, Numeric, Integer, Float, String, Symbol, Array, Hash, Range, Time, Module, Class, Complex, Proc.
Method is intentionally not reserved -- spinel's register_builtin_classes already pushes it as the first @cls_names entry, and giving it both a built-in cls_id and a user-class cls_id would diverge. With the BC=21 prefix it lands at cls_id 21 (the first user-class slot), where the constructor stamps and dispatch matches uniformly.
Implementation outline:
- codegen init: @builtin_class_names / parents / includes arrays, BC = 21.
- cls_id_for_user_internal(ci) -> BC + ci; module variant
adds @cls_names.length on top. Used at every cls_id emit:
((sp_Class){<n>LL})literals,self->cls_id = <n>in constructors,case <n>LL:in #422 chained dispatch,cls_id == <n>in poly_method_call user-class arms, sp_box_obj / sp_box_nullable_obj. - emit_class_runtime: sp_class_names / parents / ancestors tables prepended with the 21 built-in entries. Parents and includes hardcoded from the design-doc MRO; ancestors computed by compute_ancestors_for_unified extended with a built-in arm.
- sp_class_for_poly: each SP_TAG_* mapped to its built-in cls_id; SP_TAG_OBJ keeps the runtime cls_id for user classes and maps SP_BUILTIN_* to Array (13) / Hash (14) / Range (15) / Time (16) / Proc (20).
- Box-with-runtime-cls_id: a new sp_obj_cls_id_of(void *) helper in lib/sp_runtime.h reads the cls_id field off any user-class struct (guaranteed at offset 0 by #422). Used at the PtrArray dispatch sites in compile_poly_method_call and the each-block iteration emit -- the pre-Tier-4 stamp of cls_id=0 now collides with BasicObject, breaking downstream dispatch over the boxed element.
- ConstantReadNode (codegen): built-in class const -> reserved cls_id; infer_type (analyze): new is_builtin_class_const_name helper returns "class" for the same names so the value flows through the sp_Class path.
Coverage in test/class_hierarchy_builtin.rb:
- Integer.to_s / Float.to_s / String.to_s / Array.to_s via the prepended names table.
- Integer < Numeric, Integer <= Object, Integer < Comparable (transitive via Numeric's Comparable include).
- 5.is_a?(int_klass) / 5.is_a?(num_klass) / "hi".is_a?(str_klass) where klass is a Class-typed local -- the dynamic-arg path from Tier 3 now resolves primitives via the sp_class_for_poly mapping instead of returning the sp_Class{-1} sentinel.
Optcarrot: checksum 59662, fps 653.4 (within noise). Bootstrap 4-way fixpoint OK. 378 tests pass.
Refs #404 Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- f934eea0a9dd
- Parent
- b4e4e4869fe2
lib/sp_runtime.hmodifiedView file
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; }/* Issue #404 Phase 3: box a sp_Class into a poly slot. */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; }/* Issue #404 Phase 3 Tier 4: every non-value-type sp_<C> starts with `mrb_int cls_id` (#422). Read it back from a void* when the static type at the call site has lost the cls_id (e.g. sp_PtrArray_get returning void*). NULL-safe via the guard on p; expects p to actually point at a user-class struct when non-NULL. */static inline mrb_int sp_obj_cls_id_of(void *p) { return p ? *(mrb_int *)p : 0; }static sp_RbVal sp_box_nullable_str(const char *v) { return v ? sp_box_str(v) : sp_box_nil(); }static sp_RbVal sp_box_nullable_obj(void *p, int cls_id) { return p ? sp_box_obj(p, cls_id) : sp_box_nil(); }/* Built-in pointer boxes — share SP_TAG_OBJ with a reserved negativespinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/class_hierarchy_builtin.rbaddedView file
Loading diff…
test/class_hierarchy_builtin.rb.expectedaddedView file
Loading diff…