Commit 7ae3c481

7ae3c4810fedMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Add --emit-symbol-map: export emitted-symbol -> Ruby-name map as JSON

Opt-in, output-neutral sidecar (sibling to --emit-rbs #1276 / --emit-types #1298) serializing the compiler's emitted C symbol -> Ruby method-name mapping, with kind and source position:

spinel app.rb --emit-symbol-map -o app.symbols.json { "symbols": [ { "c": "sp_Article_save", "ruby": "Article#save", "kind": "imeth", "file": "app.rb", "line": 42 }, ... ] }

Built in codegen (emit_symbol_map_json), where the name-mangling (sanitize_name, the sp__ shape) lives, so the map can't drift from what is emitted -- the point of the RFC for out-of-process tools, kept true internally. The ruby field is the original method name, so predicate/bang/operator methods are exact (save? / <=> / []), which the runtime sp_bt_symbol demangler (#1300) cannot reverse from the one-way sanitized C form. The class path demangles _ -> :: like sp_bt_symbol (the one shared lossy case); position-less synthetic/builtin methods come through with file/line null.

Gated by SPINEL_EMIT_SYMBOL_MAP; release builds unchanged. The launcher flag runs codegen (the mangling lives there) but emits no binary.

Refs #1334

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
7ae3c4810fed
Parent
1f054fd8169c

spinelmodifiedView file

@@ -48,6 +48,7 @@
4848 C_ONLY=0
4949 EMIT_RBS=0
5050 EMIT_TYPES=0
51+EMIT_SYMBOL_MAP=0
5152 STDOUT_MODE=0
5253 RUN_MODE=0
5354 OPT_LEVEL="2"
@@ -127,6 +128,10 @@
127128 # diagnostics as JSON (for the ruby-lsp addon). Needs node
128129 # positions, so it turns on SPINEL_DEBUG below.
129130 EMIT_TYPES=1; DEBUG=1; shift ;;
131+ --emit-symbol-map) # Dump emitted-symbol -> Ruby-name map as JSON (#1334).
132+ # Runs codegen (the mangling lives there) but writes no
133+ # binary. DEBUG on so node positions (file/line) are stamped.
134+ EMIT_SYMBOL_MAP=1; DEBUG=1; shift ;;
130135 -S) STDOUT_MODE=1; shift ;;
131136 -E) RUN_MODE=1; shift ;;
132137 *.rb)
@@ -346,6 +351,28 @@
346351 exit 0
347352 fi
348353
354+# --emit-symbol-map needs codegen (the C name-mangling lives there) but emits
355+# no binary: run codegen with SPINEL_EMIT_SYMBOL_MAP set, route the C output
356+# to a temp, and stop.
357+if [ "$EMIT_SYMBOL_MAP" -eq 1 ]; then
358+ SYM_OUT="${OUTPUT:-$BASENAME.symbols.json}"
359+ export SPINEL_EMIT_SYMBOL_MAP="$SYM_OUT"
360+ SYM_C_TMP="$(mktemp /tmp/spinel_symc.XXXXXX)"
361+ if [ -x "$CODEGEN_BIN" ]; then
362+ "$CODEGEN_BIN" "$AST_TMP" "$IR_TMP" "$SYM_C_TMP"
363+ else
364+ ruby "$CODEGEN_RB" "$AST_TMP" "$IR_TMP" "$SYM_C_TMP"
365+ fi
366+ cg_rc=$?
367+ rm -f "$SYM_C_TMP"
368+ if [ $cg_rc -ne 0 ]; then
369+ echo "spinel: codegen failed" >&2
370+ exit 1
371+ fi
372+ echo "Wrote $SYM_OUT" >&2
373+ exit 0
374+fi
375+
349376 # Step 3: Codegen
350377 if [ $STDOUT_MODE -eq 1 ]; then
351378 if [ -x "$CODEGEN_BIN" ]; then