Commit f14172a2
f14172a22bcfMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 6 files · ~6 modified
Description
spinelc: emit #line directives so cc errors map to the Ruby source
The C spinel driver lost the legacy backend's source-mapping: a cc
error on the generated C reported a line in out.c, not the original .rb.
The parser already stamps per-node node_line / node_file (gated on
SPINEL_DEBUG / SPINEL_LINE_MAP) and emits a FILE <id> <path> table for
multi-file (require-spliced) programs, but spinelc's NodeTable loader
dropped the FILE table and codegen never emitted #line.
Parse the FILE table into NodeTable (with an nt_file_path accessor) and
have codegen emit a deduped #line N "path" at each statement boundary
when SPINEL_LINE_MAP / SPINEL_DEBUG is set. Off by default, so spinelc's
direct output (make test/bench/optcarrot/bootstrap) is byte-unchanged.
The spinel driver now defaults line-map on (parity with spinel-legacy),
adds --line-map / --no-line-map, and routes -g / --debug through
SPINEL_DEBUG (+ -rdynamic on ELF for Exception#backtrace), restoring
gdb/lldb stepping through the .rb.
make check: 846 pass, 0 fail.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- f14172a22bcf
- Parent
- a6538996d3e2
spinelmodifiedView file
# first .rb file ends spinel-flag parsing, and the rest# of the command line becomes the binary's ARGV.# -O LEVEL Optimization level for cc (default: 2)# -g Add debug info (-g) to the cc step# --debug Debug build: -g -O0# -g Add debug info (-g) + #line, leaving -O as-is# --debug Debug build: #line directives + -g -O0, so gdb/lldb step# through the original .rb source# --line-map Emit #line directives (on by default)# --no-line-map Suppress #line directives (compile errors map to the# generated C, not the Ruby source)# --cc=CMD C compiler command (default: cc)# -e STR Inline Ruby source (repeatable; multiple -e join with \n)# --int-overflow=MODE Int +/-/* overflow handling (default: raise)EVAL_USED=0EVAL_TMP=""INT_OVERFLOW="raise"DEBUG=0# #line directives are on by default: they cost nothing at runtime (the binary# is identical modulo line attribution) and let the C toolchain report a# compile error on the generated C against the Ruby source line. Opt out with# --no-line-map.LINE_MAP=1# Parse spinel-side flags. In `-E` (run) mode, the first positional `.rb`# arg marks the boundary between spinel flags and the script's ARGV: ;; -o) shift; OUTPUT="$1"; shift ;; -O) shift; OPT_LEVEL="$1"; shift ;; --debug) OPT_LEVEL="0"; EXTRA_FLAGS="$EXTRA_FLAGS -g"; shift ;; -g) EXTRA_FLAGS="$EXTRA_FLAGS -g"; shift ;; --debug) # Debug build: native-debugger stepping through the .rb. Emits # #line directives, forces -O0, and adds -g. DEBUG=1; OPT_LEVEL="0"; EXTRA_FLAGS="$EXTRA_FLAGS -g"; shift ;; -g) # Debug info + #line, but leave -O as-is. DEBUG=1; EXTRA_FLAGS="$EXTRA_FLAGS -g"; shift ;; --line-map) # Emit #line directives (on by default; kept for explicitness). LINE_MAP=1; shift ;; --no-line-map) # Suppress #line directives (compile errors report against # the generated C instead of the Ruby source). LINE_MAP=0; shift ;; -e) shift if [ "$EVAL_USED" -eq 0 ]; then EVAL_SCRIPT="$1" echo " -S Print C to stdout" >&2 echo " -E Run the compiled binary; leftover args become its ARGV" >&2 echo " -O LEVEL Optimization level (default: 2)" >&2 echo " -g Add debug info (-g)" >&2 echo " --debug Debug build (-g -O0)" >&2 echo " -g Add debug info (-g) + #line, leaving -O as-is" >&2 echo " --debug Debug build: step through the .rb in gdb/lldb (#line, -g -O0)" >&2 echo " --no-line-map Suppress #line directives (compile errors report" >&2 echo " against generated C, not the Ruby source)" >&2 echo " --cc=CMD C compiler (default: cc)" >&2 echo " -e STR Inline Ruby source (repeatable; joined with newlines)" >&2 echo " --int-overflow=MODE Int +/-/* overflow handling (default: raise)" >&2fitrap 'rm -f "$EVAL_TMP"; [ -n "$C_TMP_DIR" ] && rm -rf "$C_TMP_DIR"; [ -n "$RUN_TMP_DIR" ] && rm -rf "$RUN_TMP_DIR"' EXIT# Source mapping: spinelc's parser stamps per-node source lines and codegen# emits `#line N "file"` when SPINEL_DEBUG / SPINEL_LINE_MAP is set, so a C# compile error is reported against the original Ruby line. --debug / -g imply# the fuller SPINEL_DEBUG path (plus -g, set above); plain --line-map keeps the# optimized build and just adds #line.if [ "$DEBUG" -eq 1 ]; then export SPINEL_DEBUG=1 # Native Exception#backtrace on Linux: glibc's backtrace_symbols only names # frames whose symbols are in the dynamic symbol table; -rdynamic exports # them. ELF-only -- skip on macOS (Mach-O resolves already) and MinGW/Cygwin. case "$(uname -s 2>/dev/null)" in Darwin|MINGW*|MSYS*|CYGWIN*) ;; *) EXTRA_FLAGS="$EXTRA_FLAGS -rdynamic" ;; esacelif [ "$LINE_MAP" -eq 1 ]; then export SPINEL_LINE_MAP=1fi# Step 1: Compile (parse + analyze + codegen) — spinelc emits C.if [ "$STDOUT_MODE" -eq 1 ]; then "$SPINELC" "$SOURCE"src/codegen.cmodifiedView file
Loading diff…
src/codegen_internal.hmodifiedView file
Loading diff…
src/codegen_stmt.cmodifiedView file
Loading diff…
src/node_table.cmodifiedView file
Loading diff…
src/node_table.hmodifiedView file
Loading diff…