Commit 9e9aa474
9e9aa474ec40Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Add M22: yield / block_given? via call-site inlining
Block-aware free-function methods (those containing yield or block_given?) are inlined at every call site instead of emitted as standalone functions:
- analyze: mark block-aware scopes (Scope.yields); infer block-param types from the method's yield arguments.
- codegen: emit_inline_call declares the method's locals under renamed names (a rename map keyed on method-local names, so they don't clash with the call site's locals), binds params to the call args (evaluated in the call-site scope with renames off), and emits the method body. YieldNode binds the block params to the yield args (method scope, renames on) then emits the block body in the call-site scope (renames off, so captures resolve naturally). block_given? -> whether the site passed a block. Yielding methods are not emitted as standalone functions.
Covers params, captured outer locals, while-loop + accumulator, multi-arg
yield, and block_given? with and without a block. Instance-method yield
and value-returning yield calls are future work. make test: 25.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 9e9aa474ec40
- Parent
- fc7e37e1cd43
src/analyze.cmodifiedView file
@@ -870,6 +870,15 @@
870870
return NULL;871871
}872872
873+
/* First YieldNode belonging to scope `si`, or -1. */874+
static int first_yield(Compiler *c, int si) {875+
for (int id = 0; id < c->nt->count; id++) {876+
const char *ty = nt_type(c->nt, id);877+
if (ty && !strcmp(ty, "YieldNode") && c->nscope[id] == si) return id;878+
}879+
return -1;880+
}881+
873882
/* Bind block parameter types for supported iteration methods. */874883
static int infer_block_params(Compiler *c) {875884
const NodeTable *nt = c->nt;@@ -881,7 +890,33 @@
881890
if (block < 0) continue;882891
const char *name = nt_str(nt, id, "name");883892
int recv = nt_ref(nt, id, "receiver");884-
if (!name || recv < 0) continue;893+
if (!name) continue;894+
895+
/* call to a user yielding method: block params take the yield arg types */896+
if (recv < 0) {897+
int mi = comp_method_index(c, name);898+
if (mi < 0) {899+
Scope *self = comp_scope_of(c, id);900+
if (self->class_id >= 0) mi = comp_method_in_chain(c, self->class_id, name, NULL);901+
}902+
if (mi >= 0 && c->scopes[mi].yields) {903+
int yn = first_yield(c, mi);904+
int ya = yn >= 0 ? nt_ref(nt, yn, "arguments") : -1;905+
int yc = 0;906+
const int *yargs = ya >= 0 ? nt_arr(nt, ya, "arguments", &yc) : NULL;907+
Scope *bs = comp_scope_of(c, block);908+
for (int k = 0; k < yc; k++) {909+
const char *bp = block_param_name(c, block, k);910+
if (!bp) continue;911+
LocalVar *lv = scope_local_intern(bs, bp); lv->is_block_param = 1;912+
TyKind m = ty_unify(lv->type, infer_type(c, yargs[k]));913+
if (m != lv->type) { lv->type = m; changed = 1; }914+
}915+
continue;916+
}917+
}918+
919+
if (recv < 0) continue;885920
TyKind rt = infer_type(c, recv);886921
const char *p0 = block_param_name(c, block, 0);887922
if (!p0) continue;@@ -976,6 +1011,18 @@
9761011
resolve_parents(c);9771012
inherit_members(c);9781013
1014+
/* mark block-aware methods (contain yield or block_given?) -- these are1015+
inlined at every call site so block_given? reflects the actual site */1016+
for (int id = 0; id < c->nt->count; id++) {1017+
const char *ty = nt_type(c->nt, id);1018+
if (!ty) continue;1019+
if (!strcmp(ty, "YieldNode")) comp_scope_of(c, id)->yields = 1;1020+
else if (!strcmp(ty, "CallNode") && nt_ref(c->nt, id, "receiver") < 0) {1021+
const char *nm = nt_str(c->nt, id, "name");1022+
if (nm && !strcmp(nm, "block_given?")) comp_scope_of(c, id)->yields = 1;1023+
}1024+
}1025+
9791026
/* intern every symbol literal so codegen can emit the id table */9801027
for (int id = 0; id < c->nt->count; id++) {9811028
const char *ty = nt_type(c->nt, id);src/codegen.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…