From 7364e2d2d329d81932879a3b838ebda4b71b05a3 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sat, 10 Dec 2022 18:59:03 -0600 Subject: [PATCH] Fix issue with native code optimization of TYA+STA. This would be changed to STY, but that is invalid if the A value is needed afterward. This could affect the code for certain division operations (after the optimizations in commit 4470626adeeeea). Here is an example that would be miscompiled: #pragma optimize -1 #include int main(void) { unsigned i = 55555; unsigned a,b; a = b = i / 10000; printf("%u %u\n", a,b); } Also, remove MVN from the list of "ASafe" instructions since it really isn't, although I don't think this was affecting anything in practice. --- Native.pas | 14 +++++++++----- cc.notes | 2 ++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Native.pas b/Native.pas index c110111..f2ddb42 100644 --- a/Native.pas +++ b/Native.pas @@ -1561,7 +1561,7 @@ var [m_bcc,m_bcs,m_beq,m_bmi,m_bne,m_bpl,m_bra,m_brl,m_bvs,m_jml, m_jmp_indX,m_jsl,m_lda_abs,m_lda_absx,m_lda_dir,m_lda_dirx, m_lda_imm,m_lda_indl,m_lda_indly,m_lda_long,m_lda_longx,m_lda_s, - m_mvn,m_pla,m_rtl,m_rts,m_tdc,m_txa,m_tya,m_tsc,d_end,d_bmov, + m_pla,m_rtl,m_rts,m_tdc,m_txa,m_tya,m_tsc,d_end,d_bmov, d_add,d_pin,d_wrd,d_sym,d_cns] then begin ASafe := true; goto 1; @@ -1890,12 +1890,16 @@ var m_tya: if npeep[ns+1].opcode = m_sta_dir then begin - npeep[ns+1].opcode := m_sty_dir; - Remove(ns); + if ASafe(ns+2) then begin + npeep[ns+1].opcode := m_sty_dir; + Remove(ns); + end; {if} end {if} else if npeep[ns+1].opcode = m_sta_abs then begin - npeep[ns+1].opcode := m_sty_abs; - Remove(ns); + if ASafe(ns+2) then begin + npeep[ns+1].opcode := m_sty_abs; + Remove(ns); + end; {if} end; {else if} m_tyx: diff --git a/cc.notes b/cc.notes index 149f0a3..586005f 100644 --- a/cc.notes +++ b/cc.notes @@ -2051,6 +2051,8 @@ int foo(int[42]); 226. The unary + operator should apply the integer promotions to its operand, e.g. promoting char to int. This can affect the value of sizeof(+expression). +227. If an unsigned 16-bit value was divided by a constant 1000 or larger, and the result was assigned to two different variables (e.g. a = b = c/1000), incorrect code would be generated when using native code peephole optimization. (This was a regression introduced in ORCA/C 2.2.0 B6.) + -- Bugs from C 2.1.0 that have been fixed ----------------------------------- 1. In some situations, fread() reread the first 1K or so of the file.