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-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
@@ -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 the
70057018 # 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 < 0
72287241 return
72297242 end
7230- # Skip blocks with parameters: lifted function takes only `self`.
7231- if @nd_parameters[blk] >= 0
7232- return
7243+ # Lifted sp_ieval_<N> takes only `self`. Accept `{ }`, `{ || }`,
7244+ # `{ |x| }`, `{ |x,y,z| }`, and `{ _1.foo }`. Bail on anything
7245+ # 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 >= 0
7251+ if @nd_type[bp] == "NumberedParametersNode"
7252+ if @nd_value[bp] != 1
7253+ return
7254+ end
7255+ self_param_name = "_1"
7256+ else
7257+ inner = @nd_parameters[bp]
7258+ if inner >= 0
7259+ 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 >= 0
7267+ return
7268+ end
7269+ if reqs.length >= 1
7270+ self_param_name = @nd_name[reqs[0]]
7271+ kpe = 1
7272+ while kpe < reqs.length
7273+ extra_param_names.push(@nd_name[reqs[kpe]])
7274+ kpe = kpe + 1
7275+ end
7276+ end
7277+ end
7278+ end
72337279 end
72347280 ci = -1
72357281 if @nd_type[recv] == "LocalVariableReadNode"
@@ -7279,6 +7325,11 @@
72797325 @ieval_counter = @ieval_counter + 1
72807326 @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 propagate
7331+ # through the lift's self-pointer signature.
7332+ flag_internal_ieval_lift(ci)
72827333 # Seed return type as "void"; infer_ieval_body_return_types runs
72837334 # after the main return-type fixpoint converges and overwrites with
72847335 # the body's last-expression ctype when non-void.
@@ -7353,6 +7404,32 @@
73537404 0
73547405 end
73557406
7407+ # detect_value_types reads the set via cls_has_internal_ieval_lift
7408+ # and excludes flagged classes from value-type / SRA promotion.
7409+ def flag_internal_ieval_lift(ci)
7410+ s = ci.to_s
7411+ k = 0
7412+ while k < @cls_with_internal_ieval_lift.length
7413+ if @cls_with_internal_ieval_lift[k] == s
7414+ return
7415+ end
7416+ k = k + 1
7417+ end
7418+ @cls_with_internal_ieval_lift.push(s)
7419+ end
7420+
7421+ def cls_has_internal_ieval_lift(ci)
7422+ s = ci.to_s
7423+ k = 0
7424+ while k < @cls_with_internal_ieval_lift.length
7425+ if @cls_with_internal_ieval_lift[k] == s
7426+ return 1
7427+ end
7428+ k = k + 1
7429+ end
7430+ 0
7431+ end
7432+
73567433 def is_ieval_call_name(mname)
73577434 if mname.length <= 11
73587435 return 0
@@ -21820,7 +21897,14 @@
2182021897 all_val = 0
2182121898 end
2182221899 end
21900+ # ieval-receiving classes need a by-pointer self so block-body
21901+ # ivar writes propagate; value-typed would drop them.
2182321902 if all_val == 1
21903+ if cls_has_internal_ieval_lift(i) == 1
21904+ all_val = 0
21905+ end
21906+ end
21907+ if all_val == 1
2182421908 @cls_is_value_type[i] = 1
2182521909 end
2182621910 end
@@ -21890,7 +21974,14 @@
2189021974 eligible = 0
2189121975 end
2189221976 end
21977+ # Same reason as the value-type arm: ieval-receiving classes
21978+ # need a stable heap pointer.
2189321979 if eligible == 1
21980+ if cls_has_internal_ieval_lift(i) == 1
21981+ eligible = 0
21982+ end
21983+ end
21984+ if eligible == 1
2189421985 @cls_is_sra[i] = 1
2189521986 end
2189621987 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_idx
2701627110 @current_class_idx = @ieval_class_idxs[iv]
2701727111 push_scope
27112+ # Declare the self-bound param and any extras up front with
27113+ # their typed slots, then refine body locals with those names
27114+ # excluded from the rescan so reassigns hit the param slot
27115+ # rather than inventing a fresh local of the RHS type.
27116+ ie_pnames = "".split(",")
27117+ spn_iv = ""
27118+ if iv < @ieval_self_param_names.length
27119+ spn_iv = @ieval_self_param_names[iv]
27120+ end
27121+ if spn_iv != ""
27122+ declare_var(spn_iv, "obj_" + @cls_names[@ieval_class_idxs[iv]])
27123+ ie_pnames.push(spn_iv)
27124+ end
27125+ extra_iv_csv = ""
27126+ if iv < @ieval_extra_param_names.length
27127+ extra_iv_csv = @ieval_extra_param_names[iv]
27128+ end
27129+ extra_iv_names = "".split(",")
27130+ if extra_iv_csv != ""
27131+ extra_iv_names = extra_iv_csv.split(";")
27132+ end
27133+ kex_iv = 0
27134+ while kex_iv < extra_iv_names.length
27135+ declare_var(extra_iv_names[kex_iv], "nil")
27136+ ie_pnames.push(extra_iv_names[kex_iv])
27137+ kex_iv = kex_iv + 1
27138+ end
27139+ 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+ end
27149+ kex2_iv = 0
27150+ while kex2_iv < extra_iv_names.length
27151+ final_names.push(extra_iv_names[kex2_iv])
27152+ final_types.push("nil")
27153+ kex2_iv = kex2_iv + 1
27154+ end
27155+ k_iv = 0
27156+ while k_iv < ml_iv.length
27157+ final_names.push(ml_iv[k_iv])
27158+ final_types.push(mt_iv[k_iv])
27159+ k_iv = k_iv + 1
27160+ end
27161+ @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_scope
2702027165 @current_class_idx = saved_ci_iv
@@ -27028,11 +27173,24 @@
2702827173 # into captures (in outer scope) vs true locals at emit time. The
2702927174 # scan output is purely syntactic, so we cache it here keyed by
2703027175 # the body bid. Params are the block's syntactic required names.
27176+ # Skip ieval-lifted blocks below: the ieval arm already set
27177+ # 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 = 0
27181+ while ie_seed < @ieval_body_ids.length
27182+ ieval_body_set[@ieval_body_ids[ie_seed].to_s] = 1
27183+ ie_seed = ie_seed + 1
27184+ end
2703127185 bn = 0
2703227186 while bn < @nd_count
2703327187 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] != nil
27191+ bn = bn + 1
27192+ next
27193+ end
2703627194 if body_bn >= 0
2703727195 # Collect syntactic block param names
2703827196 bp_list = "".split(",")