Commit d45b15a3

d45b15a3ec77Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · +1 added · ~1 modified

Description

feat: Symbol Phase 2 Step 3a-c - sym_int_hash type

Hash literals with all-symbol keys + int/bool/nil values now infer to sym_int_hash, where keys are sp_sym (mrb_int) compared with int equality. {a:1}[:a] returns the value while {a:1}["a"] no longer aliases to it (different key types).

Wired codegen sites: hash literal, [], has_key?/key?/include?/ member?, length/size/empty?/any?, fetch, []=, delete. Other hash methods (each, keys, values, merge, to_a, etc.) not yet implemented for sym_int_hash — no current test path needs them.

String-valued symbol-keyed hashes ({name: "Alice"}) still route to str_str_hash via the Step 2 sym_to_s bridge; sym_str_hash is deferred to Step 3d.

Add test/bm_sym_hash.rb verifying basic sym_int_hash semantics.

68/68 tests pass, bootstrap stable.

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

Details

Commit
d45b15a3ec77
Parent
29b26ea83ad5

spinel_codegen.rbmodifiedView file

@@ -1169,10 +1169,28 @@
11691169 end
11701170 k = k + 1
11711171 end
1172+ # Detect all-symbol keys → sym_int_hash variant for int-valued
1173+ # hashes. (sym_str_hash etc. not yet implemented; they fall
1174+ # through to str_str_hash with sym_to_s wrapping at hash sites.)
1175+ all_sym_keys = 1
1176+ kk = 0
1177+ while kk < elems.length
1178+ ekid = elems[kk]
1179+ if @nd_type[ekid] == "AssocNode"
1180+ kid = @nd_key[ekid]
1181+ if kid < 0 || @nd_type[kid] != "SymbolNode"
1182+ all_sym_keys = 0
1183+ end
1184+ end
1185+ kk = kk + 1
1186+ end
11721187 if all_same == 1
11731188 if first_vt == "string"
11741189 return "str_str_hash"
11751190 end
1191+ if all_sym_keys == 1 && (first_vt == "int" || first_vt == "bool" || first_vt == "nil")
1192+ return "sym_int_hash"
1193+ end
11761194 else
11771195 # Mixed value types - store as str_str_hash with auto-conversion
11781196 return "str_str_hash"
@@ -5997,6 +6015,8 @@
59976015 ht = infer_hash_val_type(nid)
59986016 if ht == "str_str_hash"
59996017 @needs_str_str_hash = 1
6018+ elsif ht == "sym_int_hash"
6019+ @needs_sym_int_hash = 1
60006020 else
60016021 @needs_str_int_hash = 1
60026022 end
@@ -13438,6 +13458,35 @@
1343813458
1343913459 def compile_hash_method_expr(nid, mname, rc, recv_type)
1344013460 # Hash methods
13461+ if recv_type == "sym_int_hash"
13462+ if mname == "[]"
13463+ return "sp_SymIntHash_get(" + rc + ", " + compile_arg0(nid) + ")"
13464+ end
13465+ if mname == "has_key?" || mname == "key?" || mname == "include?" || mname == "member?"
13466+ return "sp_SymIntHash_has_key(" + rc + ", " + compile_arg0(nid) + ")"
13467+ end
13468+ if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
13469+ return "sp_SymIntHash_length(" + rc + ")"
13470+ end
13471+ if mname == "empty?"
13472+ return "(sp_SymIntHash_length(" + rc + ") == 0)"
13473+ end
13474+ if mname == "any?" && @nd_block[nid] < 0
13475+ return "(sp_SymIntHash_length(" + rc + ") > 0)"
13476+ end
13477+ if mname == "fetch"
13478+ args_id = @nd_arguments[nid]
13479+ if args_id >= 0
13480+ aargs = get_args(args_id)
13481+ key = compile_expr(aargs[0])
13482+ if aargs.length >= 2
13483+ defval = compile_expr(aargs[1])
13484+ return "(sp_SymIntHash_has_key(" + rc + ", " + key + ") ? sp_SymIntHash_get(" + rc + ", " + key + ") : " + defval + ")"
13485+ end
13486+ return "sp_SymIntHash_get(" + rc + ", " + key + ")"
13487+ end
13488+ end
13489+ end
1344113490 if recv_type == "str_int_hash"
1344213491 if mname == "[]"
1344313492 return "sp_StrIntHash_get(" + rc + ", " + compile_str_arg0(nid) + ")"
@@ -14738,6 +14787,17 @@
1473814787 }
1473914788 return tmp
1474014789 end
14790+ if ht == "sym_int_hash"
14791+ @needs_sym_int_hash = 1
14792+ tmp = new_temp
14793+ emit(" sp_SymIntHash *" + tmp + " = sp_SymIntHash_new();")
14794+ elems.each { |el|
14795+ if @nd_type[el] == "AssocNode"
14796+ emit(" sp_SymIntHash_set(" + tmp + ", " + compile_expr(@nd_key[el]) + ", " + compile_expr(@nd_expression[el]) + ");")
14797+ end
14798+ }
14799+ return tmp
14800+ end
1474114801 @needs_str_int_hash = 1
1474214802 tmp = new_temp
1474314803 emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_new();")
@@ -15671,8 +15731,12 @@
1567115731 aargs = get_args(args_id)
1567215732 if aargs.length >= 2
1567315733 rc = compile_expr(recv)
15734+ val = compile_expr(aargs[1])
15735+ if rt == "sym_int_hash"
15736+ emit(" sp_SymIntHash_set(" + rc + ", " + compile_expr(aargs[0]) + ", " + val + ");")
15737+ return 1
15738+ end
1567415739 key = compile_expr_as_string(aargs[0])
15675- val = compile_expr(aargs[1])
1567615740 if rt == "str_int_hash"
1567715741 emit(" sp_StrIntHash_set(" + rc + ", " + key + ", " + val + ");")
1567815742 return 1
@@ -15691,6 +15755,10 @@
1569115755 if recv >= 0
1569215756 rt = infer_type(recv)
1569315757 rc = compile_expr(recv)
15758+ if rt == "sym_int_hash"
15759+ emit(" sp_SymIntHash_delete(" + rc + ", " + compile_arg0(nid) + ");")
15760+ return 1
15761+ end
1569415762 if rt == "str_int_hash"
1569515763 emit(" sp_StrIntHash_delete(" + rc + ", " + compile_str_arg0(nid) + ");")
1569615764 return 1
@@ -17410,6 +17478,10 @@
1741017478 if arg_ids.length >= 2
1741117479 val = compile_expr(arg_ids[1])
1741217480 end
17481+ if rt == "sym_int_hash"
17482+ emit(" sp_SymIntHash_set(" + rc + ", " + idx + ", " + val + ");")
17483+ return
17484+ end
1741317485 if rt == "int_array"
1741417486 # Check if value is an object pointer - needs cast
1741517487 vt = "int"