5 ms·
> improvements that could be made [...] Make a template interpreter like in the JVM. This will allow your specialized opcodes to directly make use of the call s
by pansa2 6y ago
> improvements that could be made [...] Make a template interpreter like in the JVM. This will allow your specialized opcodes to directly make use of the call stack.
What is a “template interpreter”? How does it differ from a normal bytecode interpreter?
- scaramanga 6y agoAs I understand it, template interpreters work by generating machine code corresponding to the virtual machine instructions and jumping to that machine code. As opposed to sort of a big switch statement that you'd get in a normal bytecode interpreter. A good intro to it here: https://programmersought.com/article/5521858566/ https://programmersought.com/article/5521858566/
- pansa2 6y agoThanks. Is there a performance advantage to the template approach - compared to writing the handler for each opcode directly in assembly language? Or is it just easier to implement?
- KMag 6y agoYou cut out the code between the opcodes that's used for figuring out what to do next. (Removing an indirect jump at the end of each opcode execution.)
- pansa2 6y agoThat sounds more like an advantage of a template compiler, not a template interpreter?
- chrisseaton 6y agoYes people in this thread are confusing the two. It reduces overhead because it directly refers to machine registers and the machine stack to pass information between instructions, and directly uses machine jumps, rather than going through a switch and method calls.
- pansa2 6y agoSounds like it’s similar to how LuaJIT works [0], using a machine-code handler for each VM opcode. AFAICT LuaJIT also uses a templating system to build these, except it generates them at compile-time instead of at run-time. I assume that’s easier than explicitly writing out each handler in assembly language. [0] https://www.reddit.com/r/programming/comments/badl2/luajit_2_beta_3_is_out_support_both_x32_x64/c0lrus0/ https://www.reddit.com/r/programming/comments/badl2/luajit_2...
- tekknolagi 6y agoYep, this is very similar.
- bjoli 6y agoA template JIT is a bit that just generates machine code that is exactly like the bytecode. That is in contrast to something like a tracing JIT that uses runtime information to generate more optimal machine code. One benefit of a template JIT is that the step to native compilation is a lot shorter than with a tracing JIT. Apart from being A LOT simpler. Debugging a tracing JIT is a special kind of hell for extra evil programmers.
- chrisseaton 6y agoThat's a 'template compiler' you're describing there, not a 'template interpreter'. OpenJDK doesn't include a 'template compiler'. Its lowest tier, C1, has a basic IR. OpenJDK does use a 'template interpreter'. https://github.com/openjdk/jdk/blob/master/src/hotspot/share/interpreter/templateInterpreter.cpp https://github.com/openjdk/jdk/blob/master/src/hotspot/share...
- bjoli 6y agoSorry about that. Thanks for putting it straight.
- deleted 6y ago[deleted]
- tekknolagi 6y agoI should be clearer and link to more resources. Check my PL resources page for example. But I also plan on writing a post about template interpreters soon!