Commit 25bec858

25bec8585625Merged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): unbox poly index arg in poly_array []/Method dispatch

emit_poly_builtin_dispatch referenced an arg_types local that was out of scope (only the caller emit_poly_dispatch defines it). Spinel's bootstrapped codegen silently emitted 0 for the unknown local — the build went green but the unbox guards in the dispatch arms never fired.

Concretely, def fetch(addr); @fetch[addr][addr]; end with addr inferred as poly produced:

sp_IntArray_get((sp_IntArray *)_t.v.p, lv_addr)

passing sp_RbVal where mrb_int was expected. The 1-arg sp_IntArray/Flt/Str/Sym/Ptr/PolyArray/Method arms all hit this; the 2-arg Method arm tried to unbox a1 but arg_types.length evaluated to 0 so the guard was a no-op.

Fix: thread arg_types through to emit_poly_builtin_dispatch and unbox a0 once at the top when poly. Optcarrot's @fetch[addr][addr] site (the natural CPU dispatch pattern) now type-checks; spinel-side compile errors drop 9676 → 1023.

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

Details

Commit
25bec8585625
Parent
ef05cb1aa590

spinel_codegen.rbmodifiedView file

@@ -24782,7 +24782,7 @@
2478224782 i = i + 1
2478324783 end
2478424784 # Built-in type dispatch (cls_id < 0).
24785- emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, tmp, is_poly_ret)
24785+ emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, arg_types, tmp, is_poly_ret)
2478624786 emit(" }")
2478724787 tmp
2478824788 end
@@ -24790,10 +24790,18 @@
2479024790 # Emit branches for the built-in (negative cls_id) entries. Each
2479124791 # entry maps a (SP_BUILTIN_*, method) pair to a C expression.
2479224792 # Adding a new built-in type means one more `if` branch here.
24793- def emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, result_tmp, is_poly_ret)
24793+ def emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, arg_types, result_tmp, is_poly_ret)
2479424794 a0 = ""
2479524795 if arg_compiled.length > 0
2479624796 a0 = arg_compiled[0]
24797+ # Unbox if poly — every built-in `[]` arm here passes a0 to a
24798+ # function expecting `mrb_int` (sp_IntArray_get etc., or the
24799+ # Method fn_ptr's int parameter). When the caller's arg is
24800+ # poly (e.g. `def fetch(addr); @fetch[addr][addr]; end` with
24801+ # addr widened), we must unbox before the C call.
24802+ if arg_types.length > 0 && arg_types[0] == "poly"
24803+ a0 = "(" + a0 + ").v.i"
24804+ end
2479724805 end
2479824806 # `[]` — element types differ per built-in. When the result temp
2479924807 # is sp_RbVal (poly return), every branch can box into it. When