Computed gotos on Reddit
Dmitry Olshansky
dmitry.olsh at gmail.com
Tue Jul 24 06:58:22 PDT 2012
On 24-Jul-12 14:04, Walter Bright wrote:
> 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.
And how is pc is supposed to be an opcode? It's a counter after all...
The trick is that it must be switch(code[pc++])...
It's just if code contains function pointers (or goto jump pointers)
then separate jump table table is not needed:
code = [ &op_1, &op_2, &op_1, ... ]; //generated somewhere
pc = 0;
code[pc]();
// op_1 increments ( or changes somehow) pc, decodes next op and jumps to it
So I still of opinion that enforced tail call is the cleanest way to
support this idiom.
--
Dmitry Olshansky
More information about the Digitalmars-d
mailing list