[fpc-devel] Progress on pure functions
J. Gareth Moreton
gareth at moreton-family.com
Wed Dec 14 03:24:32 CET 2022
Fixed that bug! Also fixed a bug where the code analysis would get
upset if you have "X mod Y" behind a "if (Y <> 0) then" check, and Y
evaluates to zero. I've attached a new test project to showcase this
(and which revealed the aforementioned bug). My branch also correctly
replaces the function call with the number 12.
Kit
On 14/12/2022 01:17, J. Gareth Moreton via fpc-devel wrote:
> So there are bugs in my pure function code, specifically with the use
> of current_procinfo - I didn't realise until now that the one relating
> to the current function is actually freed after the body has been parsed.
>
> Ideally I would have gone the approach of reusing more of the
> pass1_inline code and replacing the local variables and parameters
> with temprefs, but data flow analysis doesn't yet work properly with
> temprefs (and I haven't been able to work out why... basically if I
> let them get included, their life information isn't filled in, so
> dead-store elimination will strip ALL of the definitions, even the
> ones that are very much needed). I'll keep at it!
>
> Kit
>
> _______________________________________________
> fpc-devel maillist - fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>
-------------- next part --------------
program pure2;
{$MODE OBJFPC}
{$COPERATORS ON}
function GCD(X, Y: Cardinal): Cardinal; pure;
begin
if Y = 0 then
Result := X
else
Result := GCD(Y, X mod Y);
end;
function Factorial(N: Cardinal): Cardinal; pure;
var
X: Integer;
begin
Result := 1;
for X := N downto 2 do
Result *= X;
end;
var
Output: Cardinal;
begin
Output := GCD(84, Factorial(6)); { gcd(84, 720) = 12 }
if Output <> 12 then
begin
WriteLn('Incorrect gcd - got ', Output, ' for gcd(84, 6!) but expected 12');
Halt(1);
end;
WriteLn('ok');
end.
More information about the fpc-devel
mailing list