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>).
- non-ConstantReadNode and lower to
- 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, Klass2on 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
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);}") emit_raw("static sp_PolyArray *sp_class_ancestors_arr(sp_Class c) __attribute__((unused));") 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;}") # Tier 3: recover the cls_id of a poly value. SP_TAG_CLASS # returns itself; SP_TAG_OBJ with a non-negative cls_id is a # user-class instance; everything else (primitives, built-in # pointer types) returns the sentinel sp_Class{-1} because # primitives don't have user-class cls_ids in spinel's current # numbering. Programs that need `5.is_a?(Integer)` continue to # use the existing static tag-check path (poly_is_a_tag_check). emit_raw("static sp_Class sp_class_for_poly(sp_RbVal v) __attribute__((unused));") 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};}") end emit_raw("") end if args_id >= 0 a = get_args(args_id) if a.length > 0 # Issue #404 Phase 3 Tier 3: dynamic Class arg. The # statically-typed recv knows its own cls_id; the arg # carries the target cls_id at runtime. sp_class_le walks # the precomputed ancestors table to decide membership. arg_t_404 = infer_type(a[0]) if arg_t_404 == "class" && @nd_type[a[0]] != "ConstantReadNode" ci_404 = find_class_idx(cname) if ci_404 >= 0 @needs_class_table = 1 @needs_class_ancestors = 1 return "sp_class_le((sp_Class){" + ci_404.to_s + "LL}, " + compile_expr(a[0]) + ")" end end arg0 = @nd_name[a[0]] end end if args_id >= 0 a = get_args(args_id) if a.length > 0 # Issue #404 Phase 3 Tier 3: dynamic Class arg on a poly # recv. Recover the recv's cls_id with sp_class_for_poly, # then sp_class_le against the runtime-supplied Class. # instance_of? still needs equality on the resolved # cls_id; sp_class_eq handles that. arg_t_404 = infer_type(a[0]) if arg_t_404 == "class" && @nd_type[a[0]] != "ConstantReadNode" recv_tmp_404 = new_temp emit(" sp_RbVal " + recv_tmp_404 + " = " + rc + ";") @needs_class_table = 1 @needs_class_ancestors = 1 if mname == "instance_of?" return "sp_class_eq(sp_class_for_poly(" + recv_tmp_404 + "), " + compile_expr(a[0]) + ")" end return "sp_class_le(sp_class_for_poly(" + recv_tmp_404 + "), " + compile_expr(a[0]) + ")" end arg_name = @nd_name[a[0]] recv_tmp = new_temp emit(" sp_RbVal " + recv_tmp + " = " + rc + ";")test/dynamic_is_a.rbaddedView file
Loading diff…
test/dynamic_is_a.rb.expectedaddedView file
Loading diff…