Commit 5e6f0b15
5e6f0b15e3c1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
refactor(codegen): migrate cls_cmeth sites + simplify sig builders (3/N)
Migrate the @cls_cmeth_* (class-method) split-join sites to cls_cmeth_ptypes_get / put / cls_cmeth_pnames_get. Same pattern as the earlier @cls_meth* pass.
Also simplify two signature-builders by changing their interface to (ci, midx) and looking up via the helpers internally:
- method_with_self_params: was (midx, all_params, all_ptypes) requiring callers to pre-split. Now (ci, midx).
- cls_method_params_decl: same; now uses cls_cmeth_pnames_get / cls_cmeth_ptypes_get.
Forward-decl emit's caller drops two extra split() calls per class as a result.
Tests: 301 pass. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 5e6f0b15e3c1
- Parent
- d57369dcf89d
spinel_codegen.rbmodifiedView file
@@ -2119,7 +2119,6 @@
21192119
21202120
cmnames = @cls_cmeth_names[ci].split(";")21212121
cm_returns = @cls_cmeth_returns[ci].split(";")2122-
cm_ptypes = @cls_cmeth_ptypes[ci].split("|")21232122
j = 021242123
while j < cmnames.length21252124
if cmnames[j] == mname@@ -2131,10 +2130,7 @@
21312130
if j < cm_returns.length21322131
rt = cm_returns[j]21332132
end2134-
pt_list = "".split(",")2135-
if j < cm_ptypes.length2136-
pt_list = cm_ptypes[j].split(",")2137-
end2133+
pt_list = cls_cmeth_ptypes_get(ci, j)21382134
base_name = "sp_" + cname + "_cls_" + sanitize_name(mname)21392135
adapter_name = "sp_" + cname + "_" + sanitize_name(mname) + "_cls_method_adapter"21402136
sig = c_type(rt) + " " + adapter_name + "(void *_unused"@@ -10845,8 +10841,6 @@
1084510841
while ci < @cls_names.length1084610842
cmnames = @cls_cmeth_names[ci].split(";")1084710843
cm_bodies = @cls_cmeth_bodies[ci].split(";")10848-
cm_params = @cls_cmeth_params[ci].split("|")10849-
cm_ptypes = @cls_cmeth_ptypes[ci].split("|")1085010844
j = 01085110845
while j < cmnames.length1085210846
bid = -1@@ -10855,16 +10849,10 @@
1085510849
if bs != ""1085610850
bid = bs.to_i1085710851
end10858-
end10859-
pnames = "".split(",")10860-
if j < cm_params.length10861-
pnames = cm_params[j].split(",")10862-
end10863-
ptypes = "".split(",")10864-
if j < cm_ptypes.length10865-
ptypes = cm_ptypes[j].split(",")1086610852
end1086710853
if bid > 010854+
pnames = cls_cmeth_pnames_get(ci, j)10855+
ptypes = cls_cmeth_ptypes_get(ci, j)1086810856
saved_ci = @current_class_idx1086910857
@current_class_idx = ci1087010858
walk_bare_new_in_cmeth_body(bid, ci, pnames, ptypes)@@ -12069,8 +12057,6 @@
1206912057
# `def self.from_raw(...)` factory widens the ivar's type.1207012058
cm_bodies = @cls_cmeth_bodies[ci].split(";")1207112059
cm_names = @cls_cmeth_names[ci].split(";")12072-
cm_params = @cls_cmeth_params[ci].split("|")12073-
cm_ptypes = @cls_cmeth_ptypes[ci].split("|")1207412060
saved_meth = @current_method_name1207512061
cbj = 01207612062
while cbj < cm_bodies.length@@ -12083,14 +12069,8 @@
1208312069
@current_method_name = @cls_names[ci] + "_cls_" + cm_names[cbj]1208412070
end1208512071
push_scope12086-
cpnames = "".split(",")12087-
cptypes = "".split(",")12088-
if cbj < cm_params.length12089-
cpnames = cm_params[cbj].split(",")12090-
end12091-
if cbj < cm_ptypes.length12092-
cptypes = cm_ptypes[cbj].split(",")12093-
end12072+
cpnames = cls_cmeth_pnames_get(ci, cbj)12073+
cptypes = cls_cmeth_ptypes_get(ci, cbj)1209412074
cpk = 01209512075
while cpk < cpnames.length1209612076
cpt = "int"@@ -14761,8 +14741,6 @@
1476114741
ci = 01476214742
while ci < @cls_names.length1476314743
mnames = @cls_meth_names[ci].split(";")14764-
all_params = @cls_meth_params[ci].split("|")14765-
all_ptypes = @cls_meth_ptypes[ci].split("|")1476614744
bodies = @cls_meth_bodies[ci].split(";")1476714745
mi = 01476814746
while mi < mnames.length@@ -14774,14 +14752,8 @@
1477414752
@current_class_idx = ci1477514753
push_scope1477614754
# Declare params in scope with current types14777-
pnames_arr = "".split(",")14778-
if mi < all_params.length14779-
pnames_arr = all_params[mi].split(",")14780-
end14781-
ptypes_arr = "".split(",")14782-
if mi < all_ptypes.length14783-
ptypes_arr = all_ptypes[mi].split(",")14784-
end14755+
pnames_arr = cls_meth_pnames_get(ci, mi)14756+
ptypes_arr = cls_meth_ptypes_get(ci, mi)1478514757
pk = 01478614758
while pk < pnames_arr.length1478714759
pt = "int"@@ -14925,26 +14897,17 @@
1492514897
args_id = @nd_arguments[nid]1492614898
if args_id >= 01492714899
arg_ids = get_args(args_id)14928-
all_ptypes = @cls_meth_ptypes[ci].split("|")14929-
if midx < all_ptypes.length14930-
ptypes = all_ptypes[midx].split(",")14931-
kk = 014932-
while kk < arg_ids.length14933-
at = infer_type(arg_ids[kk])14934-
if kk < ptypes.length14935-
# Same unify-instead-of-only-widen-from-int change as14936-
# the obj-recv path in scan_new_calls. A no-recv self14937-
# call inside the same class is the path that, e.g.,14938-
# `def boot; add_mappings(0x..0x, ...); end` inside14939-
# CPU#boot lands on, and disagreeing arg types from14940-
# different self-calls need to widen to poly rather14941-
# than freezing on the first non-int call site.14942-
ptypes[kk] = unify_call_types(ptypes[kk], at, arg_ids[kk])14943-
end14944-
kk = kk + 114945-
end14946-
all_ptypes[midx] = ptypes.join(",")14947-
@cls_meth_ptypes[ci] = all_ptypes.join("|")14900+
# Unify rather than only-widen-from-int. A no-recv14901+
# self-call inside the same class is the path that14902+
# e.g. `def boot; add_mappings(0x..0x, ...); end`14903+
# inside CPU#boot lands on; disagreeing arg types14904+
# from different self-calls need to widen to poly14905+
# rather than freezing on the first non-int call site.14906+
ptypes = cls_meth_ptypes_get(ci, midx)14907+
if ptypes.length > 014908+
pnames = cls_meth_pnames_get(ci, midx)14909+
widen_ptypes_from_args(arg_ids, pnames, ptypes)14910+
cls_meth_ptypes_put(ci, midx, ptypes)1494814911
end1494914912
end1495014913
end@@ -17316,8 +17279,7 @@
1731617279
# Instance methods1731717280
mnames = @cls_meth_names[i].split(";")1731817281
returns = @cls_meth_returns[i].split(";")17319-
all_params = @cls_meth_params[i].split("|")17320-
all_ptypes = @cls_meth_ptypes[i].split("|")17282+
bids = @cls_meth_bodies[i].split(";")1732117283
j = 01732217284
while j < mnames.length1732317285
if mnames[j] != "initialize"@@ -17333,17 +17295,14 @@
1733317295
if @cls_is_value_type[i] == 11733417296
sp = " self"1733517297
end17336-
bids = @cls_meth_bodies[i].split(";")1733717298
bid_j = j < bids.length ? bids[j].to_i : -117338-
emit_raw(method_linkage_named(bid_j, cls_method_has_yield(i, j), mnames[j]) + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mnames[j]) + "(sp_" + cname + sp + method_with_self_params(j, all_params, all_ptypes) + yp + ");")17299+
emit_raw(method_linkage_named(bid_j, cls_method_has_yield(i, j), mnames[j]) + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mnames[j]) + "(sp_" + cname + sp + method_with_self_params(i, j) + yp + ");")1733917300
end1734017301
j = j + 11734117302
end1734217303
# Class methods1734317304
cmnames = @cls_cmeth_names[i].split(";")1734417305
cm_returns = @cls_cmeth_returns[i].split(";")17345-
cm_params = @cls_cmeth_params[i].split("|")17346-
cm_ptypes = @cls_cmeth_ptypes[i].split("|")1734717306
j = 01734817307
while j < cmnames.length1734917308
# Skip forward decls for dead cls methods so the linker@@ -17357,7 +17316,7 @@
1735717316
if j < cm_returns.length1735817317
rt = cm_returns[j]1735917318
end17360-
emit_raw("static " + c_type(rt) + " sp_" + cname + "_cls_" + sanitize_name(cmnames[j]) + "(" + cls_method_params_decl(j, cm_params, cm_ptypes) + ");")17319+
emit_raw("static " + c_type(rt) + " sp_" + cname + "_cls_" + sanitize_name(cmnames[j]) + "(" + cls_method_params_decl(i, j) + ");")1736117320
j = j + 11736217321
end1736317322
end@@ -17796,16 +17755,9 @@
1779617755
result1779717756
end1779817757
17799-
def method_with_self_params(midx, all_params, all_ptypes)17800-
pnames = "".split(",")17801-
ptypes = "".split(",")17802-
17803-
if midx < all_params.length17804-
pnames = all_params[midx].split(",")17805-
end17806-
if midx < all_ptypes.length17807-
ptypes = all_ptypes[midx].split(",")17808-
end17758+
def method_with_self_params(ci, midx)17759+
pnames = cls_meth_pnames_get(ci, midx)17760+
ptypes = cls_meth_ptypes_get(ci, midx)1780917761
result = ""1781017762
j = 01781117763
while j < pnames.length@@ -17819,16 +17771,9 @@
1781917771
result1782017772
end1782117773
17822-
def cls_method_params_decl(midx, all_params, all_ptypes)17823-
pnames = "".split(",")17824-
ptypes = "".split(",")17825-
17826-
if midx < all_params.length17827-
pnames = all_params[midx].split(",")17828-
end17829-
if midx < all_ptypes.length17830-
ptypes = all_ptypes[midx].split(",")17831-
end17774+
def cls_method_params_decl(ci, midx)17775+
pnames = cls_cmeth_pnames_get(ci, midx)17776+
ptypes = cls_cmeth_ptypes_get(ci, midx)1783217777
if pnames.length == 01783317778
return "void"1783417779
end