Commit b0ca8f7c
b0ca8f7c52abMerged
Ryan Seys <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
codegen: cover int_int and poly_poly in typed-hash each_key/each_value
The initial each_key arm dispatched through hash_iter_variant_info,
which omitted int_int_hash (a real variant, sp_IntIntHash) and
carried a dead int_poly_hash arm calling the non-existent
sp_IntPolyHash_get — int-keyed mixed-value hashes widen to
poly_poly_hash, so no int_poly variant exists. each_key also had
no poly_poly_hash walker, and each_value covered a different,
smaller set of variants than each_key.
- add
int_int_hashtohash_iter_variant_info; drop the deadint_poly_hasharm. - add a
poly_poly_hasharm toeach_keythat bindskeys[order[i]], mirroring the existingeach_valuepoly walk. - route
each_valuethrough the sharedhash_iter_variant_infohelper (byte-identical output for the variants it already handled), filling itssym_poly/str_poly/int_intgaps and removing a duplicated variant table.
Both methods now cover all nine real typed-hash variants. The expanded
test/hash_each_key_value.rb exercises every variant under both
methods, including object-valued and mixed-scalar poly cases.
make test -> 805 pass / 0 fail; make bootstrap -> 4-way fixpoint
holds.
Refs #1318
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- b0ca8f7c52ab
- Parent
- a787f55fe397
spinel_codegen.rbmodifiedView file
if t == "str_str_hash"; return ["string", "string", "const char *", "const char *", "sp_StrStrHash_get"]; end if t == "str_poly_hash"; return ["string", "poly", "const char *", "sp_RbVal", "sp_StrPolyHash_get"]; end if t == "int_str_hash"; return ["int", "string", "mrb_int", "const char *", "sp_IntStrHash_get"]; end if t == "int_poly_hash"; return ["int", "poly", "mrb_int", "sp_RbVal", "sp_IntPolyHash_get"]; end if t == "int_int_hash"; return ["int", "int", "mrb_int", "mrb_int", "sp_IntIntHash_get"]; end nil end end end # `hash.each_value { |v| ... }` for poly_poly_hash. Other hash # shapes can be added here once they surface; the underlying # representation is the same `vals[order[i]]` walk. # `hash.each_value { |v| ... }`. poly_poly_hash has its own # slot-based walk (`vals[order[i]]`); every other variant is # dispatched through hash_iter_variant_info below. if mname == "each_value" && recv >= 0 && @nd_block[nid] >= 0 rt_ev = infer_type(recv) if rt_ev == "poly_poly_hash" emit(" }") return 1 end # Issue #851: each_value on the common typed-hash variants. # The shape is the same: walk h->order[i] and dispatch the # variant's _get. Only int and string value parts here; the # poly variants flow through the poly_poly_hash arm above # already after boxing. pfx_ev = "" vty_ev = "" vtag_ev = "" if rt_ev == "sym_int_hash" pfx_ev = "SymIntHash"; vty_ev = "mrb_int"; vtag_ev = "int" @needs_sym_int_hash = 1 elsif rt_ev == "str_int_hash" pfx_ev = "StrIntHash"; vty_ev = "mrb_int"; vtag_ev = "int" @needs_str_int_hash = 1 elsif rt_ev == "int_str_hash" pfx_ev = "IntStrHash"; vty_ev = "const char *"; vtag_ev = "string" elsif rt_ev == "str_str_hash" pfx_ev = "StrStrHash"; vty_ev = "const char *"; vtag_ev = "string" elsif rt_ev == "sym_str_hash" pfx_ev = "SymStrHash"; vty_ev = "const char *"; vtag_ev = "string" end if pfx_ev != "" # each_value on the typed-hash variants, dispatched through the # shared hash_iter_variant_info helper (the same map that drives # each_key). Walk h->order[i] and read the value via the variant's # _get. Covers int, string, and poly value parts; poly_poly_hash is # handled by its own slot-based arm above. hv_ev2 = hash_iter_variant_info(rt_ev) if hv_ev2 != nil @needs_sym_int_hash = 1 if rt_ev == "sym_int_hash" @needs_str_int_hash = 1 if rt_ev == "str_int_hash" rc_ev2 = compile_expr_gc_rooted(recv) bp_ev2 = get_block_param(nid, 0) if bp_ev2 == "" end tmp_ev2 = new_temp emit(" for (mrb_int " + tmp_ev2 + " = 0; " + tmp_ev2 + " < " + rc_ev2 + "->len; " + tmp_ev2 + "++) {") emit(" " + vty_ev + " lv_" + bp_ev2 + " = sp_" + pfx_ev + "_get(" + rc_ev2 + ", " + rc_ev2 + "->order[" + tmp_ev2 + "]);") emit(" " + hv_ev2[3] + " lv_" + bp_ev2 + " = " + hv_ev2[4] + "(" + rc_ev2 + ", " + rc_ev2 + "->order[" + tmp_ev2 + "]);") @indent = @indent + 1 push_scope declare_var(bp_ev2, vtag_ev) declare_var(bp_ev2, hv_ev2[1]) compile_stmts_body(@nd_body[@nd_block[nid]]) pop_scope @indent = @indent - 1 emit(" }") return 1 end # poly_poly_hash each_key: the helper returns nil for it (keys and # values are sp_RbVal indexed by the slot in h->order). Mirrors the # each_value poly arm above but binds h->keys[order[i]]. if rt_ek == "poly_poly_hash" rc_pk = compile_expr_gc_rooted(recv) bp_pk = get_block_param(nid, 0) has_bp_pk = 1 if bp_pk == "" has_bp_pk = 0 bp_pk = "_k" end tmp_pk = new_temp emit(" for (mrb_int " + tmp_pk + " = 0; " + tmp_pk + " < " + rc_pk + "->len; " + tmp_pk + "++) {") if has_bp_pk == 1 emit(" sp_RbVal lv_" + bp_pk + " = " + rc_pk + "->keys[" + rc_pk + "->order[" + tmp_pk + "]];") end @indent = @indent + 1 push_scope if has_bp_pk == 1 declare_var(bp_pk, "poly") end redo_label_pk = push_redo_label emit_redo_label(redo_label_pk) compile_stmts_body(@nd_body[@nd_block[nid]]) pop_redo_label pop_scope @indent = @indent - 1 emit(" }") return 1 end end if mname == "each_slice"test/hash_each_key_value.rbmodifiedView file
Loading diff…
test/hash_each_key_value.rb.expectedmodifiedView file
Loading diff…