TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

C as an Intermediate Language (2012)

73 pointsby jlturnerabout 9 years ago

11 comments

cokernel_hackerabout 9 years ago
Professional compiler engineer here, C is a mediocre intermediate language.<p>Let&#x27;s start with an excellent quote from Wittgenstein. &quot;The limits of my language mean the limits of my world.&quot;<p>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.<p>For example, let&#x27;s say your language has behavior similar to scheme&#x27;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.<p>Guaranteed tail-calls are the tip of the iceberg, there are a lot more features which cannot be reasonably mapped onto C.<p>Real compiler IRs increase your expressivity beyond what the C language designers decided was important.
评论 #11704214 未加载
评论 #11704739 未加载
kragenabout 9 years ago
As far as I can tell, on all the axes that LLVM excels as an intermediate language (ease of getting started, debugging support, many backends, optimization, flexibility), C is even better. C is easier to get started with, has easier debugging support, has more backends, better optimization, and more flexibility.<p>To take a simple example, I have here a 2428-line C program generated by compiling Linus Åkesson&#x27;s Game of Life in BF (<a href="http:&#x2F;&#x2F;www.linusakesson.net&#x2F;programming&#x2F;brainfuck&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.linusakesson.net&#x2F;programming&#x2F;brainfuck&#x2F;</a>) into C using Daniel B. Cristofani&#x27;s dbf2c.b (<a href="http:&#x2F;&#x2F;www.hevanet.com&#x2F;cristofd&#x2F;brainfuck&#x2F;dbf2c.b" rel="nofollow">http:&#x2F;&#x2F;www.hevanet.com&#x2F;cristofd&#x2F;brainfuck&#x2F;dbf2c.b</a>), 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.<p>This isn&#x27;t optimized code, about equivalent to gcc -O0, typically about 3×–5× slower than optimized code. But that&#x27;s enormously better than interpretation overhead.<p>(Using decent optimization levels with GCC makes it take several seconds to compile, because GCC&#x27;s optimizer doesn&#x27;t deal well with enormous functions.)<p>dbf2c.b, the C-generating BF compiler, is 892 bytes of BF code when stripped. Now, I&#x27;m not saying you should write your compilers in deliberately obfuscated programming languages in as few bytes as possible; I&#x27;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.
评论 #11704191 未加载
dom96about 9 years ago
Indeed. C is brilliant as an intermediate language. One of the greatest things about it is that once you generate C code it&#x27;s not too much of a stretch to also generate C++&#x2F;Objective C specific code and take advantage of the libraries written in those languages.<p>Despite this, there are many people out there who view languages that compile to C as being inferior. I still don&#x27;t understand it.
评论 #11703319 未加载
评论 #11703045 未加载
FraaJadabout 9 years ago
A few languages that generate C that are currently popular are:<p>* nim<p>* Vala (GObject backend)<p>* Purescript (technically it has a C++ backend, but it is worth mentioning here for the very clean C++ it produces!)
felixangell1024about 9 years ago
It&#x27;s also insanely easy to generate code for. In addition to this, you don&#x27;t have to build an entire toolchain of LLVM stuff to get it to work on your system -- LLVM is a nightmare to setup on Windows.
neopalliumabout 9 years ago
Here is a list of compilers that can generate C code for different languages:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;dbohdan&#x2F;compilers-targeting-c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dbohdan&#x2F;compilers-targeting-c</a>
jjnoakesabout 9 years ago
Portability and interoperability are key in industry.<p>I think all of the new languages are amazing these days. I am learning Rust, Clojure, Haskell, and many more.<p>Buy I can not use any of them at work. I&#x27;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.<p>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&#x27;t excite me as much.<p>I would kill for a Rust to human-readable C++ transpiler. I think it could be used immediately by many.<p>I may have to write one.
评论 #11702899 未加载
评论 #11703099 未加载
评论 #11702804 未加载
评论 #11702709 未加载
评论 #11703078 未加载
评论 #11703574 未加载
Artlavabout 9 years ago
This begs the question of what would be better - to compile your favorite language to C and use something like Emscripten to make JS, or to compile your code directly to JS?<p>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...
评论 #11705753 未加载
keithnzabout 9 years ago
really wish this was more common, as language choices on embedded platforms are often VERY limited. ie, C.<p>Though on embedded systems you&#x27;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.
ilakshabout 9 years ago
If you guys are interested in languages that compile to C, the best programming language I know of compiles to C and it is called Nim.
评论 #11702884 未加载
vmorgulisabout 9 years ago
If the target compilers are GCC and clang, C++ is even a better intermediate language.