digitalmars.D - Address of label
- zwang (12/12) Jun 13 2005 It would be nice to get the address of a label using the operator '&'.
- MicroWizard (5/17) Jun 13 2005 Huhhh...
- Uwe Salomon (26/41) Jun 13 2005 I think you should use a simple switch-case-construct, and let the
- Chris Sauls (6/21) Jun 13 2005 I see what you're doing, and I've done similar things, but would it be e...
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.
Jun 13 2005
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.
Jun 13 2005
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 uweIt 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.
Jun 13 2005
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
Jun 13 2005