Commit f12eddfc

f12eddfc5230Merged

Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

feat(analyze,codegen): instance_eval supports 0/1/N block params, self-param writes, numbered params

CRuby's instance_eval passes self as the implicit first block arg, with extras defaulting to nil. Cover the three explicit- param shapes plus reassignment of the self-bound name and the numbered-parameter form (_1). Single-ivar receivers stay heap- allocated so ivar writes in the lifted block propagate back.

  • analyze: ieval_rewrite_call accepts { || }, { |x| }, { |x,y,z| }, and NumberedParametersNode (_1). Bails on optional / rest / keyword / post-required / kwrest / &blk.
  • analyze: @ieval_self_param_names / @ieval_extra_param_names carry per-lift names. The self-bound name binds to self; extras bind to nil per arity-mismatch tolerance.
  • analyze: precompute_all_scope_decls declares the self-bound param as obj_<C> and extras as nil, then runs refine_method_body_locals with those names excluded so body writes hit the param slot rather than inventing fresh locals. The BlockNode arm consults a pre-built Hash to skip ieval-lifted bodies in O(1).
  • analyze: flag_internal_ieval_lift(ci) records receiver classes; detect_value_types excludes them from value-type and SRA promotion so the lifted sp_ieval_<N>'s self-pointer signature can mutate ivars. The flag is serialized to IR.
  • codegen: emit_ieval_func emits lv_<name> = self; after declare_method_locals for the self-bound param.

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

Details

Commit
f12eddfc5230
Parent
87a2ce25d6b7

spinel_analyze.rbmodifiedView file

@@ -591,6 +591,16 @@
591591 @ieval_class_idxs = []
592592 @ieval_body_ids = []
593593 @ieval_return_types = "".split(",")
594+ # Self-bound block-param name per lift (empty for the no-param
595+ # forms). emit_ieval_func seeds it via `lv_<name> = self;`.
596+ @ieval_self_param_names = "".split(",")
597+ # Extra required block-param names (";"-joined) beyond the
598+ # self-bound first. CRuby binds these to nil (arity tolerance);
599+ # precompute_all_scope_decls declares them as nil-typed locals.
600+ @ieval_extra_param_names = "".split(",")
601+ # Receiver classes flagged out of value-type / SRA promotion so
602+ # the lifted sp_ieval_<N>'s self-pointer signature can mutate.
603+ @cls_with_internal_ieval_lift = "".split(",")
594604
595605 # RBS-derived seed lines. Populated by load_rbs_seeds before
596606 # analyze_phase; consumed by apply_rbs_seeds after collect_all
@@ -6959,6 +6969,9 @@
69596969 @ieval_class_idxs = []
69606970 @ieval_body_ids = []
69616971 @ieval_return_types = "".split(",")
6972+ @ieval_self_param_names = "".split(",")
6973+ @ieval_extra_param_names = "".split(",")
6974+ @cls_with_internal_ieval_lift = "".split(",")
69626975 # Widen class-method ptypes through obj-typed receivers before the
69636976 # walk so a method-param receiver (e.g. `def configure(app);
69646977 # app.instance_eval { } end` invoked as `cfg.configure(routes)`)
@@ -7185,9 +7198,42 @@
71857198 if blk < 0
71867199 return
71877200 end
7188- # Skip blocks with parameters: lifted function takes only `self`.
7189- if @nd_parameters[blk] >= 0
7190- return
7201+ # Lifted sp_ieval_<N> takes only `self`. Accept `{ }`, `{ || }`,
7202+ # `{ |x| }`, `{ |x,y,z| }`, and `{ _1.foo }`. Bail on anything
7203+ # needing extra args at the call boundary (opts/posts/rest/kw/
7204+ # kwrest/&blk) or on `_2`+ numbered params.
7205+ self_param_name = ""
7206+ extra_param_names = "".split(",")
7207+ bp = @nd_parameters[blk]
7208+ if bp >= 0
7209+ if @nd_type[bp] == "NumberedParametersNode"
7210+ if @nd_value[bp] != 1
7211+ return
7212+ end
7213+ self_param_name = "_1"
7214+ else
7215+ inner = @nd_parameters[bp]
7216+ if inner >= 0
7217+ reqs = parse_id_list(@nd_requireds[inner])
7218+ opts = parse_id_list(@nd_optionals[inner])
7219+ kws = parse_id_list(@nd_keywords[inner])
7220+ posts = parse_id_list(@nd_posts[inner])
7221+ rest = @nd_rest[inner]
7222+ kwrest = @nd_keyword_rest[inner]
7223+ blkarg = @nd_block[inner]
7224+ if opts.length > 0 || kws.length > 0 || posts.length > 0 || rest >= 0 || kwrest >= 0 || blkarg >= 0
7225+ return
7226+ end
7227+ if reqs.length >= 1
7228+ self_param_name = @nd_name[reqs[0]]
7229+ kpe = 1
7230+ while kpe < reqs.length
7231+ extra_param_names.push(@nd_name[reqs[kpe]])
7232+ kpe = kpe + 1
7233+ end
7234+ end
7235+ end
7236+ end
71917237 end
71927238 ci = -1
71937239 if @nd_type[recv] == "LocalVariableReadNode"
@@ -7237,6 +7283,11 @@
72377283 @ieval_counter = @ieval_counter + 1
72387284 @ieval_class_idxs.push(ci)
72397285 @ieval_body_ids.push(body_id)
7286+ @ieval_self_param_names.push(self_param_name)
7287+ @ieval_extra_param_names.push(extra_param_names.join(";"))
7288+ # Keep ci heap-allocated so block-body ivar writes propagate
7289+ # through the lift's self-pointer signature.
7290+ flag_internal_ieval_lift(ci)
72407291 # Seed return type as "void"; infer_ieval_body_return_types runs
72417292 # after the main return-type fixpoint converges and overwrites with
72427293 # the body's last-expression ctype when non-void.
@@ -7311,6 +7362,32 @@
73117362 0
73127363 end
73137364
7365+ # detect_value_types reads the set via cls_has_internal_ieval_lift
7366+ # and excludes flagged classes from value-type / SRA promotion.
7367+ def flag_internal_ieval_lift(ci)
7368+ s = ci.to_s
7369+ k = 0
7370+ while k < @cls_with_internal_ieval_lift.length
7371+ if @cls_with_internal_ieval_lift[k] == s
7372+ return
7373+ end
7374+ k = k + 1
7375+ end
7376+ @cls_with_internal_ieval_lift.push(s)
7377+ end
7378+
7379+ def cls_has_internal_ieval_lift(ci)
7380+ s = ci.to_s
7381+ k = 0
7382+ while k < @cls_with_internal_ieval_lift.length
7383+ if @cls_with_internal_ieval_lift[k] == s
7384+ return 1
7385+ end
7386+ k = k + 1
7387+ end
7388+ 0
7389+ end
7390+
73147391 def is_ieval_call_name(mname)
73157392 if mname.length <= 11
73167393 return 0
@@ -21778,7 +21855,14 @@
2177821855 all_val = 0
2177921856 end
2178021857 end
21858+ # ieval-receiving classes need a by-pointer self so block-body
21859+ # ivar writes propagate; value-typed would drop them.
2178121860 if all_val == 1
21861+ if cls_has_internal_ieval_lift(i) == 1
21862+ all_val = 0
21863+ end
21864+ end
21865+ if all_val == 1
2178221866 @cls_is_value_type[i] = 1
2178321867 end
2178421868 end
@@ -21848,7 +21932,14 @@
2184821932 eligible = 0
2184921933 end
2185021934 end
21935+ # Same reason as the value-type arm: ieval-receiving classes
21936+ # need a stable heap pointer.
2185121937 if eligible == 1
21938+ if cls_has_internal_ieval_lift(i) == 1
21939+ eligible = 0
21940+ end
21941+ end
21942+ if eligible == 1
2185221943 @cls_is_sra[i] = 1
2185321944 end
2185421945 i = i + 1
@@ -25388,6 +25479,9 @@
2538825479 buf = ir_emit_ia(buf, "@ieval_class_idxs", @ieval_class_idxs)
2538925480 buf = ir_emit_ia(buf, "@ieval_body_ids", @ieval_body_ids)
2539025481 buf = ir_emit_sa(buf, "@ieval_return_types", @ieval_return_types)
25482+ buf = ir_emit_sa(buf, "@ieval_self_param_names", @ieval_self_param_names)
25483+ buf = ir_emit_sa(buf, "@ieval_extra_param_names", @ieval_extra_param_names)
25484+ buf = ir_emit_sa(buf, "@cls_with_internal_ieval_lift", @cls_with_internal_ieval_lift)
2539125485 buf = ir_emit_ia(buf, "@pre_execution_blocks", @pre_execution_blocks)
2539225486 buf = ir_emit_ia(buf, "@post_execution_blocks", @post_execution_blocks)
2539325487 buf = ir_emit_sa(buf, "@toplevel_ivar_names", @toplevel_ivar_names)
@@ -26963,6 +27057,57 @@
2696327057 saved_ci_iv = @current_class_idx
2696427058 @current_class_idx = @ieval_class_idxs[iv]
2696527059 push_scope
27060+ # Declare the self-bound param and any extras up front with
27061+ # their typed slots, then refine body locals with those names
27062+ # excluded from the rescan so reassigns hit the param slot
27063+ # rather than inventing a fresh local of the RHS type.
27064+ ie_pnames = "".split(",")
27065+ spn_iv = ""
27066+ if iv < @ieval_self_param_names.length
27067+ spn_iv = @ieval_self_param_names[iv]
27068+ end
27069+ if spn_iv != ""
27070+ declare_var(spn_iv, "obj_" + @cls_names[@ieval_class_idxs[iv]])
27071+ ie_pnames.push(spn_iv)
27072+ end
27073+ extra_iv_csv = ""
27074+ if iv < @ieval_extra_param_names.length
27075+ extra_iv_csv = @ieval_extra_param_names[iv]
27076+ end
27077+ extra_iv_names = "".split(",")
27078+ if extra_iv_csv != ""
27079+ extra_iv_names = extra_iv_csv.split(";")
27080+ end
27081+ kex_iv = 0
27082+ while kex_iv < extra_iv_names.length
27083+ declare_var(extra_iv_names[kex_iv], "nil")
27084+ ie_pnames.push(extra_iv_names[kex_iv])
27085+ kex_iv = kex_iv + 1
27086+ end
27087+ ml_iv = "".split(",")
27088+ mt_iv = "".split(",")
27089+ refine_method_body_locals(bid_iv, ml_iv, mt_iv, ie_pnames)
27090+ # Order: self-bound at slot 0, extras, then body locals.
27091+ final_names = "".split(",")
27092+ final_types = "".split(",")
27093+ if spn_iv != ""
27094+ final_names.push(spn_iv)
27095+ final_types.push("obj_" + @cls_names[@ieval_class_idxs[iv]])
27096+ end
27097+ kex2_iv = 0
27098+ while kex2_iv < extra_iv_names.length
27099+ final_names.push(extra_iv_names[kex2_iv])
27100+ final_types.push("nil")
27101+ kex2_iv = kex2_iv + 1
27102+ end
27103+ k_iv = 0
27104+ while k_iv < ml_iv.length
27105+ final_names.push(ml_iv[k_iv])
27106+ final_types.push(mt_iv[k_iv])
27107+ k_iv = k_iv + 1
27108+ end
27109+ @nd_scope_names[bid_iv] = final_names.join("|")
27110+ @nd_scope_types[bid_iv] = final_types.join("|")
2696627111 walk_and_cache(bid_iv)
2696727112 pop_scope
2696827113 @current_class_idx = saved_ci_iv
@@ -26976,11 +27121,24 @@
2697627121 # into captures (in outer scope) vs true locals at emit time. The
2697727122 # scan output is purely syntactic, so we cache it here keyed by
2697827123 # the body bid. Params are the block's syntactic required names.
27124+ # Skip ieval-lifted blocks below: the ieval arm already set
27125+ # their typed scope. Spinel's Hash type-infers only StrIntHash,
27126+ # so the lookup keys are stringified body ids.
27127+ ieval_body_set = {}
27128+ ie_seed = 0
27129+ while ie_seed < @ieval_body_ids.length
27130+ ieval_body_set[@ieval_body_ids[ie_seed].to_s] = 1
27131+ ie_seed = ie_seed + 1
27132+ end
2697927133 bn = 0
2698027134 while bn < @nd_count
2698127135 tk = @nd_type[bn]
2698227136 if tk == "BlockNode" || tk == "LambdaNode" || tk == "ProcNode"
2698327137 body_bn = @nd_body[bn]
27138+ if ieval_body_set[body_bn.to_s] != nil
27139+ bn = bn + 1
27140+ next
27141+ end
2698427142 if body_bn >= 0
2698527143 # Collect syntactic block param names
2698627144 bp_list = "".split(",")