Commit c722dfb3
c722dfb32b3eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
instance_eval/exec trampolines inlined at call sites
Detect the DSL trampoline shape def m(args, &block); instance_eval(&block); end (and the instance_exec variant) and compile a call recv.m(args) { ... }
exactly like recv.instance_eval/exec(args) { ... }: splice the block body at
the call site with self rebound to the receiver, so receiverless calls and
ivar access inside the block resolve against the receiver's class via static
type inference.
- comp_trampoline_kind() classifies a method as an eval (1) / exec (2) trampoline by matching its single-statement body and verifying the block arg forwards the method's own &block parameter.
- The trampoline detection short-circuits the &block-forward yields heuristic (which would otherwise inline the stub body and hit the receiverless instance_eval), routing the call through the existing instance_eval/exec splice instead. The method body itself emits as an unreachable stub.
- analyze: build_ie_map marks trampoline-call block bodies with the receiver class; infer_call returns the block's last-expr type; block params bind to the receiver (eval) or the forwarded args (exec).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- c722dfb32b3e
- Parent
- f4c2fc342bee
src/analyze.cmodifiedView file
@@ -549,9 +549,13 @@
549549
}550550
551551
/* recv.instance_eval/exec { ... } -> the block's last-expression type552-
(bare calls inside resolve via the ie node->class map). */553-
if (recv >= 0 && (!strcmp(name, "instance_eval") || !strcmp(name, "instance_exec")) &&554-
ty_is_object(rt) && comp_method_in_chain(c, ty_object_class(rt), name, NULL) < 0) {552+
(bare calls inside resolve via the ie node->class map). A trampoline553+
method `recv.M { ... }` resolves the same way. */554+
int ie_kind = (recv >= 0 && (!strcmp(name, "instance_eval") || !strcmp(name, "instance_exec")) &&555+
ty_is_object(rt) && comp_method_in_chain(c, ty_object_class(rt), name, NULL) < 0);556+
if (!ie_kind && recv >= 0 && ty_is_object(rt) && nt_ref(nt, id, "block") >= 0)557+
ie_kind = comp_trampoline_kind(c, ty_object_class(rt), name, NULL) != 0;558+
if (ie_kind) {555559
int blk = nt_ref(nt, id, "block");556560
if (blk >= 0) {557561
int body = nt_ref(nt, blk, "body");@@ -5848,11 +5852,13 @@
58485852
const char *ty = nt_type(nt, id);58495853
if (!ty || strcmp(ty, "CallNode")) continue;58505854
const char *cname = nt_str(nt, id, "name");5851-
if (!cname || strcmp(cname, "instance_eval")) continue;5855+
if (!cname) continue;58525856
int recv = nt_ref(nt, id, "receiver");58535857
if (recv < 0) continue;58545858
TyKind rt = infer_type(c, recv);58555859
if (!ty_is_object(rt)) continue;5860+
if (strcmp(cname, "instance_eval") &&5861+
comp_trampoline_kind(c, ty_object_class(rt), cname, NULL) != 1) continue;58565862
int blk = nt_ref(nt, id, "block");58575863
if (blk < 0) continue;58585864
int pn = nt_ref(nt, blk, "parameters");@@ -5875,8 +5881,14 @@
58755881
const char *ty = nt_type(nt, id);58765882
if (!ty || strcmp(ty, "CallNode")) continue;58775883
const char *cname = nt_str(nt, id, "name");5878-
if (!cname || strcmp(cname, "instance_exec")) continue;5879-
if (nt_ref(nt, id, "receiver") < 0) continue;5884+
if (!cname) continue;5885+
int xrecv = nt_ref(nt, id, "receiver");5886+
if (xrecv < 0) continue;5887+
if (strcmp(cname, "instance_exec")) {5888+
TyKind xrt = infer_type(c, xrecv);5889+
if (!ty_is_object(xrt) ||5890+
comp_trampoline_kind(c, ty_object_class(xrt), cname, NULL) != 2) continue;5891+
}58805892
int blk = nt_ref(nt, id, "block");58815893
if (blk < 0) continue;58825894
int pn = nt_ref(nt, blk, "parameters");@@ -7114,13 +7126,17 @@
71147126
const char *ty = nt_type(nt, id);71157127
if (!ty || strcmp(ty, "CallNode")) continue;71167128
const char *nm = nt_str(nt, id, "name");7117-
if (!nm || (strcmp(nm, "instance_eval") && strcmp(nm, "instance_exec"))) continue;7129+
if (!nm) continue;71187130
int recv = nt_ref(nt, id, "receiver");71197131
int blk = nt_ref(nt, id, "block");71207132
if (recv < 0 || blk < 0) continue;71217133
TyKind rt = infer_type(c, recv);71227134
if (!ty_is_object(rt)) continue;71237135
int cls = ty_object_class(rt);7136+
if (strcmp(nm, "instance_eval") && strcmp(nm, "instance_exec")) {7137+
/* not a direct instance_eval/exec: maybe a trampoline method on `cls`? */7138+
if (!comp_trampoline_kind(c, cls, nm, NULL)) continue;7139+
}71247140
int body = nt_ref(nt, blk, "body");71257141
if (body >= 0) mark_ie_subtree(c, body, cls);71267142
}@@ -7218,6 +7234,10 @@
72187234
for (int mi = 0; mi < c->nscopes; mi++) {72197235
Scope *m = &c->scopes[mi];72207236
if (!m->blk_param) continue;7237+
/* instance_eval/exec trampolines are inlined at call sites by their own7238+
dedicated splice; don't treat the &block forward as a yield here. */7239+
if (m->class_id >= 0 && !m->is_cmethod && m->name &&7240+
comp_trampoline_kind(c, m->class_id, m->name, NULL)) continue;72217241
/* Anonymous `&`: nameless, so it can only be forwarded -- always safe72227242
to inline (there is no escaping read to worry about). */72237243
if (!m->blk_param[0]) { m->yields = 1; continue; }src/codegen.cmodifiedView file
Loading diff…
src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…