Thread overview | ||||||
---|---|---|---|---|---|---|
|
June 13, 2005 Address of label | ||||
---|---|---|---|---|
| ||||
It would be nice to get the address of a label using the operator '&'. The return value has type void*, and it can be used in a goto statement. For example: void* [] inst = [ &iconst0, &iconst1, &iadd, &exit ]; int pc = 0; goto inst[pc]; iconst0: push(0); goto inst[++pc]; iconst1: push(1); goto inst[++pc]; iadd: push(pop() + pop()); goto inst[++pc]; exit: trace(); ... Very useful for fast dispatching in an interpreter. |
June 13, 2005 Re: Address of label | ||||
---|---|---|---|---|
| ||||
Posted in reply to zwang | Huhhh... Are you sure??? This code looks like assembly or Basic. Do you need any idea for structured/object oriented solution? Tamas Nagy In article <d8kep6$24c0$1@digitaldaemon.com>, zwang says... > >It would be nice to get the address of a label using the operator '&'. The return value has type void*, and it can be used in a goto statement. For example: > > void* [] inst = [ &iconst0, &iconst1, &iadd, &exit ]; > int pc = 0; > goto inst[pc]; > iconst0: push(0); goto inst[++pc]; > iconst1: push(1); goto inst[++pc]; > iadd: push(pop() + pop()); goto inst[++pc]; > exit: trace(); > ... > > >Very useful for fast dispatching in an interpreter. |
June 13, 2005 Re: Address of label | ||||
---|---|---|---|---|
| ||||
Posted in reply to zwang | zwang wrote:
> It would be nice to get the address of a label using the operator '&'.
> The return value has type void*, and it can be used in a goto statement.
> For example:
>
> void* [] inst = [ &iconst0, &iconst1, &iadd, &exit ];
> int pc = 0;
> goto inst[pc];
> iconst0: push(0); goto inst[++pc];
> iconst1: push(1); goto inst[++pc];
> iadd: push(pop() + pop()); goto inst[++pc];
> exit: trace();
> ...
>
>
> Very useful for fast dispatching in an interpreter.
I see what you're doing, and I've done similar things, but would it be equally easy to use function pointers (or delegates) and either a dynamic or associative array? Then 'goto inst[pc];' becomes something like 'inst[pc]();' which is a little odd looking, but still clear (imho).
-- Chris Sauls
|
June 13, 2005 Re: Address of label | ||||
---|---|---|---|---|
| ||||
Posted in reply to MicroWizard | >> It would be nice to get the address of a label using the operator '&'.
>> The return value has type void*, and it can be used in a goto statement.
>> For example:
>>
>> void* [] inst = [ &iconst0, &iconst1, &iadd, &exit ];
>> int pc = 0;
>> goto inst[pc];
>> iconst0: push(0); goto inst[++pc];
>> iconst1: push(1); goto inst[++pc];
>> iadd: push(pop() + pop()); goto inst[++pc];
>> exit: trace();
>> ...
>>
>>
>> Very useful for fast dispatching in an interpreter.
I think you should use a simple switch-case-construct, and let the optimizations to the compiler:
enum Token
{
TokAdd,
TokSub,
// ...
}
// ...
foreach (Token tok; tokenList)
{
switch (tok)
{
case TokAdd: push(0); break;
case TokSub: push(1); break;
}
}
This may or may not be as fast as your idea, but it is simple, less error-prone and easy to understand. If profiling shows that this very statement is a bottleneck, you can still code it in assembly (builtin feature). For your example, i would assume that the stack should be the primary focus of optimizations:
push(pop() + pop())
is quite a lot of code if not carefully implemented...
Ciao
uwe
|
Copyright © 1999-2021 by the D Language Foundation