[virtmach] Your VM

Thomas Fjellstrom virtmach@iecc.com
Wed, 30 May 2001 06:05:04 -0600


rog@vitanuova.com wrote:
> 
> > Now I have a question: does any one know for sure
> > if an array of function pointers is faster than
> > a switch case? I know threaded code is faster but
> > is an function pointer array? (will it help the
> > cpu cache enough to make a difference?)
> 
> apart from speed considerations, i recently came across a useful reason
> for implementing as an array of function pointers rather than a switch
> statement: the core execution loop is very small, so it's possible to
> have alternative execution loops for very little cost.
> 
> the Inferno VM uses this to implement single-step/breakpoint debugging
> - a process being debugged uses an alternative core execution function;
> others use the usual one (which is only 22 lines of code).
> 
> so not only is the code prettier, it's also substantially more
> versatile.
> 
>   cheers,
>     rog.

In my VM the 'core' loop is only 12 lines long and
calls the main 'exec' function that containes the
switch case statement that defines the ops like:

case MOVL:
   traceop(MOVL);
   ARG1_NON_LIT("MOVL");
   ARG2_NORMAL_32BIT("MOVL", =);
   break;

the 2 ARG* macros basically expand to

arg1 = ...
arg2 = ...
*arg2 = *arg1;

In debug mode the traceop() macro expands
to a print statement that prints out the
instruction exactly as it was exicuted.
(including the args.)

-- 
"Computer programmers don't byte, they nybble a bit."
Thomas Fjellstrom
tfjellstrom@home.com
http://strangesoft.net/