Which backend languages should my compiler target?
I've written a compiler for a general-purpose programming language that produces an optimised parse tree of its input. This intermediate format is then run through a preprocessor to translate it into a target language for subsequent compilation to a native executable.
At present the only target language is C++, but I'd like to offer other solutions as well, in case certain programs might benefit from being compiled with a backend that better supports certain constructs in the source language.
Are there any languages that are designed for or very well suited to the role of compiler target?
I'm aware of LLVM, and though it's an exciting project in its own right, I believe it's too lo开发者_如何学JAVAw-level to target directly. I'm looking for generic, mid- to high-level languages with high-quality implementations, whose syntax is capable of being produced by a C preprocessor — thus nothing like Python or Ruby. Support for lambdas would be nice, but isn't strictly necessary.
JavaScript, I'd add. Syntax like C, has lambdas, very popular, has very fast HQ implementations which compile to native code and is available everywhere. Double-plus: you can demo your compiler in any web browser and everybody is listening today, if you have something that cranks out JavaScript.
If your backend would be just plain C you wouldn't have support for several constructs of your base language, but you would gain compatibility to other languages (nearly all languages can be easyly linked to C libraries). You could write some parts of your program in your language and some parts in other languages. And its fast to compile.
Many parser generators are generating C code, so its also not uncommon.
Neko is an high-level language/VM that has been expressely designed as target for compilers. Neko compiler generates bytecode but has also a JIT translator (x86 only).
What about O'Caml, Haskell, C99, Ada 2005, Scheme or Lisp?
I do not think that there is any particular high level language that is suited to be another compiler's target language.
Google's Go would be a good language to target, I think. Go seeks to do to modern languages what C# sought to do to C++ and Java a decade ago -- improve upon it, make it "better" (in the beholder's eye).
You should also be sure that Lua is supported, which AFAIK isn't hard to do since it piggybacks upon C/C++ I think (??).
LISP dalects aren't bad for this purpose. In fact, LISP S-expressions can be seen as a textual representation of the ASTs emitted by the front end, similar to as if you had serialized the AST using JSON or XML. Also, many LISP implementations support macros which allow you to define new syntax to realize entries in your ASTs which LISP doesn't support by default. As soon you have LISP code, you may choose from myriards of interpreters, compilers and platforms (even hardware, if you use the infamous LISP machine)
If you are looking for more `traditional' target languages, I'd recommend C or Javascript (BTW, Javascript is even a target for some of the Perl6 compilers). Or try some kind of high level assembler.
精彩评论