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 end
1456214562 return "(" + self_arrow + sanitize_ivar(iname_w) + " = " + val + ")"
1456314563 end
14564+ if t == "LocalVariableWriteNode"
14565+ # `local = expr` used as an expression (e.g. inside a chained
14566+ # `@a = local = expr` write). Without this branch compile_expr
14567+ # falls through and returns the default "0", which silently
14568+ # zeroed the outer write — every `@_p_nz = ___a__ = __data__`
14569+ # in optcarrots OptimizedCodeBuilder CPU output became
14570+ # `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+ end
14574+ if t == "LocalVariableOperatorWriteNode"
14575+ # `local OP= expr` used as an expression (e.g. ORA's
14576+ # `@_p_nz = ___a__ |= __data__`). Same shape as the chained
14577+ # write above — without this, the op-assign drops out and the
14578+ # 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+ end
1456414583 if t == "LocalVariableOrWriteNode"
1456514584 # `local ||= expr` in expression context. Lower as
1456614585 # `(local = local ? local : expr)` so the side effect runs
@@ -14694,8 +14713,37 @@
1469414713 body = @nd_body[nid]
1469514714 if body >= 0
1469614715 stmts = get_stmts(body)
14697- if stmts.length > 0
14698- return compile_expr(stmts.last)
14716+ if stmts.length == 1
14717+ return compile_expr(stmts[0])
14718+ end
14719+ if stmts.length > 1
14720+ # Multi-statement parens like `(stmt1; stmt2; ... ; expr)`
14721+ # in expression context. We must NOT lower this to a C
14722+ # comma-expression `(a, b, c)` because Cs `expr1 + expr2`
14723+ # has unspecified evaluation order between operands, and
14724+ # if either side has side effects (e.g. RTSs `(___sp__ =
14725+ # ___sp__+1 & 0xff; __ram__[0x100+sp])`), GCC can reorder
14726+ # so the increments fire out of order and the byte pulls
14727+ # come from the wrong stack slots.
14728+ # Strategy: emit the leading statements via emit() so they
14729+ # are sequenced C statements. Then evaluate the final
14730+ # expression and BIND it to a temp before returning, so
14731+ # that an enclosing expression (e.g. `parens1 + parens2`)
14732+ # cant interleave additional side effects from another
14733+ # parens between this ones evaluation and use.
14734+ k = 0
14735+ while k < stmts.length - 1
14736+ v = compile_expr(stmts[k])
14737+ if v != "" && v != "0"
14738+ emit(" " + v + ";")
14739+ end
14740+ k = k + 1
14741+ end
14742+ last_expr = compile_expr(stmts.last)
14743+ last_t = infer_type(stmts.last)
14744+ tmp = new_temp
14745+ emit(" " + c_type(last_t) + " " + tmp + " = " + last_expr + ";")
14746+ return tmp
1469914747 end
1470014748 end
1470114749 return "0"