Commit 01129720

01129720e6dfMerged

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

Description

analyze(promote): project yield-arg bigint into @meth_blk_param_types

Phase A of the per-method _block signature widening for issue #664's promote-mode tail. analyze now populates @meth_blk_param_types (previously empty for yield-only methods, set only for &proc-typed params) with a per-position projection of yield arg types. Today the projection collapses to "int" / "bigint" only -- "bigint" forces the codegen-side _block C signature to use sp_Bigint *; everything else keeps the default mrb_int (the pointer-reinterpret convention that str / array / obj recvs already use).

In promote mode the cached infer_type for an arith CallNode arg (yield x * 2) often lags at "int" because the block param x was declared via block_param_type_at's "int" fallback for no-recv user-yield-method blocks. Adds project_yield_arg_t which cross-checks syntactic shape (IntegerNode + arith CallNode are always bigint at promote-mode emit) so the projection isn't fooled by the stale cache.

@cls_cmeth_blk_param_types gets the same treatment for class methods that yield. @cls_meth_blk_param_types (instance methods) is not introduced yet -- the target regression test (nested_yield_with_block_param) exercises top-level methods; instance-method cls table is a Phase A extension if a follow-up case requires it.

No behavior change yet -- codegen still uses its own block_params_csig_for_body. Phase B will wire codegen to consult the new @meth_blk_param_types entries.

Default tests 649/0/0, promote 647/0/0, optcarrot checksum 59662 all unchanged.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
01129720e6df
Parent
ae761170a648

spinel_analyze.rbmodifiedView file

@@ -14648,6 +14648,105 @@
1464814648 end
1464914649 end
1465014650
14651+ # Project a yield-arg node's type to the small set that affects
14652+ # the `_block(<sig>, void *)` C signature: "bigint" forces
14653+ # `sp_Bigint *`; everything else fits `mrb_int` (the pointer-
14654+ # reinterpret convention used by str / array / obj args).
14655+ #
14656+ # In promote mode, `walk_and_cache` may have cached an arith
14657+ # CallNode arg as "int" before the int -> bigint sweep landed
14658+ # (block params declared via block_param_type_at's "int"
14659+ # fallback for no-recv user-yield-method blocks). The yield
14660+ # codegen's actual emit lowers `x * 2` (x: bigint) to
14661+ # sp_bigint_mul -> sp_Bigint *, so the sig has to widen too.
14662+ # Cross-check syntactic shape on top of infer_type: IntegerNode
14663+ # and arith CallNode are always bigint at promote-mode emit
14664+ # time, regardless of cache.
14665+ def project_yield_arg_t(arg_id)
14666+ t = infer_type(arg_id)
14667+ return "bigint" if base_type(t) == "bigint"
14668+ if @int_overflow_mode == "promote"
14669+ nt = @nd_type[arg_id]
14670+ if nt == "IntegerNode"
14671+ return "bigint"
14672+ end
14673+ if nt == "CallNode"
14674+ mn = @nd_name[arg_id]
14675+ if mn == "+" || mn == "-" || mn == "*" || mn == "/" || mn == "%" || mn == "**"
14676+ return "bigint"
14677+ end
14678+ end
14679+ end
14680+ "int"
14681+ end
14682+
14683+ # Mirror of body_yield_arg_types but each per-position slot is
14684+ # projected via `project_yield_arg_t` -- captures both
14685+ # cache-correct bigint inferences and the promote-mode syntactic
14686+ # signal that the cache may have missed.
14687+ def project_yield_arg_types(nid, types)
14688+ if nid < 0
14689+ return
14690+ end
14691+ if @nd_type[nid] == "YieldNode"
14692+ if @nd_arguments[nid] >= 0
14693+ args = get_args(@nd_arguments[nid])
14694+ k = 0
14695+ while k < args.length
14696+ if k < types.length
14697+ at = project_yield_arg_t(args[k])
14698+ if types[k] == ""
14699+ types[k] = at
14700+ elsif types[k] != at && at == "bigint"
14701+ # Any bigint slot wins -- once we've seen a bigint at this
14702+ # position, the sig has to accommodate.
14703+ types[k] = "bigint"
14704+ end
14705+ end
14706+ k = k + 1
14707+ end
14708+ end
14709+ return
14710+ end
14711+ if @nd_type[nid] == "DefNode"
14712+ return
14713+ end
14714+ cs = []
14715+ push_child_ids(nid, cs)
14716+ k = 0
14717+ while k < cs.length
14718+ project_yield_arg_types(cs[k], types)
14719+ k = k + 1
14720+ end
14721+ end
14722+
14723+ # Walk the yield-using method's body and return per-position
14724+ # projected yield arg types joined with "|" for storage in
14725+ # @meth_blk_param_types. Empty arity returns "".
14726+ def infer_yield_blk_param_types_for_body(body_id, ptypes_str)
14727+ arity = body_max_yield_arity(body_id, 0)
14728+ return "" if arity == 0 || body_id < 0
14729+ types = "".split(",")
14730+ k = 0
14731+ while k < arity
14732+ types.push("")
14733+ k = k + 1
14734+ end
14735+ project_yield_arg_types(body_id, types)
14736+ proj = "".split(",")
14737+ j = 0
14738+ while j < types.length
14739+ t = types[j]
14740+ if t == ""
14741+ proj.push("int")
14742+ else
14743+ proj.push(t)
14744+ end
14745+ j = j + 1
14746+ end
14747+ proj.join("|")
14748+ end
14749+
1465114750 def infer_proc_blk_param_types
1465214751 @meth_blk_param_types = "".split(",")
1465314752 mi = 0
@@ -14658,6 +14757,12 @@
1465814757 acc = "".split(",")
1465914758 collect_blk_call_arg_types(@meth_body_ids[mi], bpname, acc, @meth_body_ids[mi])
1466014759 @meth_blk_param_types.push(acc.join("|"))
14760+ elsif @meth_has_yield[mi] == 1
14761+ # Yield-using top-level method: project yield arg types so the
14762+ # codegen-side `_block` C signature can pick `sp_Bigint *` over
14763+ # the default `mrb_int` when every yield site passes bigint.
14764+ # Issue #664 (promote-mode tail).
14765+ @meth_blk_param_types.push(infer_yield_blk_param_types_for_body(@meth_body_ids[mi], @meth_param_types[mi]))
1466114766 else
1466214767 @meth_blk_param_types.push("")
1466314768 end
@@ -14682,7 +14787,18 @@
1468214787 collect_blk_call_arg_types(body_id, bpname, acc, body_id)
1468314788 @cls_cmeth_blk_param_types.push(acc.join("|"))
1468414789 else
14685- @cls_cmeth_blk_param_types.push("")
14790+ # Class methods (def self.X) don't have a per-method yield-
14791+ # tracking table -- syntactic yield in a cmeth body is rare in
14792+ # practice. body_max_yield_arity returns 0 for non-yielding
14793+ # bodies so infer_yield_blk_param_types_for_body short-circuits
14794+ # to "". Pre-check arity to skip the (cheap) walk + projection
14795+ # when no yield is present.
14796+ body_id = cm_bodies[cmidx].to_i
14797+ if body_max_yield_arity(body_id, 0) > 0
14798+ @cls_cmeth_blk_param_types.push(infer_yield_blk_param_types_for_body(body_id, cm_ptypes[cmidx]))
14799+ else
14800+ @cls_cmeth_blk_param_types.push("")
14801+ end
1468614802 end
1468714803 else
1468814804 @cls_cmeth_blk_param_types.push("")