Commit d57369dc

d57369dcf89dMerged

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

Description

refactor(codegen): migrate single-method ptypes/pnames sites (2/N)

Continue migrating split-join boilerplate to the get/put helpers introduced in 9c353c0. This pass covers single-method use sites (per-call-site widening, signature builders, init-param-decl helpers) where reading or writing one (ci, midx) slot replaces ~10 lines of split-and-bound-check. Skips per-class loops where calling put inside the inner iteration would re-split the whole table per method (O(M²) writes).

Tests: 301 pass. Bootstrap: gen2.c == gen3.c.

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

Details

Commit
d57369dcf89d
Parent
f1f90f8839bc

spinel_codegen.rbmodifiedView file

@@ -1030,20 +1030,9 @@
10301030 # `def m(&b); ...; end` shape (instance_eval trampoline today;
10311031 # extensible to instance_exec, tap, etc.).
10321032 def cls_method_sole_proc_param_name(ci, midx)
1033- all_params = @cls_meth_params[ci].split("|")
1034- all_ptypes = @cls_meth_ptypes[ci].split("|")
1035- if midx >= all_params.length
1036- return ""
1037- end
1038- if midx >= all_ptypes.length
1039- return ""
1040- end
1041- pnames = all_params[midx].split(",")
1042- ptypes = all_ptypes[midx].split(",")
1043- if pnames.length != 1
1044- return ""
1045- end
1046- if ptypes.length != 1
1033+ pnames = cls_meth_pnames_get(ci, midx)
1034+ ptypes = cls_meth_ptypes_get(ci, midx)
1035+ if pnames.length != 1 || ptypes.length != 1
10471036 return ""
10481037 end
10491038 if ptypes[0] != "proc"
@@ -6501,21 +6490,13 @@
65016490 while ci < @cls_names.length
65026491 @current_class_idx = ci
65036492 bodies = @cls_meth_bodies[ci].split(";")
6504- all_params = @cls_meth_params[ci].split("|")
6505- all_ptypes = @cls_meth_ptypes[ci].split("|")
65066493 bj = 0
65076494 while bj < bodies.length
65086495 bid = bodies[bj].to_i
65096496 if bid >= 0
65106497 push_scope
6511- pnames = "".split(",")
6512- ptypes = "".split(",")
6513- if bj < all_params.length
6514- pnames = all_params[bj].split(",")
6515- end
6516- if bj < all_ptypes.length
6517- ptypes = all_ptypes[bj].split(",")
6518- end
6498+ pnames = cls_meth_pnames_get(ci, bj)
6499+ ptypes = cls_meth_ptypes_get(ci, bj)
65196500 k = 0
65206501 while k < pnames.length
65216502 pt = "int"
@@ -10553,53 +10534,11 @@
1055310534 args_id = @nd_arguments[nid]
1055410535 if args_id >= 0
1055510536 arg_ids = get_args(args_id)
10556- all_ptypes = @cls_meth_ptypes[init_ci].split("|")
10557- all_params = @cls_meth_params[init_ci].split("|")
10558- if init_idx < all_ptypes.length
10559- ptypes = all_ptypes[init_idx].split(",")
10560- pnames = "".split(",")
10561- if init_idx < all_params.length
10562- pnames = all_params[init_idx].split(",")
10563- end
10564- k = 0
10565- while k < arg_ids.length
10566- if @nd_type[arg_ids[k]] == "KeywordHashNode"
10567- # Handle keyword args
10568- elems = parse_id_list(@nd_elements[arg_ids[k]])
10569- ek = 0
10570- while ek < elems.length
10571- if @nd_type[elems[ek]] == "AssocNode"
10572- key_id = @nd_key[elems[ek]]
10573- if key_id >= 0
10574- kname = ""
10575- if @nd_type[key_id] == "SymbolNode"
10576- kname = @nd_content[key_id]
10577- end
10578- expr_id = @nd_expression[elems[ek]]
10579- at = infer_type(expr_id)
10580- pi = 0
10581- while pi < pnames.length
10582- if pnames[pi] == kname
10583- if pi < ptypes.length
10584- ptypes[pi] = unify_call_types(ptypes[pi], at, expr_id)
10585- end
10586- end
10587- pi = pi + 1
10588- end
10589- end
10590- end
10591- ek = ek + 1
10592- end
10593- else
10594- at = infer_type(arg_ids[k])
10595- if k < ptypes.length
10596- ptypes[k] = unify_call_types(ptypes[k], at, arg_ids[k])
10597- end
10598- end
10599- k = k + 1
10600- end
10601- all_ptypes[init_idx] = ptypes.join(",")
10602- @cls_meth_ptypes[init_ci] = all_ptypes.join("|")
10537+ ptypes = cls_meth_ptypes_get(init_ci, init_idx)
10538+ if ptypes.length > 0
10539+ pnames = cls_meth_pnames_get(init_ci, init_idx)
10540+ widen_ptypes_from_args(arg_ids, pnames, ptypes)
10541+ cls_meth_ptypes_put(init_ci, init_idx, ptypes)
1060310542 end
1060410543 end
1060510544 end
@@ -11041,17 +10980,8 @@
1104110980 if init_idx2 < bodies.length
1104210981 if bodies[init_idx2].to_i == -2
1104310982 # Synthetic struct - update ivar types from init param types
11044- all_params = @cls_meth_params[i].split("|")
11045- all_ptypes = @cls_meth_ptypes[i].split("|")
11046- pnames = "".split(",")
11047- ptypes = "".split(",")
11048-
11049- if init_idx2 < all_params.length
11050- pnames = all_params[init_idx2].split(",")
11051- end
11052- if init_idx2 < all_ptypes.length
11053- ptypes = all_ptypes[init_idx2].split(",")
11054- end
10983+ pnames = cls_meth_pnames_get(i, init_idx2)
10984+ ptypes = cls_meth_ptypes_get(i, init_idx2)
1105510985 pk = 0
1105610986 while pk < pnames.length
1105710987 iname = "@" + pnames[pk]
@@ -11074,17 +11004,8 @@
1107411004 mi = 0
1107511005 while mi < mnames.length
1107611006 init_idx = mi
11077- all_params = @cls_meth_params[i].split("|")
11078- all_ptypes = @cls_meth_ptypes[i].split("|")
11079- pnames = "".split(",")
11080- ptypes = "".split(",")
11081-
11082- if init_idx < all_params.length
11083- pnames = all_params[init_idx].split(",")
11084- end
11085- if init_idx < all_ptypes.length
11086- ptypes = all_ptypes[init_idx].split(",")
11087- end
11007+ pnames = cls_meth_pnames_get(i, init_idx)
11008+ ptypes = cls_meth_ptypes_get(i, init_idx)
1108811009 bodies = @cls_meth_bodies[i].split(";")
1108911010 bid = -1
1109011011 if init_idx < bodies.length
@@ -11141,21 +11062,12 @@
1114111062 oci = 0
1114211063 while oci < @cls_names.length
1114311064 mnames = @cls_meth_names[oci].split(";")
11144- all_params = @cls_meth_params[oci].split("|")
11145- all_ptypes = @cls_meth_ptypes[oci].split("|")
1114611065 bodies = @cls_meth_bodies[oci].split(";")
1114711066 j = 0
1114811067 while j < mnames.length
1114911068 if mnames[j] != "initialize"
11150- pnames = "".split(",")
11151- ptypes = "".split(",")
11152-
11153- if j < all_params.length
11154- pnames = all_params[j].split(",")
11155- end
11156- if j < all_ptypes.length
11157- ptypes = all_ptypes[j].split(",")
11158- end
11069+ pnames = cls_meth_pnames_get(oci, j)
11070+ ptypes = cls_meth_ptypes_get(oci, j)
1115911071 bid = -1
1116011072 if j < bodies.length
1116111073 bid = bodies[j].to_i
@@ -11167,10 +11079,10 @@
1116711079 if ptypes[pk] == "int"
1116811080 # Pick the class whose surface (readers + writers +
1116911081 # methods, walked through parents) contains every
11170- # method actually called on this param. The old
11171- # algorithm matched on a single reader and ignored
11172- # later accesses, picking a class that didn't satisfy
11173- # them — issue #35.
11082+ # method actually called on this param. Matching
11083+ # only one reader and ignoring later accesses
11084+ # would pick a class that fails to satisfy the
11085+ # full method set.
1117411086 called = "".split(",")
1117511087 collect_param_methods(bid, pnames[pk], called)
1117611088 if called.length > 0 && called_methods_only_on_container_builtins(called) == 0
@@ -11184,8 +11096,7 @@
1118411096 end
1118511097 if best >= 0
1118611098 ptypes[pk] = "obj_" + @cls_names[best]
11187- all_ptypes[j] = ptypes.join(",")
11188- @cls_meth_ptypes[oci] = all_ptypes.join("|")
11099+ cls_meth_ptypes_put(oci, j, ptypes)
1118911100 end
1119011101 end
1119111102 end
@@ -17637,14 +17548,7 @@
1763717548 # Mirrors cls_method_has_yield: call sites use it to decide whether
1763817549 # to omit the trailing &block slot from default-padding.
1763917550 def cls_method_has_block_param(ci, midx)
17640- if ci < 0 || midx < 0
17641- return 0
17642- end
17643- all_ptypes = @cls_meth_ptypes[ci].split("|")
17644- if midx >= all_ptypes.length
17645- return 0
17646- end
17647- pts = all_ptypes[midx].split(",")
17551+ pts = cls_meth_ptypes_get(ci, midx)
1764817552 (pts.length > 0 && pts.last == "proc") ? 1 : 0
1764917553 end
1765017554
@@ -17847,17 +17751,8 @@
1784717751 return "void"
1784817752 end
1784917753 init_idx = cls_find_method_direct(init_ci, "initialize")
17850- all_params = @cls_meth_params[init_ci].split("|")
17851- all_ptypes = @cls_meth_ptypes[init_ci].split("|")
17852- pnames = "".split(",")
17853- ptypes = "".split(",")
17854-
17855- if init_idx < all_params.length
17856- pnames = all_params[init_idx].split(",")
17857- end
17858- if init_idx < all_ptypes.length
17859- ptypes = all_ptypes[init_idx].split(",")
17860- end
17754+ pnames = cls_meth_pnames_get(init_ci, init_idx)
17755+ ptypes = cls_meth_ptypes_get(init_ci, init_idx)
1786117756 if pnames.length == 0
1786217757 return "void"
1786317758 end
@@ -17886,17 +17781,8 @@
1788617781 if init_idx < 0
1788717782 return ""
1788817783 end
17889- all_params = @cls_meth_params[init_ci].split("|")
17890- all_ptypes = @cls_meth_ptypes[init_ci].split("|")
17891- pnames = "".split(",")
17892- ptypes = "".split(",")
17893-
17894- if init_idx < all_params.length
17895- pnames = all_params[init_idx].split(",")
17896- end
17897- if init_idx < all_ptypes.length
17898- ptypes = all_ptypes[init_idx].split(",")
17899- end
17784+ pnames = cls_meth_pnames_get(init_ci, init_idx)
17785+ ptypes = cls_meth_ptypes_get(init_ci, init_idx)
1790017786 result = ""
1790117787 j = 0
1790217788 while j < pnames.length
@@ -28011,11 +27897,7 @@
2801127897 # this candidate accepts — happens when several classes share
2801227898 # the method name but disagree on parameter count).
2801327899 arm_arg_strs = ""
28014- all_ptypes = @cls_meth_ptypes[i].split("|")
28015- arm_ptypes = "".split(",")
28016- if midx < all_ptypes.length
28017- arm_ptypes = all_ptypes[midx].split(",")
28018- end
27900+ arm_ptypes = cls_meth_ptypes_get(i, midx)
2801927901 pk = 0
2802027902 while pk < arm_ptypes.length
2802127903 if pk < arg_compiled.length
@@ -29322,16 +29204,8 @@
2932229204 if init_idx < 0
2932329205 return compile_call_args(nid)
2932429206 end
29325- all_params = @cls_meth_params[init_ci].split("|")
29326- all_ptypes = @cls_meth_ptypes[init_ci].split("|")
29327- pnames = "".split(",")
29328- ptypes = "".split(",")
29329- if init_idx < all_params.length
29330- pnames = all_params[init_idx].split(",")
29331- end
29332- if init_idx < all_ptypes.length
29333- ptypes = all_ptypes[init_idx].split(",")
29334- end
29207+ pnames = cls_meth_pnames_get(init_ci, init_idx)
29208+ ptypes = cls_meth_ptypes_get(init_ci, init_idx)
2933529209 # Build args in param order using keyword values
2933629210 result = ""
2933729211 pk = 0