Commit 286d05bb
286d05bb0f02Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified
Description
Merge pull request #369 from mame/pr/chained-ivar-op-assign-split
fix(codegen): emit ivar op-assign for all binary operators and as expression value
Details
- Commit
- 286d05bb0f02
- Parent
- b301e8efc327
- Merge parent
- f0b499e86711
spinel_codegen.rbmodifiedView file
@@ -21060,6 +21060,27 @@
2106021060
end2106121061
return "(" + vref + " " + op + "= " + val + ")"2106221062
end21063+
if t == "InstanceVariableOperatorWriteNode"21064+
# `@x OP= expr` used as an expression — typically the RHS of a21065+
# chained ivar write like `@b = @a &= 0x80`. Without this arm21066+
# compile_expr falls through to the catch-all `0`, so the inner21067+
# `@a &=` write never emits and the outer `@b = ...` zeros the21068+
# slot. Mirror the LocalVariableOperatorWriteNode shape — return21069+
# a parenthesized C-style compound assignment so the side effect21070+
# fires AND the value is the result.21071+
op_iow = @nd_binop[nid]21072+
val_iow = compile_expr(@nd_expression[nid])21073+
lhs_iow = ivar_lhs(@nd_name[nid])21074+
ivar_t_iow = ""21075+
if @current_class_idx >= 021076+
ivar_t_iow = cls_ivar_type(@current_class_idx, @nd_name[nid])21077+
end21078+
disp_iow = obj_op_dispatch_expr(ivar_t_iow, lhs_iow, op_iow, val_iow)21079+
if disp_iow != ""21080+
return "(" + lhs_iow + " = " + disp_iow + ")"21081+
end21082+
return "(" + lhs_iow + " " + op_iow + "= " + val_iow + ")"21083+
end2106321084
if t == "LocalVariableOrWriteNode"2106421085
# `local ||= expr` in expression context. Lower as2106521086
# `(local = local ? local : expr)` so the side effect runs@@ -30694,11 +30715,38 @@
3069430715
emit(" " + lhs + " = " + disp + ";")3069530716
return3069630717
end30718+
# Bitwise / shift / arithmetic op-assigns map directly onto C30719+
# `OP=`. Without these arms `@a &= v`, `@a |= v`, etc. were30720+
# silently dropped — the function emit fell through with no30721+
# `iv_<name>` write, so every read after kept the pre-call30722+
# value. Mirror the LocalVariableOperatorWriteNode arm.3069730723
if op == "+"30698-
emit(" " + lhs + " += " + val + ";")30724+
if ivar_t == "string" && infer_type(@nd_expression[nid]) == "string"30725+
emit(" " + lhs + " = sp_str_concat(" + lhs + ", " + val + ");")30726+
else30727+
emit(" " + lhs + " += " + val + ";")30728+
end30729+
return3069930730
end3070030731
if op == "-"3070130732
emit(" " + lhs + " -= " + val + ";")30733+
return30734+
end30735+
if op == "*"30736+
emit(" " + lhs + " *= " + val + ";")30737+
return30738+
end30739+
if op == "/"30740+
emit(" " + lhs + " = sp_idiv(" + lhs + ", " + val + ");")30741+
return30742+
end30743+
if op == "%"30744+
emit(" " + lhs + " = sp_imod(" + lhs + ", " + val + ");")30745+
return30746+
end30747+
if op == "<<" || op == ">>" || op == "&" || op == "|" || op == "^"30748+
emit(" " + lhs + " " + op + "= " + val + ";")30749+
return3070230750
end3070330751
return3070430752
endtest/chained_ivar_op_assign_emits_inner_write.rbaddedView file
Loading diff…
test/chained_ivar_op_assign_emits_inner_write.rb.expectedaddedView file
Loading diff…
test/ivar_op_assign_bitwise_and_shift.rbaddedView file
Loading diff…
test/ivar_op_assign_bitwise_and_shift.rb.expectedaddedView file
Loading diff…