Commit 392f33b2

392f33b212f9Merged

Ori Pekelman <[email protected]> · 5 months ago · 4 files · ~4 modified

Description

Add --debug / -g mode: #line directives for native-debugger stepping

Roadmap item 1 from the spinel-dev debuggability notes: let gdb/lldb step through the original Ruby source of a compiled program, instead of landing in generated C.

The notes assumed codegen already had per-node line numbers. It did not: the parser only stamped a line on SourceLineNode (LINE) and UnsupportedNode, both into @nd_value -- a slot also overloaded for integer /float literal values, numbered-param max, etc. So there was no reliable per-node line to read. This adds one in a dedicated slot.

  • spinel_parse.c: when SPINEL_DEBUG=1, flatten() stamps every node with a node_line field (same pm_newline_list_line lookup the existing cases use). Gated, so the AST text format is byte-for-byte unchanged otherwise.
  • compiler_helpers.rb: load node_line into a dedicated @nd_line array (never @nd_value).
  • spinel_codegen.rb: @debug from SPINEL_DEBUG; emit #line N "src.rb" before each statement (compile_stmt) and before the method tail/implicit -return expression (compile_body_return_inner); suppress consecutive duplicates. In debug builds, method_linkage_named never promotes to static inline, so every method stays a real, steppable frame.
  • spinel: --debug (forces -O0, adds -g, exports SPINEL_DEBUG=1) and -g (debug info + #line, leaves -O as-is).

Verified with lldb: break src.rb:N resolves, Ruby locals print as lv_, step advances by Ruby line, and backtrace reconstructs Ruby frames (sp_add <- main) -- the last without the item-4 shadow stack. Non-debug output is unchanged (0 #line, inline promotion intact, no node_line in the AST). Kept on the ori fork; no PR against matz/spinel.

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

Details

Commit
392f33b212f9
Parent
e9b946576a44

compiler_helpers.rbmodifiedView file

@@ -528,6 +528,7 @@
528528 @nd_type = Array.new(n, "")
529529 @nd_name = Array.new(n, "")
530530 @nd_value = Array.new(n, 0)
531+ @nd_line = Array.new(n, 0)
531532 @nd_content = Array.new(n, "")
532533 @nd_flags = Array.new(n, 0)
533534 @nd_operator = Array.new(n, "")
@@ -665,6 +666,13 @@
665666 # location in the compile error.
666667 @nd_value[nid] = val
667668 end
669+ # Debug builds only (SPINEL_DEBUG=1): the parser stamps every node
670+ # with its 1-based source line in a dedicated slot so codegen can
671+ # emit `#line` directives. Kept separate from @nd_value, which is
672+ # overloaded for integer/float literal values, numbered-param max, etc.
673+ if field == "node_line"
674+ @nd_line[nid] = val
675+ end
668676 end
669677
670678 def set_ref_field(nid, field, ref_id)