Commit 2efc0f49

2efc0f4970dcMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Force int return/params for bound-Method targets; coerce poly tail returns

The bound-Method dispatch ABI is mrb_int (*)(void *, mrb_int...): the call sites (sp_poly_arr_get_hash / sp_poly_slice) read the result as mrb_int and pass int args. A method(:sym) target that returned poly (e.g. PPU#peek_2002 returning the poly @io_latch) was called through the int cast, so its sp_RbVal struct return was misread as garbage (0) -- the CPU saw 0 at $2002 and never left the vblank-poll warmup, leaving every pixel at palette[0].

Pin such targets to int return (in addition to int params) so the body return is coerced, and teach the explicit/implicit return paths to wrap a poly value in sp_poly_to_i / sp_poly_to_f for a scalar return slot.

make optcarrot now passes: checksum 59662, pixel-identical to CRuby.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
2efc0f4970dc
Parent
a976593b14a7

src/analyze.cmodifiedView file

@@ -1260,13 +1260,7 @@
12601260 entry typed here flows into `@pads[0].poke(data)` -> Pad#poke). */
12611261 for (int s = 0; s < c->nscopes; s++) {
12621262 Scope *sc = &c->scopes[s];
1263- if (!sc->reachable || sc->nparams == 0 || !sc->name) continue;
1264- int has_unknown = 0;
1265- for (int i = 0; i < sc->nparams; i++) {
1266- LocalVar *p = sc->pnames[i] ? scope_local(sc, sc->pnames[i]) : NULL;
1267- if (p && p->type == TY_UNKNOWN) { has_unknown = 1; break; }
1268- }
1269- if (!has_unknown) continue;
1263+ if (!sc->reachable || !sc->name) continue;
12701264 int taken = 0;
12711265 for (int id = 0; id < c->nt->count && !taken; id++) {
12721266 const char *nty = nt_type(c->nt, id);
@@ -1277,11 +1271,18 @@
12771271 if (msym && !strcmp(msym, sc->name)) taken = 1;
12781272 }
12791273 if (taken) {
1274+ /* The bound-Method ABI is `mrb_int (*)(void *, mrb_int...)`: the dispatch
1275+ site (sp_poly_arr_get_hash / sp_poly_slice) reads the return as mrb_int
1276+ and passes int args. So a method(:sym) target MUST return int and take
1277+ int params -- a poly return (e.g. PPU#peek_2002 returning the poly
1278+ @io_latch) would be misread as a struct through the int cast and yield
1279+ garbage. Pin unknown params to int and a poly/unknown return to int
1280+ (codegen coerces the body's poly return via sp_poly_to_i). */
12801281 for (int i = 0; i < sc->nparams; i++) {
12811282 LocalVar *p = sc->pnames[i] ? scope_local(sc, sc->pnames[i]) : NULL;
12821283 if (p && p->type == TY_UNKNOWN) p->type = TY_INT;
12831284 }
1284- if (sc->ret == TY_UNKNOWN) sc->ret = TY_INT;
1285+ if (sc->ret == TY_UNKNOWN || sc->ret == TY_POLY) sc->ret = TY_INT;
12851286 }
12861287 }
12871288