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 >= 0
87698769 iname = @nd_name[nid]
87708770 expr_id = @nd_expression[nid]
8771+ # Drill through chained `@a = @b = ... = expr` so the chain head
8772+ # sees the bottom rhs type — infer_type returns the default
8773+ # "int" for an InstanceVariableWriteNode expr, which would
8774+ # leave @a's slot un-widened against a real concrete bottom
8775+ # type and force compile_stmt to emit a type-mismatched store.
8776+ bottom = expr_id
8777+ is_chain_head = 0
8778+ while bottom >= 0 && @nd_type[bottom] == "InstanceVariableWriteNode"
8779+ bottom = @nd_expression[bottom]
8780+ is_chain_head = 1
8781+ end
87718782 # Empty `{}` / `[]` literal: don't reset the ivar's tracked
87728783 # type to the default (`str_int_hash` / `int_array`), since a
87738784 # later `[]=` write may have already promoted the slot to a
87748785 # more specific type. Reseeding from the empty-default would
87758786 # 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) == 0
8777- at = infer_type(expr_id)
8778- if at != "int" && at != "nil"
8787+ if is_empty_hash_literal(bottom) == 0 && is_empty_array_literal(bottom) == 0
8788+ at = infer_type(bottom)
8789+ # Chain heads must widen even on int/nil rhs — the head's
8790+ # current slot type may be a concrete pointer type that
8791+ # update_ivar_type promotes to poly (or the nullable
8792+ # variant) once it sees the bottom rhs.
8793+ if is_chain_head == 1
8794+ update_ivar_type(@current_class_idx, iname, at)
8795+ elsif at != "int" && at != "nil"
87798796 update_ivar_type(@current_class_idx, iname, at)
87808797 end
87818798 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 needs
14055- # 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 = 1
14061- iv_ctor = "sp_StrStrHash_new()"
14062- elsif ivt == "int_str_hash"
14063- @needs_int_str_hash = 1
14064- iv_ctor = "sp_IntStrHash_new()"
14065- elsif ivt == "sym_int_hash"
14066- @needs_sym_int_hash = 1
14067- iv_ctor = "sp_SymIntHash_new()"
14068- elsif ivt == "sym_str_hash"
14069- @needs_sym_str_hash = 1
14070- 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 is
14072+ # evaluated once via compile_chained_ivar_writes; the
14073+ # local emit_raw special-cases below assume a non-chained
14074+ # rhs and would otherwise re-evaluate side-effecting
14075+ # CallNodes per slot.
14076+ if @nd_type[expr_id_iv] == "InstanceVariableWriteNode"
14077+ compile_stmt(sid)
14078+ else
14079+ # Match the special-case in compile_stmt: empty `{}` / `[]`
14080+ # assigned to an ivar promoted by scan_writer_calls needs
14081+ # 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 = 1
14087+ iv_ctor = "sp_StrStrHash_new()"
14088+ elsif ivt == "int_str_hash"
14089+ @needs_int_str_hash = 1
14090+ iv_ctor = "sp_IntStrHash_new()"
14091+ elsif ivt == "sym_int_hash"
14092+ @needs_sym_int_hash = 1
14093+ iv_ctor = "sp_SymIntHash_new()"
14094+ elsif ivt == "sym_str_hash"
14095+ @needs_sym_str_hash = 1
14096+ 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+ end
1407514102 end
14076- end
14077- if iv_ctor == "" && is_empty_array_literal(expr_id_iv) == 1 && ivt != ""
14078- iv_ctor = empty_array_new_for_type(ivt)
14079- end
14080- if iv_ctor != ""
14081- @needs_gc = 1
14082- emit_raw(" " + self_arrow + ivar + " = " + iv_ctor + ";")
14083- else
14084- # Issue #130: same poly-slot boxing as the general
14085- # InstanceVariableWriteNode emit path (compile_expr branch).
14086- # Initialize bodies can introduce one of the disagreeing
14087- # 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+ end
14106+ if iv_ctor != ""
14107+ @needs_gc = 1
14108+ emit_raw(" " + self_arrow + ivar + " = " + iv_ctor + ";")
1409014109 else
14091- val = compile_expr(expr_id_iv)
14110+ # Issue #130: same poly-slot boxing as the general
14111+ # InstanceVariableWriteNode emit path (compile_expr branch).
14112+ # Initialize bodies can introduce one of the disagreeing
14113+ # writes to a multi-typed ivar.
14114+ if ivt == "poly"
14115+ val = box_expr_to_poly(expr_id_iv)
14116+ else
14117+ val = compile_expr(expr_id_iv)
14118+ end
14119+ emit_raw(" " + self_arrow + ivar + " = " + val + ";")
1409214120 end
14093- emit_raw(" " + self_arrow + ivar + " = " + val + ";")
1409414121 end
1409514122 else
1409614123 if is_super == false
@@ -21907,6 +21934,86 @@
2190721934 box_value_to_poly(at, val)
2190821935 end
2190921936
21937+ # Emit a chained `@a = @b = ... = expr` write as one rhs evaluation
21938+ # plus N per-slot stores. Caller has verified that `nid` is an
21939+ # InstanceVariableWriteNode whose expression is itself an
21940+ # InstanceVariableWriteNode. Each slot is typed independently (some
21941+ # widened to poly by scan_writer_calls, others kept native), so the
21942+ # 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 bottom
21945+ # rhs node id.
21946+ ivars = []
21947+ cur = nid
21948+ while @nd_type[cur] == "InstanceVariableWriteNode"
21949+ ivars.push(@nd_name[cur])
21950+ cur = @nd_expression[cur]
21951+ end
21952+ rhs_id = cur
21953+ rhs_type = infer_type(rhs_id)
21954+ # NilNode rhs has no side effect and `compile_expr(NilNode)` is the
21955+ # null-pointer-constant `0`, which assigns cleanly to any slot
21956+ # type. Routing it through a typed temp would force a `mrb_int 0`
21957+ # variable into pointer slots, which C won't implicitly null. Emit
21958+ # one store per slot through the unchained IVW path; each slot
21959+ # picks its own boxing rule (poly slots box to sp_box_nil()).
21960+ if @nd_type[rhs_id] == "NilNode"
21961+ ki = 0
21962+ while ki < ivars.length
21963+ cur_iv = nid
21964+ kj = 0
21965+ while kj < ki
21966+ cur_iv = @nd_expression[cur_iv]
21967+ kj = kj + 1
21968+ end
21969+ saved_expr = @nd_expression[cur_iv]
21970+ @nd_expression[cur_iv] = rhs_id
21971+ compile_stmt(cur_iv)
21972+ @nd_expression[cur_iv] = saved_expr
21973+ ki = ki + 1
21974+ end
21975+ return
21976+ end
21977+ # General case: evaluate the rhs once into a typed temp, then
21978+ # store into each slot. Side-effecting rhs (CallNode etc.) must
21979+ # not be replicated.
21980+ rhs_expr = compile_expr(rhs_id)
21981+ tmp = new_temp
21982+ emit(" " + c_type(rhs_type) + " " + tmp + " = " + rhs_expr + ";")
21983+ ki = 0
21984+ while ki < ivars.length
21985+ iname_i = ivars[ki]
21986+ ivt_i = ""
21987+ if @current_class_idx >= 0
21988+ ivt_i = cls_ivar_type(@current_class_idx, iname_i)
21989+ end
21990+ val_i = tmp
21991+ if ivt_i == "poly" && rhs_type != "poly"
21992+ val_i = box_value_to_poly(rhs_type, tmp)
21993+ end
21994+ mod_ivar = 0
21995+ mi3 = 0
21996+ while mi3 < @module_names.length
21997+ 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 >= 0
22003+ emit(" cst_" + cname3 + " = " + val_i + ";")
22004+ mod_ivar = 1
22005+ end
22006+ end
22007+ end
22008+ mi3 = mi3 + 1
22009+ end
22010+ if mod_ivar == 0
22011+ emit(" " + self_arrow + sanitize_ivar(iname_i) + " = " + val_i + ";")
22012+ end
22013+ ki = ki + 1
22014+ end
22015+ end
22016+
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 destination
2191222019 # 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 as
23421+ # `iv_a = (iv_b = (... = expr))` — one `expr` evaluation, but the
23422+ # outer slot tries to absorb the inner write's typed value, so
23423+ # disagreeing slot types fail the C compile. Per-slot recursion
23424+ # would re-evaluate `expr` once per ivar and double-fire side
23425+ # effects on a CallNode rhs. Compute `expr` once into a typed
23426+ # temp and assign each slot from the temp, boxing to poly only
23427+ # for the slots that scan_writer_calls already widened.
23428+ if @nd_type[expr_id] == "InstanceVariableWriteNode"
23429+ compile_chained_ivar_writes(nid)
23430+ return
23431+ end
2331323432 # Empty `{}` / `[]` literal assigned to an ivar that scan_writer_calls
2331423433 # has promoted to a non-default container type. The literal emitters use
2331523434 # default empty container types, so without this special-case the ivar