Comment by CodeArtisan
10 hours ago
# decode next bytecode opcode
movzwl 8(%r12), %eax
# advance bytecode pointer to next bytecode
addq $8, %r12
# load the interpreter function for next bytecode
movq __deegen_interpreter_dispatch_table(,%rax,8), %rax
# dispatch to next bytecode
jmpq *%rax
You may reduce that even further by pre-decoding the bytecode: you replace a bytecode by the address of the its implementation and then do (with GCC extended goto)
goto *program_bytecodes[counter]
I've been playing around with this and its worth noting that pre-decoding the bytecode because it means every instruction (without operands) is the width of a pointer (8 bytes on x86) which means you fit far fewer instructions into cache, eg my opcodes are a byte, so that's 8x more instructions. I haven't had time to compare it in benchmarks to see what the real world difference is, but its worth keeping in mind.
Somewhat off topic, looking at that assembly... mine compiles to (for one of the opcodes):
(also compiled from C++ with clang's musttail annotation)
I have wondered whether it's worth storing instruction offsets (from the first instruction) rather than raw instruction pointers to increase cache efficiency, then they could be encoded in just 2 (or at worst 3) bytes. At the cost of an extra register.
That sounds like a good middle ground. Ahh I wish I had some more time, I’d love to benchmark all three and see how they compare.
In my own you VM, I do actually have a spare general purpose registers available for use. Now I just need to find the time to try it out…