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 end
829829 if t == "SymbolNode"
830- return "string"
830+ return "symbol"
831831 end
832832 if t == "NumberedReferenceReadNode"
833833 return "string"
@@ -2813,6 +2813,9 @@
28132813 if t == "string"
28142814 return "const char *"
28152815 end
2816+ if t == "symbol"
2817+ return "sp_sym"
2818+ end
28162819 if t == "mutable_str"
28172820 return "sp_String *"
28182821 end
@@ -2895,6 +2898,9 @@
28952898 if t == "string"
28962899 return "(\"\\xff\" + 1)"
28972900 end
2901+ if t == "symbol"
2902+ return "((sp_sym)-1)"
2903+ end
28982904 if t == "mutable_str"
28992905 return "NULL"
29002906 end
@@ -7543,17 +7549,16 @@
75437549 @sym_names = local
75447550 end
75457551
7546- # Symbol type Phase 2, Step 1: emit the intern table and helpers.
7547- # Generated code does not yet reference these (SymbolNode still compiles
7548- # 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_runtime
7550- 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 == 0
75547559 emit_raw("static const char *const sp_sym_names[1] __attribute__((unused)) = {0};")
75557560 else
7556- 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 = 0
75587563 while i < @sym_names.length
75597564 if i > 0
@@ -7581,6 +7586,45 @@
75817586 emit_raw("")
75827587 end
75837588
7589+ # Index of symbol name in @sym_names, or -1 if not found.
7590+ def sym_name_index(name)
7591+ i = 0
7592+ while i < @sym_names.length
7593+ if @sym_names[i] == name
7594+ return i
7595+ end
7596+ i = i + 1
7597+ end
7598+ -1
7599+ end
7600+
7601+ # Compile an expression in a string-context. Wraps with sp_sym_to_s
7602+ # when the expression has type "symbol", otherwise returns the raw
7603+ # expression. Used at boundaries where Symbol values flow into APIs
7604+ # 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+ end
7610+ s
7611+ end
7612+
7613+ # Compile a symbol literal (by name) to a sp_sym C expression.
7614+ # Prefers SPS_<name> for valid-C-identifier names, otherwise emits
7615+ # the raw integer cast.
7616+ def compile_symbol_literal(name)
7617+ idx = sym_name_index(name)
7618+ if idx < 0
7619+ # Should not happen — collect_sym_names has already run.
7620+ return "sp_sym_intern(" + c_string_literal(name) + ")"
7621+ end
7622+ if sym_is_c_ident(name) == 1
7623+ return "SPS_" + name
7624+ end
7625+ "((sp_sym)" + idx.to_s + ")"
7626+ end
7627+
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 end
1023510279 if t == "SymbolNode"
10236- return c_string_literal(@nd_content[nid])
10280+ return compile_symbol_literal(@nd_content[nid])
1023710281 end
1023810282 if t == "InterpolatedStringNode"
1023910283 return compile_interpolated(nid)
@@ -10742,6 +10786,19 @@
1074210786 "0"
1074310787 end
1074410788
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 >= 0
10794+ arg_ids = get_args(args_id)
10795+ if arg_ids.length > 0
10796+ return compile_expr_as_string(arg_ids[0])
10797+ end
10798+ end
10799+ "0"
10800+ end
10801+
1074510802
1074610803 # --- Fiber capture helpers ---
1074710804
@@ -13352,10 +13409,10 @@
1335213409 # Hash methods
1335313410 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 end
1335713414 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 end
1336013417 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 >= 0
1338413441 aargs = get_args(args_id)
13385- key = compile_expr(aargs[0])
13442+ key = compile_expr_as_string(aargs[0])
1338613443 if aargs.length >= 2
1338713444 defval = compile_expr(aargs[1])
1338813445 return "(sp_StrIntHash_has_key(" + rc + ", " + key + ") ? sp_StrIntHash_get(" + rc + ", " + key + ") : " + defval + ")"
@@ -13438,10 +13495,10 @@
1343813495 end
1343913496 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 end
1344313500 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 end
1344613503 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 >= 0
1349013547 aargs = get_args(args_id)
13491- key = compile_expr(aargs[0])
13548+ key = compile_expr_as_string(aargs[0])
1349213549 if aargs.length >= 2
1349313550 defval = compile_expr(aargs[1])
1349413551 return "(sp_StrStrHash_has_key(" + rc + ", " + key + ") ? sp_StrStrHash_get(" + rc + ", " + key + ") : " + defval + ")"
@@ -14645,7 +14702,7 @@
1464514702 end
1464614703 end
1464714704 end
14648- 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 end
1465014707 }
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 end
1466014717 }
1466114718 tmp
@@ -15583,7 +15640,7 @@
1558315640 aargs = get_args(args_id)
1558415641 if aargs.length >= 2
1558515642 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 1
1560915666 end
1561015667 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 1
1561315670 end
1561415671 if rt == "int_array"
@@ -17313,7 +17370,11 @@
1731317370 idx = "0"
1731417371 val = "0"
1731517372 if arg_ids.length >= 1
17316- 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+ else
17376+ idx = compile_expr(arg_ids[0])
17377+ end
1731717378 end
1731817379 if arg_ids.length >= 2
1731917380 val = compile_expr(arg_ids[1])
@@ -17381,6 +17442,11 @@
1738117442 k = k + 1
1738217443 next
1738317444 end
17445+ if at == "symbol"
17446+ emit(" puts(sp_sym_to_s(" + val + "));")
17447+ k = k + 1
17448+ next
17449+ end
1738417450 if at == "int"
1738517451 emit(" printf(\"%lld" + bsl_n + "\", (long long)" + val + ");")
1738617452 else
@@ -17468,6 +17534,11 @@
1746817534 k = k + 1
1746917535 next
1747017536 end
17537+ if at == "symbol"
17538+ emit(" fputs(sp_sym_to_s(" + val + "), stdout);")
17539+ k = k + 1
17540+ next
17541+ end
1747117542 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 = 1
18571- tag = compile_arg0(nid)
18642+ tag = compile_str_arg0(nid)
1857218643 blk = @nd_block[nid]
1857318644 tmp = new_temp
1857418645
@@ -18605,7 +18676,7 @@
1860518676 def compile_catch_stmt(nid)
1860618677 @needs_setjmp = 1
1860718678 # catch(:tag) do ... end
18608- 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 >= 1
18634- tag = compile_expr(arg_ids[0])
18705+ tag = compile_expr_as_string(arg_ids[0])
1863518706 end
1863618707 if arg_ids.length >= 2
1863718708 val = compile_expr(arg_ids[1])