3 ms·
Actually, direct threading can be done with C/C++: I just wrote a switch statement with 256 case labels and made sure the switch control variable is unsigned ch
by mojuba 18y ago
Actually, direct threading can be done with C/C++: I just wrote a switch statement with 256 case labels and made sure the switch control variable is unsigned char. With -O3 it gave me exactly the code I wanted - no range checking anymore, since the control variable is unsigned char anyway.
- cwp 18y agoNot quite. If you're dispatching based on a sequence of opcodes, that's indirect threading. Direct threading uses a sequence of pointers to the opcode implementations, and jumps directly to them. Also, one of the key benefits of threaded interpreters is that each opcode implementation ends by decoding the next instruction and jumping to its implementation. This duplication of code gives the processor more context for branch prediction, and can lead to fewer pipeline stalls. With a switch statement, the decoding and branching all happens at the top of the loop.