Search code examples
vm-implementationcompiler-theorycompiler-construction

Cross Compiling vs Virtual Machines


Clarification

When I mention cross compiling I mean from one language to another (think GWT), rather than from a host platform to a targeted platform.

Background

I am developing an Arabic programming language which I cross compiled to Java, this saved me the platform specific hassles. Now I had to put this on hold and turned to cross compiling to C for various reasons.

I want to develop a single library that would be replaced with an equivalent library of the system it's running on when compiled.

For example, if the programmer writes a GUI drawing function in the Arabic programming language and compiles, it would be cross compiled to win32 code if compiled under Windows, GTK+ under Gnome, Qt under KDE, etc. The same approach will be used for other libraries as well.

Question

Is it worth going through all this trouble to end up with a compiled executable or am I better off with a Virtual Machine approach? Advantages and disadvantages of picking either (from the language developer perspective rather than the programmer using the language)? Are there any other factors I have to consider?

Any reference links for further reading would be greatly appreciated :)


Solution

  • You should cross-compile to the language that best supports the assumptions and ideas underlying your language. If your language is garbage collected, all function calls are method calls, and doesn't let you work with addresses as data, then Java is the language to be compiling to.

    There are compilers that compile Java code to native. So if you want your program to run natively, you can use those.

    When you are ready to make your own full compiler for your language, then you can divorce yourself from Java or whatever it is you choose.

    This basic approach has been used by a number of languages. Usually the target language is C because you can do low-level stuff in C that you can't do in any other language, it has mature optimizing compilers, and runs very fast natively. The way C's symbols, libraries and calling conventions work is also well understood and tends to be a rigorously defined and supported by several languages on any given platform. This allows the new language to have immediate access to a whole host of libraries written for C.

    C++ started this way (the original compiler was called cfront), and I believe OCaml started this way too. I think if you dig a little you can find numerous other languages for which this is true.

    But if you don't need Cs low-level features a lot of those arguments apply to Java as well. A number of newer languages (Scala, for example) use the JVM in this way.