73 points · by jlturner · 10y ago · 85 comments
Let's start with an excellent quote from Wittgenstein. "The limits of my language mean the limits of my world."
Using C as your intermediate language means that your expressiveness is limited to valid C programs. This is workable but only if your language can be mapped to C in _useful_ ways.
For example, let's say your language has behavior similar to scheme's tail-call. How would you get this behavior from a C compiler? You will never be able to make this reliably across optimization levels, etc.
Guaranteed tail-calls are the tip of the iceberg, there are a lot more features which cannot be reasonably mapped onto C.
Real compiler IRs increase your expressivity beyond what the C language designers decided was important.
I would cite GC as the most prominent example of a feature that is incompatible with compiling to C. It is impossible to write a competitive high performance tracing GC on top of C. Mandatory register spills at safe points and conservative scanning have huge downsides.
Posts on C get blindly upvoted on HN and proggit. I guarantee it. Me thinks it's like the history channel for programmers; learning about their forerunners and how they had to bang rocks together to make fire.
It's also how I've gotten all of my karma. Unfortunately, the abyss has also looked into me, as I now write C code for a living...
1. Write an interpreter to host the compiler (probably in C).
2. Write a cross-compiler.
3. Write an X-to-C compiler.
I insist on C here not because of performance, but because it's ubiquitous.
Well, there's still the unreasonable way: compile in continuation passing style, and trampoline every call. I shall not be held responsible for any performance problem this advice may cause however…
To take a simple example, I have here a 2428-line C program generated by compiling Linus Åkesson's Game of Life in BF (http://www.linusakesson.net/programming/brainfuck/) into C using Daniel B. Cristofani's dbf2c.b (http://www.hevanet.com/cristofd/brainfuck/dbf2c.b), which is a BF compiler written in BF. Compiling these 2428 lines of C to machine code using tcc 0.9.25 takes 20ms on my 1.6GHz Atom netbook. Most of this is about 16ms of tcc overhead (startup and shutdown time); the rest is compiling several hundred thousand lines of C per second with tcc. You should get several million lines of C per second with tcc on a modern machine.
This isn't optimized code, about equivalent to gcc -O0, typically about 3×–5× slower than optimized code. But that's enormously better than interpretation overhead.
(Using decent optimization levels with GCC makes it take several seconds to compile, because GCC's optimizer doesn't deal well with enormous functions.)
dbf2c.b, the C-generating BF compiler, is 892 bytes of BF code when stripped. Now, I'm not saying you should write your compilers in deliberately obfuscated programming languages in as few bytes as possible; I'm saying that the fact that this is even possible at all should give you really good feelings about how easy it is to compile things to C.
Also, C may have easier debugging support, but it's significantly worse than having exact control over the contents of your DWARF DIEs. Making it easy to have a bad debugging experience isn't a plus as I see it.
You're probably right that LLVM also has better optimization than C because of things like the possibility of doing better aliasing.
I agree that BF is unrepresentative of anything in the real world. Thank goodness.
I can certainly see how debugging support could be better if you can go beyond the support C gives you.
Despite this, there are many people out there who view languages that compile to C as being inferior. I still don't understand it.
A minimal porting effort for the best of all worlds.
https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins...
could be implemented in plain C if you're careful. It's not like the other extensions like __builtin_constant_p or asm that'd leave you stuck with GNU C.
And besides, GNU C is good enough for lots of people!
* nim
* Vala (GObject backend)
* Purescript (technically it has a C++ backend, but it is worth mentioning here for the very clean C++ it produces!)
I think all of the new languages are amazing these days. I am learning Rust, Clojure, Haskell, and many more.
Buy I can not use any of them at work. I'm in a position to influence teams of smart programmers, but the only language that I would be able to use would be one that fits in to the existing infrastructure, which is natively compiled shared libraries on esoteric unix platforms.
So no LLVM. No JVM. No Haskell. I could possibly get away with a lisp or scheme that compiled portable C if I wanted. But that doesn't excite me as much.
I would kill for a Rust to human-readable C++ transpiler. I think it could be used immediately by many.
I may have to write one.
I just need the compiler to run on my systems. Since it is written in rust, I either need it ported to my systems (too much work) or I need my rust code transpiled to C or C++ so I can build it myself (seems doable).
There are no shortcuts to a proper implementation of Rust. Implement an LLVM backend for your architecture. It doesn't make sense for Rust to maintain a backend that is doomed to be forever broken in various ways.
Also, Rust is interoperable to the exact same extent C is. Making shared libraries exposing a C ABI is a snap.
Or you port it every time you need a new platform.
The x86 assembly or LLVM intermediate representation that comes out of most compiler is not very human readable, but I don't see anyone having any problem with it whatsoever. Readability of generated code was never the point of C using C as a back-end. The point is portability and tooling.
By the way /u/jjnoakes can suggest fancy languages and compilers, if only they can generate native code on obscure platforms (I guess, something other than x86 and ARM). Generating C code (even utterly unreadable C code) would solve his problem.
This is why I think rust is a winner. I bet most rust code could be compiled to c++ very cleanly.
You get rust's compiler with all of the safety checks, and you get the platform support of C++.
It would be a dream.
So, how does that prevent you from using natively compiled languages like haskell?
The c code it generates is basically the ocaml bytecode interpreter and the bytecode of my program.
Not what I was thinking of.
So far as my experiments went, a 3000 native score Dhrystone test compiles to C at 1300, and that C compiles to 850 worth of JS. Which suggests that a direct-to-JS compiler might be worth the efforts...
Though on embedded systems you'd also want more constraints for the backend, like do not use dynamic memory, perhaps being able to specify the output code is MISRA compliant.
Another language that is widely used by hobbyists and in production is D which, as far as I know, produces C code too.
EDIT: I should say there are a few D compilers, DMD, LDC, and some other one by GNU(?).
I think that DMD is the main compiler, but I could be wrong. LDC generates LLVM code.
Edit: A cursory look seems to suggest they do translate to IR: https://github.com/ldc-developers/ldc/tree/master/ir