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_hash to hash_iter_variant_info; drop the dead int_poly_hash arm.
  • add a poly_poly_hash arm to each_key that binds keys[order[i]], mirroring the existing each_value poly walk.
  • route each_value through the shared hash_iter_variant_info helper (byte-identical output for the variants it already handled), filling its sym_poly / str_poly / int_int gaps 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

@@ -5960,7 +5960,7 @@
59605960 if t == "str_str_hash"; return ["string", "string", "const char *", "const char *", "sp_StrStrHash_get"]; end
59615961 if t == "str_poly_hash"; return ["string", "poly", "const char *", "sp_RbVal", "sp_StrPolyHash_get"]; end
59625962 if t == "int_str_hash"; return ["int", "string", "mrb_int", "const char *", "sp_IntStrHash_get"]; end
5963- if t == "int_poly_hash"; return ["int", "poly", "mrb_int", "sp_RbVal", "sp_IntPolyHash_get"]; end
5963+ if t == "int_int_hash"; return ["int", "int", "mrb_int", "mrb_int", "sp_IntIntHash_get"]; end
59645964 nil
59655965 end
59665966
@@ -39721,9 +39721,9 @@
3972139721 end
3972239722 end
3972339723
39724- # `hash.each_value { |v| ... }` for poly_poly_hash. Other hash
39725- # shapes can be added here once they surface; the underlying
39726- # representation is the same `vals[order[i]]` walk.
39724+ # `hash.each_value { |v| ... }`. poly_poly_hash has its own
39725+ # slot-based walk (`vals[order[i]]`); every other variant is
39726+ # dispatched through hash_iter_variant_info below.
3972739727 if mname == "each_value" && recv >= 0 && @nd_block[nid] >= 0
3972839728 rt_ev = infer_type(recv)
3972939729 if rt_ev == "poly_poly_hash"
@@ -39753,28 +39753,15 @@
3975339753 emit(" }")
3975439754 return 1
3975539755 end
39756- # Issue #851: each_value on the common typed-hash variants.
39757- # The shape is the same: walk h->order[i] and dispatch the
39758- # variant's _get. Only int and string value parts here; the
39759- # poly variants flow through the poly_poly_hash arm above
39760- # already after boxing.
39761- pfx_ev = ""
39762- vty_ev = ""
39763- vtag_ev = ""
39764- if rt_ev == "sym_int_hash"
39765- pfx_ev = "SymIntHash"; vty_ev = "mrb_int"; vtag_ev = "int"
39766- @needs_sym_int_hash = 1
39767- elsif rt_ev == "str_int_hash"
39768- pfx_ev = "StrIntHash"; vty_ev = "mrb_int"; vtag_ev = "int"
39769- @needs_str_int_hash = 1
39770- elsif rt_ev == "int_str_hash"
39771- pfx_ev = "IntStrHash"; vty_ev = "const char *"; vtag_ev = "string"
39772- elsif rt_ev == "str_str_hash"
39773- pfx_ev = "StrStrHash"; vty_ev = "const char *"; vtag_ev = "string"
39774- elsif rt_ev == "sym_str_hash"
39775- pfx_ev = "SymStrHash"; vty_ev = "const char *"; vtag_ev = "string"
39776- end
39777- if pfx_ev != ""
39756+ # each_value on the typed-hash variants, dispatched through the
39757+ # shared hash_iter_variant_info helper (the same map that drives
39758+ # each_key). Walk h->order[i] and read the value via the variant's
39759+ # _get. Covers int, string, and poly value parts; poly_poly_hash is
39760+ # handled by its own slot-based arm above.
39761+ hv_ev2 = hash_iter_variant_info(rt_ev)
39762+ if hv_ev2 != nil
39763+ @needs_sym_int_hash = 1 if rt_ev == "sym_int_hash"
39764+ @needs_str_int_hash = 1 if rt_ev == "str_int_hash"
3977839765 rc_ev2 = compile_expr_gc_rooted(recv)
3977939766 bp_ev2 = get_block_param(nid, 0)
3978039767 if bp_ev2 == ""
@@ -39782,10 +39769,10 @@
3978239769 end
3978339770 tmp_ev2 = new_temp
3978439771 emit(" for (mrb_int " + tmp_ev2 + " = 0; " + tmp_ev2 + " < " + rc_ev2 + "->len; " + tmp_ev2 + "++) {")
39785- emit(" " + vty_ev + " lv_" + bp_ev2 + " = sp_" + pfx_ev + "_get(" + rc_ev2 + ", " + rc_ev2 + "->order[" + tmp_ev2 + "]);")
39772+ emit(" " + hv_ev2[3] + " lv_" + bp_ev2 + " = " + hv_ev2[4] + "(" + rc_ev2 + ", " + rc_ev2 + "->order[" + tmp_ev2 + "]);")
3978639773 @indent = @indent + 1
3978739774 push_scope
39788- declare_var(bp_ev2, vtag_ev)
39775+ declare_var(bp_ev2, hv_ev2[1])
3978939776 compile_stmts_body(@nd_body[@nd_block[nid]])
3979039777 pop_scope
3979139778 @indent = @indent - 1
@@ -39832,6 +39819,36 @@
3983239819 emit(" }")
3983339820 return 1
3983439821 end
39822+ # poly_poly_hash each_key: the helper returns nil for it (keys and
39823+ # values are sp_RbVal indexed by the slot in h->order). Mirrors the
39824+ # each_value poly arm above but binds h->keys[order[i]].
39825+ if rt_ek == "poly_poly_hash"
39826+ rc_pk = compile_expr_gc_rooted(recv)
39827+ bp_pk = get_block_param(nid, 0)
39828+ has_bp_pk = 1
39829+ if bp_pk == ""
39830+ has_bp_pk = 0
39831+ bp_pk = "_k"
39832+ end
39833+ tmp_pk = new_temp
39834+ emit(" for (mrb_int " + tmp_pk + " = 0; " + tmp_pk + " < " + rc_pk + "->len; " + tmp_pk + "++) {")
39835+ if has_bp_pk == 1
39836+ emit(" sp_RbVal lv_" + bp_pk + " = " + rc_pk + "->keys[" + rc_pk + "->order[" + tmp_pk + "]];")
39837+ end
39838+ @indent = @indent + 1
39839+ push_scope
39840+ if has_bp_pk == 1
39841+ declare_var(bp_pk, "poly")
39842+ end
39843+ redo_label_pk = push_redo_label
39844+ emit_redo_label(redo_label_pk)
39845+ compile_stmts_body(@nd_body[@nd_block[nid]])
39846+ pop_redo_label
39847+ pop_scope
39848+ @indent = @indent - 1
39849+ emit(" }")
39850+ return 1
39851+ end
3983539852 end
3983639853
3983739854 if mname == "each_slice"