Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a compiler; which VM?

I'm going to try to write a compiler for a dynamic language. Preferably to some existing virtual machine --- I don't (yet) want to deal with garbage collection and the myriad other concerns a good VM handles for you. What VMs do you suggest?

I'm on Linux, so I don't know if .NET (via Mono) is that good an idea. I've heard that Parrot is good for dynamic languages, but I haven't heard of any language use that. Should I invent my own? Does LLVM even count as a VM I should compile against, or is it as hard as straight x86?

Also, what pros and cons are there to stack-based vs register-based VMs?

Performance and tool support would be important. I'll be writing the compiler in Haskell, so a good interface with that is a plus.

like image 400
pavpanchekha Avatar asked Jul 20 '10 21:07

pavpanchekha


People also ask

What is a virtual machine in compiler?

Virtual machines are programs that compile an intermediate language down to machine code. They are the result of an intelligent two-step compiler design. VMs allows multiple languages to be compiled down to machine code without needing separate hardware-specific compilers for each individual language.

Is interpreter virtual machine?

Would the interpreter be considered a virtual machine? Conceptually, a VM is a program that other programs use as a platform to run inside of. Thus this interpreter suits the definition. But this interpreter only interprets plain-text source code into executions (and not bytecode).

How do you write a compiler in Go?

Writing A Compiler In Go is the sequel to Writing An Interpreter In Go. It starts right where the first one stopped, with a fully-working, fully-tested Monkey interpreter in hand, connecting both books seamlessly, ready to build a compiler and a virtual machine for Monkey.


2 Answers

JVM (Java) and the CLR (.NET) seem to be the two most common targets for this, as they both handle most of these issues for you. Both provide fairly straightforward instruction sets to work with.

The CLR has one advantage - it was really designed with the goal of supporting multiple languages from the start, and it's (IMO) slightly easier to work with, especially if you're not going to be writing a language that fits into the original "mold" of the initial languages targeting that runtime. Mono works well enough that I wouldn't shy away from a CLR target because of it.

like image 82
Reed Copsey Avatar answered Sep 25 '22 20:09

Reed Copsey


LLVM gives you a much better programming model than straight x86 assembly. Yes, it's low-level. But you don't have to worry about register schedulign or fully optimizing your output. Also, while you're still writing your front-end, you can take advantage of its type system to catch mistakes you might make.

That said, you'll have to develop your own runtime layer to take care of the "dynamic" parts of your language. Just for that part alone, I might tend to stick with CLR.

like image 37
Karmastan Avatar answered Sep 24 '22 20:09

Karmastan