Commit 81fa7bfe
81fa7bfe0d5fMerged
Ori Pekelman <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): defer top-level CONST init when RHS reads a main local (#647)
Top-level CONST = recv.method segfaulted (or silently returned
0) when recv was a main-scope local: codegen's const-init
pre-loop in emit_main planted cst_X = lv_recv.iv_z; at the top
of main, before lv_recv = sp_Recv_new() ran. With the local
struct still zero-init, the chain either:
- reads zeros through plain attr_readers —
CONSTends up 0 instead of the intended value; or - dereferences NULL via an FFI call inside the chain and the
program SIGSEGVs at startup before any
puts.
Sibling of #630 (multi-write fix in 9ac50a6) — there the push was skipped entirely; here the const must still be declared but the initializer emit moves to source order.
- codegen: new
expr_reads_main_local(nid, lnames)AST walker flags an expression that reads any LocalVariableReadNode whose name is in main's declared locals. - codegen
emit_main: compute adefer_const_init[]flag list parallel to @const_names; mark a top-level const (scope == "") as deferred when its RHS contains a main-local read. - codegen
emit_mainconst-init loop: skip flagged entries. - codegen
emit_mainbody-stmt loop: when a ConstantWriteNode's target is flagged, emitcst_X = <compiled RHS>;(plus SP_GC_ROOT for pointer types) at the in-source position so the local has already been assigned.
The change only affects top-level constants whose RHS actually reads a main local — class constants and constants over literal / method-call-on-constant RHS keep the existing pre-hoist behavior. Tests: 625 / 0 / 0 unchanged.
Repro:
class Cfg; attr_reader :nested; def initialize(n); @nested = n; end; end
class Inner; attr_reader :v; def initialize; @v = 42; end; end
inner = Inner.new
cfg = Cfg.new(inner)
CFG_VOCAB = cfg.nested.v
puts CFG_VOCAB.to_s # was 0, now 42
Closes #647.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 81fa7bfe0d5f
- Parent
- 0881702f7a17
spinel_codegen.rbmodifiedView file
end end # Return 1 if the AST rooted at `nid` reads any LocalVariableReadNode # whose name appears in `main_lnames` (main()'s declared locals). # # Used by emit_main to detect single-target top-level constants whose # initializer depends on a local that hasn't been assigned yet at the # const-init pre-loop. Without this guard, codegen plants # cst_X = lv_y.iv_z; # at the top of main, before `lv_y = sp_Y_new()` runs — so the chain # reads from a zero-init struct (silently returns 0, or SIGSEGVs when # the call reaches FFI). matz/spinel#647; sibling of #630 (multi-write # fix in 9ac50a6). def expr_reads_main_local(nid, main_lnames) if nid < 0 return 0 end if @nd_type[nid] == "LocalVariableReadNode" nm = @nd_name[nid] li = 0 while li < main_lnames.length if main_lnames[li] == nm return 1 end li = li + 1 end return 0 end cs = [] push_child_ids(nid, cs) k = 0 while k < cs.length if expr_reads_main_local(cs[k], main_lnames) == 1 return 1 end k = k + 1 end 0 end # Walk the AST looking for class-hierarchy usage patterns # (`<klass>.superclass`, `.ancestors`, `<`, `<=`, dynamic # is_a? / kind_of? / instance_of?, case-when on a Class const). j = j + 1 end # Compute deferred const inits: a top-level `CONST = recv.method` # whose RHS reads from a main local must wait for the body to run, # otherwise the pre-emit below plants `cst_X = lv_y.iv_z;` before # `lv_y = sp_Y_new()` and reads from a zero-init struct # (matz/spinel#647). The single-target sibling of #630 (9ac50a6 # multi-write fix): there the push was skipped entirely; here the # const must still be declared but the init emit moves to source # order, handled in the body-stmt loop below. defer_const_init = "".split(",") dci = 0 while dci < @const_names.length defer_const_init.push("0") dci = dci + 1 end dci = 0 while dci < @const_names.length scope_dci = "" if dci < @const_scope_names.length scope_dci = @const_scope_names[dci] end if scope_dci == "" && dci < @const_expr_ids.length eid_dci = @const_expr_ids[dci] if eid_dci >= 0 && expr_reads_main_local(eid_dci, lnames) == 1 defer_const_init[dci] = "1" end end dci = dci + 1 end # Constants (initialize global declarations) i = 0 while i < @const_names.length # expr_id == -1 means this constant came from a `A, B, C = expr` # multi-write: its value is computed by the deferred init block # below, not from a single per-target expression. if i < @const_expr_ids.length && @const_expr_ids[i] >= 0 if i < @const_expr_ids.length && @const_expr_ids[i] >= 0 && defer_const_init[i] != "1" old_scope = @current_lexical_scope if i < @const_scope_names.length @current_lexical_scope = @const_scope_names[i] pe = pe + 1 end # Compile main statements # Compile main statements. ConstantWriteNode is normally skipped # because the const-init pre-loop above already emitted it. The # exception is constants flagged in defer_const_init[]: those have # an RHS that reads from a main local, so the pre-loop deliberately # left them out and we emit them here in source order. stmts.each { |sid| if @nd_type[sid] != "DefNode" if @nd_type[sid] != "ClassNode" if @nd_type[sid] != "ConstantWriteNode" compile_stmt(sid) else cwn_name = @nd_name[sid] cwn_idx = -1 ck = 0 while ck < @const_names.length if @const_names[ck] == cwn_name scope_ck = "" if ck < @const_scope_names.length scope_ck = @const_scope_names[ck] end if scope_ck == "" cwn_idx = ck break end end ck = ck + 1 end if cwn_idx >= 0 && cwn_idx < defer_const_init.length && defer_const_init[cwn_idx] == "1" eid_b = @const_expr_ids[cwn_idx] if eid_b >= 0 old_scope_b = @current_lexical_scope @current_lexical_scope = "" val_b = compile_expr(eid_b) @current_lexical_scope = old_scope_b emit(" cst_" + cwn_name + " = " + val_b + ";") if type_is_pointer(@const_types[cwn_idx]) == 1 emit(" SP_GC_ROOT(cst_" + cwn_name + ");") end end end end end end