[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 
> <https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAKxAEZSAbAQwDtRkBSAJgCFufSAZ1QBXYskwgA5NwDMeFsgYisAag6yAwnlSCCxTEwC2G7BwAMAQQuWRghcFV70IFKIIa%2BVuw6cEXILr6hiayXtZWBJhGAA7MUepaBACeMZgsxpiqACqmfsQiyASqAGqYyLLqAOzhqjmqAB6kqsnNAF6eNnVoLHqYDTHEpeWyED169XhNquPF2ap4rTOovXMLbQCUqiCNEFMbzcl7yQeqbXub1XxVACJdy6v9g6oAZsR46ejDFaqoacRMIjEfhjFYTMo/bgANgYCEEzVm30q0OIcK2HBq9zqBgIYhYSIgsMEADoGupeKpUSTpkTiclyTxKXC6c1aW0GUySZtOlY6hi7ryHn0BkM3h8WF8IZU/pgAUCALSg1ZI8kwuEIsHFKWqqnozGC7GYXHEfFSwnMsnyzmk1nM%2BlWqks1Rs1QO5ncsL3fn3WZPUXvT4qmVykgAKiV4JGqqJGuV2pRaKuWMpRrxBNpZND1ppdtUWcdSxd%2BfdGx5lj5tx9mr9rwDEqD/0BJAA9BGtVHoTGhe3IVwobqkwaU8bTSNzdTVM3s7aSfSpwWZ8T2fOS2WKwLy92a2LA0EDMZVcGm8CtBpNBA9yFVfDuyroQA3PW1OpORKaM/krgcACsfC4XDfD971JQCtE/ZpuAAs8gLpUDNHAz84NUYD2WgsDIJ/O5v00FgMM9IccTTQQ12qDdvXwiJLF9EVVCYEQiFUcsNBuJEzxeBhUEBUwMR4WhiS4ZouGJAAWZpZGJAAOcjwmo546IYxlmNYrR2M4jxZDMGohPoVRhP45oJOJWRpJsWShnk1BVHgpSmIpPgKLM2j6MslibNdVR7Jk6saIs1RsESFjyyzTzTO8uTnNUAAxALGMnDyyxsBRiiMJgFAgJ8q3opD3wo7p3CQu5cuWLK0PgswirQErTzAyKEtuKQDgYaRvykUgWGkcxWtQaR314fgnFEcQsjkWhWoIDqGoOABrEBvwATmJKooVkWQ%2BzmiTzChLg5toASmqkYTWvaqROtIbqpFawQQHMUhxpOhrSDgWAkDQWI8AYWVyEoV6Yne2UUGYNgAH19BEFgptIF53qiYgrogAAjCbSHhhQmGIZJpFG0hXqMdICAAeRYBgMfu0gsBStgPqR/ADCKPB70wK7Sf6cp6MkKQsaSzB9tOhg8HhgF0c0DB2ax/Q8CMTGHsB9h%2BH4Rh%2BauyADj%2BAgdF6aR5Xx2RLsGiQ6Ea5qjqR86GgkqF5ShYSZhl1QIFB8GtggXBCBIclZB04W3o%2BoYRq2Xq/x4MaJo2aaQFkBauF22RzFoYS5qqWhE6hCTGGkQ62pN6RLuu26Q8emBEECeiYnor7QW9/76EwfAgToUgAHcARiKXDakFrM9J86RtUBvCAQRpzct62lFYRx7YKR3g/u0PSBmvjzC4YTZET79/1oOa5q4cxZChNODuNrvs6EXO7s62f9q4Q/TvO6fz4OBnYfVkBhKAA> in 
> 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
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

This email has been checked for viruses by Avast antivirus software.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190624/5538390b/attachment.html>

More information about the fpc-devel mailing list