Commit fa72f5e3

fa72f5e3d3dfMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · ~2 modified

Description

port: multi-arg lambda + DCE wiring + scope-decl reads for inlined yield

The cherry-picks of commits d80e21d (uncalled method DCE), 0e1be0c (multi-arg lambda), and fb7a0a7 (yield-method block-param infer) each add code my split branch had previously deleted from spinel_codegen.rb. Apply the missing pieces:

  • @cls_meth_live serialization: analyze runs compute_live_instance_methods + collect_used_method_names + cls_meth_mark_live (all moved from codegen.rb because they walk the AST), serializes the result as STR @cls_meth_live, codegen reads it back; cls_meth_is_live stays in both files for emit-time dispatch.

  • yield-method block-param infer: scan_locals (analyze.rb only) gains the obj_ arm that pushes the yielding method's scope and runs body_yield_arg_types so a block param's type matches the yield arg's actual type. body_yield_arg_types, cls_method_has_yield, cls_method_yield_arity, cls_method_body_id, body_max_yield_arity copied from codegen.rb to analyze.rb.

  • Inline yield-method body: the read-modify-write site at compile time that called scan_locals(bid) is rewritten to read the precomputed @nd_scope_names[bid] / @nd_scope_types[bid] (split on '|' to recover the str_array) so codegen no longer needs scan_locals — same wiring as declare_method_locals from earlier.

  • Multi-arg lambda return type tracking: codegen's infer_type CallNode fallback now returns 'bool' for comparison ops (<, >, ==, etc.) so scan_lambda_ret_types can record the right return type for . The fallback also consults @lambda_var_ret_types for so the outer emits the bool ternary instead of sp_int_to_s. To prevent analyze from freezing the wrong type, walk_and_cache skips caching CallNodes with mname == 'call' and a LocalVariableRead receiver — those are deferred to codegen-time inference where scan_lambda_ret_types has already run.

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

Details

Commit
fa72f5e3d3df
Parent
9e17ebea7103

spinel_analyze.rbmodifiedView file

@@ -18308,6 +18308,133 @@
1830818308 ""
1830918309 end
1831018310
18311+ # if midx is out of range or the body id is invalid. Centralises
18312+ # the @cls_meth_bodies[ci].split(";")[midx].to_i parse so detectors
18313+ # don't have to inline it.
18314+ def cls_method_body_id(ci, midx)
18315+ bodies = @cls_meth_bodies[ci].split(";")
18316+ if midx >= bodies.length
18317+ return -1
18318+ end
18319+ bid = bodies[midx].to_i
18320+ if bid < 0
18321+ return -1
18322+ end
18323+ bid
18324+ end
18325+
18326+ # Walk `nid` for YieldNode, accumulating the per-position arg
18327+ # type into `types`. Stops at nested DefNode boundaries (those
18328+ # introduce a new method scope with its own yield arity).
18329+ # Mirrors body_max_yield_arity's traversal shape.
18330+ def body_yield_arg_types(nid, types)
18331+ if nid < 0
18332+ return
18333+ end
18334+ if @nd_type[nid] == "YieldNode"
18335+ if @nd_arguments[nid] >= 0
18336+ args = get_args(@nd_arguments[nid])
18337+ k = 0
18338+ while k < args.length
18339+ if k < types.length
18340+ at = infer_type(args[k])
18341+ if types[k] == ""
18342+ types[k] = at
18343+ elsif types[k] != at
18344+ types[k] = unify_call_types(types[k], at, args[k])
18345+ end
18346+ end
18347+ k = k + 1
18348+ end
18349+ end
18350+ return
18351+ end
18352+ if @nd_type[nid] == "DefNode"
18353+ return
18354+ end
18355+ cs = []
18356+ push_child_ids(nid, cs)
18357+ k = 0
18358+ while k < cs.length
18359+ body_yield_arg_types(cs[k], types)
18360+ k = k + 1
18361+ end
18362+ end
18363+
18364+ def cls_method_has_yield(ci, midx)
18365+ ystr = @cls_meth_has_yield[ci].split(";")
18366+ if midx < ystr.length
18367+ if ystr[midx] == "1"
18368+ return 1
18369+ end
18370+ end
18371+ 0
18372+ end
18373+
18374+ # Max number of args used in any `yield` inside the top-level method
18375+ # at @meth_body_ids[mi]. Floor of 1 — yield-using methods always have
18376+ # at least one mrb_int slot (the no-arg `yield` form is padded to 0).
18377+ def method_yield_arity(mi)
18378+ if mi < 0 || mi >= @meth_body_ids.length
18379+ return 1
18380+ end
18381+ body_max_yield_arity(@meth_body_ids[mi], 1)
18382+ end
18383+
18384+ # Same as method_yield_arity, but resolved through the class method
18385+ # body table @cls_meth_bodies (parallel to @cls_meth_has_yield).
18386+ # Mirrors body_has_yield's recursion shape. `current` carries the running
18387+ # max so callers can seed a floor (1, since every yield-using method needs
18388+ # at least one mrb_int slot in `_block`'s signature).
18389+ def body_max_yield_arity(nid, current)
18390+ if nid < 0
18391+ return current
18392+ end
18393+ if @nd_type[nid] == "YieldNode"
18394+ n = 0
18395+ if @nd_arguments[nid] >= 0
18396+ n = get_args(@nd_arguments[nid]).length
18397+ end
18398+ if n < 1
18399+ n = 1
18400+ end
18401+ if n > current
18402+ current = n
18403+ end
18404+ end
18405+ if @nd_type[nid] == "DefNode"
18406+ return current
18407+ end
18408+ cs = []
18409+ push_child_ids(nid, cs)
18410+ k = 0
18411+ while k < cs.length
18412+ current = body_max_yield_arity(cs[k], current)
18413+ k = k + 1
18414+ end
18415+ current
18416+ end
18417+
18418+ # ---- Return type inference ----
18419+
18420+ # Narrow pre-pass for `rewrite_instance_eval_calls`: walk top-level
18421+ # CallNodes shaped `recv.method(args)` where recv resolves to an
18422+ # obj_<C> via top-level scope, and let scan_new_calls' receiver-method
18423+ # branch widen the class method's ptypes. Without this, a method-param
18424+
18425+ def cls_method_yield_arity(ci, midx)
18426+ if ci < 0 || midx < 0
18427+ return 1
18428+ end
18429+ bodies = @cls_meth_bodies[ci].split(";")
18430+ if midx >= bodies.length
18431+ return 1
18432+ end
18433+ bid = bodies[midx].to_i
18434+ body_max_yield_arity(bid, 1)
18435+ end
18436+
18437+
1831118438 # `recv OP rhs` lowering for an obj-typed receiver. When the
1831218439 # receiver's class (or an ancestor) defines `op` as a user method,
1831318440 # returns the C expression `sp_<owner>_<op>(<recv_c>, <rhs_c>)`.
@@ -19170,6 +19297,19 @@
1917019297 if nt == "CallNode" && @nd_receiver[nid] < 0 && @nd_name[nid] == "new"
1917119298 skip_cache = 1
1917219299 end
19300+ # `lambda.call(...)` — the return type comes from
19301+ # @lambda_var_ret_types which is built at codegen time (issue #400
19302+ # multi-arg lambda). Caching at analyze would freeze the type as
19303+ # "int" before scan_lambda_ret_types runs, and the outer `.to_s`
19304+ # would emit sp_int_to_s instead of the bool ternary. Limited to
19305+ # the literal `.call` shape (NOT `[]` — that's str_array indexing
19306+ # in too many other places to skip safely).
19307+ if nt == "CallNode" && @nd_name[nid] == "call"
19308+ crv = @nd_receiver[nid]
19309+ if crv >= 0 && @nd_type[crv] == "LocalVariableReadNode"
19310+ skip_cache = 1
19311+ end
19312+ end
1917319313 if skip_cache == 0
1917419314 @nd_inferred_type[nid] = infer_type(nid)
1917519315 end