[virtmach] First VM

Michael Wolf mwolf@netspace.net.au
Tue, 14 Nov 2000 14:22:14 +1100


On Mon, Nov 13, 2000 at 06:49:47PM -0700, Tom Fjellstrom wrote:
> I just finished ripping out that massive switch statement and put in
> place
> a direct threaded implementation using a table of labels and goto.
> but now the new version is irreversibly GCC only...

If the old and new are sufficiently similar, I bet you could do something
portable to GCC and ANSI C. 

Due to the nature of code which relies on a lot of static tables with a lot
of related data in each, the code probably lends itself well to being
automatically generated based on 1 file with definitions and several
template .c files.  

So, for example, in chip.h you have 
    enum chipop { 
        NOOP = 0, 
        MUL, 
        DIV, 
        ...
    };
and in chip.c you have
    OPDEF opargs[MAXOP] = {
        { NOOP, 0},
	...
    }
and
    switch (vm->vm_chip.ir) {
        case (NOOP):
	    ...
	...
    }

and so on.  It's redundant, but that's bad only because the files are
written and generated by hand.  You could automatically generate all of this
stuff all at once when you build Chip.

I think with a bit of effort, you could support both the label pointer table
and the switch statement.  

A definition language might look like:
    opcode, num_args, switch_func, switch_c_operator, label
    MUL, 2, 8BIT, *=, mul_label
    MULL, 2, 32BIT, *=, mull_label
or something similar -- I've probably got the exact details quite wrong.
When generating code, use that data and fill in the templates.

I would probably do this in perl, and I've heard m4 is suitable for this
sort of thing, but I don't know m4, so take that advice with a grain of
salt.  :)

Does this make sense?  Let me know how it turns out.  

mike