[fpc-devel] Progress of pure function research

J. Gareth Moreton gareth at moreton-family.com
Mon Jul 16 18:54:51 CEST 2018


In that situation, yes it is.  Max is a very simple function though, and it gets
more complicated if it appears in another pure function where the parameters are
variables, but whose values are deterministic.  In the end, Max is a function
that can be pure and inline.

For optimization, I figured to keep error checking and debugging consistent, the
compiler will attempt to evaluate the value of such functions at compile time (so
any errors are brought to light), but only replace the function call on -O2 and
higher.

Gareth aka. Kit


On Mon 16/07/18 11:43 , Martok listbox at martoks-place.de sent:
> Am 16.07.2018 um 07:01 schrieb J. Gareth Moreton:
> 
> > As stated in the Wiki page, my first test case
> is the Max function.  Since it
> > works both as an inline and a pure function, I
> can easily change the directive to
> > analyse the code flow in the compiler.
> 
> I may have missed this in the discussion before. But isn't that a prime
> example
> for "simple" const propagation?
> 
> 
> 
> ==========================================
> 
> function Max(a, b: integer): integer; inline;
> 
> begin
> 
> if a > b then
> 
> Result:= a
> 
> else
> 
> Result:= b;
> 
> end;
> 
> 
> 
> z:= Max(1, 2);
> 
> ==========================================
> 
> That already gets optimized to `z:= 2;` on -O1, while the following needs
> -O2,
> but gets to the same result:
> 
> ==========================================
> 
> x:= 1;
> 
> y:= 2;
> 
> z:= Max(x, y);
> 
> ==========================================
> 
> 
> 
> Tail recursion expansion could do the same for certain recursive functions.
> 
> 
> 
> 
> 
> -- 
> 
> Regards,
> 
> Martok
> 
> 
> 
> 
> 
> _______________________________________________
> 
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
> 
> 
> 
> 




More information about the fpc-devel mailing list