Commit 9b611afa
9b611afa73a1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #345 from mame/pr/cls-method-object
fix(codegen): Class.method(:cls_meth) class-method object
Details
- Commit
- 9b611afa73a1
- Parent
- aa17ede4def1
- Merge parent
- e7d428a6454c
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 the341+
# Method object's `(void *self, mrb_int...)` ABI fits a class342+
# 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 = 0418+
@cls_method_adapters = "".split(",")413419
414420
# Proc closure support (Phase 2)415421
@in_proc_body = 0@@ -1860,6 +1866,78 @@
18601866
-118611867
end18621868
1869+
# Emit a one-off adapter that wraps `sp_<Klass>_cls_<mname>` so the1870+
# Method dispatch ABI `(void *self, mrb_int...)` works on a class1871+
# method (which has no self param). Idempotent: re-emitting the1872+
# same (Klass, mname) pair is a no-op. Buffered into @lambda_funcs1873+
# so the function lands at the same insertion point as lambdas1874+
# (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 + "::" + mname1878+
k = 01879+
while k < @cls_method_adapters.length1880+
if @cls_method_adapters[k] == key1881+
return1882+
end1883+
k = k + 11884+
end1885+
@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 = 01891+
while j < cmnames.length1892+
if cmnames[j] == mname1893+
break1894+
end1895+
j = j + 11896+
end1897+
rt = "int"1898+
if j < cm_returns.length1899+
rt = cm_returns[j]1900+
end1901+
pt_list = "".split(",")1902+
if j < cm_ptypes.length1903+
pt_list = cm_ptypes[j].split(",")1904+
end1905+
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 = 01910+
while pi2 < pt_list.length1911+
sig = sig + ", mrb_int _a" + pi2.to_s1912+
if pi2 == 01913+
body_args = "_a" + pi2.to_s1914+
else1915+
body_args = body_args + ", _a" + pi2.to_s1916+
end1917+
pi2 = pi2 + 11918+
end1919+
sig = sig + ")"1920+
@lambda_funcs << "static "1921+
@lambda_funcs << sig1922+
@lambda_funcs << " {\n"1923+
if rt == "void"1924+
@lambda_funcs << " "1925+
@lambda_funcs << base_name1926+
@lambda_funcs << "("1927+
@lambda_funcs << body_args1928+
@lambda_funcs << ");\n"1929+
@lambda_funcs << " return 0;\n"1930+
else1931+
@lambda_funcs << " return "1932+
@lambda_funcs << base_name1933+
@lambda_funcs << "("1934+
@lambda_funcs << body_args1935+
@lambda_funcs << ");\n"1936+
end1937+
@lambda_funcs << "}\n"1938+
nil1939+
end1940+
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_" + rcname41934271
end4272+
# `Klass.method(:cls_meth)` — bind to a class method.4273+
# Returns a Method object exactly like the instance-recv4274+
# form. The compile path below emits an adapter trampoline4275+
# so the Method's `(void *, mrb_int...)` ABI absorbs the4276+
# missing self.4277+
if mname == "method"4278+
args_idm = @nd_arguments[nid]4279+
if args_idm >= 04280+
ams = get_args(args_idm)4281+
if ams.length >= 14282+
cm_ref = @nd_content[ams[0]]4283+
if cm_ref == ""4284+
cm_ref = @nd_name[ams[0]]4285+
end4286+
if cls_cmethod_owner(ci2, cm_ref) >= 04287+
return "obj_Method"4288+
end4289+
end4290+
end4291+
end41944292
# Issue #208: walk the parent chain so an inherited41954293
# `def self.<mname>` on a base class resolves correctly41964294
# when called on the subclass (e.g. `Leaf.all` →@@ -16997,9 +17095,43 @@
1699717095
if @nd_type[recv] == "ConstantReadNode" || @nd_type[recv] == "ConstantPathNode"1699817096
cname = constructor_class_name(recv)1699917097
if cname != ""17000-
tci = find_class_idx(cname)17001-
if tci >= 017002-
cls_cmeth_mark_live(tci, mname)17098+
# constructor_class_name resolves via lexical scope, but17099+
# this DCE walker runs without method-body context. Fall17100+
# back to suffix-matching when the unscoped name doesn't17101+
# match a registered class — `CPU` (called inside17102+
# `Optcarrot::CPU`) needs to mark `Optcarrot_CPU::poke_nop`17103+
# live, but cname is plain `CPU`. Walks every cls name17104+
# and marks any matching suffix.17105+
cm_lit_for_method = ""17106+
if mname == "method"17107+
args_ic = @nd_arguments[nid]17108+
if args_ic >= 017109+
ac_arg_ids = get_args(args_ic)17110+
if ac_arg_ids.length >= 117111+
cm_lit_for_method = @nd_content[ac_arg_ids[0]]17112+
if cm_lit_for_method == ""17113+
cm_lit_for_method = @nd_name[ac_arg_ids[0]]17114+
end17115+
end17116+
end17117+
end17118+
mark_idx = 017119+
while mark_idx < @cls_names.length17120+
cn_full = @cls_names[mark_idx]17121+
if cn_full == cname || cn_full.end_with?("_" + cname)17122+
cls_cmeth_mark_live(mark_idx, mname)17123+
# `Klass.method(:cls_meth)` — keeps the *named* cls17124+
# method live so the adapter trampoline emitted by17125+
# compile_constant_recv_expr resolves to a real17126+
# symbol at link time. Without this, the cls method17127+
# is DCE'd (no direct call site, only an indirect17128+
# bind) and the adapter references an undefined17129+
# function.17130+
if cm_lit_for_method != ""17131+
cls_cmeth_mark_live(mark_idx, cm_lit_for_method)17132+
end17133+
end17134+
mark_idx = mark_idx + 11700317135
end1700417136
end1700517137
end@@ -25446,6 +25578,37 @@
2544625578
end2544725579
mi2 = mi2 + 12544825580
end25581+
# `Klass.method(:cls_meth)` — bind to a class method. Mirrors25582+
# the instance-recv `obj.method(:bar)` form (line ~20940), but25583+
# the underlying class method has no `self *` param. Emit a25584+
# static adapter trampoline that ignores the dispatch ABI's25585+
# `void *self` slot and forwards the rest, then bind the25586+
# Method's iv_fn_ptr to the adapter. iv_self_obj is unused but25587+
# set to a stable stand-in (a NULL pointer cast through25588+
# sp_Method *); the Method dispatch in compile_poly_method_call25589+
# passes it as the void * arg, which the adapter discards.25590+
if mname == "method"25591+
ci_cm = find_class_idx(rcname)25592+
if ci_cm >= 025593+
args_id_cm = @nd_arguments[nid]25594+
if args_id_cm >= 025595+
arg_ids_cm = get_args(args_id_cm)25596+
if arg_ids_cm.length >= 125597+
cm_ref = @nd_content[arg_ids_cm[0]]25598+
if cm_ref == ""25599+
cm_ref = @nd_name[arg_ids_cm[0]]25600+
end25601+
owner_cm = cls_cmethod_owner(ci_cm, cm_ref)25602+
if owner_cm >= 025603+
owner_cm_name = @cls_names[owner_cm]25604+
emit_cls_method_adapter(owner_cm, cm_ref)25605+
adapter_name = "sp_" + owner_cm_name + "_" + sanitize_name(cm_ref) + "_cls_method_adapter"25606+
return "sp_Method_new((sp_Method *)0, (mrb_int)(uintptr_t)&" + adapter_name + ")"25607+
end25608+
end25609+
end25610+
end25611+
end2544925612
# Class method dispatch (def self.xxx). Issue #208: walk the2545025613
# parent chain so `Leaf.all` resolves to `Base.all`'s emitter2545125614
# (`sp_Base_cls_all`, not `sp_Leaf_cls_all` which is nevertest/cls_method_object.rbaddedView file
Loading diff…
test/cls_method_object.rb.expectedaddedView file
Loading diff…