[fpc-devel] Pure function development

J. Gareth Moreton gareth at moreton-family.com
Wed Apr 29 23:29:56 CEST 2020


On 29/04/2020 22:15, Sven Barth via fpc-devel wrote:
> Assuming we want to have support for constant evaluation of functions 
> (and I do admit that sometimes that is a really nice to have), I don't 
> really see a way around marking such functions somehow, cause 
> otherwise we'd have to store the node trees of all functions that we 
> detect as pure in the PPU.

That and also attempting to propagate or evaluate every single function 
you come across will make the compiler prohibitively slow.  Inline 
functions benefit most from constant propagation because a copy of the 
node tree exists and you can often cut away entire branches based on the 
propagation of constant input parameters.

Pure functions are a slightly tricky one because there is a lot of 
overlap with inline functions and constant propagation, and yet there 
are concepts that aren't covered by either, such as 'promotion' of pure 
function calls outside of loops and assignment of pure function results 
to compile-time constants.  Also, pure functions can be recursive, like 
with the factorial function, but there will have to be safeguards 
against infinite loops or other constructs designed to break the 
compiler in that regard (e.g. the Ackermann Function).

I'm sure others can come up with uses for pure functions.

Gareth aka. Kit


-- 
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus



More information about the fpc-devel mailing list