Commit eb66995f
eb66995fd0cdMerged
Yusuke Endoh <[email protected]> · 6 months ago · 1 file · ~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
- eb66995fd0cd
- Parent
- ef05cb1aa590
spinel_codegen.rbmodifiedView file
i = i + 1 end # Built-in type dispatch (cls_id < 0). emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, tmp, is_poly_ret) emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, arg_types, tmp, is_poly_ret) emit(" }") tmp end # Emit branches for the built-in (negative cls_id) entries. Each # entry maps a (SP_BUILTIN_*, method) pair to a C expression. # Adding a new built-in type means one more `if` branch here. def emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, result_tmp, is_poly_ret) def emit_poly_builtin_dispatch(recv_tmp, mname, arg_compiled, arg_types, result_tmp, is_poly_ret) a0 = "" if arg_compiled.length > 0 a0 = arg_compiled[0] # Unbox if poly — every built-in `[]` arm here passes a0 to a # function expecting `mrb_int` (sp_IntArray_get etc., or the # Method fn_ptr's int parameter). When the caller's arg is # poly (e.g. `def fetch(addr); @fetch[addr][addr]; end` with # addr widened), we must unbox before the C call. if arg_types.length > 0 && arg_types[0] == "poly" a0 = "(" + a0 + ").v.i" end end # `[]` — element types differ per built-in. When the result temp # is sp_RbVal (poly return), every branch can box into it. When