[fpc-devel] Pure function development

Sven Barth pascaldragon at googlemail.com
Thu Apr 30 07:24:22 CEST 2020

Am 29.04.2020 um 23:59 schrieb J. Gareth Moreton:
> I suppose node emulation would be equivalent to constant propagation.  
> I sense we might need to refactor things afterwards so the same code 
> can be reused for both purposes.  My current design for the emulation 
> would introduce a new virtual method for TNode that descendants 
> override to perform their specific behaviour, rather than have it all 
> in a monolithic 'propagate' procedure.  By default, this method would 
> return a 'fail' code to indicate the function is not pure (a function 
> that is marked as 'pure' but is discovered not to be will raise a 
> compiler warning, and an error if it's part of a constant assignment), 
> so you don't have to program something for every single node and the 
> introduction of a new node type won't break the emulator.
> I think the difference between constant propagation and node emulation 
> in this regard is that the former is trying to simplify nodes and 
> eliminate conditional branches that will not be executed, among other 
> things, while the latter is trying to generate a single output (or 
> multiple if the routine has "out" parameters).

Well, Jonas is right that if you have a node tree that only has constant 
nodes as inputs the tnode.simplify method should (in theory) be able to 
completely collapse that (under the assumption that this is also handled 
by call nodes and such).

Maybe as a first step you could try to
- determine the likely constness of a node during typechecking (the 
final result can then be stored in the implprocoptions of tprocdef)
- store the node tree if it can possibly be pure
- improve simplify of nodes to make use of that
- have this be used in node tree optimizations (like the loop-hoisting 
you mentioned)

If this works correctly we'll already have a first optimization that 
makes use of this and we can in principle see whether the concept works 
without introducing some emulator. After that we can still decide to 
allow this for constant declarations (with or without a pure attribute).


More information about the fpc-devel mailing list