Computed gotos on Reddit

Walter Bright newshound2 at digitalmars.com
Tue Jul 24 03:04:52 PDT 2012


On 7/24/2012 12:58 AM, Dmitry Olshansky wrote:
> Now if I use final switches there is still:
>
> A) jump table per switch, or maybe less but there is no guarantees
>   (= depend on another optimization that's not here)
> B) it's an ugly and a roundabout way to do this
>
> However I think that always requiring tail call optimization or providing syntax
> to enforce it would work:
>
> void op_1()
> {
>      ...//some code for instruction 1
>      opcode = cast(function void ())code[pc++];
>      goto opcode(); //opcode is pointer to function op_xx
> }
> //can do without goto fn() iff tail call is GUARANTEED

I believe you can do this with:

     switch (pc++)

and there are the same number of indirections.




More information about the Digitalmars-d mailing list