From 25500815179508d2e7eb01846468db94274ff3ca Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sun, 4 Dec 2022 21:54:29 -0600 Subject: [PATCH] Fix bug with 4-byte comparisons against globals in large memory model. Long addressing was not being used to access the values, which could lead to mis-evaluation of comparisons against values in global structs, unions, or arrays, depending on the memory layout. This could sometimes affect the c99desinit.c test, when run with large memory model and at least intermediate code peephole optimization. It could also affect this simpler test (depending on memory layout): #pragma memorymodel 1 #pragma optimize 1 struct S { void *p; } s = {&s}; int main(void) { return s.p != &s; /* should be 0 */ } --- Gen.pas | 14 +++++++++++--- cc.notes | 2 ++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Gen.pas b/Gen.pas index ccabb39..376a61b 100644 --- a/Gen.pas +++ b/Gen.pas @@ -2048,9 +2048,17 @@ var lab1 := GenLabel; with op^ do begin if opcode = pc_ldo then begin - GenNative(m_cmp_abs, absolute, q, lab, 0); - GenNative(m_bne, relative, lab1, nil, 0); - GenNative(m_cpx_abs, absolute, q+2, lab, 0); + if smallMemoryModel then begin + GenNative(m_cmp_abs, absolute, q, lab, 0); + GenNative(m_bne, relative, lab1, nil, 0); + GenNative(m_cpx_abs, absolute, q+2, lab, 0); + end {if} + else begin + GenNative(m_cmp_long, longabsolute, q, lab, 0); + GenNative(m_bne, relative, lab1, nil, 0); + GenImplied(m_txa); + GenNative(m_cmp_long, longabsolute, q+2, lab, 0); + end; {else} end {if} else begin disp := LabelToDisp(r) + q; diff --git a/cc.notes b/cc.notes index 225f627..73bdec8 100644 --- a/cc.notes +++ b/cc.notes @@ -2013,6 +2013,8 @@ int foo(int[42]); 223. Expressions of floating-point type could not be used in initializers for integer variables with static storage duration. This should be allowed, with a conversion performed as in the case of assignment. +224. Comparisons against four-byte values in global structures, unions, or arrays might not work correctly when using the large memory model. + -- Bugs from C 2.1.0 that have been fixed ----------------------------------- 1. In some situations, fread() reread the first 1K or so of the file.