Commit 26a33e95

26a33e95628cMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

refactor(codegen): unify cmeth/imeth dispatch helpers

#436 added cls_imethod_owner / cls_imeth_override_descendants as near-duplicates of the cmeth versions added in #422; only the storage array (@cls_cmeth_names vs @cls_meth_names) differed. Same shape for the return-type lookups (cls_cmeth_return_type vs cls_method_return).

Collapse the three pairs into one helper each, selected by a kind ("cmeth" or "imeth") parameter:

  • cls_method_owner_for(ci, mname, kind)
  • cls_method_override_descendants(inner_ci, base_owner, mname, kind)
  • cls_method_return(ci, mname, kind = "imeth")

cls_method_return keeps the imeth default so its existing call sites stay untouched; cmeth callers pass "cmeth" explicitly. -59 lines net, no behavior change (bootstrap fixpoint OK, 388 tests green, optcarrot checksum 59662).

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

Details

Commit
26a33e95628c
Parent
9638f6ec14f2

spinel_codegen.rbmodifiedView file

@@ -1884,88 +1884,20 @@
18841884 -1
18851885 end
18861886
1887- # Walk the parent chain looking for a class method
1888- # (`def self.<mname>`) named `mname`. Returns the class index that
1889- # defines it, or -1 if not found. Lets `Leaf.all` resolve to
1890- # `Base.all` (and emit `sp_Base_cls_all(...)`) when Leaf inherits
1891- # from Base without overriding `.all`. Mirrors cls_method_return's
1892- # parent walk for instance methods.
1893- def cls_cmethod_owner(ci, mname)
1887+ # Walk the parent chain looking for a method named `mname`.
1888+ # Returns the class index that defines it (the owner), or -1.
1889+ # `kind` selects @cls_cmeth_names ("cmeth") or @cls_meth_names
1890+ # ("imeth"). Used by the dispatch lowering to find the static
1891+ # owner before deciding whether a cls_id switch is needed.
1892+ def cls_method_owner_for(ci, mname, kind)
18941893 if ci < 0
18951894 return -1
18961895 end
1897- cmnames = @cls_cmeth_names[ci].split(";")
1898- cj = 0
1899- while cj < cmnames.length
1900- if cmnames[cj] == mname
1901- return ci
1902- end
1903- cj = cj + 1
1904- end
1905- if @cls_parents[ci] != ""
1906- pi = find_class_idx(@cls_parents[ci])
1907- if pi >= 0
1908- return cls_cmethod_owner(pi, mname)
1909- end
1910- end
1911- -1
1912- end
1913-
1914- # look up the C return type of `ci`'s cmeth named
1915- # `mname`. Walks @cls_cmeth_returns directly (cls_cmethod_owner
1916- # already resolves inheritance, so callers pass the owner ci).
1917- # Defaults to "int" when the name isn't present -- the chained
1918- # dispatch path verifies owner >= 0 before consulting this.
1919- def cls_cmeth_return_type(ci, mname)
1920- if ci < 0
1921- return "int"
1922- end
1923- cmnames = @cls_cmeth_names[ci].split(";")
1924- cm_returns = @cls_cmeth_returns[ci].split(";")
1925- cj = 0
1926- while cj < cmnames.length
1927- if cmnames[cj] == mname
1928- if cj < cm_returns.length
1929- return cm_returns[cj]
1930- end
1931- return "int"
1932- end
1933- cj = cj + 1
1934- end
1935- "int"
1936- end
1937-
1938- # enumerate descendants of inner_ci (including
1939- # inner_ci itself) whose cmeth owner differs from base_owner.
1940- # Each entry is encoded `<descendant_ci>,<owner_ci>` separated
1941- # by `;`. Empty string when no overrides exist (caller stays on
1942- # the static-dispatch path).
1943- def cls_cmeth_override_descendants(inner_ci, base_owner, mname)
1944- out = ""
1945- ck = 0
1946- while ck < @cls_names.length
1947- if ck == inner_ci || cls_is_descendant(ck, inner_ci) == 1
1948- ow = cls_cmethod_owner(ck, mname)
1949- if ow >= 0 && ow != base_owner
1950- if out != ""
1951- out = out + ";"
1952- end
1953- out = out + ck.to_s + "," + ow.to_s
1954- end
1955- end
1956- ck = ck + 1
1957- end
1958- out
1959- end
1960-
1961- # Walk the parent chain looking for an instance method named
1962- # `mname`. Returns the class index that defines it (the owner),
1963- # or -1. Imeth analog of cls_cmethod_owner.
1964- def cls_imethod_owner(ci, mname)
1965- if ci < 0
1966- return -1
1896+ if kind == "cmeth"
1897+ mnames = @cls_cmeth_names[ci].split(";")
1898+ else
1899+ mnames = @cls_meth_names[ci].split(";")
19671900 end
1968- mnames = @cls_meth_names[ci].split(";")
19691901 cj = 0
19701902 while cj < mnames.length
19711903 if mnames[cj] == mname
@@ -1976,22 +1908,23 @@
19761908 if @cls_parents[ci] != ""
19771909 pi = find_class_idx(@cls_parents[ci])
19781910 if pi >= 0
1979- return cls_imethod_owner(pi, mname)
1911+ return cls_method_owner_for(pi, mname, kind)
19801912 end
19811913 end
19821914 -1
19831915 end
19841916
19851917 # Enumerate descendants of inner_ci (including inner_ci itself)
1986- # whose imeth owner differs from base_owner. Imeth analog of
1987- # cls_cmeth_override_descendants. Used by the bare-receiver imeth
1988- # dispatch site to decide whether a cls_id switch is needed.
1989- def cls_imeth_override_descendants(inner_ci, base_owner, mname)
1918+ # whose method owner differs from base_owner. Each entry is
1919+ # encoded `<descendant_ci>,<owner_ci>` separated by `;`. Empty
1920+ # string when no overrides exist (caller stays on the
1921+ # static-dispatch path). `kind` is "cmeth" or "imeth".
1922+ def cls_method_override_descendants(inner_ci, base_owner, mname, kind)
19901923 out = ""
19911924 ck = 0
19921925 while ck < @cls_names.length
19931926 if ck == inner_ci || cls_is_descendant(ck, inner_ci) == 1
1994- ow = cls_imethod_owner(ck, mname)
1927+ ow = cls_method_owner_for(ck, mname, kind)
19951928 if ow >= 0 && ow != base_owner
19961929 if out != ""
19971930 out = out + ";"
@@ -2180,9 +2113,17 @@
21802113 end
21812114
21822115
2183- def cls_method_return(ci, mname)
2184- names = @cls_meth_names[ci].split(";")
2185- returns = @cls_meth_returns[ci].split(";")
2116+ def cls_method_return(ci, mname, kind = "imeth")
2117+ if ci < 0
2118+ return "int"
2119+ end
2120+ if kind == "cmeth"
2121+ names = @cls_cmeth_names[ci].split(";")
2122+ returns = @cls_cmeth_returns[ci].split(";")
2123+ else
2124+ names = @cls_meth_names[ci].split(";")
2125+ returns = @cls_meth_returns[ci].split(";")
2126+ end
21862127 j = 0
21872128 while j < names.length
21882129 if names[j] == mname
@@ -2196,7 +2137,7 @@
21962137 if @cls_parents[ci] != ""
21972138 pi = find_class_idx(@cls_parents[ci])
21982139 if pi >= 0
2199- return cls_method_return(pi, mname)
2140+ return cls_method_return(pi, mname, kind)
22002141 end
22012142 end
22022143 "int"
@@ -10235,7 +10176,7 @@
1023510176 # cls_id is just the class's index in @cls_names; the
1023610177 # generated sp_class_names[] table indexes by that. Class
1023710178 # constants used as method-call receivers are handled by
10238- # constructor_class_name / cls_cmethod_owner without ever
10179+ # constructor_class_name / cls_method_owner_for without ever
1023910180 # going through compile_expr on the receiver, so this
1024010181 # branch only ever fires in a true value-position site.
1024110182 # built-in class const (Integer,
@@ -12628,7 +12569,7 @@
1262812569 # call site doesn't propagate arg types to override defs, so
1262912570 # mismatched signatures would emit a broken C call).
1263012571 if owner_ci >= 0 && @cls_is_value_type[@current_class_idx] == 0 && has_proc == 0
12631- ovr_pairs = cls_imeth_override_descendants(@current_class_idx, owner_ci, mname)
12572+ ovr_pairs = cls_method_override_descendants(@current_class_idx, owner_ci, mname, "imeth")
1263212573 if ovr_pairs != ""
1263312574 base_rt = cls_method_return(owner_ci, mname)
1263412575 base_rt_c = c_type(base_rt)
@@ -16864,7 +16805,7 @@
1686416805 if cm_ref == ""
1686516806 cm_ref = @nd_name[arg_ids_cm[0]]
1686616807 end
16867- owner_cm = cls_cmethod_owner(ci_cm, cm_ref)
16808+ owner_cm = cls_method_owner_for(ci_cm, cm_ref, "cmeth")
1686816809 if owner_cm >= 0
1686916810 owner_cm_name = @cls_names[owner_cm]
1687016811 emit_cls_method_adapter(owner_cm, cm_ref)
@@ -16881,7 +16822,7 @@
1688116822 # generated when Leaf doesn't override).
1688216823 ci3 = find_class_idx(rcname)
1688316824 if ci3 >= 0
16884- owner_ci = cls_cmethod_owner(ci3, mname)
16825+ owner_ci = cls_method_owner_for(ci3, mname, "cmeth")
1688516826 if owner_ci >= 0
1688616827 owner_name = @cls_names[owner_ci]
1688716828 ca = compile_call_args(nid)
@@ -17223,7 +17164,7 @@
1722317164 inner_cname = inner_bt[4, inner_bt.length - 4]
1722417165 inner_ci = find_class_idx(inner_cname)
1722517166 if inner_ci >= 0
17226- owner_idx = cls_cmethod_owner(inner_ci, mname)
17167+ owner_idx = cls_method_owner_for(inner_ci, mname, "cmeth")
1722717168 if owner_idx >= 0
1722817169 owner_name = @cls_names[owner_idx]
1722917170 ca = compile_call_args(nid)
@@ -17248,18 +17189,18 @@
1724817189 if @cls_is_value_type[inner_ci] == 0 && ca == ""
1724917190 nty = @nd_type[inner_recv]
1725017191 if nty == "SelfNode" || nty == "LocalVariableReadNode"
17251- ovr = cls_cmeth_override_descendants(inner_ci, owner_idx, mname)
17192+ ovr = cls_method_override_descendants(inner_ci, owner_idx, mname, "cmeth")
1725217193 end
1725317194 end
1725417195 if ovr != ""
17255- base_rt = cls_cmeth_return_type(owner_idx, mname)
17196+ base_rt = cls_method_return(owner_idx, mname, "cmeth")
1725617197 ovr_pairs = ovr.split(";")
1725717198 rt_ok = 1
1725817199 op = 0
1725917200 while op < ovr_pairs.length
1726017201 pair = ovr_pairs[op].split(",")
1726117202 cand_owner_check = pair[1].to_i
17262- cand_rt = cls_cmeth_return_type(cand_owner_check, mname)
17203+ cand_rt = cls_method_return(cand_owner_check, mname, "cmeth")
1726317204 if cand_rt != base_rt
1726417205 rt_ok = 0
1726517206 op = ovr_pairs.length
@@ -17320,7 +17261,7 @@
1732017261 cand_cid_div = pair_div[0].to_i
1732117262 cand_owner_div = pair_div[1].to_i
1732217263 cand_name_div = @cls_names[cand_owner_div]
17323- cand_rt_div = cls_cmeth_return_type(cand_owner_div, mname)
17264+ cand_rt_div = cls_method_return(cand_owner_div, mname, "cmeth")
1732417265 cand_call_div = "sp_" + cand_name_div + "_cls_" + sanitize_name(mname) + "()"
1732517266 cand_box_div = box_value_to_poly(cand_rt_div, cand_call_div)
1732617267 emit(" case " + cls_id_for_user_internal(cand_cid_div).to_s + "LL: " + tmp_chain + " = " + cand_box_div + "; break;")