Commit b801924b

b801924b7c1dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

Merge PR #648: defer top-level CONST init when RHS reads a main local (#647)

fix(codegen): defer top-level CONST init when RHS reads a main local (#647)

Details

Commit
b801924b7c1d
Parent
e46be3abf883
Merge parent
949c6d6ebb08

spinel_codegen.rbmodifiedView file

@@ -10669,6 +10669,44 @@
1066910669 end
1067010670 end
1067110671
10672+ # Return 1 if the AST rooted at `nid` reads any LocalVariableReadNode
10673+ # whose name appears in `main_lnames` (main()'s declared locals).
10674+ #
10675+ # Used by emit_main to detect single-target top-level constants whose
10676+ # initializer depends on a local that hasn't been assigned yet at the
10677+ # const-init pre-loop. Without this guard, codegen plants
10678+ # cst_X = lv_y.iv_z;
10679+ # at the top of main, before `lv_y = sp_Y_new()` runs — so the chain
10680+ # reads from a zero-init struct (silently returns 0, or SIGSEGVs when
10681+ # the call reaches FFI). matz/spinel#647; sibling of #630 (multi-write
10682+ # fix in 9ac50a6).
10683+ def expr_reads_main_local(nid, main_lnames)
10684+ if nid < 0
10685+ return 0
10686+ end
10687+ if @nd_type[nid] == "LocalVariableReadNode"
10688+ nm = @nd_name[nid]
10689+ li = 0
10690+ while li < main_lnames.length
10691+ if main_lnames[li] == nm
10692+ return 1
10693+ end
10694+ li = li + 1
10695+ end
10696+ return 0
10697+ end
10698+ cs = []
10699+ push_child_ids(nid, cs)
10700+ k = 0
10701+ while k < cs.length
10702+ if expr_reads_main_local(cs[k], main_lnames) == 1
10703+ return 1
10704+ end
10705+ k = k + 1
10706+ end
10707+ 0
10708+ end
10709+
1067210710 # Walk the AST looking for class-hierarchy usage patterns
1067310711 # (`<klass>.superclass`, `.ancestors`, `<`, `<=`, dynamic
1067410712 # is_a? / kind_of? / instance_of?, case-when on a Class const).
@@ -11088,13 +11126,42 @@
1108811126 j = j + 1
1108911127 end
1109011128
11129+ # Compute deferred const inits: a top-level `CONST = recv.method`
11130+ # whose RHS reads from a main local must wait for the body to run,
11131+ # otherwise the pre-emit below plants `cst_X = lv_y.iv_z;` before
11132+ # `lv_y = sp_Y_new()` and reads from a zero-init struct
11133+ # (matz/spinel#647). The single-target sibling of #630 (9ac50a6
11134+ # multi-write fix): there the push was skipped entirely; here the
11135+ # const must still be declared but the init emit moves to source
11136+ # order, handled in the body-stmt loop below.
11137+ defer_const_init = "".split(",")
11138+ dci = 0
11139+ while dci < @const_names.length
11140+ defer_const_init.push("0")
11141+ dci = dci + 1
11142+ end
11143+ dci = 0
11144+ while dci < @const_names.length
11145+ scope_dci = ""
11146+ if dci < @const_scope_names.length
11147+ scope_dci = @const_scope_names[dci]
11148+ end
11149+ if scope_dci == "" && dci < @const_expr_ids.length
11150+ eid_dci = @const_expr_ids[dci]
11151+ if eid_dci >= 0 && expr_reads_main_local(eid_dci, lnames) == 1
11152+ defer_const_init[dci] = "1"
11153+ end
11154+ end
11155+ dci = dci + 1
11156+ end
11157+
1109111158 # Constants (initialize global declarations)
1109211159 i = 0
1109311160 while i < @const_names.length
1109411161 # expr_id == -1 means this constant came from a `A, B, C = expr`
1109511162 # multi-write: its value is computed by the deferred init block
1109611163 # below, not from a single per-target expression.
11097- if i < @const_expr_ids.length && @const_expr_ids[i] >= 0
11164+ if i < @const_expr_ids.length && @const_expr_ids[i] >= 0 && defer_const_init[i] != "1"
1109811165 old_scope = @current_lexical_scope
1109911166 if i < @const_scope_names.length
1110011167 @current_lexical_scope = @const_scope_names[i]
@@ -11276,12 +11343,46 @@
1127611343 pe = pe + 1
1127711344 end
1127811345
11279- # Compile main statements
11346+ # Compile main statements. ConstantWriteNode is normally skipped
11347+ # because the const-init pre-loop above already emitted it. The
11348+ # exception is constants flagged in defer_const_init[]: those have
11349+ # an RHS that reads from a main local, so the pre-loop deliberately
11350+ # left them out and we emit them here in source order.
1128011351 stmts.each { |sid|
1128111352 if @nd_type[sid] != "DefNode"
1128211353 if @nd_type[sid] != "ClassNode"
1128311354 if @nd_type[sid] != "ConstantWriteNode"
1128411355 compile_stmt(sid)
11356+ else
11357+ cwn_name = @nd_name[sid]
11358+ cwn_idx = -1
11359+ ck = 0
11360+ while ck < @const_names.length
11361+ if @const_names[ck] == cwn_name
11362+ scope_ck = ""
11363+ if ck < @const_scope_names.length
11364+ scope_ck = @const_scope_names[ck]
11365+ end
11366+ if scope_ck == ""
11367+ cwn_idx = ck
11368+ break
11369+ end
11370+ end
11371+ ck = ck + 1
11372+ end
11373+ if cwn_idx >= 0 && cwn_idx < defer_const_init.length && defer_const_init[cwn_idx] == "1"
11374+ eid_b = @const_expr_ids[cwn_idx]
11375+ if eid_b >= 0
11376+ old_scope_b = @current_lexical_scope
11377+ @current_lexical_scope = ""
11378+ val_b = compile_expr(eid_b)
11379+ @current_lexical_scope = old_scope_b
11380+ emit(" cst_" + cwn_name + " = " + val_b + ";")
11381+ if type_is_pointer(@const_types[cwn_idx]) == 1
11382+ emit(" SP_GC_ROOT(cst_" + cwn_name + ");")
11383+ end
11384+ end
11385+ end
1128511386 end
1128611387 end
1128711388 end