Commit 1be30648

1be306489136Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

codegen: block-form Hash#select/reject/count/any?/all? for all 8 variants

Block-dispatch for select/reject/filter/count/any?/all? was only wired up for str_int_hash and str_str_hash; the other 6 variants (sym_int, sym_str, int_str, str_poly, sym_poly, poly_poly) hit "cannot resolve call to ''" and emitted 0.

Generalize compile_hash_select_reject and compile_hash_block_predicate behind a hash_variant_info table that names the ctor/getter/setter, key/value type labels, and key iteration expression per variant (poly_poly_hash needs h->keys[h->order[i]] since keys are sp_RbVal).

Block params are now re-declared scope-locally inside each loop body with the variant's key/value C type, so successive loops in the same function with the same Ruby block-param name no longer fight the function-level decl that picked one type.

find/detect stays limited to string-keyed variants because the analyzer commits the return type to const char *.

Fixes #802

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
1be306489136
Parent
7743f25b4508

spinel_codegen.rbmodifiedView file

@@ -21051,6 +21051,15 @@
2105121051 if mname == "any?" && @nd_block[nid] < 0
2105221052 return "(sp_SymIntHash_length(" + rc + ") > 0)"
2105321053 end
21054+ # Issue #802: block-form filters across all 8 hash variants. The
21055+ # select/reject path returns the same variant; count/any?/all?
21056+ # return mrb_int / mrb_bool.
21057+ if (mname == "select" || mname == "filter" || mname == "reject") && @nd_block[nid] >= 0
21058+ return compile_hash_select_reject(nid, "sym_int_hash", rc, mname == "filter" ? "select" : mname)
21059+ end
21060+ if (mname == "count" || mname == "any?" || mname == "all?") && @nd_block[nid] >= 0
21061+ return compile_hash_block_predicate(nid, "sym_int_hash", rc, mname)
21062+ end
2105421063 if mname == "fetch"
2105521064 args_id = @nd_arguments[nid]
2105621065 if args_id >= 0
@@ -21245,6 +21254,12 @@
2124521254 if mname == "empty?"
2124621255 return "(sp_SymStrHash_length(" + rc + ") == 0)"
2124721256 end
21257+ if (mname == "select" || mname == "filter" || mname == "reject") && @nd_block[nid] >= 0
21258+ return compile_hash_select_reject(nid, "sym_str_hash", rc, mname == "filter" ? "select" : mname)
21259+ end
21260+ if (mname == "count" || mname == "any?" || mname == "all?") && @nd_block[nid] >= 0
21261+ return compile_hash_block_predicate(nid, "sym_str_hash", rc, mname)
21262+ end
2124821263 if mname == "any?" && @nd_block[nid] < 0
2124921264 return "(sp_SymStrHash_length(" + rc + ") > 0)"
2125021265 end
@@ -21337,6 +21352,12 @@
2133721352 if mname == "any?" && @nd_block[nid] < 0
2133821353 return "(sp_SymPolyHash_length(" + rc + ") > 0)"
2133921354 end
21355+ if (mname == "select" || mname == "filter" || mname == "reject") && @nd_block[nid] >= 0
21356+ return compile_hash_select_reject(nid, "sym_poly_hash", rc, mname == "filter" ? "select" : mname)
21357+ end
21358+ if (mname == "count" || mname == "any?" || mname == "all?") && @nd_block[nid] >= 0
21359+ return compile_hash_block_predicate(nid, "sym_poly_hash", rc, mname)
21360+ end
2134021361 if mname == "keys"
2134121362 @needs_int_array = 1
2134221363 return "sp_SymPolyHash_keys(" + rc + ")"
@@ -21460,6 +21481,12 @@
2146021481 if mname == "any?" && @nd_block[nid] < 0
2146121482 return "(sp_StrPolyHash_length(" + rc + ") > 0)"
2146221483 end
21484+ if (mname == "select" || mname == "filter" || mname == "reject") && @nd_block[nid] >= 0
21485+ return compile_hash_select_reject(nid, "str_poly_hash", rc, mname == "filter" ? "select" : mname)
21486+ end
21487+ if (mname == "count" || mname == "any?" || mname == "all?" || mname == "find" || mname == "detect") && @nd_block[nid] >= 0
21488+ return compile_hash_block_predicate(nid, "str_poly_hash", rc, mname)
21489+ end
2146321490 if mname == "keys"
2146421491 return "sp_StrPolyHash_keys(" + rc + ")"
2146521492 end
@@ -21564,6 +21591,12 @@
2156421591 if mname == "any?" && @nd_block[nid] < 0
2156521592 return "(sp_PolyPolyHash_length(" + rc + ") > 0)"
2156621593 end
21594+ if (mname == "select" || mname == "filter" || mname == "reject") && @nd_block[nid] >= 0
21595+ return compile_hash_select_reject(nid, "poly_poly_hash", rc, mname == "filter" ? "select" : mname)
21596+ end
21597+ if (mname == "count" || mname == "any?" || mname == "all?") && @nd_block[nid] >= 0
21598+ return compile_hash_block_predicate(nid, "poly_poly_hash", rc, mname)
21599+ end
2156721600 if mname == "keys"
2156821601 @needs_poly_array = 1
2156921602 @needs_rb_value = 1
@@ -21802,6 +21835,12 @@
2180221835 if mname == "any?" && @nd_block[nid] < 0
2180321836 return "(sp_IntStrHash_length(" + rc + ") > 0)"
2180421837 end
21838+ if (mname == "select" || mname == "filter" || mname == "reject") && @nd_block[nid] >= 0
21839+ return compile_hash_select_reject(nid, "int_str_hash", rc, mname == "filter" ? "select" : mname)
21840+ end
21841+ if (mname == "count" || mname == "any?" || mname == "all?") && @nd_block[nid] >= 0
21842+ return compile_hash_block_predicate(nid, "int_str_hash", rc, mname)
21843+ end
2180521844 if mname == "keys"
2180621845 @needs_int_array = 1
2180721846 return "sp_IntStrHash_keys(" + rc + ")"
@@ -36887,8 +36926,67 @@
3688736926 tmp_res
3688836927 end
3688936928
36929+ # Per-variant lookup: returns a dispatch struct describing how to
36930+ # iterate a hash, read keys/values, declare block-param locals, and
36931+ # build a same-type result. Index [0]=ctor, [1]=getter, [2]=setter,
36932+ # [3]=key_type_label, [4]=val_type_label, [5]=key_iter_expr_fmt
36933+ # (where %RC% is the receiver and %I% the index var).
36934+ # Issue #802.
36935+ def hash_variant_info(ht)
36936+ if ht == "str_int_hash"
36937+ @needs_str_int_hash = 1
36938+ return ["sp_StrIntHash_new", "sp_StrIntHash_get", "sp_StrIntHash_set", "string", "int", "%RC%->order[%I%]"]
36939+ end
36940+ if ht == "str_str_hash"
36941+ @needs_str_str_hash = 1
36942+ return ["sp_StrStrHash_new", "sp_StrStrHash_get", "sp_StrStrHash_set", "string", "string", "%RC%->order[%I%]"]
36943+ end
36944+ if ht == "sym_int_hash"
36945+ @needs_sym_int_hash = 1
36946+ return ["sp_SymIntHash_new", "sp_SymIntHash_get", "sp_SymIntHash_set", "symbol", "int", "%RC%->order[%I%]"]
36947+ end
36948+ if ht == "sym_str_hash"
36949+ @needs_sym_str_hash = 1
36950+ return ["sp_SymStrHash_new", "sp_SymStrHash_get", "sp_SymStrHash_set", "symbol", "string", "%RC%->order[%I%]"]
36951+ end
36952+ if ht == "int_str_hash"
36953+ @needs_int_str_hash = 1
36954+ return ["sp_IntStrHash_new", "sp_IntStrHash_get", "sp_IntStrHash_set", "int", "string", "%RC%->order[%I%]"]
36955+ end
36956+ if ht == "str_poly_hash"
36957+ @needs_str_poly_hash = 1
36958+ @needs_rb_value = 1
36959+ return ["sp_StrPolyHash_new", "sp_StrPolyHash_get", "sp_StrPolyHash_set", "string", "poly", "%RC%->order[%I%]"]
36960+ end
36961+ if ht == "sym_poly_hash"
36962+ @needs_rb_value = 1
36963+ return ["sp_SymPolyHash_new", "sp_SymPolyHash_get", "sp_SymPolyHash_set", "symbol", "poly", "%RC%->order[%I%]"]
36964+ end
36965+ if ht == "poly_poly_hash"
36966+ @needs_poly_poly_hash = 1
36967+ @needs_rb_value = 1
36968+ return ["sp_PolyPolyHash_new", "sp_PolyPolyHash_get", "sp_PolyPolyHash_set", "poly", "poly", "%RC%->keys[%RC%->order[%I%]]"]
36969+ end
36970+ nil
36971+ end
36972+
36973+ def hash_key_iter_expr(info, rc, itmp)
36974+ fmt = info[5]
36975+ fmt = fmt.gsub("%RC%", rc)
36976+ fmt = fmt.gsub("%I%", itmp)
36977+ fmt
36978+ end
36979+
3689036980 def compile_hash_select_reject(nid, hash_type, rc, mname)
36891- # Build a new hash by filtering entries using the block
36981+ # Build a new hash by filtering entries using the block. Block
36982+ # params (lv_<k>, lv_<v>) are re-declared scope-locally inside
36983+ # the for body so the C type matches this variant's key/value
36984+ # ABI even when the same Ruby block-param name was used earlier
36985+ # at a different variant (the function-level decl picks one).
36986+ info = hash_variant_info(hash_type)
36987+ if info.nil?
36988+ return "0"
36989+ end
3689236990 bp1 = get_block_param(nid, 0)
3689336991 bp2 = get_block_param(nid, 1)
3689436992 if bp1 == ""
@@ -36896,33 +36994,22 @@
3689636994 end
3689736995 if bp2 == ""
3689836996 bp2 = "_v"
36899- end
36900- ctor = ""
36901- getter = ""
36902- setter = ""
36903- val_type = ""
36904- if hash_type == "str_int_hash"
36905- ctor = "sp_StrIntHash_new"
36906- getter = "sp_StrIntHash_get"
36907- setter = "sp_StrIntHash_set"
36908- val_type = "int"
36909- @needs_str_int_hash = 1
36910- else
36911- ctor = "sp_StrStrHash_new"
36912- getter = "sp_StrStrHash_get"
36913- setter = "sp_StrStrHash_set"
36914- val_type = "string"
36915- @needs_str_str_hash = 1
3691636997 end
36998+ ctor = info[0]
36999+ getter = info[1]
37000+ setter = info[2]
37001+ key_type = info[3]
37002+ val_type = info[4]
3691737003 @needs_gc = 1
3691837004 tmp = new_temp
3691937005 itmp = new_temp
3692037006 emit(" " + c_type(hash_type) + tmp + " = " + ctor + "();")
3692137007 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")
36922- emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")
36923- emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
37008+ key_expr = hash_key_iter_expr(info, rc, itmp)
37009+ emit(" " + c_type(key_type) + " lv_" + bp1 + " = " + key_expr + ";")
37010+ emit(" " + c_type(val_type) + " lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
3692437011 push_scope
36925- declare_var(bp1, "string")
37012+ declare_var(bp1, key_type)
3692637013 declare_var(bp2, val_type)
3692737014 blk = @nd_block[nid]
3692837015 bbody = @nd_body[blk]
@@ -36930,7 +37017,6 @@
3693037017 if bbody >= 0
3693137018 bs = get_stmts(bbody)
3693237019 if bs.length > 0
36933- # Emit all but last as stmts
3693437020 k = 0
3693537021 while k < bs.length - 1
3693637022 compile_stmt(bs[k])
@@ -36950,6 +37036,10 @@
3695037036 end
3695137037
3695237038 def compile_hash_block_predicate(nid, hash_type, rc, mname)
37039+ info = hash_variant_info(hash_type)
37040+ if info.nil?
37041+ return "0"
37042+ end
3695337043 bp1 = get_block_param(nid, 0)
3695437044 bp2 = get_block_param(nid, 1)
3695537045 if bp1 == ""
@@ -36958,38 +37048,23 @@
3695837048 if bp2 == ""
3695937049 bp2 = "_v"
3696037050 end
36961- val_type = "int"
36962- getter = "sp_StrIntHash_get"
36963- if hash_type == "str_str_hash"
36964- val_type = "string"
36965- getter = "sp_StrStrHash_get"
36966- end
37051+ getter = info[1]
37052+ key_type = info[3]
37053+ val_type = info[4]
3696737054 push_scope
36968- declare_var(bp1, "string")
37055+ declare_var(bp1, key_type)
3696937056 declare_var(bp2, val_type)
3697037057 itmp = new_temp
36971- # Compile block expression
37058+ key_expr = hash_key_iter_expr(info, rc, itmp)
3697237059 blk = @nd_block[nid]
3697337060 bbody = @nd_body[blk]
3697437061 bexpr = "0"
36975- blk_stmts = "".split(",", -1)
36976- if bbody >= 0
36977- bs = get_stmts(bbody)
36978- if bs.length > 0
36979- k = 0
36980- while k < bs.length - 1
36981- blk_stmts.push(bs[k].to_s)
36982- k = k + 1
36983- end
36984- bexpr = "PLACEHOLDER"
36985- end
36986- end
3698737062 if mname == "count"
3698837063 tmp_c = new_temp
3698937064 emit(" mrb_int " + tmp_c + " = 0;")
3699037065 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")
36991- emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")
36992- emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
37066+ emit(" " + c_type(key_type) + " lv_" + bp1 + " = " + key_expr + ";")
37067+ emit(" " + c_type(val_type) + " lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
3699337068 if bbody >= 0
3699437069 bs = get_stmts(bbody)
3699537070 k = 0
@@ -37010,8 +37085,8 @@
3701037085 tmp_r = new_temp
3701137086 emit(" mrb_bool " + tmp_r + " = FALSE;")
3701237087 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")
37013- emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")
37014- emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
37088+ emit(" " + c_type(key_type) + " lv_" + bp1 + " = " + key_expr + ";")
37089+ emit(" " + c_type(val_type) + " lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
3701537090 if bbody >= 0
3701637091 bs = get_stmts(bbody)
3701737092 k = 0
@@ -37032,8 +37107,8 @@
3703237107 tmp_r = new_temp
3703337108 emit(" mrb_bool " + tmp_r + " = TRUE;")
3703437109 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")
37035- emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")
37036- emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
37110+ emit(" " + c_type(key_type) + " lv_" + bp1 + " = " + key_expr + ";")
37111+ emit(" " + c_type(val_type) + " lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
3703737112 if bbody >= 0
3703837113 bs = get_stmts(bbody)
3703937114 k = 0
@@ -37050,13 +37125,21 @@
3705037125 pop_scope
3705137126 return tmp_r
3705237127 end
37053- # find / detect — return key of first match
37128+ # find / detect -- key-only result (CRuby returns [k,v] array;
37129+ # spinel emits the key for the str variants today, keep that
37130+ # shape for consistency. Restricted to string-keyed variants
37131+ # because the return type is currently committed to const char *
37132+ # by the analyzer.
3705437133 if mname == "find" || mname == "detect"
37134+ if key_type != "string"
37135+ pop_scope
37136+ return "0"
37137+ end
3705537138 tmp_r = new_temp
3705637139 emit(" const char *" + tmp_r + " = \"\";")
3705737140 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")
37058- emit(" lv_" + bp1 + " = " + rc + "->order[" + itmp + "];")
37059- emit(" lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
37141+ emit(" " + c_type(key_type) + " lv_" + bp1 + " = " + key_expr + ";")
37142+ emit(" " + c_type(val_type) + " lv_" + bp2 + " = " + getter + "(" + rc + ", lv_" + bp1 + ");")
3706037143 if bbody >= 0
3706137144 bs = get_stmts(bbody)
3706237145 k = 0