Get address of label?
Heywood Floyd
soul8o8 at gmail.com
Sat Dec 25 14:46:11 PST 2010
Thanks for the answer!
Ok, hm, how about this then:
auto opTable = [&op_add, &op_cmp, &op_end]; //etc.
ubyte[] eval(ubyte[] prog)
{
int ip = 0, sp = 0;
ubyte[4096] stack;
next:
goto opTable[prog[ip++] & OP_TABLE_MASK];
op_add:
stack[sp] += stack[sp-1];
goto next;
op_cmp:
stack[sp] = stack[sp-1] == stack[sp-2];
goto next;
/// and so on...
op_end:
return stack;
}
What I'm looking for here is a way of interpreting code without creating branches in the machine code, unless the interpreted code actually does a branch (ie a conditional jump). Seems to me a switch would introduce branching (?) of some sort.
I mean, even if switch is implemented as a jump table, it would still do some basic bounds checking, or?
I'm also interested in trying to inline the "next"-operation here, ie like
string op_next(){ return "goto opTable[prog[ip++] & OP_TABLE_MASK];"; }
//...
op_add:
stack[sp] += stack[sp-1];
mixin(op_next());
..in order to reduce a jump. Of course I'm just playing around with different strategies for creating a fast interepreter. In C, at least, using a jump table instead of a switch is faster, especially in 32-bit mode (according to some very simple experiments, which may or may not hold water in reality).
Any ideas for how to make a jump-table interpreter in D? Is it doable with inline asm perhaps? If at least not for any other reason than to investigate if it's faster. (Or is it a stupid idea to begin with? Is this overkill? : )
cheers!
BR
/HF
PS. How do you export assembler code from the DMD-compiler?
Simen kjaeraas Wrote:
> Heywood Floyd <soul8o8 at gmail.com> wrote:
>
> > Is this possible somehow:
> > int op(int r, int i)
> > {
> > static auto tbl = [&add, &sub, &mul];
> > goto tbl[i % 3];
> > add:
> > r++;
> > goto end;
> > sub:
> > r--;
> > goto end;
> > mul:
> > r*=r;
> > goto end;
> > end:
> > return r;
> > }
>
> Absolutely:
>
> enum ops : int {
> add = 0,
> sub = 1,
> mul = 2;
> }
>
> int op( int r, int i ) {
> switch( i % 3 ) {
> case add:
> r++;
> break;
> case sub:
> r--;
> break;
> case mul:
> r*=r;
> break;
> }
> return r;
> }
>
> --
> Simen
More information about the Digitalmars-d-learn
mailing list