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
"" end # if midx is out of range or the body id is invalid. Centralises # the @cls_meth_bodies[ci].split(";")[midx].to_i parse so detectors # don't have to inline it. def cls_method_body_id(ci, midx) bodies = @cls_meth_bodies[ci].split(";") if midx >= bodies.length return -1 end bid = bodies[midx].to_i if bid < 0 return -1 end bid end # Walk `nid` for YieldNode, accumulating the per-position arg # type into `types`. Stops at nested DefNode boundaries (those # introduce a new method scope with its own yield arity). # Mirrors body_max_yield_arity's traversal shape. def body_yield_arg_types(nid, types) if nid < 0 return end if @nd_type[nid] == "YieldNode" if @nd_arguments[nid] >= 0 args = get_args(@nd_arguments[nid]) k = 0 while k < args.length if k < types.length at = infer_type(args[k]) if types[k] == "" types[k] = at elsif types[k] != at types[k] = unify_call_types(types[k], at, args[k]) end end k = k + 1 end end return end if @nd_type[nid] == "DefNode" return end cs = [] push_child_ids(nid, cs) k = 0 while k < cs.length body_yield_arg_types(cs[k], types) k = k + 1 end end def cls_method_has_yield(ci, midx) ystr = @cls_meth_has_yield[ci].split(";") if midx < ystr.length if ystr[midx] == "1" return 1 end end 0 end # Max number of args used in any `yield` inside the top-level method # at @meth_body_ids[mi]. Floor of 1 — yield-using methods always have # at least one mrb_int slot (the no-arg `yield` form is padded to 0). def method_yield_arity(mi) if mi < 0 || mi >= @meth_body_ids.length return 1 end body_max_yield_arity(@meth_body_ids[mi], 1) end # Same as method_yield_arity, but resolved through the class method # body table @cls_meth_bodies (parallel to @cls_meth_has_yield). # Mirrors body_has_yield's recursion shape. `current` carries the running # max so callers can seed a floor (1, since every yield-using method needs # at least one mrb_int slot in `_block`'s signature). def body_max_yield_arity(nid, current) if nid < 0 return current end if @nd_type[nid] == "YieldNode" n = 0 if @nd_arguments[nid] >= 0 n = get_args(@nd_arguments[nid]).length end if n < 1 n = 1 end if n > current current = n end end if @nd_type[nid] == "DefNode" return current end cs = [] push_child_ids(nid, cs) k = 0 while k < cs.length current = body_max_yield_arity(cs[k], current) k = k + 1 end current end # ---- Return type inference ---- # Narrow pre-pass for `rewrite_instance_eval_calls`: walk top-level # CallNodes shaped `recv.method(args)` where recv resolves to an # obj_<C> via top-level scope, and let scan_new_calls' receiver-method # branch widen the class method's ptypes. Without this, a method-param def cls_method_yield_arity(ci, midx) if ci < 0 || midx < 0 return 1 end bodies = @cls_meth_bodies[ci].split(";") if midx >= bodies.length return 1 end bid = bodies[midx].to_i body_max_yield_arity(bid, 1) end # `recv OP rhs` lowering for an obj-typed receiver. When the # receiver's class (or an ancestor) defines `op` as a user method, # returns the C expression `sp_<owner>_<op>(<recv_c>, <rhs_c>)`. if nt == "CallNode" && @nd_receiver[nid] < 0 && @nd_name[nid] == "new" skip_cache = 1 end # `lambda.call(...)` — the return type comes from # @lambda_var_ret_types which is built at codegen time (issue #400 # multi-arg lambda). Caching at analyze would freeze the type as # "int" before scan_lambda_ret_types runs, and the outer `.to_s` # would emit sp_int_to_s instead of the bool ternary. Limited to # the literal `.call` shape (NOT `[]` — that's str_array indexing # in too many other places to skip safely). if nt == "CallNode" && @nd_name[nid] == "call" crv = @nd_receiver[nid] if crv >= 0 && @nd_type[crv] == "LocalVariableReadNode" skip_cache = 1 end end if skip_cache == 0 @nd_inferred_type[nid] = infer_type(nid) endspinel_codegen.rbmodifiedView file
Loading diff…