Node:Dispatch, Previous:Data handling, Up:Concepts



Dispatch

Understanding this section is probably not necessary for using Vmgen, but it may help. You may want to skip it now, and read it if you find statements about dispatch methods confusing.

After executing one VM instruction, the VM interpreter has to dispatch the next VM instruction (Vmgen calls the dispatch routine NEXT). Vmgen supports two methods of dispatch:


switch dispatch
In this method the VM interpreter contains a giant switch statement, with one case for each VM instruction. The VM instruction opcodes are represented by integers (e.g., produced by an enum) in the VM code, and dispatch occurs by loading the next opcode, switching on it, and continuing at the appropriate case; after executing the VM instruction, the VM interpreter jumps back to the dispatch code.
threaded code
This method represents a VM instruction opcode by the address of the start of the machine code fragment for executing the VM instruction. Dispatch consists of loading this address, jumping to it, and incrementing the VM instruction pointer. Typically the threaded-code dispatch code is appended directly to the code for executing the VM instruction. Threaded code cannot be implemented in ANSI C, but it can be implemented using GNU C's labels-as-values extension (see Labels as Values).

Threaded code can be twice as fast as switch dispatch, depending on the interpreter, the benchmark, and the machine.