Commit 7a38b375
7a38b3751353Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #188 from mame/pr/parens-multi-stmt
fix(codegen): ParenthesesNode multi-stmt + LocalVariableWriteNode in expr context
Conflicts:
spinel_codegen.rb
Details
- Commit
- 7a38b3751353
- Parent
- dc9979041919
- Merge parent
- f84f485cb86a
spinel_codegen.rbmodifiedView file
@@ -14561,6 +14561,25 @@
1456114561
end1456214562
return "(" + self_arrow + sanitize_ivar(iname_w) + " = " + val + ")"1456314563
end14564+
if t == "LocalVariableWriteNode"14565+
# `local = expr` used as an expression (e.g. inside a chained14566+
# `@a = local = expr` write). Without this branch compile_expr14567+
# falls through and returns the default "0", which silently14568+
# zeroed the outer write — every `@_p_nz = ___a__ = __data__`14569+
# in optcarrots OptimizedCodeBuilder CPU output became14570+
# `iv__p_nz = 0`. Lower as a parenthesized C-style assignment.14571+
val = compile_expr(@nd_expression[nid])14572+
return "(" + fiber_var_ref(@nd_name[nid]) + " = " + val + ")"14573+
end14574+
if t == "LocalVariableOperatorWriteNode"14575+
# `local OP= expr` used as an expression (e.g. ORA's14576+
# `@_p_nz = ___a__ |= __data__`). Same shape as the chained14577+
# write above — without this, the op-assign drops out and the14578+
# outer write zeroes the ivar.14579+
op = @nd_binop[nid]14580+
val = compile_expr(@nd_expression[nid])14581+
return "(" + fiber_var_ref(@nd_name[nid]) + " " + op + "= " + val + ")"14582+
end1456414583
if t == "LocalVariableOrWriteNode"1456514584
# `local ||= expr` in expression context. Lower as1456614585
# `(local = local ? local : expr)` so the side effect runs@@ -14694,8 +14713,37 @@
1469414713
body = @nd_body[nid]1469514714
if body >= 01469614715
stmts = get_stmts(body)14697-
if stmts.length > 014698-
return compile_expr(stmts.last)14716+
if stmts.length == 114717+
return compile_expr(stmts[0])14718+
end14719+
if stmts.length > 114720+
# Multi-statement parens like `(stmt1; stmt2; ... ; expr)`14721+
# in expression context. We must NOT lower this to a C14722+
# comma-expression `(a, b, c)` because Cs `expr1 + expr2`14723+
# has unspecified evaluation order between operands, and14724+
# if either side has side effects (e.g. RTSs `(___sp__ =14725+
# ___sp__+1 & 0xff; __ram__[0x100+sp])`), GCC can reorder14726+
# so the increments fire out of order and the byte pulls14727+
# come from the wrong stack slots.14728+
# Strategy: emit the leading statements via emit() so they14729+
# are sequenced C statements. Then evaluate the final14730+
# expression and BIND it to a temp before returning, so14731+
# that an enclosing expression (e.g. `parens1 + parens2`)14732+
# cant interleave additional side effects from another14733+
# parens between this ones evaluation and use.14734+
k = 014735+
while k < stmts.length - 114736+
v = compile_expr(stmts[k])14737+
if v != "" && v != "0"14738+
emit(" " + v + ";")14739+
end14740+
k = k + 114741+
end14742+
last_expr = compile_expr(stmts.last)14743+
last_t = infer_type(stmts.last)14744+
tmp = new_temp14745+
emit(" " + c_type(last_t) + " " + tmp + " = " + last_expr + ";")14746+
return tmp1469914747
end1470014748
end1470114749
return "0"test/parens_multi_stmt_and_local_write_expr.rbaddedView file
Loading diff…