From 709f9b3f25c7beb6ded5fbaca560c0fa0a49f468 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Wed, 23 Nov 2016 00:56:20 -0600 Subject: [PATCH] Fix bug where comparing 32-bit values in static arrays or structs against 0 may give wrong results with large memory model. The issue was that 16-bit absolute addressing (in the data bank) was being used to access the data to compare, but with the large memory model the static arrays or structs are not necessarily in the same bank, so absolute long addressing should be used. This was sometimes causing failures in the C4.6.4.1.CC and C4.6.6.1.CC conformance tests in the ORCA/C test suite. The following program often demonstrates the problem (depending on memory layout and contents): #pragma memorymodel 1 #pragma optimize 1 #include int i; char ch1[32000]; long L1[1]; int main (void) { if (L1 [0] != 0) printf("%li\n", L1[0]); /* shouldn't print */ /* buggy behavior can happen if the bank bytes of these pointers differ */ printf("%p %p\n", &L1[0], &i); } --- CGI.pas | 1 + Gen.pas | 12 +++++++++--- Native.pas | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CGI.pas b/CGI.pas index 225e231..8d9d339 100644 --- a/CGI.pas +++ b/CGI.pas @@ -119,6 +119,7 @@ const m_ora_dir = 5; m_ora_dirX = 21; m_ora_imm = 9; + m_ora_long = 15; m_ora_longX = 31; m_ora_s = 3; m_pea = 244; diff --git a/Gen.pas b/Gen.pas index 4578189..6b38858 100644 --- a/Gen.pas +++ b/Gen.pas @@ -1375,15 +1375,21 @@ var begin {DoOr} with op^ do begin if opcode = pc_ldo then begin - GenNative(m_lda_abs, absolute, q, lab, 0); - GenNative(m_ora_abs, absolute, q+2, lab, 0); + if smallMemoryModel then begin + GenNative(m_lda_abs, absolute, q, lab, 0); + GenNative(m_ora_abs, absolute, q+2, lab, 0); + end {if} + else begin + GenNative(m_lda_long, longabsolute, q, lab, 0); + GenNative(m_ora_long, longabsolute, q+2, lab, 0); + end; {else} end {if} else begin disp := LabelToDisp(r) + q; if disp < 254 then begin GenNative(m_lda_dir, direct, disp, nil, 0); GenNative(m_ora_dir, direct, disp+2, nil, 0); - end {else if} + end {if} else begin GenNative(m_ldx_imm, immediate, disp, nil, 0); GenNative(m_lda_dirX, direct, 0, nil, 0); diff --git a/Native.pas b/Native.pas index c709c63..63d7ee3 100644 --- a/Native.pas +++ b/Native.pas @@ -765,8 +765,8 @@ case p_opcode of m_adc_abs,m_adc_dir,m_adc_imm,m_adc_s,m_and_abs,m_and_dir,m_and_imm, m_and_s,m_asl_a,m_dea,m_eor_abs,m_eor_dir,m_eor_imm,m_eor_s,m_lda_absx, m_lda_dirx,m_lda_indl,m_lda_indly,m_lda_longx,m_lda_s,m_lsr_a,m_ora_abs, - m_ora_dir,m_ora_dirX,m_ora_imm,m_ora_longX,m_ora_s,m_pla,m_sbc_abs, - m_sbc_dir,m_sbc_imm,m_sbc_s,m_tdc,m_tsc,m_tsb_dir,m_tsb_abs: + m_ora_dir,m_ora_dirX,m_ora_imm,m_ora_long,m_ora_longX,m_ora_s,m_pla, + m_sbc_abs,m_sbc_dir,m_sbc_imm,m_sbc_s,m_tdc,m_tsc,m_tsb_dir,m_tsb_abs: aRegister.condition := regUnknown; m_ldy_absX,m_ldy_dirX,m_ply: