Commit 3b24523b
3b24523ba11bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified
Description
Merge pull request #234 from mame/pr/chained-ivar-write-split
fix(codegen): chained @a = @b = expr evaluates RHS once via poly
Details
- Commit
- 3b24523ba11b
- Parent
- 4510cc9c4f62
- Merge parent
- dadb281a6ed9
spinel_codegen.rbmodifiedView file
@@ -8768,14 +8768,31 @@
87688768
if @current_class_idx >= 087698769
iname = @nd_name[nid]87708770
expr_id = @nd_expression[nid]8771+
# Drill through chained `@a = @b = ... = expr` so the chain head8772+
# sees the bottom rhs type — infer_type returns the default8773+
# "int" for an InstanceVariableWriteNode expr, which would8774+
# leave @a's slot un-widened against a real concrete bottom8775+
# type and force compile_stmt to emit a type-mismatched store.8776+
bottom = expr_id8777+
is_chain_head = 08778+
while bottom >= 0 && @nd_type[bottom] == "InstanceVariableWriteNode"8779+
bottom = @nd_expression[bottom]8780+
is_chain_head = 18781+
end87718782
# Empty `{}` / `[]` literal: don't reset the ivar's tracked87728783
# type to the default (`str_int_hash` / `int_array`), since a87738784
# later `[]=` write may have already promoted the slot to a87748785
# more specific type. Reseeding from the empty-default would87758786
# widen the promoted type to poly on the next iteration.8776-
if is_empty_hash_literal(expr_id) == 0 && is_empty_array_literal(expr_id) == 08777-
at = infer_type(expr_id)8778-
if at != "int" && at != "nil"8787+
if is_empty_hash_literal(bottom) == 0 && is_empty_array_literal(bottom) == 08788+
at = infer_type(bottom)8789+
# Chain heads must widen even on int/nil rhs — the head's8790+
# current slot type may be a concrete pointer type that8791+
# update_ivar_type promotes to poly (or the nullable8792+
# variant) once it sees the bottom rhs.8793+
if is_chain_head == 18794+
update_ivar_type(@current_class_idx, iname, at)8795+
elsif at != "int" && at != "nil"87798796
update_ivar_type(@current_class_idx, iname, at)87808797
end87818798
end@@ -14050,47 +14067,57 @@
1405014067
ivar_name = @nd_name[sid]1405114068
ivar = sanitize_ivar(ivar_name)1405214069
expr_id_iv = @nd_expression[sid]14053-
# Match the special-case in compile_stmt: empty `{}` / `[]`14054-
# assigned to an ivar promoted by scan_writer_calls needs14055-
# the matching container constructor.14056-
ivt = cls_ivar_type(@current_class_idx, ivar_name)14057-
iv_ctor = ""14058-
if is_empty_hash_literal(expr_id_iv) == 1 && ivt != "" && ivt != "str_int_hash"14059-
if ivt == "str_str_hash"14060-
@needs_str_str_hash = 114061-
iv_ctor = "sp_StrStrHash_new()"14062-
elsif ivt == "int_str_hash"14063-
@needs_int_str_hash = 114064-
iv_ctor = "sp_IntStrHash_new()"14065-
elsif ivt == "sym_int_hash"14066-
@needs_sym_int_hash = 114067-
iv_ctor = "sp_SymIntHash_new()"14068-
elsif ivt == "sym_str_hash"14069-
@needs_sym_str_hash = 114070-
iv_ctor = "sp_SymStrHash_new()"14071-
elsif ivt == "str_poly_hash"14072-
iv_ctor = "sp_StrPolyHash_new()"14073-
elsif ivt == "sym_poly_hash"14074-
iv_ctor = "sp_SymPolyHash_new()"14070+
# Chained `@a = @b = ... = expr` inside `def initialize`.14071+
# Delegate to compile_stmt's IVW path so the rhs is14072+
# evaluated once via compile_chained_ivar_writes; the14073+
# local emit_raw special-cases below assume a non-chained14074+
# rhs and would otherwise re-evaluate side-effecting14075+
# CallNodes per slot.14076+
if @nd_type[expr_id_iv] == "InstanceVariableWriteNode"14077+
compile_stmt(sid)14078+
else14079+
# Match the special-case in compile_stmt: empty `{}` / `[]`14080+
# assigned to an ivar promoted by scan_writer_calls needs14081+
# the matching container constructor.14082+
ivt = cls_ivar_type(@current_class_idx, ivar_name)14083+
iv_ctor = ""14084+
if is_empty_hash_literal(expr_id_iv) == 1 && ivt != "" && ivt != "str_int_hash"14085+
if ivt == "str_str_hash"14086+
@needs_str_str_hash = 114087+
iv_ctor = "sp_StrStrHash_new()"14088+
elsif ivt == "int_str_hash"14089+
@needs_int_str_hash = 114090+
iv_ctor = "sp_IntStrHash_new()"14091+
elsif ivt == "sym_int_hash"14092+
@needs_sym_int_hash = 114093+
iv_ctor = "sp_SymIntHash_new()"14094+
elsif ivt == "sym_str_hash"14095+
@needs_sym_str_hash = 114096+
iv_ctor = "sp_SymStrHash_new()"14097+
elsif ivt == "str_poly_hash"14098+
iv_ctor = "sp_StrPolyHash_new()"14099+
elsif ivt == "sym_poly_hash"14100+
iv_ctor = "sp_SymPolyHash_new()"14101+
end1407514102
end14076-
end14077-
if iv_ctor == "" && is_empty_array_literal(expr_id_iv) == 1 && ivt != ""14078-
iv_ctor = empty_array_new_for_type(ivt)14079-
end14080-
if iv_ctor != ""14081-
@needs_gc = 114082-
emit_raw(" " + self_arrow + ivar + " = " + iv_ctor + ";")14083-
else14084-
# Issue #130: same poly-slot boxing as the general14085-
# InstanceVariableWriteNode emit path (compile_expr branch).14086-
# Initialize bodies can introduce one of the disagreeing14087-
# writes to a multi-typed ivar.14088-
if ivt == "poly"14089-
val = box_expr_to_poly(expr_id_iv)14103+
if iv_ctor == "" && is_empty_array_literal(expr_id_iv) == 1 && ivt != ""14104+
iv_ctor = empty_array_new_for_type(ivt)14105+
end14106+
if iv_ctor != ""14107+
@needs_gc = 114108+
emit_raw(" " + self_arrow + ivar + " = " + iv_ctor + ";")1409014109
else14091-
val = compile_expr(expr_id_iv)14110+
# Issue #130: same poly-slot boxing as the general14111+
# InstanceVariableWriteNode emit path (compile_expr branch).14112+
# Initialize bodies can introduce one of the disagreeing14113+
# writes to a multi-typed ivar.14114+
if ivt == "poly"14115+
val = box_expr_to_poly(expr_id_iv)14116+
else14117+
val = compile_expr(expr_id_iv)14118+
end14119+
emit_raw(" " + self_arrow + ivar + " = " + val + ";")1409214120
end14093-
emit_raw(" " + self_arrow + ivar + " = " + val + ";")1409414121
end1409514122
else1409614123
if is_super == false@@ -21907,6 +21934,86 @@
2190721934
box_value_to_poly(at, val)2190821935
end2190921936
21937+
# Emit a chained `@a = @b = ... = expr` write as one rhs evaluation21938+
# plus N per-slot stores. Caller has verified that `nid` is an21939+
# InstanceVariableWriteNode whose expression is itself an21940+
# InstanceVariableWriteNode. Each slot is typed independently (some21941+
# widened to poly by scan_writer_calls, others kept native), so the21942+
# store boxes through sp_RbVal only for the poly slots.21943+
def compile_chained_ivar_writes(nid)21944+
# Collect the chain of ivar names (head -> tail) and the bottom21945+
# rhs node id.21946+
ivars = []21947+
cur = nid21948+
while @nd_type[cur] == "InstanceVariableWriteNode"21949+
ivars.push(@nd_name[cur])21950+
cur = @nd_expression[cur]21951+
end21952+
rhs_id = cur21953+
rhs_type = infer_type(rhs_id)21954+
# NilNode rhs has no side effect and `compile_expr(NilNode)` is the21955+
# null-pointer-constant `0`, which assigns cleanly to any slot21956+
# type. Routing it through a typed temp would force a `mrb_int 0`21957+
# variable into pointer slots, which C won't implicitly null. Emit21958+
# one store per slot through the unchained IVW path; each slot21959+
# picks its own boxing rule (poly slots box to sp_box_nil()).21960+
if @nd_type[rhs_id] == "NilNode"21961+
ki = 021962+
while ki < ivars.length21963+
cur_iv = nid21964+
kj = 021965+
while kj < ki21966+
cur_iv = @nd_expression[cur_iv]21967+
kj = kj + 121968+
end21969+
saved_expr = @nd_expression[cur_iv]21970+
@nd_expression[cur_iv] = rhs_id21971+
compile_stmt(cur_iv)21972+
@nd_expression[cur_iv] = saved_expr21973+
ki = ki + 121974+
end21975+
return21976+
end21977+
# General case: evaluate the rhs once into a typed temp, then21978+
# store into each slot. Side-effecting rhs (CallNode etc.) must21979+
# not be replicated.21980+
rhs_expr = compile_expr(rhs_id)21981+
tmp = new_temp21982+
emit(" " + c_type(rhs_type) + " " + tmp + " = " + rhs_expr + ";")21983+
ki = 021984+
while ki < ivars.length21985+
iname_i = ivars[ki]21986+
ivt_i = ""21987+
if @current_class_idx >= 021988+
ivt_i = cls_ivar_type(@current_class_idx, iname_i)21989+
end21990+
val_i = tmp21991+
if ivt_i == "poly" && rhs_type != "poly"21992+
val_i = box_value_to_poly(rhs_type, tmp)21993+
end21994+
mod_ivar = 021995+
mi3 = 021996+
while mi3 < @module_names.length21997+
mmod = @module_names[mi3]21998+
if mmod != ""21999+
if @current_method_name.start_with?(mmod + "_cls_")22000+
cname3 = mmod + "_" + iname_i[1, iname_i.length - 1]22001+
ci3 = find_const_idx(cname3)22002+
if ci3 >= 022003+
emit(" cst_" + cname3 + " = " + val_i + ";")22004+
mod_ivar = 122005+
end22006+
end22007+
end22008+
mi3 = mi3 + 122009+
end22010+
if mod_ivar == 022011+
emit(" " + self_arrow + sanitize_ivar(iname_i) + " = " + val_i + ";")22012+
end22013+
ki = ki + 122014+
end22015+
end22016+
2191022017
# Emit a runtime loop that pushes every element of the array `src_expr`2191122018
# (a node id whose value is some typed array) onto the destination2191222019
# int_array variable `dst`. Used when expanding `*args` into a rest@@ -23310,6 +23417,18 @@
2331023417
if t == "InstanceVariableWriteNode"2331123418
iname = @nd_name[nid]2331223419
expr_id = @nd_expression[nid]23420+
# Chained `@a = @b = ... = expr`. The naive emit lowers this as23421+
# `iv_a = (iv_b = (... = expr))` — one `expr` evaluation, but the23422+
# outer slot tries to absorb the inner write's typed value, so23423+
# disagreeing slot types fail the C compile. Per-slot recursion23424+
# would re-evaluate `expr` once per ivar and double-fire side23425+
# effects on a CallNode rhs. Compute `expr` once into a typed23426+
# temp and assign each slot from the temp, boxing to poly only23427+
# for the slots that scan_writer_calls already widened.23428+
if @nd_type[expr_id] == "InstanceVariableWriteNode"23429+
compile_chained_ivar_writes(nid)23430+
return23431+
end2331323432
# Empty `{}` / `[]` literal assigned to an ivar that scan_writer_calls2331423433
# has promoted to a non-default container type. The literal emitters use2331523434
# default empty container types, so without this special-case the ivartest/chained_ivar_write_call_rhs.rbaddedView file
Loading diff…
test/chained_ivar_write_call_rhs.rb.expectedaddedView file
Loading diff…
test/chained_ivar_write_split.rbaddedView file
Loading diff…
test/chained_ivar_write_split.rb.expectedaddedView file
Loading diff…