Commit 139b5d37

139b5d374568Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 13 files · +12 added · ~1 modified

Description

Merge pull request #385 from mame/pr/forward-call-class-method-inherited-init

fix(codegen): forward-ref Class.new ptype widening — qualified-name normalization + super-call propagation

Details

Commit
139b5d374568
Parent
45b412267afb
Merge parent
60016a69a438

spinel_codegen.rbmodifiedView file

@@ -7875,6 +7875,34 @@
78757875 if names[k] == iname
78767876 if k < types.length
78777877 old = types[k]
7878+ # Stale unqualified obj-name normalization. collect_ivars
7879+ # registers ivar types during Pass 1 (class enumeration), in
7880+ # source order; at that point sibling classes that come later
7881+ # in the file haven't been registered yet. A
7882+ # `@cpu = CPU.new(@conf)` write inside the lexically-first
7883+ # class then resolves "CPU" against an incomplete class
7884+ # table, lands at unqualified "obj_CPU", and pins the slot.
7885+ # When a subsequent pass — with all classes registered —
7886+ # records the qualified "obj_<scope>_CPU" via scan_writer_calls
7887+ # (whose lexical resolution does succeed), the stale and
7888+ # qualified forms compare unequal and the heterogeneity
7889+ # branch below widens to "poly". Detect the relationship
7890+ # (old's bare name matches the trailing segment of new's
7891+ # name AND old's bare name doesn't refer to any registered
7892+ # class) and accept the qualified form as a refinement
7893+ # rather than a disagreement.
7894+ if is_obj_type(old) == 1 && is_obj_type(new_type) == 1 && old != new_type
7895+ old_bare_uit = old[4, old.length - 4]
7896+ new_bare_uit = new_type[4, new_type.length - 4]
7897+ if find_class_idx(old_bare_uit) < 0 && find_class_idx(new_bare_uit) >= 0
7898+ suffix_uit = "_" + old_bare_uit
7899+ if new_bare_uit.length > suffix_uit.length && new_bare_uit[(new_bare_uit.length - suffix_uit.length), suffix_uit.length] == suffix_uit
7900+ types[k] = new_type
7901+ @cls_ivar_types[ci] = types.join(";")
7902+ old = new_type
7903+ end
7904+ end
7905+ end
78787906 # Heterogeneous int/nil + obj → poly when the prior write
78797907 # was a *definite* int/nil literal. The previous "int wins"
78807908 # / "nil wins" overwrite silently cast the int payload to a
@@ -10017,6 +10045,32 @@
1001710045 if old_pt == at
1001810046 return old_pt
1001910047 end
10048+ # Stale unqualified obj-name normalization. Same shape as the
10049+ # update_ivar_type fix: a Pass 1 ivar / param scan that ran
10050+ # before all sibling classes were registered may have stamped
10051+ # an unqualified `obj_<bare>` on a slot, which a later pass
10052+ # then sees alongside the qualified `obj_<scope>_<bare>`.
10053+ # Without this normalization the two strings compare unequal
10054+ # and the trailing `incompatible → poly` tail collapses the
10055+ # ptype, then propagates poly across every Class.new call site
10056+ # that reads the slot. Detect the relationship and accept the
10057+ # qualified form.
10058+ if is_obj_type(old_pt) == 1 && is_obj_type(at) == 1
10059+ old_bare_uct = old_pt[4, old_pt.length - 4]
10060+ new_bare_uct = at[4, at.length - 4]
10061+ if find_class_idx(old_bare_uct) < 0 && find_class_idx(new_bare_uct) >= 0
10062+ suffix_uct = "_" + old_bare_uct
10063+ if new_bare_uct.length > suffix_uct.length && new_bare_uct[(new_bare_uct.length - suffix_uct.length), suffix_uct.length] == suffix_uct
10064+ return at
10065+ end
10066+ end
10067+ if find_class_idx(new_bare_uct) < 0 && find_class_idx(old_bare_uct) >= 0
10068+ suffix_uct = "_" + new_bare_uct
10069+ if old_bare_uct.length > suffix_uct.length && old_bare_uct[(old_bare_uct.length - suffix_uct.length), suffix_uct.length] == suffix_uct
10070+ return old_pt
10071+ end
10072+ end
10073+ end
1002010074 arg_is_literal = 0
1002110075 if arg_id >= 0 && is_literal_value_expr(arg_id) == 1
1002210076 arg_is_literal = 1
@@ -10926,6 +10980,150 @@
1092610980 # resolve via the cls method's pnames/ptypes; other args fall
1092710981 # through to infer_type with @current_class_idx pinned so
1092810982 # @ivar refs in the args resolve against the right class.
10983+ # Propagate ptypes from child#initialize to parent#initialize via
10984+ # super calls. Without this, a `Sub.new(owner_obj)` site widens
10985+ # `Sub#initialize`'s `_owner` param to `obj_<C>` correctly (via
10986+ # the existing constructor branch in scan_new_calls), but the
10987+ # body's bare `super` lowers to a C call against the parent's
10988+ # `Base#initialize`, whose param ptype stays at the default
10989+ # `mrb_int` because no call-site path widens the *parent's*
10990+ # initialize through super. The C output then casts the typed
10991+ # pointer to `mrb_int` at the super-call site, the parent's body's
10992+ # `@owner = owner` records the int into the slot, and every
10993+ # subsequent `@owner.<method>` dispatch on a child instance lands
10994+ # on whatever class shares the int recv's cls_id — commonly the
10995+ # wrong one.
10996+ #
10997+ # ForwardingSuperNode (bare `super`): forwards every param of the
10998+ # current method by name. Unify the parent ptypes element-wise
10999+ # against the child ptypes (capped at min length, so a wider
11000+ # parent signature stays unwidened past the forwarded prefix).
11001+ #
11002+ # SuperNode (`super(arg1, arg2, …)`): explicit arg list. Each arg
11003+ # might be a LocalVariableReadNode that points back at one of the
11004+ # current method's params; if so, use the param's ptype to unify
11005+ # at the super arg's position. Other shapes (literals, calls)
11006+ # fall through to infer_type.
11007+ def propagate_super_init_to_parent
11008+ ci = 0
11009+ while ci < @cls_names.length
11010+ if @cls_parents[ci] != ""
11011+ parent_ci = find_class_idx(@cls_parents[ci])
11012+ # The parent chain may insert classes without their own
11013+ # #initialize (e.g. an empty marker subclass between the
11014+ # super caller and its semantic parent). Walk up until we
11015+ # find one that does.
11016+ walk_ci = parent_ci
11017+ parent_init_ci = -1
11018+ while walk_ci >= 0
11019+ if cls_find_method_direct(walk_ci, "initialize") >= 0
11020+ parent_init_ci = walk_ci
11021+ break
11022+ end
11023+ parent_str = @cls_parents[walk_ci]
11024+ if parent_str == ""
11025+ break
11026+ end
11027+ walk_ci = find_class_idx(parent_str)
11028+ end
11029+ if parent_init_ci >= 0
11030+ propagate_super_init_one(ci, parent_init_ci)
11031+ end
11032+ end
11033+ ci = ci + 1
11034+ end
11035+ end
11036+
11037+ def propagate_super_init_one(child_ci, parent_init_ci)
11038+ child_init_idx = cls_find_method_direct(child_ci, "initialize")
11039+ if child_init_idx < 0
11040+ return
11041+ end
11042+ bodies = @cls_meth_bodies[child_ci].split(";")
11043+ if child_init_idx >= bodies.length
11044+ return
11045+ end
11046+ bid = bodies[child_init_idx].to_i
11047+ if bid < 0
11048+ return
11049+ end
11050+ child_pnames = cls_meth_pnames_get(child_ci, child_init_idx)
11051+ child_ptypes = cls_meth_ptypes_get(child_ci, child_init_idx)
11052+ parent_init_idx = cls_find_method_direct(parent_init_ci, "initialize")
11053+ if parent_init_idx < 0
11054+ return
11055+ end
11056+ parent_ptypes = cls_meth_ptypes_get(parent_init_ci, parent_init_idx)
11057+ if parent_ptypes.length == 0
11058+ return
11059+ end
11060+ propagate_super_walk(bid, child_pnames, child_ptypes, parent_ptypes)
11061+ cls_meth_ptypes_put(parent_init_ci, parent_init_idx, parent_ptypes)
11062+ end
11063+
11064+ def propagate_super_walk(nid, child_pnames, child_ptypes, parent_ptypes)
11065+ if nid < 0
11066+ return
11067+ end
11068+ nt = @nd_type[nid]
11069+ if nt == "DefNode"
11070+ return
11071+ end
11072+ if nt == "ForwardingSuperNode"
11073+ # Bare `super`: forward every child param into the parent's slot
11074+ # at the same position, capped at min length.
11075+ kk = 0
11076+ lim = child_ptypes.length
11077+ if parent_ptypes.length < lim
11078+ lim = parent_ptypes.length
11079+ end
11080+ while kk < lim
11081+ ct = child_ptypes[kk]
11082+ if ct != "" && ct != "int"
11083+ parent_ptypes[kk] = unify_call_types(parent_ptypes[kk], ct, -1)
11084+ end
11085+ kk = kk + 1
11086+ end
11087+ return
11088+ end
11089+ if nt == "SuperNode"
11090+ args_id = @nd_arguments[nid]
11091+ if args_id >= 0
11092+ a_ids = get_args(args_id)
11093+ kk = 0
11094+ while kk < a_ids.length && kk < parent_ptypes.length
11095+ aid = a_ids[kk]
11096+ at = ""
11097+ if @nd_type[aid] == "LocalVariableReadNode"
11098+ vname = @nd_name[aid]
11099+ pi = 0
11100+ while pi < child_pnames.length
11101+ if child_pnames[pi] == vname && pi < child_ptypes.length
11102+ at = child_ptypes[pi]
11103+ end
11104+ pi = pi + 1
11105+ end
11106+ end
11107+ if at == ""
11108+ at = infer_type(aid)
11109+ end
11110+ if at != "" && at != "int"
11111+ parent_ptypes[kk] = unify_call_types(parent_ptypes[kk], at, aid)
11112+ end
11113+ kk = kk + 1
11114+ end
11115+ end
11116+ return
11117+ end
11118+ cs = []
11119+ push_child_ids(nid, cs)
11120+ k = 0
11121+ while k < cs.length
11122+ propagate_super_walk(cs[k], child_pnames, child_ptypes, parent_ptypes)
11123+ k = k + 1
11124+ end
11125+ end
11126+
1092911127 def propagate_bare_new_to_subclass_initialize
1093011128 ci = 0
1093111129 while ci < @cls_names.length
@@ -12426,6 +12624,65 @@
1242612624 finalize_ivar_heterogeneity
1242712625 end
1242812626
12627+ # Drop "obj_<bare>" observations from `obs` when (a) <bare> doesn't
12628+ # resolve to any registered class, AND (b) some other observation in
12629+ # the list is "obj_<scope>_<bare>" for the same trailing <bare> and
12630+ # <scope>_<bare> IS registered. Caller passes the raw split list of
12631+ # observed types; we return a filtered list with stale unqualified
12632+ # obj-names removed (their qualified peer carries the same slot type
12633+ # information). Used by finalize_ivar_heterogeneity so the
12634+ # distinct-count poly-widen decision doesn't flip a single-class
12635+ # ivar to poly just because Pass 1 recorded the bare name and a
12636+ # later pass recorded the qualified one.
12637+ def drop_stale_unqualified_obj_obs(obs)
12638+ out = "".split(",")
12639+ # Type-prime locals as strings so spinel-self-compile picks the
12640+ # right C type (sp_str_sub_range / sp_str_concat returns
12641+ # `const char *`; without the empty-string seed scan_locals
12642+ # falls back to `mrb_int`, which then fails the `-Wint-conversion`
12643+ # bootstrap when this method is invoked from finalize_ivar_heterogeneity).
12644+ o = ""
12645+ p = ""
12646+ ob = ""
12647+ pb = ""
12648+ sfx = ""
12649+ tail = ""
12650+ oi = 0
12651+ while oi < obs.length
12652+ o = obs[oi]
12653+ keep = 1
12654+ if is_obj_type(o) == 1
12655+ ob = o[4, o.length - 4]
12656+ if find_class_idx(ob) < 0
12657+ oj = 0
12658+ while oj < obs.length
12659+ if oj != oi
12660+ p = obs[oj]
12661+ if is_obj_type(p) == 1
12662+ pb = p[4, p.length - 4]
12663+ if find_class_idx(pb) >= 0
12664+ sfx = "_" + ob
12665+ if pb.length > sfx.length
12666+ tail = pb[(pb.length - sfx.length), sfx.length]
12667+ if tail == sfx
12668+ keep = 0
12669+ end
12670+ end
12671+ end
12672+ end
12673+ end
12674+ oj = oj + 1
12675+ end
12676+ end
12677+ end
12678+ if keep == 1
12679+ out.push(o)
12680+ end
12681+ oi = oi + 1
12682+ end
12683+ out
12684+ end
12685+
1242912686 def finalize_ivar_heterogeneity
1243012687 ci = 0
1243112688 while ci < @cls_names.length
@@ -12437,6 +12694,17 @@
1243712694 while ivk < names.length
1243812695 if ivk < types.length && ivk < obs.length && types[ivk] != "poly"
1243912696 distinct = obs[ivk].split(",")
12697+ # Collapse stale unqualified obj-name observations against
12698+ # their qualified form before the distinct-count widening
12699+ # decision. A first-pass `@x = Foo.new(...)` recorded inside
12700+ # a class whose sibling `Foo` hadn't been registered yet
12701+ # stamped "obj_Foo" into the observation list; a later pass
12702+ # (with all classes registered) records the proper
12703+ # "obj_<scope>_Foo". They refer to the same class, but the
12704+ # raw distinct-count below would treat them as 2 types and
12705+ # widen the slot to poly. Mirror the update_ivar_type /
12706+ # unify_call_types normalization.
12707+ distinct = drop_stale_unqualified_obj_obs(distinct)
1244012708 if distinct.length >= 2
1244112709 # When every observed type is itself an array AND the
1244212710 # current type is already `poly_array` (set by an earlier
@@ -13188,6 +13456,15 @@
1318813456 # subsequent rounds pick up cls method widening from new call
1318913457 # sites.
1319013458 propagate_bare_new_to_subclass_initialize
13459+ # `super` inside a child's `#initialize` calls the parent's
13460+ # initialize with the child's params. Propagate the child's
13461+ # ptypes (already widened by scan_new_calls' constructor branch
13462+ # for `Child.new(args)` call sites) into the parent's ptypes so
13463+ # the parent's body sees the right param types for `@apu = apu`
13464+ # ivar widening — without this the parent's slot stays at the
13465+ # default `mrb_int` even when every `Child.new` site passes a
13466+ # typed pointer.
13467+ propagate_super_init_to_parent
1319113468 # Update ivar types from constructor params
1319213469 update_ivar_types_from_params
1319313470 # Infer setter param types from ivar types
@@ -15336,11 +15613,6 @@
1533615613 infer_function_body_call_types
1533715614 infer_class_body_call_types
1533815615 infer_ivar_types_from_writers
15339- # After scan_locals has populated @meth_param_empty via the
15340- # per-call-site forward propagation, promote int_array
15341- # params to concrete typed-arrays where bodies push known
15342- # types. The next iteration's scan_locals back-propagates
15343- # those promoted types to caller-side locals.
1534415616 infer_param_array_type_from_body
1534515617 narrow_param_types_from_body_method_calls
1534615618 detect_poly_params