Commit 3e89023d

3e89023db62eMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Merge pull request #124 from ryanseys/rs-instance-eval-trampoline

feat(codegen): inline single-stmt instance_eval(&b) trampoline

Details

Commit
3e89023db62e
Parent
f137975e5e4e
Merge parent
f3d1da49a6ba

spinel_codegen.rbmodifiedView file

@@ -157,6 +157,12 @@
157157 @in_yield_method = 0
158158 @current_method_yield_arity = 1
159159 @in_gc_scope = 0
160+ # Set during the arity-0 instance_eval trampoline inlining so
161+ # receiverless calls in the spliced block body dispatch against
162+ # the rebound self (the .instance_eval receiver) instead of the
163+ # enclosing method's self.
164+ @instance_eval_self_var = ""
165+ @instance_eval_self_type = ""
160166
161167 # Yield/block tracking (parallel with meth_names / cls_meth_names)
162168 @meth_has_yield = []
@@ -764,6 +770,97 @@
764770 ""
765771 end
766772
773+ # Returns the body node id for class ci's midx'th method, or -1
774+ # if midx is out of range or the body id is invalid. Centralises
775+ # the @cls_meth_bodies[ci].split(";")[midx].to_i parse so detectors
776+ # don't have to inline it.
777+ def cls_method_body_id(ci, midx)
778+ bodies = @cls_meth_bodies[ci].split(";")
779+ if midx >= bodies.length
780+ return -1
781+ end
782+ bid = bodies[midx].to_i
783+ if bid < 0
784+ return -1
785+ end
786+ bid
787+ end
788+
789+ # Returns the name of the class method's single proc-typed param
790+ # (its `&block` slot), or "" if the signature isn't exactly one
791+ # proc param. Used by detectors that match the
792+ # `def m(&b); ...; end` shape (instance_eval trampoline today;
793+ # extensible to instance_exec, tap, etc.).
794+ def cls_method_sole_proc_param_name(ci, midx)
795+ all_params = @cls_meth_params[ci].split("|")
796+ all_ptypes = @cls_meth_ptypes[ci].split("|")
797+ if midx >= all_params.length
798+ return ""
799+ end
800+ if midx >= all_ptypes.length
801+ return ""
802+ end
803+ pnames = all_params[midx].split(",")
804+ ptypes = all_ptypes[midx].split(",")
805+ if pnames.length != 1
806+ return ""
807+ end
808+ if ptypes.length != 1
809+ return ""
810+ end
811+ if ptypes[0] != "proc"
812+ return ""
813+ end
814+ pnames[0]
815+ end
816+
817+ # Detects the exact arity-0 instance_eval trampoline shape:
818+ # `def m(&b); instance_eval(&b); end`. Returns 1 when the
819+ # (ci, midx) method body is a single CallNode of `instance_eval`
820+ # forwarded the method's sole proc-typed param via &-arg, 0
821+ # otherwise. Spinel inlines these at the call site (yield-style)
822+ # with self rebound to the receiver — full Ruby instance_eval is
823+ # dynamic, but this AOT compromise covers the common DSL-trampoline
824+ # shape. Anything wider falls through to today's silent no-op.
825+ def is_instance_eval_trampoline(ci, midx)
826+ # AST shape gates first (no string splits — cheap reject path).
827+ bid = cls_method_body_id(ci, midx)
828+ if bid < 0
829+ return 0
830+ end
831+ stmts = get_stmts(bid)
832+ if stmts.length != 1
833+ return 0
834+ end
835+ s = stmts[0]
836+ if @nd_type[s] != "CallNode"
837+ return 0
838+ end
839+ if @nd_name[s] != "instance_eval"
840+ return 0
841+ end
842+ if @nd_receiver[s] >= 0
843+ return 0
844+ end
845+ inner = find_block_arg(s)
846+ if inner < 0
847+ return 0
848+ end
849+ if @nd_type[inner] != "LocalVariableReadNode"
850+ return 0
851+ end
852+ # Param signature gate (does the string splits) — only methods
853+ # that pass the AST shape get here.
854+ pname = cls_method_sole_proc_param_name(ci, midx)
855+ if pname == ""
856+ return 0
857+ end
858+ if @nd_name[inner] != pname
859+ return 0
860+ end
861+ 1
862+ end
863+
767864 # Flatten a constant reference into an internal name.
768865 # C -> C
769866 # ::C -> C
@@ -14282,6 +14379,31 @@
1428214379 end
1428314380 end
1428414381 end
14382+ # Inside an instance_eval inlined block: receiverless calls in
14383+ # the spliced body dispatch against the rebound self (the
14384+ # receiver that .instance_eval was called on), not the enclosing
14385+ # method's self. Static type inference gives us the class name,
14386+ # so we resolve the method at compile time and emit a typed-self
14387+ # call.
14388+ if @instance_eval_self_var != ""
14389+ target_ci = find_class_idx(@instance_eval_self_type)
14390+ if target_ci >= 0
14391+ cidx = cls_find_method(target_ci, mname)
14392+ if cidx >= 0
14393+ owner = find_method_owner(target_ci, mname)
14394+ cast_recv = "(sp_" + owner + " *)" + @instance_eval_self_var
14395+ tail = build_call_tail(compile_call_args(nid), "")
14396+ return "sp_" + owner + "_" + sanitize_name(mname) + "(" + cast_recv + tail + ")"
14397+ end
14398+ end
14399+ # Fall through deliberately when the rebound class doesn't
14400+ # define `mname`. Ruby's instance_eval rebinds `self` for
14401+ # instance-method dispatch only — Kernel methods (puts, p,
14402+ # raise, etc.) and top-level helpers must still resolve in the
14403+ # enclosing scope, which the gates below handle. Removing this
14404+ # fallthrough would silently break common DSL patterns like
14405+ # `b.configure { puts "hi"; add(10) }`.
14406+ end
1428514407 if @current_class_idx >= 0
1428614408 cidx = cls_find_method(@current_class_idx, mname)
1428714409 if cidx >= 0
@@ -20959,8 +21081,11 @@
2095921081 end
2096021082 end
2096121083
20962- # User-defined yield function called with block
20963- if @nd_block[nid] >= 0
21084+ # User-defined yield function or instance_eval trampoline, called
21085+ # with a literal block. (A `&proc_var` forward at the call site
21086+ # doesn't trigger inlining — those flow through compile_call_expr's
21087+ # regular block-forwarding path.)
21088+ if has_literal_block(nid) == 1
2096421089 if recv < 0
2096521090 mi = find_method_idx(mname)
2096621091 if mi >= 0
@@ -20970,30 +21095,24 @@
2097021095 end
2097121096 end
2097221097 end
20973- # Class method with yield
21098+ # Class method with yield, or an arity-0 instance_eval trampoline.
21099+ # The direct-class and parent-class lookups share the same dispatch
21100+ # shape (find midx, check yield, check trampoline); collapsed into
21101+ # try_yield_or_trampoline_dispatch.
2097421102 if recv >= 0
2097521103 rtype = infer_type(recv)
2097621104 if is_obj_type(rtype) == 1
2097721105 cn = rtype[4, rtype.length - 4]
2097821106 cci = find_class_idx(cn)
2097921107 if cci >= 0
20980- midx = cls_find_method_direct(cci, mname)
20981- if midx >= 0
20982- if cls_method_has_yield(cci, midx) == 1
20983- compile_yield_method_call_stmt(nid, cci, midx, mname)
20984- return 1
20985- end
21108+ if try_yield_or_trampoline_dispatch(nid, recv, cci, mname) == 1
21109+ return 1
2098621110 end
20987- # Check parent
2098821111 if @cls_parents[cci] != ""
2098921112 pci = find_class_idx(@cls_parents[cci])
2099021113 if pci >= 0
20991- pidx = cls_find_method_direct(pci, mname)
20992- if pidx >= 0
20993- if cls_method_has_yield(pci, pidx) == 1
20994- compile_yield_method_call_stmt(nid, pci, pidx, mname)
20995- return 1
20996- end
21114+ if try_yield_or_trampoline_dispatch(nid, recv, pci, mname) == 1
21115+ return 1
2099721116 end
2099821117 end
2099921118 end
@@ -24672,6 +24791,80 @@
2467224791 "0"
2467324792 end
2467424793
24794+ # Tries the yield-method or instance_eval-trampoline dispatch
24795+ # against a single class index. Returns 1 if dispatch fired (caller
24796+ # should return immediately), 0 otherwise (caller falls through to
24797+ # the next gate, e.g. parent class). Shared by the direct-class and
24798+ # parent-class branches in compile_no_recv_call_expr.
24799+ def try_yield_or_trampoline_dispatch(nid, recv, cls_idx, mname)
24800+ midx = cls_find_method_direct(cls_idx, mname)
24801+ if midx < 0
24802+ return 0
24803+ end
24804+ if cls_method_has_yield(cls_idx, midx) == 1
24805+ compile_yield_method_call_stmt(nid, cls_idx, midx, mname)
24806+ return 1
24807+ end
24808+ if is_instance_eval_trampoline(cls_idx, midx) == 1
24809+ compile_instance_eval_inlined_stmt(nid, recv)
24810+ return 1
24811+ end
24812+ 0
24813+ end
24814+
24815+ # Splice the statements of a block body in place with `self`
24816+ # rebound to self_var (typed as cname). Saves and restores the
24817+ # rebound-self ivars (@instance_eval_self_var / _type) so nested
24818+ # splices compose. compile_no_recv_call_expr's instance_eval-self
24819+ # branch reads these to dispatch receiverless calls inside the
24820+ # splice against the rebound class. Reusable by future
24821+ # rebind-and-splice features (e.g. instance_exec, tap-shape
24822+ # trampolines).
24823+ def splice_block_with_self_rebound(body, self_var, cname)
24824+ prev_self_var = @instance_eval_self_var
24825+ prev_self_type = @instance_eval_self_type
24826+ @instance_eval_self_var = self_var
24827+ @instance_eval_self_type = cname
24828+ if body >= 0
24829+ stmts = get_stmts(body)
24830+ k = 0
24831+ while k < stmts.length
24832+ compile_stmt(stmts[k])
24833+ k = k + 1
24834+ end
24835+ end
24836+ @instance_eval_self_var = prev_self_var
24837+ @instance_eval_self_type = prev_self_type
24838+ end
24839+
24840+ # Inlines a `recv.m { body }` call when `m` is an arity-0
24841+ # instance_eval trampoline. The entire method body is the call
24842+ # `instance_eval(&block)`, so we splice the block body in place
24843+ # with self rebound to the receiver. Modeled on
24844+ # compile_yield_method_call_stmt but simpler — the trampoline body
24845+ # has no locals/params to remap.
24846+ def compile_instance_eval_inlined_stmt(nid, recv)
24847+ blk = @nd_block[nid]
24848+ if blk < 0
24849+ return
24850+ end
24851+ rtype = infer_type(recv)
24852+ cname = ""
24853+ if is_obj_type(rtype) == 1
24854+ cname = rtype[4, rtype.length - 4]
24855+ end
24856+ if cname == ""
24857+ return
24858+ end
24859+ rc = compile_expr_gc_rooted(recv)
24860+ self_var = new_temp
24861+ emit(" sp_" + cname + " *" + self_var + " = (sp_" + cname + " *)" + rc + ";")
24862+ if @in_gc_scope == 1
24863+ emit(" SP_GC_ROOT(" + self_var + ");")
24864+ end
24865+ splice_block_with_self_rebound(@nd_body[blk], self_var, cname)
24866+ end
24867+
2467524868 def compile_yield_method_call_stmt(nid, cci, midx, mname)
2467624869 # Call a yield-using class method with a block - inline the method body
2467724870 blk = @nd_block[nid]