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_callaccepts{ || },{ |x| },{ |x,y,z| }, and NumberedParametersNode (_1). Bails on optional / rest / keyword / post-required / kwrest /&blk. - analyze:
@ieval_self_param_names/@ieval_extra_param_namescarry per-lift names. The self-bound name binds to self; extras bind to nil per arity-mismatch tolerance. - analyze:
precompute_all_scope_declsdeclares the self-bound param asobj_<C>and extras asnil, then runsrefine_method_body_localswith 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_typesexcludes them from value-type and SRA promotion so the liftedsp_ieval_<N>'s self-pointer signature can mutate ivars. The flag is serialized to IR. - codegen:
emit_ieval_funcemitslv_<name> = self;afterdeclare_method_localsfor 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-param595+
# forms). emit_ieval_func seeds it via `lv_<name> = self;`.596+
@ieval_self_param_names = "".split(",")597+
# Extra required block-param names (";"-joined) beyond the598+
# 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 so602+
# 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 before596606
# 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 the69636976
# 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 < 071867199
return71877200
end7188-
# Skip blocks with parameters: lifted function takes only `self`.7189-
if @nd_parameters[blk] >= 07190-
return7201+
# Lifted sp_ieval_<N> takes only `self`. Accept `{ }`, `{ || }`,7202+
# `{ |x| }`, `{ |x,y,z| }`, and `{ _1.foo }`. Bail on anything7203+
# 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 >= 07209+
if @nd_type[bp] == "NumberedParametersNode"7210+
if @nd_value[bp] != 17211+
return7212+
end7213+
self_param_name = "_1"7214+
else7215+
inner = @nd_parameters[bp]7216+
if inner >= 07217+
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 >= 07225+
return7226+
end7227+
if reqs.length >= 17228+
self_param_name = @nd_name[reqs[0]]7229+
kpe = 17230+
while kpe < reqs.length7231+
extra_param_names.push(@nd_name[reqs[kpe]])7232+
kpe = kpe + 17233+
end7234+
end7235+
end7236+
end71917237
end71927238
ci = -171937239
if @nd_type[recv] == "LocalVariableReadNode"@@ -7237,6 +7283,11 @@
72377283
@ieval_counter = @ieval_counter + 172387284
@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 propagate7289+
# through the lift's self-pointer signature.7290+
flag_internal_ieval_lift(ci)72407291
# Seed return type as "void"; infer_ieval_body_return_types runs72417292
# after the main return-type fixpoint converges and overwrites with72427293
# the body's last-expression ctype when non-void.@@ -7311,6 +7362,32 @@
73117362
073127363
end73137364
7365+
# detect_value_types reads the set via cls_has_internal_ieval_lift7366+
# and excludes flagged classes from value-type / SRA promotion.7367+
def flag_internal_ieval_lift(ci)7368+
s = ci.to_s7369+
k = 07370+
while k < @cls_with_internal_ieval_lift.length7371+
if @cls_with_internal_ieval_lift[k] == s7372+
return7373+
end7374+
k = k + 17375+
end7376+
@cls_with_internal_ieval_lift.push(s)7377+
end7378+
7379+
def cls_has_internal_ieval_lift(ci)7380+
s = ci.to_s7381+
k = 07382+
while k < @cls_with_internal_ieval_lift.length7383+
if @cls_with_internal_ieval_lift[k] == s7384+
return 17385+
end7386+
k = k + 17387+
end7388+
07389+
end7390+
73147391
def is_ieval_call_name(mname)73157392
if mname.length <= 1173167393
return 0@@ -21778,7 +21855,14 @@
2177821855
all_val = 02177921856
end2178021857
end21858+
# ieval-receiving classes need a by-pointer self so block-body21859+
# ivar writes propagate; value-typed would drop them.2178121860
if all_val == 121861+
if cls_has_internal_ieval_lift(i) == 121862+
all_val = 021863+
end21864+
end21865+
if all_val == 12178221866
@cls_is_value_type[i] = 12178321867
end2178421868
end@@ -21848,7 +21932,14 @@
2184821932
eligible = 02184921933
end2185021934
end21935+
# Same reason as the value-type arm: ieval-receiving classes21936+
# need a stable heap pointer.2185121937
if eligible == 121938+
if cls_has_internal_ieval_lift(i) == 121939+
eligible = 021940+
end21941+
end21942+
if eligible == 12185221943
@cls_is_sra[i] = 12185321944
end2185421945
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_idx2696427058
@current_class_idx = @ieval_class_idxs[iv]2696527059
push_scope27060+
# Declare the self-bound param and any extras up front with27061+
# their typed slots, then refine body locals with those names27062+
# excluded from the rescan so reassigns hit the param slot27063+
# rather than inventing a fresh local of the RHS type.27064+
ie_pnames = "".split(",")27065+
spn_iv = ""27066+
if iv < @ieval_self_param_names.length27067+
spn_iv = @ieval_self_param_names[iv]27068+
end27069+
if spn_iv != ""27070+
declare_var(spn_iv, "obj_" + @cls_names[@ieval_class_idxs[iv]])27071+
ie_pnames.push(spn_iv)27072+
end27073+
extra_iv_csv = ""27074+
if iv < @ieval_extra_param_names.length27075+
extra_iv_csv = @ieval_extra_param_names[iv]27076+
end27077+
extra_iv_names = "".split(",")27078+
if extra_iv_csv != ""27079+
extra_iv_names = extra_iv_csv.split(";")27080+
end27081+
kex_iv = 027082+
while kex_iv < extra_iv_names.length27083+
declare_var(extra_iv_names[kex_iv], "nil")27084+
ie_pnames.push(extra_iv_names[kex_iv])27085+
kex_iv = kex_iv + 127086+
end27087+
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+
end27097+
kex2_iv = 027098+
while kex2_iv < extra_iv_names.length27099+
final_names.push(extra_iv_names[kex2_iv])27100+
final_types.push("nil")27101+
kex2_iv = kex2_iv + 127102+
end27103+
k_iv = 027104+
while k_iv < ml_iv.length27105+
final_names.push(ml_iv[k_iv])27106+
final_types.push(mt_iv[k_iv])27107+
k_iv = k_iv + 127108+
end27109+
@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_scope2696827113
@current_class_idx = saved_ci_iv@@ -26976,11 +27121,24 @@
2697627121
# into captures (in outer scope) vs true locals at emit time. The2697727122
# scan output is purely syntactic, so we cache it here keyed by2697827123
# the body bid. Params are the block's syntactic required names.27124+
# Skip ieval-lifted blocks below: the ieval arm already set27125+
# 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 = 027129+
while ie_seed < @ieval_body_ids.length27130+
ieval_body_set[@ieval_body_ids[ie_seed].to_s] = 127131+
ie_seed = ie_seed + 127132+
end2697927133
bn = 02698027134
while bn < @nd_count2698127135
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] != nil27139+
bn = bn + 127140+
next27141+
end2698427142
if body_bn >= 02698527143
# Collect syntactic block param names2698627144
bp_list = "".split(",")spinel_codegen.rbmodifiedView file
Loading diff…
test/instance_eval_block_params.rbaddedView file
Loading diff…
test/instance_eval_block_params.rb.expectedaddedView file
Loading diff…