From 0f45e1d0ffec2eba3d3d6561df09599e1d3f907d Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Thu, 18 Feb 2021 20:31:22 -0600 Subject: [PATCH] Fix optimizer bug affecting casts to char types. When an expression that the intermediate code peephole optimizer could reduce to a constant was cast to a char type, the resulting value could be outside the range of that type. The following program illustrates the problem: #pragma optimize 1 #include int main(void) { int i = 0; i = (unsigned char)(i | -1); printf("%i\n", i); } --- DAG.pas | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DAG.pas b/DAG.pas index 784057b..ed27638 100644 --- a/DAG.pas +++ b/DAG.pas @@ -1166,6 +1166,12 @@ case op^.opcode of {check for optimizations of this node} [cgByte,cgUByte,cgWord,cgUWord,cgLong,cgULong,cgReal,cgDouble, cgComp,cgExtended] then begin op^.left^.optype := totype.optype; + if totype.optype in [cgByte,cgUByte] then begin + op^.left^.q := op^.left^.q & $00FF; + if totype.optype = cgByte then + if (op^.left^.q & $0080) <> 0 then + op^.left^.q := op^.left^.q | $FF00; + end; {if} opv := op^.left; end; {if} end {if}