Commit e7d428a6

e7d428a6454cMerged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): Class.method(:cls_meth) class-method object

Instance-receiver obj.method(:bar) already produced a real Method (Ref #215, since #312), but Klass.method(:cls_meth) (class receiver, class method) fell through to the unresolved- call fallback — the result was an int 0, .call() warned cannot resolve call to 'call' on int, and downstream stores into a PolyArray slot poisoned the slot's element type.

Three coordinated changes:

  1. infer_constant_recv_type: when Klass.method(:cm) and :cm is a defined cls method, return obj_Method so the captured value types as a Method instead of int.

  2. compile_constant_recv_expr: emit sp_Method_new(NULL, &<adapter>) where is a static trampoline that wraps the cls method's no-self ABI to fit the Method-dispatch ABI (void *self, mrb_int...). The adapter discards the unused self slot. Tracked in @cls_method_adapters to avoid duplicates; buffered into @lambda_funcs to land at the same pre-main insertion point as lambdas.

  3. collect_cls_calls: for Klass.method(:cm), mark :cm live in addition to "method" — without this, the cls method body has no direct call site and DCE drops it, leaving the adapter's sp_<Klass>_cls_<m> reference unresolved at link. The walker runs without lexical-scope context (just CPU not Optcarrot_CPU), so suffix-match every _<cname> cls name to cover unprefixed receivers inside a module body.

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

Details

Commit
e7d428a6454c
Parent
a0e14abe8ff5

spinel_codegen.rbmodifiedView file

@@ -337,6 +337,11 @@
337337 @lambda_var_ret_names = "".split(",")
338338 @lambda_var_ret_types = "".split(",")
339339 @last_lambda_ret_type = ""
340+ # `Klass.method(:cls_meth)` generates an adapter trampoline so the
341+ # Method object's `(void *self, mrb_int...)` ABI fits a class
342+ # method's no-self C signature. Tracks emitted (Klass, method)
343+ # pairs to avoid duplicate definitions.
344+ @cls_method_adapters = "".split(",")
340345
341346 # Proc closure support (Phase 2)
342347 @in_proc_body = 0
@@ -410,6 +415,7 @@
410415 @lambda_params = "".split(",")
411416 @lambda_captures = "".split(",")
412417 @lambda_insert_pos = 0
418+ @cls_method_adapters = "".split(",")
413419
414420 # Proc closure support (Phase 2)
415421 @in_proc_body = 0
@@ -1860,6 +1866,78 @@
18601866 -1
18611867 end
18621868
1869+ # Emit a one-off adapter that wraps `sp_<Klass>_cls_<mname>` so the
1870+ # Method dispatch ABI `(void *self, mrb_int...)` works on a class
1871+ # method (which has no self param). Idempotent: re-emitting the
1872+ # same (Klass, mname) pair is a no-op. Buffered into @lambda_funcs
1873+ # so the function lands at the same insertion point as lambdas
1874+ # (before main, after forward declarations of every cls method).
1875+ def emit_cls_method_adapter(ci, mname)
1876+ cname = @cls_names[ci]
1877+ key = cname + "::" + mname
1878+ k = 0
1879+ while k < @cls_method_adapters.length
1880+ if @cls_method_adapters[k] == key
1881+ return
1882+ end
1883+ k = k + 1
1884+ end
1885+ @cls_method_adapters.push(key)
1886+
1887+ cmnames = @cls_cmeth_names[ci].split(";")
1888+ cm_returns = @cls_cmeth_returns[ci].split(";")
1889+ cm_ptypes = @cls_cmeth_ptypes[ci].split("|")
1890+ j = 0
1891+ while j < cmnames.length
1892+ if cmnames[j] == mname
1893+ break
1894+ end
1895+ j = j + 1
1896+ end
1897+ rt = "int"
1898+ if j < cm_returns.length
1899+ rt = cm_returns[j]
1900+ end
1901+ pt_list = "".split(",")
1902+ if j < cm_ptypes.length
1903+ pt_list = cm_ptypes[j].split(",")
1904+ end
1905+ base_name = "sp_" + cname + "_cls_" + sanitize_name(mname)
1906+ adapter_name = "sp_" + cname + "_" + sanitize_name(mname) + "_cls_method_adapter"
1907+ sig = c_type(rt) + " " + adapter_name + "(void *_unused"
1908+ body_args = ""
1909+ pi2 = 0
1910+ while pi2 < pt_list.length
1911+ sig = sig + ", mrb_int _a" + pi2.to_s
1912+ if pi2 == 0
1913+ body_args = "_a" + pi2.to_s
1914+ else
1915+ body_args = body_args + ", _a" + pi2.to_s
1916+ end
1917+ pi2 = pi2 + 1
1918+ end
1919+ sig = sig + ")"
1920+ @lambda_funcs << "static "
1921+ @lambda_funcs << sig
1922+ @lambda_funcs << " {\n"
1923+ if rt == "void"
1924+ @lambda_funcs << " "
1925+ @lambda_funcs << base_name
1926+ @lambda_funcs << "("
1927+ @lambda_funcs << body_args
1928+ @lambda_funcs << ");\n"
1929+ @lambda_funcs << " return 0;\n"
1930+ else
1931+ @lambda_funcs << " return "
1932+ @lambda_funcs << base_name
1933+ @lambda_funcs << "("
1934+ @lambda_funcs << body_args
1935+ @lambda_funcs << ");\n"
1936+ end
1937+ @lambda_funcs << "}\n"
1938+ nil
1939+ end
1940+
18631941 def cls_cmethod_return_inherited(ci, mname)
18641942 owner = cls_cmethod_owner(ci, mname)
18651943 if owner < 0
@@ -4191,6 +4269,26 @@
41914269 if mname == "new"
41924270 return "obj_" + rcname
41934271 end
4272+ # `Klass.method(:cls_meth)` — bind to a class method.
4273+ # Returns a Method object exactly like the instance-recv
4274+ # form. The compile path below emits an adapter trampoline
4275+ # so the Method's `(void *, mrb_int...)` ABI absorbs the
4276+ # missing self.
4277+ if mname == "method"
4278+ args_idm = @nd_arguments[nid]
4279+ if args_idm >= 0
4280+ ams = get_args(args_idm)
4281+ if ams.length >= 1
4282+ cm_ref = @nd_content[ams[0]]
4283+ if cm_ref == ""
4284+ cm_ref = @nd_name[ams[0]]
4285+ end
4286+ if cls_cmethod_owner(ci2, cm_ref) >= 0
4287+ return "obj_Method"
4288+ end
4289+ end
4290+ end
4291+ end
41944292 # Issue #208: walk the parent chain so an inherited
41954293 # `def self.<mname>` on a base class resolves correctly
41964294 # when called on the subclass (e.g. `Leaf.all` →
@@ -16927,9 +17025,43 @@
1692717025 if @nd_type[recv] == "ConstantReadNode" || @nd_type[recv] == "ConstantPathNode"
1692817026 cname = constructor_class_name(recv)
1692917027 if cname != ""
16930- tci = find_class_idx(cname)
16931- if tci >= 0
16932- cls_cmeth_mark_live(tci, mname)
17028+ # constructor_class_name resolves via lexical scope, but
17029+ # this DCE walker runs without method-body context. Fall
17030+ # back to suffix-matching when the unscoped name doesn't
17031+ # match a registered class — `CPU` (called inside
17032+ # `Optcarrot::CPU`) needs to mark `Optcarrot_CPU::poke_nop`
17033+ # live, but cname is plain `CPU`. Walks every cls name
17034+ # and marks any matching suffix.
17035+ cm_lit_for_method = ""
17036+ if mname == "method"
17037+ args_ic = @nd_arguments[nid]
17038+ if args_ic >= 0
17039+ ac_arg_ids = get_args(args_ic)
17040+ if ac_arg_ids.length >= 1
17041+ cm_lit_for_method = @nd_content[ac_arg_ids[0]]
17042+ if cm_lit_for_method == ""
17043+ cm_lit_for_method = @nd_name[ac_arg_ids[0]]
17044+ end
17045+ end
17046+ end
17047+ end
17048+ mark_idx = 0
17049+ while mark_idx < @cls_names.length
17050+ cn_full = @cls_names[mark_idx]
17051+ if cn_full == cname || cn_full.end_with?("_" + cname)
17052+ cls_cmeth_mark_live(mark_idx, mname)
17053+ # `Klass.method(:cls_meth)` — keeps the *named* cls
17054+ # method live so the adapter trampoline emitted by
17055+ # compile_constant_recv_expr resolves to a real
17056+ # symbol at link time. Without this, the cls method
17057+ # is DCE'd (no direct call site, only an indirect
17058+ # bind) and the adapter references an undefined
17059+ # function.
17060+ if cm_lit_for_method != ""
17061+ cls_cmeth_mark_live(mark_idx, cm_lit_for_method)
17062+ end
17063+ end
17064+ mark_idx = mark_idx + 1
1693317065 end
1693417066 end
1693517067 end
@@ -25359,6 +25491,37 @@
2535925491 end
2536025492 mi2 = mi2 + 1
2536125493 end
25494+ # `Klass.method(:cls_meth)` — bind to a class method. Mirrors
25495+ # the instance-recv `obj.method(:bar)` form (line ~20940), but
25496+ # the underlying class method has no `self *` param. Emit a
25497+ # static adapter trampoline that ignores the dispatch ABI's
25498+ # `void *self` slot and forwards the rest, then bind the
25499+ # Method's iv_fn_ptr to the adapter. iv_self_obj is unused but
25500+ # set to a stable stand-in (a NULL pointer cast through
25501+ # sp_Method *); the Method dispatch in compile_poly_method_call
25502+ # passes it as the void * arg, which the adapter discards.
25503+ if mname == "method"
25504+ ci_cm = find_class_idx(rcname)
25505+ if ci_cm >= 0
25506+ args_id_cm = @nd_arguments[nid]
25507+ if args_id_cm >= 0
25508+ arg_ids_cm = get_args(args_id_cm)
25509+ if arg_ids_cm.length >= 1
25510+ cm_ref = @nd_content[arg_ids_cm[0]]
25511+ if cm_ref == ""
25512+ cm_ref = @nd_name[arg_ids_cm[0]]
25513+ end
25514+ owner_cm = cls_cmethod_owner(ci_cm, cm_ref)
25515+ if owner_cm >= 0
25516+ owner_cm_name = @cls_names[owner_cm]
25517+ emit_cls_method_adapter(owner_cm, cm_ref)
25518+ adapter_name = "sp_" + owner_cm_name + "_" + sanitize_name(cm_ref) + "_cls_method_adapter"
25519+ return "sp_Method_new((sp_Method *)0, (mrb_int)(uintptr_t)&" + adapter_name + ")"
25520+ end
25521+ end
25522+ end
25523+ end
25524+ end
2536225525 # Class method dispatch (def self.xxx). Issue #208: walk the
2536325526 # parent chain so `Leaf.all` resolves to `Base.all`'s emitter
2536425527 # (`sp_Base_cls_all`, not `sp_Leaf_cls_all` which is never