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 = 0
21242123 while j < cmnames.length
21252124 if cmnames[j] == mname
@@ -2131,10 +2130,7 @@
21312130 if j < cm_returns.length
21322131 rt = cm_returns[j]
21332132 end
2134- pt_list = "".split(",")
2135- if j < cm_ptypes.length
2136- pt_list = cm_ptypes[j].split(",")
2137- end
2133+ 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.length
1084610842 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 = 0
1085110845 while j < cmnames.length
1085210846 bid = -1
@@ -10855,16 +10849,10 @@
1085510849 if bs != ""
1085610850 bid = bs.to_i
1085710851 end
10858- end
10859- pnames = "".split(",")
10860- if j < cm_params.length
10861- pnames = cm_params[j].split(",")
10862- end
10863- ptypes = "".split(",")
10864- if j < cm_ptypes.length
10865- ptypes = cm_ptypes[j].split(",")
1086610852 end
1086710853 if bid > 0
10854+ pnames = cls_cmeth_pnames_get(ci, j)
10855+ ptypes = cls_cmeth_ptypes_get(ci, j)
1086810856 saved_ci = @current_class_idx
1086910857 @current_class_idx = ci
1087010858 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_name
1207512061 cbj = 0
1207612062 while cbj < cm_bodies.length
@@ -12083,14 +12069,8 @@
1208312069 @current_method_name = @cls_names[ci] + "_cls_" + cm_names[cbj]
1208412070 end
1208512071 push_scope
12086- cpnames = "".split(",")
12087- cptypes = "".split(",")
12088- if cbj < cm_params.length
12089- cpnames = cm_params[cbj].split(",")
12090- end
12091- if cbj < cm_ptypes.length
12092- cptypes = cm_ptypes[cbj].split(",")
12093- end
12072+ cpnames = cls_cmeth_pnames_get(ci, cbj)
12073+ cptypes = cls_cmeth_ptypes_get(ci, cbj)
1209412074 cpk = 0
1209512075 while cpk < cpnames.length
1209612076 cpt = "int"
@@ -14761,8 +14741,6 @@
1476114741 ci = 0
1476214742 while ci < @cls_names.length
1476314743 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 = 0
1476814746 while mi < mnames.length
@@ -14774,14 +14752,8 @@
1477414752 @current_class_idx = ci
1477514753 push_scope
1477614754 # Declare params in scope with current types
14777- pnames_arr = "".split(",")
14778- if mi < all_params.length
14779- pnames_arr = all_params[mi].split(",")
14780- end
14781- ptypes_arr = "".split(",")
14782- if mi < all_ptypes.length
14783- ptypes_arr = all_ptypes[mi].split(",")
14784- end
14755+ pnames_arr = cls_meth_pnames_get(ci, mi)
14756+ ptypes_arr = cls_meth_ptypes_get(ci, mi)
1478514757 pk = 0
1478614758 while pk < pnames_arr.length
1478714759 pt = "int"
@@ -14925,26 +14897,17 @@
1492514897 args_id = @nd_arguments[nid]
1492614898 if args_id >= 0
1492714899 arg_ids = get_args(args_id)
14928- all_ptypes = @cls_meth_ptypes[ci].split("|")
14929- if midx < all_ptypes.length
14930- ptypes = all_ptypes[midx].split(",")
14931- kk = 0
14932- while kk < arg_ids.length
14933- at = infer_type(arg_ids[kk])
14934- if kk < ptypes.length
14935- # Same unify-instead-of-only-widen-from-int change as
14936- # the obj-recv path in scan_new_calls. A no-recv self
14937- # call inside the same class is the path that, e.g.,
14938- # `def boot; add_mappings(0x..0x, ...); end` inside
14939- # CPU#boot lands on, and disagreeing arg types from
14940- # different self-calls need to widen to poly rather
14941- # than freezing on the first non-int call site.
14942- ptypes[kk] = unify_call_types(ptypes[kk], at, arg_ids[kk])
14943- end
14944- kk = kk + 1
14945- end
14946- all_ptypes[midx] = ptypes.join(",")
14947- @cls_meth_ptypes[ci] = all_ptypes.join("|")
14900+ # Unify rather than only-widen-from-int. A no-recv
14901+ # self-call inside the same class is the path that
14902+ # e.g. `def boot; add_mappings(0x..0x, ...); end`
14903+ # inside CPU#boot lands on; disagreeing arg types
14904+ # from different self-calls need to widen to poly
14905+ # rather than freezing on the first non-int call site.
14906+ ptypes = cls_meth_ptypes_get(ci, midx)
14907+ if ptypes.length > 0
14908+ 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 end
1494914912 end
1495014913 end
@@ -17316,8 +17279,7 @@
1731617279 # Instance methods
1731717280 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 = 0
1732217284 while j < mnames.length
1732317285 if mnames[j] != "initialize"
@@ -17333,17 +17295,14 @@
1733317295 if @cls_is_value_type[i] == 1
1733417296 sp = " self"
1733517297 end
17336- bids = @cls_meth_bodies[i].split(";")
1733717298 bid_j = j < bids.length ? bids[j].to_i : -1
17338- 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 end
1734017301 j = j + 1
1734117302 end
1734217303 # Class methods
1734317304 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 = 0
1734817307 while j < cmnames.length
1734917308 # Skip forward decls for dead cls methods so the linker
@@ -17357,7 +17316,7 @@
1735717316 if j < cm_returns.length
1735817317 rt = cm_returns[j]
1735917318 end
17360- 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 + 1
1736217321 end
1736317322 end
@@ -17796,16 +17755,9 @@
1779617755 result
1779717756 end
1779817757
17799- def method_with_self_params(midx, all_params, all_ptypes)
17800- pnames = "".split(",")
17801- ptypes = "".split(",")
17802-
17803- if midx < all_params.length
17804- pnames = all_params[midx].split(",")
17805- end
17806- if midx < all_ptypes.length
17807- ptypes = all_ptypes[midx].split(",")
17808- end
17758+ 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 = 0
1781117763 while j < pnames.length
@@ -17819,16 +17771,9 @@
1781917771 result
1782017772 end
1782117773
17822- def cls_method_params_decl(midx, all_params, all_ptypes)
17823- pnames = "".split(",")
17824- ptypes = "".split(",")
17825-
17826- if midx < all_params.length
17827- pnames = all_params[midx].split(",")
17828- end
17829- if midx < all_ptypes.length
17830- ptypes = all_ptypes[midx].split(",")
17831- end
17774+ 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 == 0
1783317778 return "void"
1783417779 end