How to Start Learning about Virtual Machines and Language Design?
Asked by
left.handed.scissors
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?
Answers
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/
Reply to this answer
Definitely look into WebAssembly (WASM) if you want to broaden your understanding of virtual machines.
Reply to this answer
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.
left.handed.scissors: Thanks for the recommendation! Iโll definitely give it a read.
Reply to this answer
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!
~+NearlyThere: I appreciate it! I did some digging, but I think a conversation would be more helpful for me. Thanks, though!
Reply to this answer
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.
left.handed.scissors: 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!
Reply to this answer
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!
left.handed.scissors: 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!
Reply to this answer
Add your answer
Answers are reviewed before publication.
left.handed.scissors: Iโll check that out! Thanks for the suggestion!