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 >= 0
99529952 arg_ids = get_args(args_id)
9953- all_ptypes = @cls_meth_ptypes[owner_ci].split("|")
9954- if midx < all_ptypes.length
9955- ptypes = all_ptypes[midx].split(",")
9953+ ptypes = cls_meth_ptypes_get(owner_ci, midx)
9954+ if ptypes.length > 0
99569955 kk = 0
99579956 while kk < arg_ids.length
99589957 at = infer_type(arg_ids[kk])
@@ -9965,8 +9964,7 @@
99659964 end
99669965 kk = kk + 1
99679966 end
9968- all_ptypes[midx] = ptypes.join(",")
9969- @cls_meth_ptypes[owner_ci] = all_ptypes.join("|")
9967+ cls_meth_ptypes_put(owner_ci, midx, ptypes)
99709968 end
99719969 end
99729970 end
@@ -10637,27 +10635,19 @@
1063710635 args_id = @nd_arguments[nid]
1063810636 if args_id >= 0
1063910637 arg_ids = get_args(args_id)
10640- all_ptypes = @cls_meth_ptypes[owner_ci].split("|")
10641- if midx < all_ptypes.length
10642- ptypes = all_ptypes[midx].split(",")
10643- kk = 0
10644- while kk < arg_ids.length
10645- at = infer_type(arg_ids[kk])
10646- if kk < ptypes.length
10647- # Unify against the existing param type rather
10648- # than only widening from "int". The previous
10649- # rule let the FIRST non-int call site freeze
10650- # the param type; subsequent disagreeing calls
10651- # (e.g. addr arg seen as Range from one site
10652- # and as Integer from another) compiled to a
10653- # signature mismatch. unify_call_types collapses
10654- # incompatible types to "poly".
10655- ptypes[kk] = unify_call_types(ptypes[kk], at, arg_ids[kk])
10656- end
10657- kk = kk + 1
10658- end
10659- all_ptypes[midx] = ptypes.join(",")
10660- @cls_meth_ptypes[owner_ci] = all_ptypes.join("|")
10638+ # Unify against the existing param type rather
10639+ # than only widening from "int". A first-non-int-
10640+ # wins rule would let one call site freeze the
10641+ # param type and a later disagreeing site (e.g.
10642+ # `addr` seen as Range from one and Integer from
10643+ # another) lands as a signature mismatch.
10644+ # unify_call_types collapses incompatibles to
10645+ # "poly".
10646+ ptypes = cls_meth_ptypes_get(owner_ci, midx)
10647+ if ptypes.length > 0
10648+ 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 end
1066210652 end
1066310653 end
@@ -10714,24 +10704,18 @@
1071410704 if cci >= 0
1071510705 cmname = @nd_name[nid]
1071610706 cmnames = @cls_cmeth_names[cci].split(";")
10717- cm_ptypes_all = @cls_cmeth_ptypes[cci].split("|")
1071810707 cmidx = 0
1071910708 while cmidx < cmnames.length
1072010709 if cmnames[cmidx] == cmname
1072110710 args_id = @nd_arguments[nid]
10722- if args_id >= 0 && cmidx < cm_ptypes_all.length
10711+ if args_id >= 0
1072310712 arg_ids = get_args(args_id)
10724- cmptypes = cm_ptypes_all[cmidx].split(",")
10725- kk = 0
10726- while kk < arg_ids.length
10727- at = infer_type(arg_ids[kk])
10728- if kk < cmptypes.length
10729- cmptypes[kk] = unify_call_types(cmptypes[kk], at, arg_ids[kk])
10730- end
10731- kk = kk + 1
10713+ cmptypes = cls_cmeth_ptypes_get(cci, cmidx)
10714+ if cmptypes.length > 0
10715+ 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 end
10733- cm_ptypes_all[cmidx] = cmptypes.join(",")
10734- @cls_cmeth_ptypes[cci] = cm_ptypes_all.join("|")
1073510719 end
1073610720 end
1073710721 cmidx = cmidx + 1
@@ -10965,9 +10949,8 @@
1096510949 if init_ci >= 0
1096610950 init_idx = cls_find_method_direct(init_ci, "initialize")
1096710951 if init_idx >= 0
10968- all_ptypes = @cls_meth_ptypes[init_ci].split("|")
10969- if init_idx < all_ptypes.length
10970- init_ptypes = all_ptypes[init_idx].split(",")
10952+ init_ptypes = cls_meth_ptypes_get(init_ci, init_idx)
10953+ if init_ptypes.length > 0
1097110954 kk = 0
1097210955 while kk < arg_ids.length
1097310956 at = ""
@@ -10989,8 +10972,7 @@
1098910972 end
1099010973 kk = kk + 1
1099110974 end
10992- 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 end
1099510977 end
1099610978 end
@@ -17692,6 +17674,81 @@
1769217674 -1
1769317675 end
1769417676
17677+ # Helpers for the per-method split-join boilerplate around
17678+ # @cls_meth_ptypes / @cls_cmeth_ptypes / @cls_meth_params /
17679+ # @cls_cmeth_params. Each table stores per-class strings where
17680+ # methods are pipe-separated (`|`) and per-method names/types
17681+ # are comma-separated (`,`). Without these, every read site
17682+ # opens with two split() calls + bound checks and every write
17683+ # site closes with two join() calls.
17684+ def cls_meth_ptypes_get(ci, midx)
17685+ if ci < 0 || ci >= @cls_meth_ptypes.length || midx < 0
17686+ return "".split(",")
17687+ end
17688+ all = @cls_meth_ptypes[ci].split("|")
17689+ if midx >= all.length
17690+ return "".split(",")
17691+ end
17692+ all[midx].split(",")
17693+ end
17694+
17695+ def cls_meth_ptypes_put(ci, midx, ptypes)
17696+ if ci < 0 || ci >= @cls_meth_ptypes.length || midx < 0
17697+ return
17698+ end
17699+ all = @cls_meth_ptypes[ci].split("|")
17700+ if midx >= all.length
17701+ return
17702+ end
17703+ all[midx] = ptypes.join(",")
17704+ @cls_meth_ptypes[ci] = all.join("|")
17705+ end
17706+
17707+ def cls_meth_pnames_get(ci, midx)
17708+ if ci < 0 || ci >= @cls_meth_params.length || midx < 0
17709+ return "".split(",")
17710+ end
17711+ all = @cls_meth_params[ci].split("|")
17712+ if midx >= all.length
17713+ return "".split(",")
17714+ end
17715+ all[midx].split(",")
17716+ end
17717+
17718+ def cls_cmeth_ptypes_get(ci, midx)
17719+ if ci < 0 || ci >= @cls_cmeth_ptypes.length || midx < 0
17720+ return "".split(",")
17721+ end
17722+ all = @cls_cmeth_ptypes[ci].split("|")
17723+ if midx >= all.length
17724+ return "".split(",")
17725+ end
17726+ all[midx].split(",")
17727+ end
17728+
17729+ def cls_cmeth_ptypes_put(ci, midx, ptypes)
17730+ if ci < 0 || ci >= @cls_cmeth_ptypes.length || midx < 0
17731+ return
17732+ end
17733+ all = @cls_cmeth_ptypes[ci].split("|")
17734+ if midx >= all.length
17735+ return
17736+ end
17737+ all[midx] = ptypes.join(",")
17738+ @cls_cmeth_ptypes[ci] = all.join("|")
17739+ end
17740+
17741+ def cls_cmeth_pnames_get(ci, midx)
17742+ if ci < 0 || ci >= @cls_cmeth_params.length || midx < 0
17743+ return "".split(",")
17744+ end
17745+ all = @cls_cmeth_params[ci].split("|")
17746+ if midx >= all.length
17747+ return "".split(",")
17748+ end
17749+ all[midx].split(",")
17750+ end
17751+
1769517752 # Walk the inheritance chain starting at class `ci` looking for the
1769617753 # first ancestor that defines `mname` directly. Returns that class's
1769717754 # name, or "" if no ancestor defines it. Used by the