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
end1067010670
end1067110671
10672+
# Return 1 if the AST rooted at `nid` reads any LocalVariableReadNode10673+
# whose name appears in `main_lnames` (main()'s declared locals).10674+
#10675+
# Used by emit_main to detect single-target top-level constants whose10676+
# initializer depends on a local that hasn't been assigned yet at the10677+
# const-init pre-loop. Without this guard, codegen plants10678+
# cst_X = lv_y.iv_z;10679+
# at the top of main, before `lv_y = sp_Y_new()` runs — so the chain10680+
# reads from a zero-init struct (silently returns 0, or SIGSEGVs when10681+
# the call reaches FFI). matz/spinel#647; sibling of #630 (multi-write10682+
# fix in 9ac50a6).10683+
def expr_reads_main_local(nid, main_lnames)10684+
if nid < 010685+
return 010686+
end10687+
if @nd_type[nid] == "LocalVariableReadNode"10688+
nm = @nd_name[nid]10689+
li = 010690+
while li < main_lnames.length10691+
if main_lnames[li] == nm10692+
return 110693+
end10694+
li = li + 110695+
end10696+
return 010697+
end10698+
cs = []10699+
push_child_ids(nid, cs)10700+
k = 010701+
while k < cs.length10702+
if expr_reads_main_local(cs[k], main_lnames) == 110703+
return 110704+
end10705+
k = k + 110706+
end10707+
010708+
end10709+
1067210710
# Walk the AST looking for class-hierarchy usage patterns1067310711
# (`<klass>.superclass`, `.ancestors`, `<`, `<=`, dynamic1067410712
# is_a? / kind_of? / instance_of?, case-when on a Class const).@@ -11088,13 +11126,42 @@
1108811126
j = j + 11108911127
end1109011128
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;` before11132+
# `lv_y = sp_Y_new()` and reads from a zero-init struct11133+
# (matz/spinel#647). The single-target sibling of #630 (9ac50a611134+
# multi-write fix): there the push was skipped entirely; here the11135+
# const must still be declared but the init emit moves to source11136+
# order, handled in the body-stmt loop below.11137+
defer_const_init = "".split(",")11138+
dci = 011139+
while dci < @const_names.length11140+
defer_const_init.push("0")11141+
dci = dci + 111142+
end11143+
dci = 011144+
while dci < @const_names.length11145+
scope_dci = ""11146+
if dci < @const_scope_names.length11147+
scope_dci = @const_scope_names[dci]11148+
end11149+
if scope_dci == "" && dci < @const_expr_ids.length11150+
eid_dci = @const_expr_ids[dci]11151+
if eid_dci >= 0 && expr_reads_main_local(eid_dci, lnames) == 111152+
defer_const_init[dci] = "1"11153+
end11154+
end11155+
dci = dci + 111156+
end11157+
1109111158
# Constants (initialize global declarations)1109211159
i = 01109311160
while i < @const_names.length1109411161
# expr_id == -1 means this constant came from a `A, B, C = expr`1109511162
# multi-write: its value is computed by the deferred init block1109611163
# below, not from a single per-target expression.11097-
if i < @const_expr_ids.length && @const_expr_ids[i] >= 011164+
if i < @const_expr_ids.length && @const_expr_ids[i] >= 0 && defer_const_init[i] != "1"1109811165
old_scope = @current_lexical_scope1109911166
if i < @const_scope_names.length1110011167
@current_lexical_scope = @const_scope_names[i]@@ -11276,12 +11343,46 @@
1127611343
pe = pe + 11127711344
end1127811345
11279-
# Compile main statements11346+
# Compile main statements. ConstantWriteNode is normally skipped11347+
# because the const-init pre-loop above already emitted it. The11348+
# exception is constants flagged in defer_const_init[]: those have11349+
# an RHS that reads from a main local, so the pre-loop deliberately11350+
# 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+
else11357+
cwn_name = @nd_name[sid]11358+
cwn_idx = -111359+
ck = 011360+
while ck < @const_names.length11361+
if @const_names[ck] == cwn_name11362+
scope_ck = ""11363+
if ck < @const_scope_names.length11364+
scope_ck = @const_scope_names[ck]11365+
end11366+
if scope_ck == ""11367+
cwn_idx = ck11368+
break11369+
end11370+
end11371+
ck = ck + 111372+
end11373+
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 >= 011376+
old_scope_b = @current_lexical_scope11377+
@current_lexical_scope = ""11378+
val_b = compile_expr(eid_b)11379+
@current_lexical_scope = old_scope_b11380+
emit(" cst_" + cwn_name + " = " + val_b + ";")11381+
if type_is_pointer(@const_types[cwn_idx]) == 111382+
emit(" SP_GC_ROOT(cst_" + cwn_name + ");")11383+
end11384+
end11385+
end1128511386
end1128611387
end1128711388
end