Commit 43648e5d
43648e5dee3dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Add M4: block iteration as inline loops
Compile the common non-escaping block forms to inline C for-loops (the way the legacy backend lowers them), no closures:
- analyze: infer block-parameter types for n.times / array.each / range.each / n.upto / n.downto / step (and map/select param binding), folded into the analysis fixpoint via block_param_name.
- codegen: emit_iteration_stmt lowers n.times, array.each, range.each ((a..b)/(a...b) inclusive/exclusive), and n.upto/downto to for-loops, evaluating the receiver once and binding the block param each turn.
times/each/range-each/upto/downto programs match CRuby exactly. make test: 4 pass (the remaining iteration tests also need hashes, symbols,
scalar receiver methods, or classes). The one FAIL is int_overflow_promote
(needs --int-overflow=promote, deferred).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 43648e5dee3d
- Parent
- 108810da9006
src/analyze.cmodifiedView file
@@ -292,6 +292,55 @@
292292
return changed;293293
}294294
295+
/* Name of a block's idx-th required parameter, or NULL. */296+
const char *block_param_name(Compiler *c, int block, int idx) {297+
int bp = nt_ref(c->nt, block, "parameters"); /* BlockParametersNode */298+
if (bp < 0) return NULL;299+
int pn = nt_ref(c->nt, bp, "parameters"); /* ParametersNode */300+
if (pn < 0) return NULL;301+
int n = 0;302+
const int *reqs = nt_arr(c->nt, pn, "requireds", &n);303+
if (idx < n) return nt_str(c->nt, reqs[idx], "name");304+
return NULL;305+
}306+
307+
/* Bind block parameter types for supported iteration methods. */308+
static int infer_block_params(Compiler *c) {309+
const NodeTable *nt = c->nt;310+
int changed = 0;311+
for (int id = 0; id < nt->count; id++) {312+
const char *ty = nt_type(nt, id);313+
if (!ty || strcmp(ty, "CallNode")) continue;314+
int block = nt_ref(nt, id, "block");315+
if (block < 0) continue;316+
const char *name = nt_str(nt, id, "name");317+
int recv = nt_ref(nt, id, "receiver");318+
if (!name || recv < 0) continue;319+
TyKind rt = infer_type(c, recv);320+
const char *p0 = block_param_name(c, block, 0);321+
if (!p0) continue;322+
323+
TyKind pt = TY_UNKNOWN;324+
if ((!strcmp(name, "times") || !strcmp(name, "upto") ||325+
!strcmp(name, "downto") || !strcmp(name, "step")) && rt == TY_INT)326+
pt = TY_INT;327+
else if (!strcmp(name, "each") && rt == TY_RANGE)328+
pt = TY_INT;329+
else if ((!strcmp(name, "each") || !strcmp(name, "map") ||330+
!strcmp(name, "select") || !strcmp(name, "reject") ||331+
!strcmp(name, "find") || !strcmp(name, "each_with_index")) &&332+
ty_is_array(rt))333+
pt = ty_array_elem(rt);334+
335+
if (pt == TY_UNKNOWN) continue;336+
Scope *s = comp_scope_of(c, block);337+
LocalVar *lv = scope_local_intern(s, p0);338+
TyKind merged = ty_unify(lv->type, pt);339+
if (merged != lv->type) { lv->type = merged; changed = 1; }340+
}341+
return changed;342+
}343+
295344
/* Value type of an explicit `return expr` (or nil for bare return). */296345
static TyKind return_node_type(Compiler *c, int id) {297346
int args = nt_ref(c->nt, id, "arguments");@@ -331,6 +380,7 @@
331380
int ch = 0;332381
ch |= infer_write_types(c);333382
ch |= infer_param_types(c);383+
ch |= infer_block_params(c);334384
ch |= infer_return_types(c);335385
if (!ch) break;336386
}src/analyze.hmodifiedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…