Commit c631cf13

c631cf13a71fMerged

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

Description

Narrow a poly method-dispatch table double-index to int

optcarrot's CPU memory map is @fetch = [nil]*0x10000, each slot a bound method (peek_ram, ...) or an int array; fetch(addr) is @fetch[addr][addr]. spinelc compiled the inner [] as a generic poly index returning a boxed value, so fetch/peek16/store returned poly and the CPU's registers (@_pc, @addr, @clk, @_a/@_x/@_y, ...) all widened to poly -- the whole interpreter ran boxed (sp_poly_add/sp_poly_to_i everywhere). Legacy types these int.

Add the legacy poly_index_narrow_int rule: @table[i][j] where @table is a poly array whose element writes are all int-returning kinds (bound method, int array, int, or a poly assumed to be a dispatch entry) yields an int. Emit it via a new sp_poly_index_int runtime helper (calls a bound method on the int-ABI, gets an int-array element, etc., returning a raw mrb_int).

CPU fetch/peek16/store now return mrb_int and most CPU registers de-poly. optcarrot ~410 -> ~445 fps, checksum 59662. make test 845, make bench 57.

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

Details

Commit
c631cf13a71f
Parent
d1dbf6902bfa

lib/sp_runtime.hmodifiedView file

@@ -4176,6 +4176,23 @@
41764176 mrb_int i = (idx.tag == SP_TAG_INT) ? idx.v.i : 0;
41774177 return sp_poly_arr_get_hash(recv, i);
41784178 }
4179+
4180+/* Integer-returning counterpart of sp_poly_index_poly for `poly[int]` where
4181+ the poly element holds an int-returning callable/container -- a bound
4182+ method (called with the int arg, int ABI) or an int array. Used when
4183+ inference proves the double index `@table[i][j]` yields an int (a method
4184+ dispatch table). Falls back to coercing the generic poly result. */
4185+static inline mrb_int sp_poly_index_int(sp_RbVal a, mrb_int i) {
4186+ if (a.tag == SP_TAG_INT) return (a.v.i >> i) & 1;
4187+ if (a.tag == SP_TAG_OBJ) {
4188+ if (a.cls_id == SP_BUILTIN_METHOD) {
4189+ sp_BoundMethod *m = (sp_BoundMethod *)a.v.p;
4190+ return ((mrb_int (*)(void *, mrb_int))(uintptr_t)m->fn)((void *)m->self, i);
4191+ }
4192+ if (a.cls_id == SP_BUILTIN_INT_ARRAY) return sp_IntArray_get((sp_IntArray *)a.v.p, i);
4193+ }
4194+ return sp_poly_to_i(sp_poly_arr_get_hash(a, i));
4195+}
41794196 static sp_RbVal sp_poly_arr_set_hash(sp_RbVal v, mrb_int idx, sp_RbVal val) {
41804197 if (v.tag != SP_TAG_OBJ) return val;
41814198 switch (v.cls_id) {