[fpc-devel] XML node dump feature
J. Gareth Moreton
gareth at moreton-family.com
Mon Jun 24 22:11:18 CEST 2019
"constexpr" is essentially to C++ as "pure" would be to FPC. Some of the
methods that involve pointers and var parameters (which is probably the
closest equivalent to the "&" parameters in C++) may be flagged as
impure because they can't be dereferenced at compile time.
Nevertheless, a function that simply returns a formatted string, rather
than writing directly to an output stream, is certainly doable.
There will likely be a lot of experimentation and trial and error in its
development, but I'll try to at least make it consistent.
I don't quite know how to use generic advanced records yet so that
specific operators are tied to them (can someone enlighten me?), but to
use a vector addition operator as an example, you could probably write
it akin to this:
TVector4 = *packed **record*
X, Y, Z, W: Single;
*operator *+(LHS, RHS: TVector4) Result: TVector4; *vectorcall*; *pure*;
Result.X := LHS.X + RHS.X;
Result.Y := LHS.Y + RHS.Y;
Result.Z := LHS.Z + RHS.Z;
Result.W := LHS.W + RHS.W;
In theory, this would behave as expected, and with some additions to the
compiler under x86 systems, the 4 commands can be collapsed into a
single ADDPS instruction (so long as 'vectorcall' is used) that it may
then be able to inline in the case of non-constant inputs (although this
would have to be done at the node level). I'm not sure how well it would
work on other platforms - I don't know enough about machine codes
outside of the x86 family.
There is a fair bit of overlap between pure and inline routines and they
may internally end up sharing the node tree that gets stored in the PPU
file, although you wouldn't want to inline a particularly complex
function, while a short function that deals with pointers cannot be made
pure. There are also situations where a function is pure in the sense
of being a mathematical function, but are impossible to inline (e.g. the
Ackermann function <https://en.wikipedia.org/wiki/Ackermann_function>).
Note I'm using the directive '*pure*' because Pascal has traditionally
been a language used for programming tuition and hence has relatively
intuitive keywords, so if you see '*pure*', you know it's a pure
function <https://en.wikipedia.org/wiki/Pure_function> analogous to a
mathematical function. If anyone has a better directive name, go for
it! (Note that because it's a directive, it won't cause code to fail to
compile if you happen to have a unit or identifier named 'pure').
Gareth aka. Kit
On 24/06/2019 17:53, Ben Grasset wrote:
> On Sat, Jun 22, 2019 at 4:36 PM J. Gareth Moreton
> <gareth at moreton-family.com <mailto:gareth at moreton-family.com>> wrote:
> I've noticed you're particularly keen on pure functions... what's the
> main use that you envision for them?
> I'm not the same person of course, but I'm pretty excited about them
> in regards to the initialization of constants.
> E.G. I'd love to (eventually, at least) be able to write something
> roughly equivalent to this
> Pascal, where ultimately the structs (or records in Pascal's case)
> simply don't directly exist at all at runtime as they're just
> completely boiled down to their values.
> fpc-devel maillist - fpc-devel at lists.freepascal.org
This email has been checked for viruses by Avast antivirus software.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the fpc-devel