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
@lambda_params = "".split(",") @lambda_captures = "".split(",") @lambda_insert_pos = 0 # Symbol type Phase 2 Step 1: intern table (infrastructure only; unused yet). @sym_names = "".split(",") end # Backslash-n for C string literals - bootstrap-safe (avoids escape level issues) recalc_needs_gc emit_raw("/* Generated by Spinel AOT compiler */") emit_raw("#include \"sp_runtime.h\"") # Emit Symbol intern table (Phase 2 Step 1: infrastructure only). collect_sym_names emit_sym_runtime # Emit program-specific regexp patterns if @needs_regexp == 1 emit_regexp_runtime emit_raw("") end # Symbol type Phase 2, Step 1: collect all SymbolNode content strings # into @sym_names as a separate pass (dedup, stable order). def collect_sym_names # Build into a local array and assign at the end. # (Pushing directly to @sym_names in this loop triggers a # self-host codegen regression — see HANDOFF notes.) local = "".split(",") i = 0 while i < @nd_type.length if @nd_type[i] == "SymbolNode" sname = @nd_content[i] if not_in(sname, local) == 1 local.push(sname) end end i = i + 1 end @sym_names = local end # Symbol type Phase 2, Step 1: emit the intern table and helpers. # Generated code does not yet reference these (SymbolNode still compiles # as a string literal). This lays the groundwork for Step 2. def emit_sym_runtime emit_raw("/* sp_sym intern table (Phase 2 Step 1: unused) */") emit_raw("typedef mrb_int sp_sym;") emit_raw("#define SP_SYM_COUNT " + @sym_names.length.to_s) if @sym_names.length == 0 emit_raw("static const char *const sp_sym_names[1] __attribute__((unused)) = {0};") else line = "static const char *const sp_sym_names[" + @sym_names.length.to_s + "] __attribute__((unused)) = {" i = 0 while i < @sym_names.length if i > 0 line = line + "," end line = line + c_string_literal(@sym_names[i]) i = i + 1 end line = line + "};" emit_raw(line) end emit_raw("static sp_sym sp_sym_intern(const char *s) __attribute__((unused));") 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;}") emit_raw("static const char *sp_sym_to_s(sp_sym id) __attribute__((unused));") 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];}") # Emit SPS_<name> defines for symbols that form valid C identifiers. i = 0 while i < @sym_names.length nm = @sym_names[i] if sym_is_c_ident(nm) == 1 emit_raw("#define SPS_" + nm + " ((sp_sym)" + i.to_s + ")") end i = i + 1 end emit_raw("") end # True (1) iff s is a non-empty valid C identifier: [A-Za-z_][A-Za-z0-9_]* def sym_is_c_ident(s) if s.length == 0 return 0 end i = 0 while i < s.length ch = s[i] ok = 0 if ch == "_" ok = 1 end if ch >= "A" && ch <= "Z" ok = 1 end if ch >= "a" && ch <= "z" ok = 1 end if i > 0 && ch >= "0" && ch <= "9" ok = 1 end if ok == 0 return 0 end i = i + 1 end 1 end def emit_string_helpers 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;}") 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;}")