4 ms·
Compiling to another language is always going to be very slow, and very frustrating, because one's expressiveness is limited to the expressiveness of the target
by WalterBright 5y ago
Compiling to another language is always going to be very slow, and very frustrating, because one's expressiveness is limited to the expressiveness of the target language.
(Yes, I know, because C++ is Turing Complete, anything is expressible, but it's a matter of efficiency.)
For example, what if the language needed exceptions to work in a different way than the target does?
And these days, one can easily use existing backends like the Digital Mars one (Boost Licensed!), the Gnu one or the LLVM one. Why punish yourself by emitting code in another language? Natalie already requires gcc or clang anyway.
I could never have gotten D to work if it emitted C code as output.
- sitkack 5y agoC++, a more complex language in all dimensions managed to produce C code via the first C++ compiler, Cfront. Dlang has its own native code generator? You have amazing chops, I am sure you could have emitted C code. What are your thoughts on producing Wasm instead of C? Maybe Scheme is the perfect compiler target. https://en.wikipedia.org/wiki/Cfront https://en.wikipedia.org/wiki/Cfront
- mhh__ 5y agoThe dmd code generator was/is a C++ compiler backend too, and can trace it's lineage back to the first compilers to make Cfront obsolete. We could emit C code, but it's just worse when you need to get to the cutting edge. Even if you end up going through the same backend via C, you've now (say) lost devirtualization.
- WalterBright 5y agocfront in the 80s was a far simpler C++ compiler than today. And if you ever looked at the code it generated, you'd be horrified. One major problem it had in generating C was the lack of COMDAT sections in the generated object file. This was needed for generating code for the same member function in multiple source files. C compilers would just stick the code into the text segment, resulting in multiple declaration errors from the linker. I am not saying Bjarne was an inept programmer. He's actually brilliant, it was just a very very hard problem he set for himself, and it's amazing cfront worked as well as it did. The cfront on DOS also was very slow (imagine writing the generated source code out to floppies!) and crippled because it didn't have near and far pointers. Zortech C++ was the first native C++ compiler, which bypassed all these problems. ZTC++ simply vaulted over cfront by making C++ simple and fast on DOS. DOS at the time was where 90% of the programming action was, so this was no small thing. My opinion, and that of a few others at the time, was that Zortech saved C++ from oblivion by having a usable implementation on DOS. You can see that by the traffic level in comp.lang.c++ at the time - it took off immediately after ZTC++ was released, and never looked back.
- sitkack 5y agoI always love your responses. I didn't know about COMDAT, but I did use the SAS compiler on the Amiga which was Cfront based. I didn't know enough to be horrified, but I often looked at the output because I was integrating the output with regular C code (interpreters, Arexx, etc). It was kinda like Hungarian Notation++ ;) I got to the point where it felt natural to read the name mangling. The Amiga SAS/C compiler was also pretty slow. I can see how ZTC would be popular in by skipping intermediates and not having to reinvoke the compiler. How many problems from the 80s melted away because of the large amount of ram available in the 90s?
- WalterBright 5y agoOne problem that melted away was the compiler's 3 separate passes could be merged into one! Also, the amount of code in the compiler increased enormously from the 80's to today.
- WalterBright 5y ago> I could never have gotten D to work if it emitted C code as output. D goes even further. It also compiles C code :-/
- Banana699 5y ago>Compiling to another language is always going to be very slow I'm curious as to why you think this is necessarily true ? It's always going to be _slower_ than writing the target language directly, off course, that's just a tautology. But if I pick a target language with a really fast compiler, say Golang (judging purely by reputation, not actual experience), then I have a huge headstart on other languages, right ? Even though I'm slower than Golang, I'm faster than other languages because Golang is so much faster than other languages. This is not the case with C++, but that's just a special case. I think that the true comparison you're probably making here is not what I said above, but that parsing a language, processing it, then writing it to another language (which itself needs to be parsed and processed etc...); I think that you're basically saying that this is always slower than just emitting whatever final IR we eventually reach directly ? But like I said above, if the target language is correctly chosen the overhead doesn't matter much, and in return you get the benefits of using a source language as IR, I will get to that later in reply to another point you made. >and very frustrating, because one's expressiveness is limited to the expressiveness of the target language. But C++ is extremely expressive though, indeed it's _too_ expressive for humans to grok and control. >For example, what if the language needed exceptions to work in a different way than the target does? I'm not the brightest bulb on runtimes and exceptions so I will take your (admittedly brilliant, I'm a fan) word that this is actually an insurmountable problem. But isn't this a time-precision tradeoff? if you don't compile to a language with exceptions then you need to make exceptions work from the very scratch. Using a source language as target, you get a working, heavily-debugged, exception mechanism, but you're now constrained to what it can express. >Why punish yourself by emitting code in another language? This is where the benefit of compiling-to-source that I mentioned above comes in: it's an extremely low-barrier-to-entry strategy. Even though VM bytecode formats and other IRs are specifically designed to be clean and abstract targets that wraps the ugly details of platforms/architectures, there is nothing more clean and abstract than a source language designed to be used by humans. It's extremely attractive for new compiler writers to not have to learn yet another language and keep its (quite low level) details in their mind along with the source one, they can just specify an equivalent program in another source language they already know and get all of its toolchain for free. This can backfire in cases like C and C++, where the languages are not actually clean at all and there are tons of special cases and undefined behaviour that most developers ignore, but again that's just a special case, there is no reason the overall approach can't work with other languages. >I could never have gotten D to work if it emitted C code as output Like I said, it's an entry-level strategy. It works when you need something working and you need it fast, once you have something working you can ditch the makeshift backend and create a proper one (hopefully you haven't relied on any implicit semantics of the previous backend). It's like an intermediate point between an honest-to-God compiler and a naive tree-walking interpreter, they are all points on the same tradeoff axis.