Commit b4e4e486

b4e4e4869fe2Merged

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

Description

feat(codegen): dynamic is_a?(klass) on sp_Class value (#404 Phase 3 Tier 3)

Tier 3 lands runtime is_a? dispatch when the class argument is a sp_Class value (variable, parameter, or any non-ConstantReadNode expression typed "class") rather than a literal class constant. The pre-Tier-3 lowering read @nd_name on the arg AST as the bare class name and so silently lost the dynamic case -- obj.is_a?(klass) with klass = SomeClass always returned FALSE.

Changes:

  • lib/sp_runtime.h: forward decl of sp_class_for_poly (the codegen-emitted implementation already lives in emit_class_runtime).
  • emit_class_runtime: emit sp_class_for_poly(sp_RbVal) -> sp_Class. SP_TAG_CLASS unboxes its own cls_id; SP_TAG_OBJ with non-negative cls_id reads the user-class index off the boxed instance; everything else returns the sp_Class{-1} sentinel (primitives don't have user-class cls_ids in the current numbering and continue to flow through the static poly_is_a_tag_check path).
  • compile_introspection_expr (typed obj recv): before the existing static-name resolution, detect arg_t == "class"
    • non-ConstantReadNode and lower to sp_class_le((sp_Class){<recv_ci>LL}, <arg>).
  • compile_poly_method_call (poly recv): same detection, lowered to sp_class_le(sp_class_for_poly(recv), <arg>). instance_of? keeps strict equality via sp_class_eq.

The recv's static type still drives whether the typed-obj or poly arm fires; both rely on the precomputed ancestors table emitted in Tier 1, so the runtime cost is one indirect index plus a small ancestor scan (typical user-class depths are under 10, well within a cache line on the flat ancestors array).

Out of scope, deferred:

  • case obj when Klass1, Klass2 on a Class value subject. The WhenNode lowering currently handles primitive === cases statically; threading the same sp_class_le machinery through it needs more care for the multi-arm shape.
  • 5.is_a?(klass) where klass is dynamic but the recv is a primitive (poly carrying SP_TAG_INT etc.). sp_class_for_poly returns -1 for primitives; the user-class hierarchy lookup won't find them. Needs a built-in cls_id table -- the design doc's "BasicObject .. Proc" prefix -- and renumbers every existing user-class cls_id, deferred to a separate PR.

Optcarrot: checksum 59662 unchanged, fps 644.97 (noise). Bootstrap fixpoint OK.

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

Details

Commit
b4e4e4869fe2
Parent
14dd502c6f36

spinel_codegen.rbmodifiedView file

@@ -6418,6 +6418,15 @@
64186418 emit_raw("static mrb_bool sp_class_lt(sp_Class child, sp_Class anc){if(child.cls_id==anc.cls_id)return FALSE;return sp_class_le(child,anc);}")
64196419 emit_raw("static sp_PolyArray *sp_class_ancestors_arr(sp_Class c) __attribute__((unused));")
64206420 emit_raw("static sp_PolyArray *sp_class_ancestors_arr(sp_Class c){sp_PolyArray*r=sp_PolyArray_new();if(c.cls_id<0||c.cls_id>=SP_CLASS_COUNT)return r;mrb_int s=sp_class_ancestors_off[c.cls_id];mrb_int e=sp_class_ancestors_off[c.cls_id+1];for(mrb_int k=s;k<e;k++){sp_Class el={sp_class_ancestors_flat[k]};sp_PolyArray_push(r,sp_box_class(el));}return r;}")
6421+ # Tier 3: recover the cls_id of a poly value. SP_TAG_CLASS
6422+ # returns itself; SP_TAG_OBJ with a non-negative cls_id is a
6423+ # user-class instance; everything else (primitives, built-in
6424+ # pointer types) returns the sentinel sp_Class{-1} because
6425+ # primitives don't have user-class cls_ids in spinel's current
6426+ # numbering. Programs that need `5.is_a?(Integer)` continue to
6427+ # use the existing static tag-check path (poly_is_a_tag_check).
6428+ emit_raw("static sp_Class sp_class_for_poly(sp_RbVal v) __attribute__((unused));")
6429+ emit_raw("static sp_Class sp_class_for_poly(sp_RbVal v){if(v.tag==SP_TAG_CLASS)return (sp_Class){(mrb_int)v.cls_id};if(v.tag==SP_TAG_OBJ&&v.cls_id>=0)return (sp_Class){(mrb_int)v.cls_id};return (sp_Class){-1};}")
64216430 end
64226431 emit_raw("")
64236432 end
@@ -16224,6 +16233,19 @@
1622416233 if args_id >= 0
1622516234 a = get_args(args_id)
1622616235 if a.length > 0
16236+ # Issue #404 Phase 3 Tier 3: dynamic Class arg. The
16237+ # statically-typed recv knows its own cls_id; the arg
16238+ # carries the target cls_id at runtime. sp_class_le walks
16239+ # the precomputed ancestors table to decide membership.
16240+ arg_t_404 = infer_type(a[0])
16241+ if arg_t_404 == "class" && @nd_type[a[0]] != "ConstantReadNode"
16242+ ci_404 = find_class_idx(cname)
16243+ if ci_404 >= 0
16244+ @needs_class_table = 1
16245+ @needs_class_ancestors = 1
16246+ return "sp_class_le((sp_Class){" + ci_404.to_s + "LL}, " + compile_expr(a[0]) + ")"
16247+ end
16248+ end
1622716249 arg0 = @nd_name[a[0]]
1622816250 end
1622916251 end
@@ -17010,6 +17032,22 @@
1701017032 if args_id >= 0
1701117033 a = get_args(args_id)
1701217034 if a.length > 0
17035+ # Issue #404 Phase 3 Tier 3: dynamic Class arg on a poly
17036+ # recv. Recover the recv's cls_id with sp_class_for_poly,
17037+ # then sp_class_le against the runtime-supplied Class.
17038+ # instance_of? still needs equality on the resolved
17039+ # cls_id; sp_class_eq handles that.
17040+ arg_t_404 = infer_type(a[0])
17041+ if arg_t_404 == "class" && @nd_type[a[0]] != "ConstantReadNode"
17042+ recv_tmp_404 = new_temp
17043+ emit(" sp_RbVal " + recv_tmp_404 + " = " + rc + ";")
17044+ @needs_class_table = 1
17045+ @needs_class_ancestors = 1
17046+ if mname == "instance_of?"
17047+ return "sp_class_eq(sp_class_for_poly(" + recv_tmp_404 + "), " + compile_expr(a[0]) + ")"
17048+ end
17049+ return "sp_class_le(sp_class_for_poly(" + recv_tmp_404 + "), " + compile_expr(a[0]) + ")"
17050+ end
1701317051 arg_name = @nd_name[a[0]]
1701417052 recv_tmp = new_temp
1701517053 emit(" sp_RbVal " + recv_tmp + " = " + rc + ";")