Hey everyone! I'm diving into the world of virtual machines, specifically stack-based and register-based types. My goal is to build one from the ground up because I'm really interested in how languages like Java function. I'm not aiming for anything groundbreaking, but I'm keen to understand the mechanics behind language design—like why certain languages are structured in specific ways and which ones are optimized for portability, much like Java. So, I'm looking to learn about various virtual machine types, especially the stack-based and register-based approaches. Any advice on where to start?
6 Answers
I've tinkered with a few languages and found stack-based languages are usually easier to implement than register-based ones. Register-based VMs can be faster and easier to convert to native code later if you want to add JIT/AOT. If you're interested in speed, consider using direct threaded code, which is like stack-based but generates machine code instruction directly—no need for a separate VM. A good start would be learning how to write a lexer, a parser for an AST, and then an emitter for generating bytecode. It’s totally possible to skip the AST for simpler languages, too!
Instead of focusing on VM implementation right away, you might want to start by writing a parser for your language, which can create an Abstract Syntax Tree (AST) and execute it directly. This can be a faster way to get going than diving into a VM.
Haha, I’m interested in all aspects! But I think I’ll follow your advice. I’ve done some parsing work, but I’m unsure whether to prioritize the VM or the parser. Thanks for the insight!
I’d suggest doing a bit of research first to formulate more specific questions. Just asking 'how do I start?' can be too broad. If you're still stuck, talking to an AI like ChatGPT could help!
I appreciate it! I did some digging, but I think a conversation would be more helpful for me. Thanks, though!
If you're into designing programming languages, I recommend starting with a simple Lisp. It could be your foundation for everything else. Check out the book 'Structure and Interpretation of Computer Programs' for a deeper dive.
Thanks for the recommendation! I’ll definitely give it a read.
Definitely look into WebAssembly (WASM) if you want to broaden your understanding of virtual machines.
I’ll check that out! Thanks for the suggestion!
I found some great resources online for building VMs, like courses and tutorials. Here are a few:
- https://www.udemy.com/course/virtual-machine/
- https://www.jmeiners.com/lc3-vm/
- https://craftinginterpreters.com/a-virtual-machine.html
- https://dmitrysoshnikov.teachable.com/p/virtual-machine
- https://www.andreinc.net/2021/12/01/writing-a-simple-vm-in-less-than-125-lines-of-c
- https://dev.to/bosley/building-a-virtual-machine-3ocj
- https://blog.subnetzero.io/post/building-language-vm-part-00/
Thank you so much for the detailed info! I began crafting my lexer but got puzzled about whether I should focus on the VM or the language itself first. The tidbit about the Java compiler is super helpful!