Commit 6eecc46a
6eecc46a0840Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #608 from ryanseys/rs-ieval-block-params
feat(analyze,codegen): instance_eval supports 0/1/N block params, self-param writes, numbered params
Details
- Commit
- 6eecc46a0840
- Parent
- 25d4bb471a10
- Merge parent
- f12eddfc5230
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@@ -7001,6 +7011,9 @@
70017011
@ieval_class_idxs = []70027012
@ieval_body_ids = []70037013
@ieval_return_types = "".split(",")7014+
@ieval_self_param_names = "".split(",")7015+
@ieval_extra_param_names = "".split(",")7016+
@cls_with_internal_ieval_lift = "".split(",")70047017
# Widen class-method ptypes through obj-typed receivers before the70057018
# walk so a method-param receiver (e.g. `def configure(app);70067019
# app.instance_eval { } end` invoked as `cfg.configure(routes)`)@@ -7227,9 +7240,42 @@
72277240
if blk < 072287241
return72297242
end7230-
# Skip blocks with parameters: lifted function takes only `self`.7231-
if @nd_parameters[blk] >= 07232-
return7243+
# Lifted sp_ieval_<N> takes only `self`. Accept `{ }`, `{ || }`,7244+
# `{ |x| }`, `{ |x,y,z| }`, and `{ _1.foo }`. Bail on anything7245+
# needing extra args at the call boundary (opts/posts/rest/kw/7246+
# kwrest/&blk) or on `_2`+ numbered params.7247+
self_param_name = ""7248+
extra_param_names = "".split(",")7249+
bp = @nd_parameters[blk]7250+
if bp >= 07251+
if @nd_type[bp] == "NumberedParametersNode"7252+
if @nd_value[bp] != 17253+
return7254+
end7255+
self_param_name = "_1"7256+
else7257+
inner = @nd_parameters[bp]7258+
if inner >= 07259+
reqs = parse_id_list(@nd_requireds[inner])7260+
opts = parse_id_list(@nd_optionals[inner])7261+
kws = parse_id_list(@nd_keywords[inner])7262+
posts = parse_id_list(@nd_posts[inner])7263+
rest = @nd_rest[inner]7264+
kwrest = @nd_keyword_rest[inner]7265+
blkarg = @nd_block[inner]7266+
if opts.length > 0 || kws.length > 0 || posts.length > 0 || rest >= 0 || kwrest >= 0 || blkarg >= 07267+
return7268+
end7269+
if reqs.length >= 17270+
self_param_name = @nd_name[reqs[0]]7271+
kpe = 17272+
while kpe < reqs.length7273+
extra_param_names.push(@nd_name[reqs[kpe]])7274+
kpe = kpe + 17275+
end7276+
end7277+
end7278+
end72337279
end72347280
ci = -172357281
if @nd_type[recv] == "LocalVariableReadNode"@@ -7279,6 +7325,11 @@
72797325
@ieval_counter = @ieval_counter + 172807326
@ieval_class_idxs.push(ci)72817327
@ieval_body_ids.push(body_id)7328+
@ieval_self_param_names.push(self_param_name)7329+
@ieval_extra_param_names.push(extra_param_names.join(";"))7330+
# Keep ci heap-allocated so block-body ivar writes propagate7331+
# through the lift's self-pointer signature.7332+
flag_internal_ieval_lift(ci)72827333
# Seed return type as "void"; infer_ieval_body_return_types runs72837334
# after the main return-type fixpoint converges and overwrites with72847335
# the body's last-expression ctype when non-void.@@ -7353,6 +7404,32 @@
73537404
073547405
end73557406
7407+
# detect_value_types reads the set via cls_has_internal_ieval_lift7408+
# and excludes flagged classes from value-type / SRA promotion.7409+
def flag_internal_ieval_lift(ci)7410+
s = ci.to_s7411+
k = 07412+
while k < @cls_with_internal_ieval_lift.length7413+
if @cls_with_internal_ieval_lift[k] == s7414+
return7415+
end7416+
k = k + 17417+
end7418+
@cls_with_internal_ieval_lift.push(s)7419+
end7420+
7421+
def cls_has_internal_ieval_lift(ci)7422+
s = ci.to_s7423+
k = 07424+
while k < @cls_with_internal_ieval_lift.length7425+
if @cls_with_internal_ieval_lift[k] == s7426+
return 17427+
end7428+
k = k + 17429+
end7430+
07431+
end7432+
73567433
def is_ieval_call_name(mname)73577434
if mname.length <= 1173587435
return 0@@ -21820,7 +21897,14 @@
2182021897
all_val = 02182121898
end2182221899
end21900+
# ieval-receiving classes need a by-pointer self so block-body21901+
# ivar writes propagate; value-typed would drop them.2182321902
if all_val == 121903+
if cls_has_internal_ieval_lift(i) == 121904+
all_val = 021905+
end21906+
end21907+
if all_val == 12182421908
@cls_is_value_type[i] = 12182521909
end2182621910
end@@ -21890,7 +21974,14 @@
2189021974
eligible = 02189121975
end2189221976
end21977+
# Same reason as the value-type arm: ieval-receiving classes21978+
# need a stable heap pointer.2189321979
if eligible == 121980+
if cls_has_internal_ieval_lift(i) == 121981+
eligible = 021982+
end21983+
end21984+
if eligible == 12189421985
@cls_is_sra[i] = 12189521986
end2189621987
i = i + 1@@ -25430,6 +25521,9 @@
2543025521
buf = ir_emit_ia(buf, "@ieval_class_idxs", @ieval_class_idxs)2543125522
buf = ir_emit_ia(buf, "@ieval_body_ids", @ieval_body_ids)2543225523
buf = ir_emit_sa(buf, "@ieval_return_types", @ieval_return_types)25524+
buf = ir_emit_sa(buf, "@ieval_self_param_names", @ieval_self_param_names)25525+
buf = ir_emit_sa(buf, "@ieval_extra_param_names", @ieval_extra_param_names)25526+
buf = ir_emit_sa(buf, "@cls_with_internal_ieval_lift", @cls_with_internal_ieval_lift)2543325527
buf = ir_emit_ia(buf, "@pre_execution_blocks", @pre_execution_blocks)2543425528
buf = ir_emit_ia(buf, "@post_execution_blocks", @post_execution_blocks)2543525529
buf = ir_emit_sa(buf, "@toplevel_ivar_names", @toplevel_ivar_names)@@ -27015,6 +27109,57 @@
2701527109
saved_ci_iv = @current_class_idx2701627110
@current_class_idx = @ieval_class_idxs[iv]2701727111
push_scope27112+
# Declare the self-bound param and any extras up front with27113+
# their typed slots, then refine body locals with those names27114+
# excluded from the rescan so reassigns hit the param slot27115+
# rather than inventing a fresh local of the RHS type.27116+
ie_pnames = "".split(",")27117+
spn_iv = ""27118+
if iv < @ieval_self_param_names.length27119+
spn_iv = @ieval_self_param_names[iv]27120+
end27121+
if spn_iv != ""27122+
declare_var(spn_iv, "obj_" + @cls_names[@ieval_class_idxs[iv]])27123+
ie_pnames.push(spn_iv)27124+
end27125+
extra_iv_csv = ""27126+
if iv < @ieval_extra_param_names.length27127+
extra_iv_csv = @ieval_extra_param_names[iv]27128+
end27129+
extra_iv_names = "".split(",")27130+
if extra_iv_csv != ""27131+
extra_iv_names = extra_iv_csv.split(";")27132+
end27133+
kex_iv = 027134+
while kex_iv < extra_iv_names.length27135+
declare_var(extra_iv_names[kex_iv], "nil")27136+
ie_pnames.push(extra_iv_names[kex_iv])27137+
kex_iv = kex_iv + 127138+
end27139+
ml_iv = "".split(",")27140+
mt_iv = "".split(",")27141+
refine_method_body_locals(bid_iv, ml_iv, mt_iv, ie_pnames)27142+
# Order: self-bound at slot 0, extras, then body locals.27143+
final_names = "".split(",")27144+
final_types = "".split(",")27145+
if spn_iv != ""27146+
final_names.push(spn_iv)27147+
final_types.push("obj_" + @cls_names[@ieval_class_idxs[iv]])27148+
end27149+
kex2_iv = 027150+
while kex2_iv < extra_iv_names.length27151+
final_names.push(extra_iv_names[kex2_iv])27152+
final_types.push("nil")27153+
kex2_iv = kex2_iv + 127154+
end27155+
k_iv = 027156+
while k_iv < ml_iv.length27157+
final_names.push(ml_iv[k_iv])27158+
final_types.push(mt_iv[k_iv])27159+
k_iv = k_iv + 127160+
end27161+
@nd_scope_names[bid_iv] = final_names.join("|")27162+
@nd_scope_types[bid_iv] = final_types.join("|")2701827163
walk_and_cache(bid_iv)2701927164
pop_scope2702027165
@current_class_idx = saved_ci_iv@@ -27028,11 +27173,24 @@
2702827173
# into captures (in outer scope) vs true locals at emit time. The2702927174
# scan output is purely syntactic, so we cache it here keyed by2703027175
# the body bid. Params are the block's syntactic required names.27176+
# Skip ieval-lifted blocks below: the ieval arm already set27177+
# their typed scope. Spinel's Hash type-infers only StrIntHash,27178+
# so the lookup keys are stringified body ids.27179+
ieval_body_set = {}27180+
ie_seed = 027181+
while ie_seed < @ieval_body_ids.length27182+
ieval_body_set[@ieval_body_ids[ie_seed].to_s] = 127183+
ie_seed = ie_seed + 127184+
end2703127185
bn = 02703227186
while bn < @nd_count2703327187
tk = @nd_type[bn]2703427188
if tk == "BlockNode" || tk == "LambdaNode" || tk == "ProcNode"2703527189
body_bn = @nd_body[bn]27190+
if ieval_body_set[body_bn.to_s] != nil27191+
bn = bn + 127192+
next27193+
end2703627194
if body_bn >= 02703727195
# Collect syntactic block param names2703827196
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…