On 06/01/2013 07:29 AM, Alex Rønne Petersen wrote:
Hi,

I'm sure this has been brought up before, but I feel I need to bring it
up again (because I'm going to be writing a threaded-code interpreter):
http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

This is an incredibly important extension. The final switch statement is
not a replacement because it doesn't allow the programmer to store a
label address directly into a code stream, which is what's essential to
write a threaded-code interpreter.


I'd also like to see this.

The Erlang folks went through hell just to use this feature; see the 5th
Q at:
http://www.erlang.org/doc/installation_guide/INSTALL-WIN32.html#Frequently-Asked-Questions


The idea is to be able to write code like this:

----

import std.algorithm;

enum Op : ubyte
{
     imm,
     add,
     sub,
     // ...
     ret,
}

final class Insn
{
     Op op;
     size_t[] args;
     void* lbl;
     Insn next;
}

final class State
{
     Insn pc;
     size_t[64] regs;
}

size_t interp(Insn[] code)
{
     // Set up the instruction stream with label addresses
     // the first time that it is executed. Label addresses
     // are stable, so we only do this once.

     foreach (insn; code.filter!(x => !x.lbl)())
     {
         void* lbl;

         with (Op)
         {
             final switch (insn.op)
             {
                 case imm: lbl = &&handle_imm; break;
                 case add: lbl = &&handle_add; break;
                 case sub: lbl = &&handle_sub; break;
                 // ...
                 case ret: lbl = &&handle_ret; break;
             }
         }

         insn.lbl = lbl;
     }
...

(This must be supported at compile time!)

Reply via email to