Commit f01243db
f01243db55b3Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 18 files · +16 added · ~2 modified
Description
Merge pull request #1361 from ryanseys/rs-iexec-core
analyze,codegen: inline-splice lowering for direct instance_exec/eval
Details
- Commit
- f01243db55b3
- Parent
- 2f3d03c6f06d
- Merge parent
- 0aea90f3dccc
spinel_analyze.rbmodifiedView file
@@ -3475,11 +3475,17 @@
34753475
return "obj_" + @cls_names[@ieval_class_idxs[n]]34763476
end34773477
end3478-
# iexec direct-path lift: the synthetic `__sp_iexec_<N>` function is3479-
# void-returning in the baseline, so its call resolves to "void".3480-
# The expression-position follow-up surfaces the real last-expression3481-
# type here via infer_iexec_body_return_types.3478+
# iexec direct-path lift: a heap receiver is inlined at the call site3479+
# and the call's value is the block's last expression, so type the call3480+
# by that expression (simple ctypes only -- array / hash / poly bodies3481+
# stay "void" and take the receiver-yielding comma-expr fallback, like3482+
# the ieval arm above). Computed on the fly to avoid a per-lift ivar.34823483
if is_iexec_call_name(mname) == 13484+
suffix = mname[11, mname.length - 11]3485+
rt = iexec_call_value_type(suffix.to_i)3486+
if ieval_return_type_is_simple(rt) == 13487+
return rt3488+
end34833489
return "void"34843490
end34853491
@@ -9321,6 +9327,14 @@
93219327
if ci < 093229328
return93239329
end9330+
# Override-aware intrinsic: if the receiver's class (or its parent9331+
# chain) defines its own `instance_eval`, the user has shadowed the9332+
# intrinsic. Skip the lift so ordinary dispatch resolves to the9333+
# user's method, matching CRuby. A gate on the method-name string9334+
# alone would silently bypass the override.9335+
if cls_find_method(ci, "instance_eval") >= 09336+
return9337+
end93249338
body_id = @nd_body[blk]93259339
# v1: bail if the block uses yield/block_given?. Lifting it as a93269340
# plain function would lose the enclosing method's block plumbing.@@ -9329,6 +9343,15 @@
93299343
if body_id >= 0 && body_has_yield(body_id) == 193309344
return93319345
end9346+
# Reject non-local control flow and runtime method definition inside9347+
# the block -- return / break / next / def / define_method have no9348+
# statically-translatable semantics once the body is lifted.9349+
if body_id >= 09350+
kind = body_iexec_unsupported_kind(body_id)9351+
if kind != ""9352+
iexec_reject_kind("instance_eval", kind)9353+
end9354+
end93329355
n = @ieval_counter93339356
@ieval_counter = @ieval_counter + 193349357
@ieval_class_idxs.push(ci)@@ -9349,6 +9372,105 @@
93499372
@nd_block[nid] = -193509373
end93519374
9375+
# Emit a hard `Spinel: <intrinsic>: <reason>; <suggestion>` diagnostic9376+
# and exit. `intrinsic` is the user-facing method name so the message9377+
# points at the right call.9378+
def iexec_reject(intrinsic, reason, suggestion)9379+
msg = "Spinel: " + intrinsic + ": " + reason9380+
if suggestion != ""9381+
msg = msg + "; " + suggestion9382+
end9383+
$stderr.puts msg9384+
exit(1)9385+
end9386+
9387+
# Rejection helper for the body_iexec_unsupported_kind enum. Maps the9388+
# construct name to a reason + suggestion, then rejects.9389+
def iexec_reject_kind(intrinsic, kind)9390+
reason = ""9391+
suggestion = ""9392+
if kind == "return"9393+
reason = "block uses 'return', which would return from the enclosing method"9394+
suggestion = "remove the 'return' (the block's last expression is its value), or move the non-local exit outside the " + intrinsic + " block"9395+
elsif kind == "break"9396+
reason = "block uses 'break', which has no enclosing-iterator semantics under " + intrinsic9397+
suggestion = "restructure to avoid non-local control flow"9398+
elsif kind == "next"9399+
reason = "block uses 'next', which has no enclosing-iterator semantics under " + intrinsic9400+
suggestion = "restructure to avoid non-local control flow"9401+
elsif kind == "def"9402+
reason = "block contains a 'def', defining a method at runtime"9403+
suggestion = "Spinel does not support runtime method definition; define the method statically on the receiver's class"9404+
elsif kind == "define_method"9405+
reason = "block calls 'define_method', defining a method at runtime"9406+
suggestion = "Spinel does not support runtime method definition; define the method statically on the receiver's class"9407+
else9408+
reason = "block uses unsupported construct '" + kind + "'"9409+
suggestion = "remove the construct or restructure the block body"9410+
end9411+
iexec_reject(intrinsic, reason, suggestion)9412+
end9413+
9414+
# Walks an instance_eval / instance_exec block body and returns the9415+
# name of the first unsupported construct found (return / break /9416+
# next / def / define_method), or "" when the body uses only9417+
# constructs the lift can model. Nested DefNodes return "def" at the9418+
# boundary rather than descending -- their bodies belong to a9419+
# different scope and are checked when those methods compile.9420+
def body_iexec_unsupported_kind(nid)9421+
if nid < 09422+
return ""9423+
end9424+
t = @nd_type[nid]9425+
if t == "ReturnNode"9426+
return "return"9427+
end9428+
if t == "BreakNode"9429+
return "break"9430+
end9431+
if t == "NextNode"9432+
return "next"9433+
end9434+
if t == "DefNode"9435+
return "def"9436+
end9437+
if t == "CallNode" && @nd_name[nid] == "define_method"9438+
return "define_method"9439+
end9440+
cs = []9441+
push_child_ids(nid, cs)9442+
k = 09443+
while k < cs.length9444+
r = body_iexec_unsupported_kind(cs[k])9445+
if r != ""9446+
return r9447+
end9448+
k = k + 19449+
end9450+
""9451+
end9452+
9453+
# A `recv.<intrinsic>(args)` with no literal block and no `&proc`9454+
# argument is a no-block call. CRuby raises LocalJumpError at9455+
# runtime; Spinel surfaces the same as a compile-time error. Returns9456+
# 1 when the args carry a BlockArgumentNode (`&proc` form), so the9457+
# caller can leave that case for the proc-arg path instead.9458+
def iexec_args_have_block_arg(nid)9459+
args_id = @nd_arguments[nid]9460+
if args_id < 09461+
return 09462+
end9463+
aids = get_args(args_id)9464+
k = 09465+
while k < aids.length9466+
if @nd_type[aids[k]] == "BlockArgumentNode"9467+
return 19468+
end9469+
k = k + 19470+
end9471+
09472+
end9473+
93529474
# Direct-path lift for `recv.instance_exec(args) { |params| body }`.93539475
# Sibling of ieval_rewrite_call -- detects an explicit-receiver93549476
# instance_exec call with a block literal, validates the@@ -9379,14 +9501,41 @@
93799501
end93809502
recv = @nd_receiver[nid]93819503
blk = @nd_block[nid]9504+
# Receiverless `instance_exec(...)` is the trampoline-body shape9505+
# (`def m(x, &b); instance_exec(x, &b); end`), handled by codegen's9506+
# call-site inlining. Leave it for that path.93829507
if recv < 093839508
return93849509
end93859510
if blk < 09511+
# An explicit-receiver call with no literal block: either a no-block9512+
# call (CRuby raises LocalJumpError -- surface it at compile time)9513+
# or a `&proc` form (left for the proc-arg path).9514+
if iexec_args_have_block_arg(nid) == 09515+
iexec_reject(9516+
"instance_exec",9517+
"no block given",9518+
"pass a literal block (`recv.instance_exec { ... }`)"9519+
)9520+
end93869521
return93879522
end93889523
ci = recv_class_idx_for_rebind(recv, local_class)93899524
if ci < 09525+
# AOT codegen needs the receiver's class to emit a typed9526+
# `sp_iexec_<N>(sp_<C> *self, ...)` signature; a polymorphic /9527+
# un-narrowed receiver can't be lifted without runtime dispatch.9528+
iexec_reject(9529+
"instance_exec",9530+
"receiver type cannot be statically resolved",9531+
"assign the receiver to a typed local first (e.g. `r = Foo.new`), or narrow via a class predicate (`raise unless r.is_a?(Foo)`) before the call"9532+
)9533+
end9534+
# Override-aware intrinsic: bypass the lift when the receiver's9535+
# class defines its own `instance_exec`; ordinary dispatch then9536+
# resolves to the user's method, preserving CRuby semantics. Same9537+
# shape as the override check in ieval_rewrite_call.9538+
if cls_find_method(ci, "instance_exec") >= 093909539
return93919540
end93929541
body_id = @nd_body[blk]@@ -9394,7 +9543,21 @@
93949543
# plumbing once we lift the body to a static function. Same93959544
# gate as ieval_rewrite_call.93969545
if body_id >= 0 && body_has_yield(body_id) == 19397-
return9546+
iexec_reject(9547+
"instance_exec",9548+
"block uses 'yield' or 'block_given?', which require an enclosing block-receiving method",9549+
"restructure the inner logic to take a Proc parameter instead of yielding"9550+
)9551+
end9552+
# Reject non-local control flow and runtime method definition --9553+
# return / break / next have no enclosing-method semantics once the9554+
# body is lifted to a static function, and def / define_method are9555+
# runtime definitions Spinel does not model.9556+
if body_id >= 09557+
kind = body_iexec_unsupported_kind(body_id)9558+
if kind != ""9559+
iexec_reject_kind("instance_exec", kind)9560+
end93989561
end93999562
94009563
# Block param extraction (mirrors compile_yield_method_call_stmt@@ -9622,6 +9785,45 @@
96229785
end96239786
end96249787
9788+
# On-the-fly last-expression type of a lifted iexec block, used to9789+
# type a local assigned from `recv.instance_exec(...) { ... }`. Computed9790+
# here rather than cached in a per-lift ivar so no new serialized9791+
# compiler state is added. Mirrors infer_ieval_body_return_types' setup:9792+
# rebind the class and declare the block params, then infer the body's9793+
# last-expression type.9794+
def iexec_call_value_type(n)9795+
if n < 0 || n >= @iexec_body_ids.length9796+
return "void"9797+
end9798+
bid = @iexec_body_ids[n]9799+
if bid < 09800+
return "void"9801+
end9802+
ci = @iexec_class_idxs[n]9803+
saved_ci = @current_class_idx9804+
@current_class_idx = ci9805+
push_scope9806+
pnames_s = @iexec_block_pnames[n]9807+
ptypes_s = @iexec_block_ptypes[n]9808+
if pnames_s != ""9809+
pnames = pnames_s.split("|")9810+
ptypes = ptypes_s.split("|")9811+
k = 09812+
while k < pnames.length9813+
pt = "int"9814+
if k < ptypes.length9815+
pt = ptypes[k]9816+
end9817+
declare_var(pnames[k], pt)9818+
k = k + 19819+
end9820+
end9821+
rt = infer_body_return(bid)9822+
pop_scope9823+
@current_class_idx = saved_ci9824+
rt9825+
end9826+
96259827
def is_iexec_call_name(mname)96269828
if mname.length <= 1196279829
return 0spinel_codegen.rbmodifiedView file
Loading diff…
test/analyze_fail/instance_exec_def_in_block.rbaddedView file
Loading diff…
test/analyze_fail/instance_exec_def_in_block.stderr.expectedaddedView file
Loading diff…
test/analyze_fail/instance_exec_define_method.rbaddedView file
Loading diff…
test/analyze_fail/instance_exec_define_method.stderr.expectedaddedView file
Loading diff…
test/analyze_fail/instance_exec_no_block.rbaddedView file
Loading diff…
test/analyze_fail/instance_exec_no_block.stderr.expectedaddedView file
Loading diff…
test/instance_exec_capture.rbaddedView file
Loading diff…
test/instance_exec_capture.rb.expectedaddedView file
Loading diff…
test/instance_exec_override.rbaddedView file
Loading diff…
test/instance_exec_override.rb.expectedaddedView file
Loading diff…
test/instance_exec_parity.rbaddedView file
Loading diff…
test/instance_exec_parity.rb.expectedaddedView file
Loading diff…
test/instance_exec_return.rbaddedView file
Loading diff…
test/instance_exec_return.rb.expectedaddedView file
Loading diff…
test/instance_exec_valuetype.rbaddedView file
Loading diff…
test/instance_exec_valuetype.rb.expectedaddedView file
Loading diff…