Commit 4f9818cc

4f9818cce612Merged

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

Description

feat: Symbol Phase 2 Step 1 - intern table infrastructure

Add static Symbol intern table to generated C output as groundwork for Phase 2 (独立した Symbol 型). Behavior unchanged: infer_type(SymbolNode) is still "string" and SymbolNode still compiles to a string literal.

Emitted per translation unit (all marked attribute((unused))):

  • typedef mrb_int sp_sym;
  • static const char *const sp_sym_names[];
  • sp_sym_intern(const char*)
  • sp_sym_to_s(sp_sym)
  • #define SPS_ ((sp_sym)N) for each unique symbol whose name is a valid C identifier

Symbol names are collected in collect_sym_names by a flat walk over @nd_type, gathering @nd_content of every SymbolNode (dedup, stable order). Tests: 67/67 pass, bootstrap stable (gen2.c == gen3.c).

Self-host gotcha: pushing directly to @sym_names (instance variable) inside the collection loop corrupts later compile_expr output (e.g. "+ 1" gets emitted as "+ et(lv_counter, "y")") in the bootstrapped compiler. Does not reproduce under Ruby MRI and bootstrap still passes gen2==gen3 self-equality, so it's easy to miss. Workaround: collect into a local array and assign to @sym_names at the end.

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

Details

Commit
4f9818cce612
Parent
1d916c680f0d

spinel_codegen.rbmodifiedView file

@@ -215,6 +215,9 @@
215215 @lambda_params = "".split(",")
216216 @lambda_captures = "".split(",")
217217 @lambda_insert_pos = 0
218+
219+ # Symbol type Phase 2 Step 1: intern table (infrastructure only; unused yet).
220+ @sym_names = "".split(",")
218221 end
219222
220223 # Backslash-n for C string literals - bootstrap-safe (avoids escape level issues)
@@ -7002,6 +7005,9 @@
70027005 recalc_needs_gc
70037006 emit_raw("/* Generated by Spinel AOT compiler */")
70047007 emit_raw("#include \"sp_runtime.h\"")
7008+ # Emit Symbol intern table (Phase 2 Step 1: infrastructure only).
7009+ collect_sym_names
7010+ emit_sym_runtime
70057011 # Emit program-specific regexp patterns
70067012 if @needs_regexp == 1
70077013 emit_regexp_runtime
@@ -7517,6 +7523,93 @@
75177523 emit_raw("")
75187524 end
75197525
7526+ # Symbol type Phase 2, Step 1: collect all SymbolNode content strings
7527+ # into @sym_names as a separate pass (dedup, stable order).
7528+ def collect_sym_names
7529+ # Build into a local array and assign at the end.
7530+ # (Pushing directly to @sym_names in this loop triggers a
7531+ # self-host codegen regression — see HANDOFF notes.)
7532+ local = "".split(",")
7533+ i = 0
7534+ while i < @nd_type.length
7535+ if @nd_type[i] == "SymbolNode"
7536+ sname = @nd_content[i]
7537+ if not_in(sname, local) == 1
7538+ local.push(sname)
7539+ end
7540+ end
7541+ i = i + 1
7542+ end
7543+ @sym_names = local
7544+ end
7545+
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.
7549+ def emit_sym_runtime
7550+ emit_raw("/* sp_sym intern table (Phase 2 Step 1: unused) */")
7551+ emit_raw("typedef mrb_int sp_sym;")
7552+ emit_raw("#define SP_SYM_COUNT " + @sym_names.length.to_s)
7553+ if @sym_names.length == 0
7554+ emit_raw("static const char *const sp_sym_names[1] __attribute__((unused)) = {0};")
7555+ else
7556+ line = "static const char *const sp_sym_names[" + @sym_names.length.to_s + "] __attribute__((unused)) = {"
7557+ i = 0
7558+ while i < @sym_names.length
7559+ if i > 0
7560+ line = line + ","
7561+ end
7562+ line = line + c_string_literal(@sym_names[i])
7563+ i = i + 1
7564+ end
7565+ line = line + "};"
7566+ emit_raw(line)
7567+ end
7568+ emit_raw("static sp_sym sp_sym_intern(const char *s) __attribute__((unused));")
7569+ emit_raw("static sp_sym sp_sym_intern(const char *s){mrb_int i;for(i=0;i<SP_SYM_COUNT;i++){if(strcmp(sp_sym_names[i],s)==0)return i;}return -1;}")
7570+ emit_raw("static const char *sp_sym_to_s(sp_sym id) __attribute__((unused));")
7571+ emit_raw("static const char *sp_sym_to_s(sp_sym id){if(id<0||id>=SP_SYM_COUNT)return \"\";return sp_sym_names[id];}")
7572+ # Emit SPS_<name> defines for symbols that form valid C identifiers.
7573+ i = 0
7574+ while i < @sym_names.length
7575+ nm = @sym_names[i]
7576+ if sym_is_c_ident(nm) == 1
7577+ emit_raw("#define SPS_" + nm + " ((sp_sym)" + i.to_s + ")")
7578+ end
7579+ i = i + 1
7580+ end
7581+ emit_raw("")
7582+ end
7583+
7584+ # True (1) iff s is a non-empty valid C identifier: [A-Za-z_][A-Za-z0-9_]*
7585+ def sym_is_c_ident(s)
7586+ if s.length == 0
7587+ return 0
7588+ end
7589+ i = 0
7590+ while i < s.length
7591+ ch = s[i]
7592+ ok = 0
7593+ if ch == "_"
7594+ ok = 1
7595+ end
7596+ if ch >= "A" && ch <= "Z"
7597+ ok = 1
7598+ end
7599+ if ch >= "a" && ch <= "z"
7600+ ok = 1
7601+ end
7602+ if i > 0 && ch >= "0" && ch <= "9"
7603+ ok = 1
7604+ end
7605+ if ok == 0
7606+ return 0
7607+ end
7608+ i = i + 1
7609+ end
7610+ 1
7611+ end
7612+
75207613 def emit_string_helpers
75217614 emit_raw("static const char*sp_str_concat(const char*a,const char*b){size_t la=strlen(a),lb=strlen(b);char*r=(char*)malloc(la+lb+1);memcpy(r,a,la);memcpy(r+la,b,lb+1);return r;}")
75227615 emit_raw("static const char*sp_int_to_s(mrb_int n){char*b=(char*)malloc(32);snprintf(b,32,\"%lld\",(long long)n);return b;}")