From 77e0b8fc5910b4fc574e6aa1986b752222b812a2 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Wed, 3 Apr 2024 21:04:47 -0500 Subject: [PATCH] Fix codegen error for some indirect accesses to 64-bit values. The code was not properly adding in the offset of the 64-bit value from the pointed-to location, so the wrong memory location would be accessed. This affected indirect accesses to non-initial structure members, when used as operands to certain operations. Here is an example showing the problem: #include long long x = 123456; struct S { long long a; long long b; } s = {0, 123456}; int main(void) { struct S *sp = &s; if (sp->b != x) { puts("error"); } } --- Gen.pas | 1 + cc.notes | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Gen.pas b/Gen.pas index c9b2146..82f5c2f 100644 --- a/Gen.pas +++ b/Gen.pas @@ -321,6 +321,7 @@ case op^.opcode of loc := LabelToDisp(op^.left^.r) + op^.left^.q; if (op^.left^.opcode <> pc_lod) or (loc > 255) then Error(cge1); + offset := offset + op^.q; if offset = 0 then GenNative(mop, direct, loc, nil, 0) else begin diff --git a/cc.notes b/cc.notes index efd3583..d82ea86 100644 --- a/cc.notes +++ b/cc.notes @@ -1622,6 +1622,8 @@ If you use #pragma debug 0x0010 to enable stack check debug code, the compiler w 16. When an expression of const- or volatile-qualified struct or union type was passed as a function parameter, incorrect code would be generated. This could lead to incorrect program behavior or crashes. +17. Incorrect code could sometimes be generated if a long long or unsigned long long value was a non-initial member of a structure, and it was accessed through a pointer to the structure and used as an operand of certain arithmetic, bitwise, or comparison operators. + -- Bugs from C 2.1.1 B3 that have been fixed in C 2.2.0 --------------------- 1. There were various bugs that could cause incorrect code to be generated in certain cases. Some of these were specific to certain optimization passes, alone or in combination.