Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

Does compiling to C-- have any benefits compared to compiling equivalent C code (maybe a subset of C)? My guess would be modern C compilers, powered by decades of R&D and programming, would be able to optimize just as well and possibly better.

EDIT: One advantage that comes to my mind is that compiling to C-- will give slightly more power to deal with stack. E.g. implementing exceptions, continuations etc would be simpler. This is possible in C using inline assembly.



sort by: page size:

I think the biggest benefit of compiling to C is that you can output comments about which C sections correspond to which lines in the higher level code, and then you can use step-through debuggers to see why your emitted code isn't working right.

You don't have to deal with all the idiosyncrasies of C, because you can restrict yourself to a small subset of C that is straightforward and easily understood.


Well I find it more interesting...

Also C compilers can do a lot of micro optimizations that are not trivial, so compiling something to C or maybe LLIR seems like a better choice.


Sorry for the stupid question, but what is the benefit of building a C compiler nowadays, and what is interesting about this particular one?

I thought the principle benefit of compiling to C was the ease of bring up on a new system.

Well, my thoughts are that if a C compiler can do all the heavy lifting I may as well take advantage of that. Transpiling to C also reduces, if not eliminates, any internal recursion in the expression evaluation. You also get decades of optimisations too, although I don't see there being much of that.

A lot of the claimed benefits of compiling to C--compilation to embedded targets without a GCC backend, most importantly--evaporate if you only compile to GCC's dialect of C. It's another way in which compiling to C ends up worse in the long run.

No reason you can't compile C down to that. It's just that there will be less of a clear performance advantage for C as some of the semantics will be slightly awkward to map to that instruction set.

You might still get a speedup by writing C in some areas, but I think the real reason for supporting it is that you have a bunch of legacy software and drivers written in C, and it's easier to get a C compiler working on that platform than to port all that software.


There are a lot of compilers that target directly C.

Compiling to C puts some significant constraints on your language. For example, C doesn't have COMDAT support, thread local storage, exception handling is limited to setjmp/longjmp, static uplevel function links are a problem, symbolic debug info is going to all look like C, etc.

C compilers

C compiler performance in the 80's for home micros was so bad, that functions were basically naked entry points to full inline Assembly.

And here we are now discussing how good modern C compilers are.

If there isn't the willingness to spend the money to optimize performance, of course it won't improve.


This is mostly only true for modern optimising C compilers. If you take a simple C compiler from the 90's, or disable optimisations in a modern compiler, there's a near 1:1 relationship between the C code and compiler output.

Still not as good as emitting C code in most cases? C code gets optimized using either llvm or any other optimizer so it’s a more portable compile target.

>Why make a pit-stop at C?

Optimizing C compilers and platform independence properly. There have been a lot of research involved in making C compilers very fast, so you get to copy that for free.


Is there a benefit to building C code with a C++ compiler?

It compiles to C.

Not necessarily true, although C compilers are usually very good.

If you're going to "beat" C with another language, it will obviously be one that compiles to C.

Anyone who's decided to use C has already made that choice, so it makes sense that C compiler writers would follow their priorities.
next

Legal | privacy