[fpc-devel] Progress on pure functions
J. Gareth Moreton
gareth at moreton-family.com
Wed Dec 14 12:39:54 CET 2022
On 14/12/2022 10:18, Sven Barth via fpc-devel wrote:
>
> Wouldn't it make more sense to ensure that the Str() and Val()
> intrinsic work correctly inside "pure" functions? After all the
> compiler can then simply evaluate the inlinen nodes and does not
> have to "interpret" a ton of other code as well.
>
So I just tried a simple test with "System.Str(5, Output);" (with Output
defined as a string), and it produced the following nodes:
<calln resultdef="$void" pos="12,3">
...<procname>$fpc_shortstr_sint(Int64;Int64;out OpenString;<const
Int64>);</procname>
...<callparan resultdef="Int64" pos="12,3">
......<ordconstn resultdef="Int64" pos="12,3" flags="nf_explicit"
rangecheck="TRUE">
.........<value>255</value>
......</ordconstn>
...</callparan>
...<callparan resultdef="OpenString" pos="12,23">
......<loadn resultdef="ShortString" pos="12,23" flags="nf_write">
.........<symbol>OUTPUT</symbol>
......</loadn>
...</callparan>
...<callparan resultdef="Int64" pos="12,3">
......<ordconstn resultdef="Int64" pos="12,3" rangecheck="FALSE">
.........<value>-1</value>
......</ordconstn>
...</callparan>
...<callparan resultdef="Int64" pos="12,15">
......<ordconstn resultdef="Int64" pos="12,15" rangecheck="FALSE">
.........<value>5</value>
......</ordconstn>
...</callparan>
</calln>
This is before the first pass, so the parser immediately replaces it
with a call to the internal compiler procedure.
How should I handle this? There are a few problems I need to consider.
If I use a new kind of inline node that expands into a regular call
after the first pass, I'll still need to handle the case where it is a
call node because there are situations during purity analysis where
nested pure functions are not analysed because their actual parameters
haven't yet been fully calculated (usually requires another found of
constant propagation and inline simplification). It may be that I need
to add some kind of field to the TCallNode to indicate which internal
function it's referring to.
I'll try to do a new merge request soon so Str and Val can be simplified
at the node level, as this can easily have benefits outside of pure
functions.
Kit
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20221214/a01631dc/attachment.htm>
More information about the fpc-devel
mailing list