Commit d2a0ca58

d2a0ca58a369Merged

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

Description

codegen: derive _block C sig from @meth_blk_param_types

Phase B of the per-method _block signature widening. Codegen now consults the analyze-projected @meth_blk_param_types / @cls_cmeth_blk_param_types in block_params_csig_for_body before falling back to its own body-walk. When the analyze projection declares a slot as "bigint", the C signature emits sp_Bigint * instead of the default mrb_int; non-"int" non-"bigint" projections (string, str_str_hash, obj_, ...) flow through c_type so the existing typed-arg behaviour is preserved.

Adds blk_param_types_for_body(bid): walks @meth_body_ids and the per-class cmeth body tables to map a body id to the right @*_blk_param_types entry.

Also fixes analyze's infer_yield_blk_param_types_for_body to push the method's params into scope before walking, so yield <param> shapes (maybe_yield(x); yield x) resolve through find_var_type to the post-promote bigint slot instead of falling back to "int".

Default tests 649/0/0 (unchanged from Phase A). Promote tests 646/0/1 -- block2 / yield_typed_hash_block_local now pass; only multi_arg_yield still fails. The remaining error is the yield emit not boxing literal IntegerNode args (_block(1LL, ...) against a _block(sp_Bigint *, ...) sig) -- Phase C addresses that. optcarrot checksum 59662 unchanged.

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

Details

Commit
d2a0ca58a369
Parent
01129720e6df

spinel_analyze.rbmodifiedView file

@@ -14677,7 +14677,15 @@
1467714677 end
1467814678 end
1467914679 end
14680- "int"
14680+ # Preserve the fully-inferred type (string / str_str_hash /
14681+ # obj_<C> / etc.) so the codegen-side block_params_csig_for_body
14682+ # picks the matching c_type instead of collapsing to mrb_int.
14683+ # "int" / "nil" / "" all map to mrb_int at codegen and don't
14684+ # need to be distinguished here.
14685+ if t == "" || t == "nil"
14686+ return "int"
14687+ end
14688+ t
1468114689 end
1468214690
1468314691 # Mirror of body_yield_arg_types but each per-position slot is
@@ -14697,10 +14705,16 @@
1469714705 at = project_yield_arg_t(args[k])
1469814706 if types[k] == ""
1469914707 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"
14708+ elsif types[k] != at
14709+ if at == "bigint" || types[k] == "bigint"
14710+ # Any bigint slot wins so the sig is wide enough to carry it.
14711+ types[k] = "bigint"
14712+ else
14713+ # Mixed non-bigint types: fall through to the same unifier
14714+ # body_yield_arg_types uses (collapse to "poly" via
14715+ # unify_call_types' rules, or pick the widest pointer).
14716+ types[k] = unify_call_types(types[k], at, args[k])
14717+ end
1470414718 end
1470514719 end
1470614720 k = k + 1
@@ -14732,7 +14746,66 @@
1473214746 types.push("")
1473314747 k = k + 1
1473414748 end
14749+ # Set up scope so project_yield_arg_t's `infer_type` (the
14750+ # LocalVariableReadNode arm in particular) lands on the right
14751+ # types. body_max_yield_arity guarantees body_id >= 0 here, and
14752+ # @nd_scope_names/types are precomputed by
14753+ # precompute_all_scope_decls. Params come from the method's
14754+ # ptypes_str (passed by caller) since they live outside the body's
14755+ # scope_names entry.
14756+ push_scope
14757+ # Locate the method's params (top-level or class-instance) so the
14758+ # `yield <param>` shape resolves through find_var_type to the
14759+ # (post-promote) param type instead of falling back to "int".
14760+ pnames_arr = "".split(",")
14761+ ptypes_arr = ptypes_str.split(",")
14762+ mi_ll = 0
14763+ while mi_ll < @meth_body_ids.length
14764+ if @meth_body_ids[mi_ll] == body_id
14765+ pnames_arr = @meth_param_names[mi_ll].split(",")
14766+ mi_ll = @meth_body_ids.length
14767+ else
14768+ mi_ll = mi_ll + 1
14769+ end
14770+ end
14771+ if pnames_arr.length == 0
14772+ ci_ll = 0
14773+ while ci_ll < @cls_names.length
14774+ bodies_ll = ci_ll < @cls_meth_bodies.length ? @cls_meth_bodies[ci_ll].split(";") : "".split(",")
14775+ midx_ll = 0
14776+ while midx_ll < bodies_ll.length
14777+ if bodies_ll[midx_ll].to_i == body_id
14778+ pnames_arr = cls_meth_pnames_get(ci_ll, midx_ll)
14779+ ci_ll = @cls_names.length
14780+ midx_ll = bodies_ll.length
14781+ else
14782+ midx_ll = midx_ll + 1
14783+ end
14784+ end
14785+ ci_ll = ci_ll + 1
14786+ end
14787+ end
14788+ j_p = 0
14789+ while j_p < pnames_arr.length
14790+ if pnames_arr[j_p] != "" && j_p < ptypes_arr.length
14791+ declare_var(pnames_arr[j_p], ptypes_arr[j_p])
14792+ end
14793+ j_p = j_p + 1
14794+ end
14795+ lnames = "".split(",")
14796+ ltypes = "".split(",")
14797+ sn_b = @nd_scope_names[body_id]
14798+ if sn_b != ""
14799+ lnames = sn_b.split("|")
14800+ ltypes = @nd_scope_types[body_id].split("|")
14801+ end
14802+ j = 0
14803+ while j < lnames.length
14804+ declare_var(lnames[j], ltypes[j])
14805+ j = j + 1
14806+ end
1473514807 project_yield_arg_types(body_id, types)
14808+ pop_scope
1473614809 proj = "".split(",")
1473714810 j = 0
1473814811 while j < types.length