Commit 8897b70f

8897b70f6018Merged

Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

feat(parse,codegen): support PostExecutionNode (END { ... })

CRuby runs END blocks at program exit in REVERSE registration order (LIFO). Spinel emits each block as a static C function and registers them via atexit() at main() startup -- atexit naturally invokes handlers LIFO, so the order matches without any extra bookkeeping.

Three pieces:

  1. Parser case in spinel_parse.c -- emits the node with the `statements` ref slot.

  2. collect_all Pass 2.1 walks the toplevel for PostExecutionNode and queues each body onto @post_execution_blocks (in source-encounter order).

  3. emit_post_execution_funcs (called from compile() right before emit_main) emits one zero-arg static C function per block:

    static void sp_end_block_<n>(void) { /* body */ }
    

    emit_main then inserts `atexit(sp_end_block_);` calls in source order, after the BEGIN-block hoist and before the regular statement loop.

The compile_stmt arm for PostExecutionNode at its source location is a no-op.

Out of scope:

  • END blocks that capture closure state from the surrounding scope (the static C function has no enclosing locals to read).

Test exercises (test/post_execution.rb):

  • Two END blocks with `puts "last-1"` and `puts "last-2"` -- verifies LIFO order ("last-2" prints before "last-1") and that END runs after the regular statement-flow output.

Tests: 279 pass. Bootstrap: gen2.c == gen3.c.

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

Details

Commit
8897b70f6018
Parent
7fc5e446c39f

spinel_codegen.rbmodifiedView file

@@ -228,6 +228,12 @@
228228 # the top of main() during emit_main.
229229 @pre_execution_blocks = []
230230
231+ # `END { ... }` bodies, in source-encounter order. Each emits a
232+ # static C function; main() startup registers them via atexit()
233+ # which naturally invokes handlers LIFO -- matches CRuby's
234+ # reverse-of-source-order END execution.
235+ @post_execution_blocks = []
236+
231237 # ---- Scope stack for local variables ----
232238 @scope_names = "".split(",")
233239 @scope_types = "".split(",")
@@ -5673,6 +5679,12 @@
56735679 @pre_execution_blocks.push(bid)
56745680 end
56755681 end
5682+ if @nd_type[sid] == "PostExecutionNode"
5683+ bid = @nd_body[sid]
5684+ if bid >= 0
5685+ @post_execution_blocks.push(bid)
5686+ end
5687+ end
56765688 }
56775689
56785690 # Pass 2.6: hoist `recv.instance_eval do ... end` blocks into
@@ -13710,6 +13722,11 @@
1371013722 emit_class_methods
1371113723 emit_ieval_funcs
1371213724 emit_toplevel_methods
13725+ # `END { ... }` -- emit one zero-arg static C function per
13726+ # PostExecutionNode body. main() startup will atexit()-register
13727+ # them in source order; atexit invokes handlers LIFO, matching
13728+ # CRuby's reverse-order END execution.
13729+ emit_post_execution_funcs
1371313730 # Emit lambda functions before main (they are generated during compilation)
1371413731 # We emit them in emit_main after forward declarations
1371513732 emit_main
@@ -16764,6 +16781,29 @@
1676416781 end
1676516782 end
1676616783
16784+ # `END { ... }` -- one zero-arg static C function per registered
16785+ # PostExecutionNode body. main() registers them via atexit() at
16786+ # startup. atexit invokes handlers LIFO, matching CRuby's
16787+ # reverse-of-source-order END execution.
16788+ def emit_post_execution_funcs
16789+ pe = 0
16790+ while pe < @post_execution_blocks.length
16791+ bnid = @post_execution_blocks[pe]
16792+ emit_raw("static void sp_end_block_" + pe.to_s + "(void) {")
16793+ @indent = 1
16794+ @in_main = 1
16795+ push_scope
16796+ if bnid >= 0
16797+ compile_stmt(bnid)
16798+ end
16799+ pop_scope
16800+ @in_main = 0
16801+ emit_raw("}")
16802+ emit_raw("")
16803+ pe = pe + 1
16804+ end
16805+ end
16806+
1676716807 def emit_toplevel_method(mi)
1676816808 mfullname = @meth_names[mi]
1676916809 @current_method_name = mfullname
@@ -18437,6 +18477,17 @@
1843718477 pi = pi + 1
1843818478 end
1843918479
18480+ # `END { ... }` register-atexit: each PostExecutionNode body
18481+ # was emitted as a static `sp_end_block_<n>` function during
18482+ # emit_post_execution_funcs. We register them in source order;
18483+ # atexit invokes handlers LIFO, matching CRuby's reverse-order
18484+ # END semantics.
18485+ pe = 0
18486+ while pe < @post_execution_blocks.length
18487+ emit(" atexit(sp_end_block_" + pe.to_s + ");")
18488+ pe = pe + 1
18489+ end
18490+
1844018491 # Compile main statements
1844118492 stmts.each { |sid|
1844218493 if @nd_type[sid] != "DefNode"
@@ -26310,6 +26361,13 @@
2631026361 # source location.
2631126362 return
2631226363 end
26364+ if t == "PostExecutionNode"
26365+ # `END { ... }`. collect_all pushed the body onto
26366+ # @post_execution_blocks; emit_post_execution_funcs emits the
26367+ # static sp_end_block_<n> function and emit_main inserts an
26368+ # atexit() call at startup.
26369+ return
26370+ end
2631326371 if t == "LocalVariableWriteNode"
2631426372 lname = @nd_name[nid]
2631526373 # Check for method(:name) assignment