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 = 0158158
@current_method_yield_arity = 1159159
@in_gc_scope = 0160+
# Set during the arity-0 instance_eval trampoline inlining so161+
# receiverless calls in the spliced block body dispatch against162+
# the rebound self (the .instance_eval receiver) instead of the163+
# 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
end766772
773+
# Returns the body node id for class ci's midx'th method, or -1774+
# if midx is out of range or the body id is invalid. Centralises775+
# the @cls_meth_bodies[ci].split(";")[midx].to_i parse so detectors776+
# 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.length780+
return -1781+
end782+
bid = bodies[midx].to_i783+
if bid < 0784+
return -1785+
end786+
bid787+
end788+
789+
# Returns the name of the class method's single proc-typed param790+
# (its `&block` slot), or "" if the signature isn't exactly one791+
# proc param. Used by detectors that match the792+
# `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.length798+
return ""799+
end800+
if midx >= all_ptypes.length801+
return ""802+
end803+
pnames = all_params[midx].split(",")804+
ptypes = all_ptypes[midx].split(",")805+
if pnames.length != 1806+
return ""807+
end808+
if ptypes.length != 1809+
return ""810+
end811+
if ptypes[0] != "proc"812+
return ""813+
end814+
pnames[0]815+
end816+
817+
# Detects the exact arity-0 instance_eval trampoline shape:818+
# `def m(&b); instance_eval(&b); end`. Returns 1 when the819+
# (ci, midx) method body is a single CallNode of `instance_eval`820+
# forwarded the method's sole proc-typed param via &-arg, 0821+
# otherwise. Spinel inlines these at the call site (yield-style)822+
# with self rebound to the receiver — full Ruby instance_eval is823+
# dynamic, but this AOT compromise covers the common DSL-trampoline824+
# 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 < 0829+
return 0830+
end831+
stmts = get_stmts(bid)832+
if stmts.length != 1833+
return 0834+
end835+
s = stmts[0]836+
if @nd_type[s] != "CallNode"837+
return 0838+
end839+
if @nd_name[s] != "instance_eval"840+
return 0841+
end842+
if @nd_receiver[s] >= 0843+
return 0844+
end845+
inner = find_block_arg(s)846+
if inner < 0847+
return 0848+
end849+
if @nd_type[inner] != "LocalVariableReadNode"850+
return 0851+
end852+
# Param signature gate (does the string splits) — only methods853+
# that pass the AST shape get here.854+
pname = cls_method_sole_proc_param_name(ci, midx)855+
if pname == ""856+
return 0857+
end858+
if @nd_name[inner] != pname859+
return 0860+
end861+
1862+
end863+
767864
# Flatten a constant reference into an internal name.768865
# C -> C769866
# ::C -> C@@ -14282,6 +14379,31 @@
1428214379
end1428314380
end1428414381
end14382+
# Inside an instance_eval inlined block: receiverless calls in14383+
# the spliced body dispatch against the rebound self (the14384+
# receiver that .instance_eval was called on), not the enclosing14385+
# method's self. Static type inference gives us the class name,14386+
# so we resolve the method at compile time and emit a typed-self14387+
# call.14388+
if @instance_eval_self_var != ""14389+
target_ci = find_class_idx(@instance_eval_self_type)14390+
if target_ci >= 014391+
cidx = cls_find_method(target_ci, mname)14392+
if cidx >= 014393+
owner = find_method_owner(target_ci, mname)14394+
cast_recv = "(sp_" + owner + " *)" + @instance_eval_self_var14395+
tail = build_call_tail(compile_call_args(nid), "")14396+
return "sp_" + owner + "_" + sanitize_name(mname) + "(" + cast_recv + tail + ")"14397+
end14398+
end14399+
# Fall through deliberately when the rebound class doesn't14400+
# define `mname`. Ruby's instance_eval rebinds `self` for14401+
# instance-method dispatch only — Kernel methods (puts, p,14402+
# raise, etc.) and top-level helpers must still resolve in the14403+
# enclosing scope, which the gates below handle. Removing this14404+
# fallthrough would silently break common DSL patterns like14405+
# `b.configure { puts "hi"; add(10) }`.14406+
end1428514407
if @current_class_idx >= 01428614408
cidx = cls_find_method(@current_class_idx, mname)1428714409
if cidx >= 0@@ -20959,8 +21081,11 @@
2095921081
end2096021082
end2096121083
20962-
# User-defined yield function called with block20963-
if @nd_block[nid] >= 021084+
# User-defined yield function or instance_eval trampoline, called21085+
# with a literal block. (A `&proc_var` forward at the call site21086+
# doesn't trigger inlining — those flow through compile_call_expr's21087+
# regular block-forwarding path.)21088+
if has_literal_block(nid) == 12096421089
if recv < 02096521090
mi = find_method_idx(mname)2096621091
if mi >= 0@@ -20970,30 +21095,24 @@
2097021095
end2097121096
end2097221097
end20973-
# Class method with yield21098+
# Class method with yield, or an arity-0 instance_eval trampoline.21099+
# The direct-class and parent-class lookups share the same dispatch21100+
# shape (find midx, check yield, check trampoline); collapsed into21101+
# try_yield_or_trampoline_dispatch.2097421102
if recv >= 02097521103
rtype = infer_type(recv)2097621104
if is_obj_type(rtype) == 12097721105
cn = rtype[4, rtype.length - 4]2097821106
cci = find_class_idx(cn)2097921107
if cci >= 020980-
midx = cls_find_method_direct(cci, mname)20981-
if midx >= 020982-
if cls_method_has_yield(cci, midx) == 120983-
compile_yield_method_call_stmt(nid, cci, midx, mname)20984-
return 120985-
end21108+
if try_yield_or_trampoline_dispatch(nid, recv, cci, mname) == 121109+
return 12098621110
end20987-
# Check parent2098821111
if @cls_parents[cci] != ""2098921112
pci = find_class_idx(@cls_parents[cci])2099021113
if pci >= 020991-
pidx = cls_find_method_direct(pci, mname)20992-
if pidx >= 020993-
if cls_method_has_yield(pci, pidx) == 120994-
compile_yield_method_call_stmt(nid, pci, pidx, mname)20995-
return 120996-
end21114+
if try_yield_or_trampoline_dispatch(nid, recv, pci, mname) == 121115+
return 12099721116
end2099821117
end2099921118
end@@ -24672,6 +24791,80 @@
2467224791
"0"2467324792
end2467424793
24794+
# Tries the yield-method or instance_eval-trampoline dispatch24795+
# against a single class index. Returns 1 if dispatch fired (caller24796+
# should return immediately), 0 otherwise (caller falls through to24797+
# the next gate, e.g. parent class). Shared by the direct-class and24798+
# 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 < 024802+
return 024803+
end24804+
if cls_method_has_yield(cls_idx, midx) == 124805+
compile_yield_method_call_stmt(nid, cls_idx, midx, mname)24806+
return 124807+
end24808+
if is_instance_eval_trampoline(cls_idx, midx) == 124809+
compile_instance_eval_inlined_stmt(nid, recv)24810+
return 124811+
end24812+
024813+
end24814+
24815+
# Splice the statements of a block body in place with `self`24816+
# rebound to self_var (typed as cname). Saves and restores the24817+
# rebound-self ivars (@instance_eval_self_var / _type) so nested24818+
# splices compose. compile_no_recv_call_expr's instance_eval-self24819+
# branch reads these to dispatch receiverless calls inside the24820+
# splice against the rebound class. Reusable by future24821+
# rebind-and-splice features (e.g. instance_exec, tap-shape24822+
# trampolines).24823+
def splice_block_with_self_rebound(body, self_var, cname)24824+
prev_self_var = @instance_eval_self_var24825+
prev_self_type = @instance_eval_self_type24826+
@instance_eval_self_var = self_var24827+
@instance_eval_self_type = cname24828+
if body >= 024829+
stmts = get_stmts(body)24830+
k = 024831+
while k < stmts.length24832+
compile_stmt(stmts[k])24833+
k = k + 124834+
end24835+
end24836+
@instance_eval_self_var = prev_self_var24837+
@instance_eval_self_type = prev_self_type24838+
end24839+
24840+
# Inlines a `recv.m { body }` call when `m` is an arity-024841+
# instance_eval trampoline. The entire method body is the call24842+
# `instance_eval(&block)`, so we splice the block body in place24843+
# with self rebound to the receiver. Modeled on24844+
# compile_yield_method_call_stmt but simpler — the trampoline body24845+
# has no locals/params to remap.24846+
def compile_instance_eval_inlined_stmt(nid, recv)24847+
blk = @nd_block[nid]24848+
if blk < 024849+
return24850+
end24851+
rtype = infer_type(recv)24852+
cname = ""24853+
if is_obj_type(rtype) == 124854+
cname = rtype[4, rtype.length - 4]24855+
end24856+
if cname == ""24857+
return24858+
end24859+
rc = compile_expr_gc_rooted(recv)24860+
self_var = new_temp24861+
emit(" sp_" + cname + " *" + self_var + " = (sp_" + cname + " *)" + rc + ";")24862+
if @in_gc_scope == 124863+
emit(" SP_GC_ROOT(" + self_var + ");")24864+
end24865+
splice_block_with_self_rebound(@nd_body[blk], self_var, cname)24866+
end24867+
2467524868
def compile_yield_method_call_stmt(nid, cci, midx, mname)2467624869
# Call a yield-using class method with a block - inline the method body2467724870
blk = @nd_block[nid]test/instance_eval_trampoline.rbaddedView file
Loading diff…