Computed gotos on Reddit

Walter Bright newshound2 at digitalmars.com
Tue Jul 24 10:03:14 PDT 2012


On 7/24/2012 6:58 AM, Dmitry Olshansky wrote:
>>> 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.
>
> And how is pc is supposed to be an opcode? It's a counter after all...

     switch (pc++)

and
     goto code[pc++]

are the same (same as in same number of indirections).

     switch (code[pc++])

and

     goto code[pc++]()

are the same, too.


More information about the Digitalmars-d mailing list