From 30e0df384d5961c5525c70f2878963f04ec9bafd Mon Sep 17 00:00:00 2001 From: florian Date: Thu, 22 Dec 2022 22:41:17 +0100 Subject: [PATCH] * second part of #40041 fixed + tests --- compiler/nadd.pas | 2 +- tests/webtbs/tw39785.pp | 28 ++++++++++++++++++++------ tests/webtbs/tw40041b.pp | 24 ++++++++++++++++++++++ tests/webtbs/tw40041.pp => tw40041a.pp | 0 4 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 tests/webtbs/tw40041b.pp rename tests/webtbs/tw40041.pp => tw40041a.pp (100%) diff --git a/compiler/nadd.pas b/compiler/nadd.pas index 865d1adac6..e6f6c3bac6 100644 --- a/compiler/nadd.pas +++ b/compiler/nadd.pas @@ -1495,7 +1495,7 @@ implementation begin Result:=getcopy; taddnode(Result).left.nodetype:=addn; - taddnode(left).right:=cunaryminusnode.create(taddnode(left).right); + taddnode(taddnode(Result).left).right:=cunaryminusnode.create(taddnode(taddnode(Result).left).right); exit; end; diff --git a/tests/webtbs/tw39785.pp b/tests/webtbs/tw39785.pp index f282ea599d..10c13ac15f 100644 --- a/tests/webtbs/tw39785.pp +++ b/tests/webtbs/tw39785.pp @@ -4,38 +4,54 @@ label A0, A1, B0, B1, C0, C1, D0, D1; var x, y: single; - dontDrop: string; size: ptrint; begin - x := random; + x := random(0); A0: y := x + 1 + 2 + 3 + 4 + 5 + 6; // folds A1: - writestr(dontDrop, y); size:=CodePointer(@A1) - CodePointer(@A0); writeln('x + 1 + 2 + 3 + 4 + 5 + 6 = ', y, ' ', CodePointer(@A1) - CodePointer(@A0), ' b'); + if y <> 21 then + begin + writeln('Expected 21!'); + halt(1); + end; B0: y := x + 1 - 2 + 3 - 4 + 5 - 6; // doesn’t fold B1: - writestr(dontDrop, y); if CodePointer(@B1) - CodePointer(@B0) > size then halt(1); writeln('x + 1 - 2 + 3 - 4 + 5 - 6 = ', y, ' ', CodePointer(@B1) - CodePointer(@B0), ' b'); + if y <> -3 then + begin + writeln('Expected -3!'); + halt(2); + end; C0: y := x - 1 - 2 - 3 - 4 - 5 - 6; // didn't fold at the time of making the issue, now folds C1: - writestr(dontDrop, y); if CodePointer(@C1) - CodePointer(@C0) > size then halt(1); writeln('x - 1 - 2 - 3 - 4 - 5 - 6 = ', y, ' ', CodePointer(@C1) - CodePointer(@C0), ' b'); + if y <> -21 then + begin + writeln('Expected -21!'); + halt(3); + end; + D0: y := x - 1 + 2 - 3 + 4 - 5 + 6; // didn't fold at the time of making the issue, now folds D1: - writestr(dontDrop, y); if CodePointer(@D1) - CodePointer(@D0) > size then halt(1); writeln('x - 1 + 2 - 3 + 4 - 5 + 6 = ', y, ' ', CodePointer(@D1) - CodePointer(@D0), ' b'); + if y <> 3 then + begin + writeln('Expected 3!'); + halt(4); + end; end. diff --git a/tests/webtbs/tw40041b.pp b/tests/webtbs/tw40041b.pp new file mode 100644 index 0000000000..030227a0cd --- /dev/null +++ b/tests/webtbs/tw40041b.pp @@ -0,0 +1,24 @@ +{$mode objfpc} +function ShiftPM(x: single): single; noinline; +begin + result := x + 10.0 - 1.0; +end; + +function ShiftMP(x: single): single; noinline; +begin + result := x - 1.0 + 10.0; +end; + +begin + if ShiftPM(5.0) <> 14.0 then + begin + writeln('ShiftPM: got ', ShiftPM(5.0):0:1, ', expected 14.0.'); + halt(1); + end; + + if ShiftMP(5.0) <> 14.0 then + begin + writeln('ShiftMP: got ', ShiftMP(5.0):0:1, ', expected 14.0.'); + halt(2); + end; +end. diff --git a/tests/webtbs/tw40041.pp b/tw40041a.pp similarity index 100% rename from tests/webtbs/tw40041.pp rename to tw40041a.pp