Commit f1f90f88
f1f90f8839bcMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
refactor(codegen): introduce ptypes/pnames split-join helpers (1/N)
Add per-table get/put helpers that abstract the repeated split-
on-| + index + split-on-, (and the reverse on writes) used
throughout call-site widening:
- cls_meth_ptypes_get / cls_meth_ptypes_put
- cls_cmeth_ptypes_get / cls_cmeth_ptypes_put
- cls_meth_pnames_get / cls_cmeth_pnames_get
Migrate the four most repetitive widening call sites (recv-method
unify, class-method-via-constant unify, bare-new-in-cls-method,
inherited recv-method walk). The unify_call_types loops collapse
to a widen_ptypes_from_args call where the previous logic
matched (kwargs handling becomes uniform as a side effect).
Tests: 301 pass. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- f1f90f8839bc
- Parent
- 31a59e30af5e
spinel_codegen.rbmodifiedView file
@@ -9950,9 +9950,8 @@
99509950
args_id = @nd_arguments[nid]99519951
if args_id >= 099529952
arg_ids = get_args(args_id)9953-
all_ptypes = @cls_meth_ptypes[owner_ci].split("|")9954-
if midx < all_ptypes.length9955-
ptypes = all_ptypes[midx].split(",")9953+
ptypes = cls_meth_ptypes_get(owner_ci, midx)9954+
if ptypes.length > 099569955
kk = 099579956
while kk < arg_ids.length99589957
at = infer_type(arg_ids[kk])@@ -9965,8 +9964,7 @@
99659964
end99669965
kk = kk + 199679966
end9968-
all_ptypes[midx] = ptypes.join(",")9969-
@cls_meth_ptypes[owner_ci] = all_ptypes.join("|")9967+
cls_meth_ptypes_put(owner_ci, midx, ptypes)99709968
end99719969
end99729970
end@@ -10637,27 +10635,19 @@
1063710635
args_id = @nd_arguments[nid]1063810636
if args_id >= 01063910637
arg_ids = get_args(args_id)10640-
all_ptypes = @cls_meth_ptypes[owner_ci].split("|")10641-
if midx < all_ptypes.length10642-
ptypes = all_ptypes[midx].split(",")10643-
kk = 010644-
while kk < arg_ids.length10645-
at = infer_type(arg_ids[kk])10646-
if kk < ptypes.length10647-
# Unify against the existing param type rather10648-
# than only widening from "int". The previous10649-
# rule let the FIRST non-int call site freeze10650-
# the param type; subsequent disagreeing calls10651-
# (e.g. addr arg seen as Range from one site10652-
# and as Integer from another) compiled to a10653-
# signature mismatch. unify_call_types collapses10654-
# incompatible types to "poly".10655-
ptypes[kk] = unify_call_types(ptypes[kk], at, arg_ids[kk])10656-
end10657-
kk = kk + 110658-
end10659-
all_ptypes[midx] = ptypes.join(",")10660-
@cls_meth_ptypes[owner_ci] = all_ptypes.join("|")10638+
# Unify against the existing param type rather10639+
# than only widening from "int". A first-non-int-10640+
# wins rule would let one call site freeze the10641+
# param type and a later disagreeing site (e.g.10642+
# `addr` seen as Range from one and Integer from10643+
# another) lands as a signature mismatch.10644+
# unify_call_types collapses incompatibles to10645+
# "poly".10646+
ptypes = cls_meth_ptypes_get(owner_ci, midx)10647+
if ptypes.length > 010648+
pnames = cls_meth_pnames_get(owner_ci, midx)10649+
widen_ptypes_from_args(arg_ids, pnames, ptypes)10650+
cls_meth_ptypes_put(owner_ci, midx, ptypes)1066110651
end1066210652
end1066310653
end@@ -10714,24 +10704,18 @@
1071410704
if cci >= 01071510705
cmname = @nd_name[nid]1071610706
cmnames = @cls_cmeth_names[cci].split(";")10717-
cm_ptypes_all = @cls_cmeth_ptypes[cci].split("|")1071810707
cmidx = 01071910708
while cmidx < cmnames.length1072010709
if cmnames[cmidx] == cmname1072110710
args_id = @nd_arguments[nid]10722-
if args_id >= 0 && cmidx < cm_ptypes_all.length10711+
if args_id >= 01072310712
arg_ids = get_args(args_id)10724-
cmptypes = cm_ptypes_all[cmidx].split(",")10725-
kk = 010726-
while kk < arg_ids.length10727-
at = infer_type(arg_ids[kk])10728-
if kk < cmptypes.length10729-
cmptypes[kk] = unify_call_types(cmptypes[kk], at, arg_ids[kk])10730-
end10731-
kk = kk + 110713+
cmptypes = cls_cmeth_ptypes_get(cci, cmidx)10714+
if cmptypes.length > 010715+
cmpnames = cls_cmeth_pnames_get(cci, cmidx)10716+
widen_ptypes_from_args(arg_ids, cmpnames, cmptypes)10717+
cls_cmeth_ptypes_put(cci, cmidx, cmptypes)1073210718
end10733-
cm_ptypes_all[cmidx] = cmptypes.join(",")10734-
@cls_cmeth_ptypes[cci] = cm_ptypes_all.join("|")1073510719
end1073610720
end1073710721
cmidx = cmidx + 1@@ -10965,9 +10949,8 @@
1096510949
if init_ci >= 01096610950
init_idx = cls_find_method_direct(init_ci, "initialize")1096710951
if init_idx >= 010968-
all_ptypes = @cls_meth_ptypes[init_ci].split("|")10969-
if init_idx < all_ptypes.length10970-
init_ptypes = all_ptypes[init_idx].split(",")10952+
init_ptypes = cls_meth_ptypes_get(init_ci, init_idx)10953+
if init_ptypes.length > 01097110954
kk = 01097210955
while kk < arg_ids.length1097310956
at = ""@@ -10989,8 +10972,7 @@
1098910972
end1099010973
kk = kk + 11099110974
end10992-
all_ptypes[init_idx] = init_ptypes.join(",")10993-
@cls_meth_ptypes[init_ci] = all_ptypes.join("|")10975+
cls_meth_ptypes_put(init_ci, init_idx, init_ptypes)1099410976
end1099510977
end1099610978
end@@ -17692,6 +17674,81 @@
1769217674
-11769317675
end1769417676
17677+
# Helpers for the per-method split-join boilerplate around17678+
# @cls_meth_ptypes / @cls_cmeth_ptypes / @cls_meth_params /17679+
# @cls_cmeth_params. Each table stores per-class strings where17680+
# methods are pipe-separated (`|`) and per-method names/types17681+
# are comma-separated (`,`). Without these, every read site17682+
# opens with two split() calls + bound checks and every write17683+
# site closes with two join() calls.17684+
def cls_meth_ptypes_get(ci, midx)17685+
if ci < 0 || ci >= @cls_meth_ptypes.length || midx < 017686+
return "".split(",")17687+
end17688+
all = @cls_meth_ptypes[ci].split("|")17689+
if midx >= all.length17690+
return "".split(",")17691+
end17692+
all[midx].split(",")17693+
end17694+
17695+
def cls_meth_ptypes_put(ci, midx, ptypes)17696+
if ci < 0 || ci >= @cls_meth_ptypes.length || midx < 017697+
return17698+
end17699+
all = @cls_meth_ptypes[ci].split("|")17700+
if midx >= all.length17701+
return17702+
end17703+
all[midx] = ptypes.join(",")17704+
@cls_meth_ptypes[ci] = all.join("|")17705+
end17706+
17707+
def cls_meth_pnames_get(ci, midx)17708+
if ci < 0 || ci >= @cls_meth_params.length || midx < 017709+
return "".split(",")17710+
end17711+
all = @cls_meth_params[ci].split("|")17712+
if midx >= all.length17713+
return "".split(",")17714+
end17715+
all[midx].split(",")17716+
end17717+
17718+
def cls_cmeth_ptypes_get(ci, midx)17719+
if ci < 0 || ci >= @cls_cmeth_ptypes.length || midx < 017720+
return "".split(",")17721+
end17722+
all = @cls_cmeth_ptypes[ci].split("|")17723+
if midx >= all.length17724+
return "".split(",")17725+
end17726+
all[midx].split(",")17727+
end17728+
17729+
def cls_cmeth_ptypes_put(ci, midx, ptypes)17730+
if ci < 0 || ci >= @cls_cmeth_ptypes.length || midx < 017731+
return17732+
end17733+
all = @cls_cmeth_ptypes[ci].split("|")17734+
if midx >= all.length17735+
return17736+
end17737+
all[midx] = ptypes.join(",")17738+
@cls_cmeth_ptypes[ci] = all.join("|")17739+
end17740+
17741+
def cls_cmeth_pnames_get(ci, midx)17742+
if ci < 0 || ci >= @cls_cmeth_params.length || midx < 017743+
return "".split(",")17744+
end17745+
all = @cls_cmeth_params[ci].split("|")17746+
if midx >= all.length17747+
return "".split(",")17748+
end17749+
all[midx].split(",")17750+
end17751+
1769517752
# Walk the inheritance chain starting at class `ci` looking for the1769617753
# first ancestor that defines `mname` directly. Returns that class's1769717754
# name, or "" if no ancestor defines it. Used by the