Commit 068be916
068be916339cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: Symbol Phase 2 Step 2 - infer SymbolNode as "symbol" type
- infer_type(SymbolNode) → "symbol" (was "string")
- c_type("symbol") = "sp_sym", c_default_val = ((sp_sym)-1)
- SymbolNode compiles to SPS_ or ((sp_sym))
- Add compile_expr_as_string / compile_str_arg0 for the Symbol→String bridge at API boundaries (catch/throw tag, str_*_hash key/value lookups, hash literal keys, []=)
- compile_puts / compile_print: emit sp_sym_to_s for symbol args
67/67 tests pass, bootstrap stable (gen2 == gen3), benchmarks unchanged from baseline (47 pass, 3 fail, 3 err).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 068be916339c
- Parent
- 4f9818cce612
spinel_codegen.rbmodifiedView file
@@ -827,7 +827,7 @@
827827
return "string"828828
end829829
if t == "SymbolNode"830-
return "string"830+
return "symbol"831831
end832832
if t == "NumberedReferenceReadNode"833833
return "string"@@ -2813,6 +2813,9 @@
28132813
if t == "string"28142814
return "const char *"28152815
end2816+
if t == "symbol"2817+
return "sp_sym"2818+
end28162819
if t == "mutable_str"28172820
return "sp_String *"28182821
end@@ -2895,6 +2898,9 @@
28952898
if t == "string"28962899
return "(\"\\xff\" + 1)"28972900
end2901+
if t == "symbol"2902+
return "((sp_sym)-1)"2903+
end28982904
if t == "mutable_str"28992905
return "NULL"29002906
end@@ -7543,17 +7549,16 @@
75437549
@sym_names = local75447550
end75457551
7546-
# Symbol type Phase 2, Step 1: emit the intern table and helpers.7547-
# Generated code does not yet reference these (SymbolNode still compiles7548-
# as a string literal). This lays the groundwork for Step 2.7552+
# Symbol type Phase 2, Step 2: emit the intern table and helpers.7553+
# SymbolNode now compiles to sp_sym values that index into sp_sym_names.75497554
def emit_sym_runtime7550-
emit_raw("/* sp_sym intern table (Phase 2 Step 1: unused) */")7555+
emit_raw("/* sp_sym intern table */")75517556
emit_raw("typedef mrb_int sp_sym;")75527557
emit_raw("#define SP_SYM_COUNT " + @sym_names.length.to_s)75537558
if @sym_names.length == 075547559
emit_raw("static const char *const sp_sym_names[1] __attribute__((unused)) = {0};")75557560
else7556-
line = "static const char *const sp_sym_names[" + @sym_names.length.to_s + "] __attribute__((unused)) = {"7561+
line = "static const char *const sp_sym_names[" + @sym_names.length.to_s + "] = {"75577562
i = 075587563
while i < @sym_names.length75597564
if i > 0@@ -7581,6 +7586,45 @@
75817586
emit_raw("")75827587
end75837588
7589+
# Index of symbol name in @sym_names, or -1 if not found.7590+
def sym_name_index(name)7591+
i = 07592+
while i < @sym_names.length7593+
if @sym_names[i] == name7594+
return i7595+
end7596+
i = i + 17597+
end7598+
-17599+
end7600+
7601+
# Compile an expression in a string-context. Wraps with sp_sym_to_s7602+
# when the expression has type "symbol", otherwise returns the raw7603+
# expression. Used at boundaries where Symbol values flow into APIs7604+
# that still expect const char * (catch/throw tag, hash key, etc.).7605+
def compile_expr_as_string(nid)7606+
s = compile_expr(nid)7607+
if infer_type(nid) == "symbol"7608+
return "sp_sym_to_s(" + s + ")"7609+
end7610+
s7611+
end7612+
7613+
# Compile a symbol literal (by name) to a sp_sym C expression.7614+
# Prefers SPS_<name> for valid-C-identifier names, otherwise emits7615+
# the raw integer cast.7616+
def compile_symbol_literal(name)7617+
idx = sym_name_index(name)7618+
if idx < 07619+
# Should not happen — collect_sym_names has already run.7620+
return "sp_sym_intern(" + c_string_literal(name) + ")"7621+
end7622+
if sym_is_c_ident(name) == 17623+
return "SPS_" + name7624+
end7625+
"((sp_sym)" + idx.to_s + ")"7626+
end7627+
75847628
# True (1) iff s is a non-empty valid C identifier: [A-Za-z_][A-Za-z0-9_]*75857629
def sym_is_c_ident(s)75867630
if s.length == 0@@ -10233,7 +10277,7 @@
1023310277
return c_string_literal(@nd_content[nid])1023410278
end1023510279
if t == "SymbolNode"10236-
return c_string_literal(@nd_content[nid])10280+
return compile_symbol_literal(@nd_content[nid])1023710281
end1023810282
if t == "InterpolatedStringNode"1023910283
return compile_interpolated(nid)@@ -10742,6 +10786,19 @@
1074210786
"0"1074310787
end1074410788
10789+
# Like compile_arg0, but converts symbol-typed arg to const char *10790+
# (sp_sym_to_s wrap). Use for callsites that need a string key.10791+
def compile_str_arg0(nid)10792+
args_id = @nd_arguments[nid]10793+
if args_id >= 010794+
arg_ids = get_args(args_id)10795+
if arg_ids.length > 010796+
return compile_expr_as_string(arg_ids[0])10797+
end10798+
end10799+
"0"10800+
end10801+
1074510802
1074610803
# --- Fiber capture helpers ---1074710804
@@ -13352,10 +13409,10 @@
1335213409
# Hash methods1335313410
if recv_type == "str_int_hash"1335413411
if mname == "[]"13355-
return "sp_StrIntHash_get(" + rc + ", " + compile_arg0(nid) + ")"13412+
return "sp_StrIntHash_get(" + rc + ", " + compile_str_arg0(nid) + ")"1335613413
end1335713414
if mname == "has_key?" || mname == "key?" || mname == "include?" || mname == "member?"13358-
return "sp_StrIntHash_has_key(" + rc + ", " + compile_arg0(nid) + ")"13415+
return "sp_StrIntHash_has_key(" + rc + ", " + compile_str_arg0(nid) + ")"1335913416
end1336013417
if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)1336113418
return "sp_StrIntHash_length(" + rc + ")"@@ -13382,7 +13439,7 @@
1338213439
args_id = @nd_arguments[nid]1338313440
if args_id >= 01338413441
aargs = get_args(args_id)13385-
key = compile_expr(aargs[0])13442+
key = compile_expr_as_string(aargs[0])1338613443
if aargs.length >= 21338713444
defval = compile_expr(aargs[1])1338813445
return "(sp_StrIntHash_has_key(" + rc + ", " + key + ") ? sp_StrIntHash_get(" + rc + ", " + key + ") : " + defval + ")"@@ -13438,10 +13495,10 @@
1343813495
end1343913496
if recv_type == "str_str_hash"1344013497
if mname == "[]"13441-
return "sp_StrStrHash_get(" + rc + ", " + compile_arg0(nid) + ")"13498+
return "sp_StrStrHash_get(" + rc + ", " + compile_str_arg0(nid) + ")"1344213499
end1344313500
if mname == "has_key?" || mname == "key?" || mname == "include?" || mname == "member?"13444-
return "sp_StrStrHash_has_key(" + rc + ", " + compile_arg0(nid) + ")"13501+
return "sp_StrStrHash_has_key(" + rc + ", " + compile_str_arg0(nid) + ")"1344513502
end1344613503
if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)1344713504
return "sp_StrStrHash_length(" + rc + ")"@@ -13488,7 +13545,7 @@
1348813545
args_id = @nd_arguments[nid]1348913546
if args_id >= 01349013547
aargs = get_args(args_id)13491-
key = compile_expr(aargs[0])13548+
key = compile_expr_as_string(aargs[0])1349213549
if aargs.length >= 21349313550
defval = compile_expr(aargs[1])1349413551
return "(sp_StrStrHash_has_key(" + rc + ", " + key + ") ? sp_StrStrHash_get(" + rc + ", " + key + ") : " + defval + ")"@@ -14645,7 +14702,7 @@
1464514702
end1464614703
end1464714704
end14648-
emit(" sp_StrStrHash_set(" + tmp + ", " + compile_expr(@nd_key[el]) + ", " + val + ");")14705+
emit(" sp_StrStrHash_set(" + tmp + ", " + compile_expr_as_string(@nd_key[el]) + ", " + val + ");")1464914706
end1465014707
}1465114708
return tmp@@ -14655,7 +14712,7 @@
1465514712
emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_new();")1465614713
elems.each { |el|1465714714
if @nd_type[el] == "AssocNode"14658-
emit(" sp_StrIntHash_set(" + tmp + ", " + compile_expr(@nd_key[el]) + ", " + compile_expr(@nd_expression[el]) + ");")14715+
emit(" sp_StrIntHash_set(" + tmp + ", " + compile_expr_as_string(@nd_key[el]) + ", " + compile_expr(@nd_expression[el]) + ");")1465914716
end1466014717
}1466114718
tmp@@ -15583,7 +15640,7 @@
1558315640
aargs = get_args(args_id)1558415641
if aargs.length >= 21558515642
rc = compile_expr(recv)15586-
key = compile_expr(aargs[0])15643+
key = compile_expr_as_string(aargs[0])1558715644
val = compile_expr(aargs[1])1558815645
if rt == "str_int_hash"1558915646
emit(" sp_StrIntHash_set(" + rc + ", " + key + ", " + val + ");")@@ -15604,11 +15661,11 @@
1560415661
rt = infer_type(recv)1560515662
rc = compile_expr(recv)1560615663
if rt == "str_int_hash"15607-
emit(" sp_StrIntHash_delete(" + rc + ", " + compile_arg0(nid) + ");")15664+
emit(" sp_StrIntHash_delete(" + rc + ", " + compile_str_arg0(nid) + ");")1560815665
return 11560915666
end1561015667
if rt == "str_str_hash"15611-
emit(" sp_StrStrHash_delete(" + rc + ", " + compile_arg0(nid) + ");")15668+
emit(" sp_StrStrHash_delete(" + rc + ", " + compile_str_arg0(nid) + ");")1561215669
return 11561315670
end1561415671
if rt == "int_array"@@ -17313,7 +17370,11 @@
1731317370
idx = "0"1731417371
val = "0"1731517372
if arg_ids.length >= 117316-
idx = compile_expr(arg_ids[0])17373+
if rt == "str_int_hash" || rt == "str_str_hash"17374+
idx = compile_expr_as_string(arg_ids[0])17375+
else17376+
idx = compile_expr(arg_ids[0])17377+
end1731717378
end1731817379
if arg_ids.length >= 21731917380
val = compile_expr(arg_ids[1])@@ -17381,6 +17442,11 @@
1738117442
k = k + 11738217443
next1738317444
end17445+
if at == "symbol"17446+
emit(" puts(sp_sym_to_s(" + val + "));")17447+
k = k + 117448+
next17449+
end1738417450
if at == "int"1738517451
emit(" printf(\"%lld" + bsl_n + "\", (long long)" + val + ");")1738617452
else@@ -17468,6 +17534,11 @@
1746817534
k = k + 11746917535
next1747017536
end17537+
if at == "symbol"17538+
emit(" fputs(sp_sym_to_s(" + val + "), stdout);")17539+
k = k + 117540+
next17541+
end1747117542
if at == "int"1747217543
emit(" printf(\"%lld\", (long long)" + val + ");")1747317544
else@@ -18568,7 +18639,7 @@
1856818639
1856918640
def compile_catch_expr(nid)1857018641
@needs_setjmp = 118571-
tag = compile_arg0(nid)18642+
tag = compile_str_arg0(nid)1857218643
blk = @nd_block[nid]1857318644
tmp = new_temp1857418645
@@ -18605,7 +18676,7 @@
1860518676
def compile_catch_stmt(nid)1860618677
@needs_setjmp = 11860718678
# catch(:tag) do ... end18608-
tag = compile_arg0(nid)18679+
tag = compile_str_arg0(nid)1860918680
blk = @nd_block[nid]1861018681
emit(" sp_catch_tag[sp_catch_top] = " + tag + ";")1861118682
emit(" sp_catch_top++;")@@ -18631,7 +18702,7 @@
1863118702
tag = "\"\""1863218703
val = "0"1863318704
if arg_ids.length >= 118634-
tag = compile_expr(arg_ids[0])18705+
tag = compile_expr_as_string(arg_ids[0])1863518706
end1863618707
if arg_ids.length >= 21863718708
val = compile_expr(arg_ids[1])