Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

VM instruction implementation without huge switch()?

Name: Anonymous 2014-06-09 23:26

How would one go about making a virtual machine without using a huge and clumsy switch( opcode ) statement?

The architecture I'm implementing (DCPU-16) has about 35-40 different instructions.

Name: Anonymous 2014-06-11 4:26

>>19
In the beginning there were only int and char,

>>25
I find that sort of usage annoying since it will trigger "enum constant not covered" warnings in switch statements. One of many annoyances with enums in C... (I'll confess I (ab)use them frequently, solely because it makes it easy to traverse definitions with ctags.)

I prefer this ENTERPRISE QUALITY ENUM HANDING:

#include <errno.h>

typedef enum { /* yes typedef, I hate typing "enum" */
FOO,
BAR,
BAZ,
QUX,
} meta meta_t; /* and the POSIX committee can bite me */

static void do_foo();
static void do_bar();
static void do_baz();
static void do_qux();

/* ... */

int dispatch_meta(meta_t val)
{
switch (val) {
case FOO:
return do_foo();
case BAR:
return do_bar();
case BAZ:
return do_baz();
case QUX:
return do_qux();
}

return -EINVAL;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List