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 end
2106121061 return "(" + vref + " " + op + "= " + val + ")"
2106221062 end
21063+ if t == "InstanceVariableOperatorWriteNode"
21064+ # `@x OP= expr` used as an expression — typically the RHS of a
21065+ # chained ivar write like `@b = @a &= 0x80`. Without this arm
21066+ # compile_expr falls through to the catch-all `0`, so the inner
21067+ # `@a &=` write never emits and the outer `@b = ...` zeros the
21068+ # slot. Mirror the LocalVariableOperatorWriteNode shape — return
21069+ # a parenthesized C-style compound assignment so the side effect
21070+ # 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 >= 0
21076+ ivar_t_iow = cls_ivar_type(@current_class_idx, @nd_name[nid])
21077+ end
21078+ 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+ end
21082+ return "(" + lhs_iow + " " + op_iow + "= " + val_iow + ")"
21083+ end
2106321084 if t == "LocalVariableOrWriteNode"
2106421085 # `local ||= expr` in expression context. Lower as
2106521086 # `(local = local ? local : expr)` so the side effect runs
@@ -30694,11 +30715,38 @@
3069430715 emit(" " + lhs + " = " + disp + ";")
3069530716 return
3069630717 end
30718+ # Bitwise / shift / arithmetic op-assigns map directly onto C
30719+ # `OP=`. Without these arms `@a &= v`, `@a |= v`, etc. were
30720+ # silently dropped — the function emit fell through with no
30721+ # `iv_<name>` write, so every read after kept the pre-call
30722+ # 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+ else
30727+ emit(" " + lhs + " += " + val + ";")
30728+ end
30729+ return
3069930730 end
3070030731 if op == "-"
3070130732 emit(" " + lhs + " -= " + val + ";")
30733+ return
30734+ end
30735+ if op == "*"
30736+ emit(" " + lhs + " *= " + val + ";")
30737+ return
30738+ end
30739+ if op == "/"
30740+ emit(" " + lhs + " = sp_idiv(" + lhs + ", " + val + ");")
30741+ return
30742+ end
30743+ if op == "%"
30744+ emit(" " + lhs + " = sp_imod(" + lhs + ", " + val + ");")
30745+ return
30746+ end
30747+ if op == "<<" || op == ">>" || op == "&" || op == "|" || op == "^"
30748+ emit(" " + lhs + " " + op + "= " + val + ";")
30749+ return
3070230750 end
3070330751 return
3070430752 end