On Thursday, 16 February 2017 at 21:05:51 UTC, Stefan Koch wrote:
[ ... ]

Hi I want to share another story.
I was pretty happy to have recursive function calls working.
So happy in fact that I overlooked that they were actually generated twice.
Let me illustrate what happend.

Suppose we have the following module :
void fn(uint rcount)
{
  uint ctr = rcount;

  while (rcount--)
    ctr += fn(rcount);

  return ctr;
}

pragma(msg, fn(26));

the compiler hits the pragma(msg) and goes on to evaluate fn(26)
newCTFE receives the functionBody as an ast-node.
it starts processing (function 1)
and hits the fn(rcount)

it checks if it has the code for fn already.
this check returns false since fn has not been completely generated yet.

when this check returns it will write the FunctionBody in it's todo list.
it wires up the call to the entry in the todo list. (function 2)
it then hits the end of fn (function 1)and saves it in its code-cache.

now it processes the TODO list
it finds that it has to process fn.
it starts processing fn (function 2)
it hits the call.
This time it does find an entry in the code cache for fn (funcion 1)
it wires of the call
and returns.

The generate pseudo-code looks like :

... fn_0 (...) {
 ...
 call (fn_1, ...)
 ...
}
... fn_1 (...) {
  ...
  call (fn_0, ...)
  ...
}

It was very surprised then I saw this :)

Cheers,
Stefan

Reply via email to