Reimplementing the bulk of std.meta iteratively

Stefan Koch uplink.coder at gmail.com
Tue Sep 29 12:38:42 UTC 2020


On Tuesday, 29 September 2020 at 03:14:34 UTC, Andrei 
Alexandrescu wrote:
I'd rather live without that DIP.
>
> So I'm interested in exploring reification mainly - 
> overwhelmingly - because it already works in the existing 
> language. To the extent it has difficulties, it's because of 
> undue limitations in CTFE and introspection. And these are 
> problems we must fix anyway. So it's all a virtuous circle. 
> Bringing myself to write `reify` and `dereify` around my types 
> is a very small price to pay for all that.

Alright mate,
Let's assume we had the minimal langauge.
Very small core.
All features are library.

You end up with LISP.
What's with optimization?
Optimization, just in case you don't know,
is nothing but a semantic invariant transform, which is based on 
proving that observed semantics are actually invariant.
Let's take:
int mul(
   int x,
   int y
) {
   int result = 0;
   for(int i = 0; i < x; i++)
   {
     result += y;
   }
   return result;
}

A sufficiently advanced optimizer should be able to most every 
calls to `mul` into an imul instruction.

If that is the case there is no need to burden the language with 
a * operator and you could make the parser much simpler.

and Indeed ldc with -O3 will generate the following sequence for 
the function:

	imull	%esi, %edi // y *= x
	xorl	%eax, %eax // result = 0
	testl	%esi, %esi // if (y)
	cmovgl	%edi, %eax // if (x > 0) result = x;
         retq               // return result;

There are a few superfluous checks, for example the loop entrance 
condition (x > 0)
So how does this compare if we write a function that only does * ?

int mul(
   int x,
   int y
) {
   return x*y;
}

The code we get, from the same compiler and the same compiler 
flags (-O3) is this:

	movl	%edi, %eax // result = x;
	imull	%esi, %eax // result *= y;
	retq               // return result;

Which removes one test, and one xor, furthermore one move becomes 
unconditional.
(That reduces code size and the load on the branch predictor.)




More information about the Digitalmars-d mailing list