Commit a62a1ad5
a62a1ad58dfaMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Implement self-recursive yield method lowering (is_lowered_yield)
Methods that both use implicit yield and call themselves recursively are lowered to an explicit &block (sp_Proc *) form: a synthetic yblk parameter replaces yield, and each yield site becomes sp_proc_call(lv___yblk__, ...). The lowering runs after compute_reachable so reachability is already set.
Return type: lowered methods return mrb_int (raw carrier). Call sites use per-call-site block return type (via method_call_ret) to cast the raw mrb_int to const char * (string blocks) or use it directly (int).
Details:
- analyze.c: detect pass moved after compute_reachable; sets is_lowered_yield=1, yields=0, ret=TY_INT, blk_param=yblk
- analyze.c: method_call_ret uses is_lowered_yield as a trigger to return the call site's block body type (per-call type dispatch)
- codegen.c: YieldNode in lowered body emits sp_proc_call without cast; emit_proc_literal force-captures yblk when body has yield; emit_scope_decls handles TY_PROC cell with uintptr_t cast
Fixes test/recursive_implicit_yield_value.rb (was ERR, now PASS).
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- a62a1ad58dfa
- Parent
- 0f87d407fae9
src/analyze.cmodifiedView file
static TyKind method_call_ret(Compiler *c, int mi, int call_id) { int last = scope_body_last(c, mi); int is_yield = last >= 0 && nt_type(c->nt, last) && !strcmp(nt_type(c->nt, last), "YieldNode"); if (is_yield || is_blk_param_call(c, last, mi)) { /* Lowered yield methods (self-recursive + yield) carry the block's return value: return the per-call-site block body type so puts/assign use the right type. */ if (c->scopes[mi].is_lowered_yield || is_yield || is_blk_param_call(c, last, mi)) { int blk = nt_ref(c->nt, call_id, "block"); if (blk >= 0) { int bbody = nt_ref(c->nt, blk, "body"); (e.g. a never-called method with an uninferrable param). */ compute_reachable(c); /* Lower self-recursive yield methods: methods that use `yield` AND call themselves recursively. Their implicit block is forwarded as a synthetic __yblk__ sp_Proc * parameter, so the method is emitted (yields=0) and each `yield` in its body calls sp_proc_call(__yblk__, ...). */ for (int mi = 1; mi < c->nscopes; mi++) { Scope *m = &c->scopes[mi]; if (!m->name || !m->reachable || m->blk_param) continue; if (!m->yields) continue; if (m->body < 0) continue; int has_yld = 0; for (int id = 0; id < c->nt->count && !has_yld; id++) { if (c->nscope[id] != mi) continue; const char *ty = nt_type(c->nt, id); if (ty && !strcmp(ty, "YieldNode")) has_yld = 1; } if (!has_yld) continue; int has_self_call = 0; for (int id = 0; id < c->nt->count && !has_self_call; id++) { if (c->nscope[id] != mi) continue; const char *ty = nt_type(c->nt, id); if (!ty || strcmp(ty, "CallNode")) continue; const char *nm = nt_str(c->nt, id, "name"); if (!nm || strcmp(nm, m->name)) continue; int recv = nt_ref(c->nt, id, "receiver"); const char *rty = recv >= 0 ? nt_type(c->nt, recv) : NULL; if (recv < 0 || (rty && !strcmp(rty, "SelfNode"))) has_self_call = 1; } if (!has_self_call) continue; m->is_lowered_yield = 1; m->yields = 0; m->ret = TY_INT; m->blk_param = strdup("__yblk__"); LocalVar *yblk = scope_local_intern(m, "__yblk__"); if (yblk) { yblk->type = TY_PROC; yblk->is_param = 1; yblk->is_cell = 1; } } /* Post-fixpoint: propagate include-copy param types back to the source scope so the final infer_type scan (which uses comp_scope_of, mapping body nodes to the ORIGINAL scope) sees the correctly-typed params.src/codegen.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…