Table lookups - this is pretty definitive

Dmitry Olshansky dmitry.olsh at gmail.com
Wed Apr 2 12:03:25 PDT 2014


02-Apr-2014 15:26, bearophile пишет:
> Dmitry Olshansky:
>
>> _Explicit_ tail call (aka switch-call) would have been cleaner and
>> more modular. It covers all relevant cases of computed-goto such as
>> threaded-code interpreters and also recursion-heavy functional code.
>
> It looks like an additive change. Can you show a possible syntax and
> semantics for D? If it's so good it could also become a DIP later.
>

Easily. In grammar, a new kind of statement:

SwitchCallStatment:
	goto CallExpression;

Semantics:
Same as that of a function call expression except:
a) Can Switch-Call only to a function with the same return type.
b) Anything below this statement is unreachable, i.e. treat it as return.
c) Stack frame of the current function is overwritten with that of 
switched-to function. In other words after a switch-call stack looks as 
if the switched-to function was called instead in the first place.

The last point is what modern tail-call optimizations do when one has 2 
functions that tail-recurse to each other. They overwrite stackframes on 
such tail calls.

However this only happens with certain optimization switch which IMO 
makes the thing totally unreliable. In fact I even observed that I could 
implement threaded-code interpreter by relying on tail-call optimization 
with LDC, but only when compiling with all optimizations enabled. 
Unoptimized builds simply blowup stack. This "semantics are tied to 
optimization" is a situation that I hate about C/C++ and would hope to 
address in D.


-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list