Commit f84f485c

f84f485cb86aMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): ParenthesesNode multi-stmt + LocalVariableWriteNode in expr context

Two compile_expr bugs surfaced via diff with Ruby on optcarrot:

  1. ParenthesesNode with multiple statements (stmt1; stmt2; ...; expr) only emitted the LAST statement, dropping side effects in the leading statements. RTSs (___sp__ = (___sp__+1) & 0xff; __ram__[0x100+___sp__]) lost the sp increment, so each ram pull used the same un-incremented sp; both pulls returned the high byte, RTS jumped to the wrong return address, and the program walked off into ROM memory at $b0a2 / $ffff.

    We cannot lower this as a C comma-expression (a, b) because Cs expr1 + expr2 has unspecified evaluation order between operands, and reordering would interleave the side effects from the two parens. Instead emit the leading statements via emit() and bind the final value to a fresh temp before returning, so the C compiler cant interleave with another parenss side effects.

  2. LocalVariableWriteNode and LocalVariableOperatorWriteNode were missing from compile_expr — fell through to the default "0" return. So @a = local = expr and @a = local OP= expr chains silently zeroed the outer ivar. Lower as (local = ...) and (local OP= ...) parenthesized assignments.

The build-script chained-assignment regex workarounds are also removed: spinel codegen now compiles those chains correctly without source rewriting.

After this, the first 2053 cpu_poke calls in spinel match Ruby exactly (boot setup + RAM clear + STA $2000=0x80 enabling NMI). The next divergence is at the second JSR-to-vblank-wait, where spinels PPU doesnt flip vblank again — separate bug.

Details

Commit
f84f485cb86a
Parent
78de785dc7bf

spinel_codegen.rbmodifiedView file

@@ -13978,6 +13978,25 @@
1397813978 end
1397913979 return "(" + self_arrow + sanitize_ivar(iname_w) + " = " + val + ")"
1398013980 end
13981+ if t == "LocalVariableWriteNode"
13982+ # `local = expr` used as an expression (e.g. inside a chained
13983+ # `@a = local = expr` write). Without this branch compile_expr
13984+ # falls through and returns the default "0", which silently
13985+ # zeroed the outer write — every `@_p_nz = ___a__ = __data__`
13986+ # in optcarrots OptimizedCodeBuilder CPU output became
13987+ # `iv__p_nz = 0`. Lower as a parenthesized C-style assignment.
13988+ val = compile_expr(@nd_expression[nid])
13989+ return "(" + fiber_var_ref(@nd_name[nid]) + " = " + val + ")"
13990+ end
13991+ if t == "LocalVariableOperatorWriteNode"
13992+ # `local OP= expr` used as an expression (e.g. ORA's
13993+ # `@_p_nz = ___a__ |= __data__`). Same shape as the chained
13994+ # write above — without this, the op-assign drops out and the
13995+ # outer write zeroes the ivar.
13996+ op = @nd_binop[nid]
13997+ val = compile_expr(@nd_expression[nid])
13998+ return "(" + fiber_var_ref(@nd_name[nid]) + " " + op + "= " + val + ")"
13999+ end
1398114000 if t == "ConstantReadNode"
1398214001 if @nd_name[nid] == "ARGV"
1398314002 return "sp_argv"
@@ -14072,8 +14091,37 @@
1407214091 body = @nd_body[nid]
1407314092 if body >= 0
1407414093 stmts = get_stmts(body)
14075- if stmts.length > 0
14076- return compile_expr(stmts.last)
14094+ if stmts.length == 1
14095+ return compile_expr(stmts[0])
14096+ end
14097+ if stmts.length > 1
14098+ # Multi-statement parens like `(stmt1; stmt2; ... ; expr)`
14099+ # in expression context. We must NOT lower this to a C
14100+ # comma-expression `(a, b, c)` because Cs `expr1 + expr2`
14101+ # has unspecified evaluation order between operands, and
14102+ # if either side has side effects (e.g. RTSs `(___sp__ =
14103+ # ___sp__+1 & 0xff; __ram__[0x100+sp])`), GCC can reorder
14104+ # so the increments fire out of order and the byte pulls
14105+ # come from the wrong stack slots.
14106+ # Strategy: emit the leading statements via emit() so they
14107+ # are sequenced C statements. Then evaluate the final
14108+ # expression and BIND it to a temp before returning, so
14109+ # that an enclosing expression (e.g. `parens1 + parens2`)
14110+ # cant interleave additional side effects from another
14111+ # parens between this ones evaluation and use.
14112+ k = 0
14113+ while k < stmts.length - 1
14114+ v = compile_expr(stmts[k])
14115+ if v != "" && v != "0"
14116+ emit(" " + v + ";")
14117+ end
14118+ k = k + 1
14119+ end
14120+ last_expr = compile_expr(stmts.last)
14121+ last_t = infer_type(stmts.last)
14122+ tmp = new_temp
14123+ emit(" " + c_type(last_t) + " " + tmp + " = " + last_expr + ";")
14124+ return tmp
1407714125 end
1407814126 end
1407914127 return "0"