Or you could use the non-obfuscated version, <code>tcc</code>. It's pretty awesome for a minimal C compiler:
#!/usr/local/bin/tcc -run
at the first line of your C source, and execute it directly from the command line (compilation will happen entirely in memory).libtcc
, you can use TCC as a backend for dynamic code generation.That last part is pretty amazing, it's basically eval
for C; you can create C code on the fly, and then compile-and-execute it.
Code written in C needs to be compiled before it can be run on a computer. The same goes for the Linux kernel. The tccboot project modified the Linux startup process to include tcc and compile the Linux source code into an executable format directly to memory and then run it from there.
In other words: magic.
GCC might not be as easy to port, but perhaps TCC would be easier (being much smaller).
But yeah, once you get C somewhere, the floodgates are open :-P. Although i think that in such cases it feels a bit like cheating (like some programs being ported to Haiku via Qt instead of the native GUI).
Not quite... C and C++ use virtually the same pre processor with very very minor differences, and yet C can be compiled incredibly fast (http://bellard.org/tcc/)
C++ doesn't compile quickly because of the language itself, which is full of context sensitive grammar, a very complex type system, and of course the number one reason is due to templates.
You might check Fabrice Bellard's projects.
Among other things, he's written a bootable version of Linux in JS (be sure to check the technical notes link at the bottom), which includes his TinyCC compiler.
tcc is a C compiler that would probably do that in a couple of seconds. It did the whole linux kernel in 15 seconds on an average machine in 2004. http://bellard.org/tcc/tccboot.html for more details.
The kernel has grown since then, and tcc has since sped up and gained arm+x64 backends. Recent measurement from 2009 is either 800k lines/sec or 30k lines/sec (depending on how you count a file included multiple times). Even gcc, no speed daemon, does 90k/sec or 4k/sec on the same benchmark - http://bellard.org/tcc/#speed
I'm a little baffled by how you can say "a couple of minutes" for a 50k line project is not slow. D, Go, Modula, Oberan, Delphi and almost any other compiled language with comparable power has a compiler that would eat 50K in a few seconds.
I love this submission and I will always upvote this cool programming links. Reading through this, I found this: http://bellard.org/otcc/ and this: http://bellard.org/tcc/ and I found them both to be very inspiring!
the latter is a pretty important one, and speeds up the compiler more than just asking the compiler for no optimisations (-O0): not having optimisation code in the compiler means the compiler is smaller and has less branching (no need for all the architecture surrounding optimisation passes). Witness tcc which produces relatively slow code compared to e.g. GCC or MSC, but is so fast at compiling said code you can use it to boot from the Linux kernel source, said code being compiled by the bootloader.
> Linux requires glibc libraries for instance to run at all.
Most Linux distros do require glibc, but there are also several (mostly small, specialized) distros using other libcs.
> It also requires gcc to be compiled
You can compile it, at least with some minor patches, with TCC too
> It is then totally dependent on GNU programs to actually work;
I've built and deployed plenty of systems with entirely non-GNU userlands (mostly small embedded setups, granted, but there's nothing stopping you from building functional Linux systems without a GNU userland).
Fabrice Bellard's Tiny C Compiler can do this out of the box. You just do tcc -run myprogram.c
. No makefiles or extra shell scripts required. It's fast enough that they even have a related project that can compile and then boot the Linux kernel from source.
If you want something that is lightweight and has incredibly fast compiles, but doesn't necessarily produce the fastest code, take a look at Tiny C Compiler. Although they say it is no longer maintained, it only aims to support C which hasn't changed much in a while, and I believe it is still a fairly popular project for uses like this.
I've seen someone build an extremely simple compiler for a subset of x86 assembler and used that to build a simple C compiler hacked to build TCC, to prove that it is reasonably possible to get a decent compiler if you start from scratch.
I'm not sure about this, but I think you need an older version of GCC to be able to compile GCC.
> The real-time compilation and running stuff
This is not really a language issue, but an implementation issue. Consider that TCC was able to compile the Linux kernel at <em>boot time</em> in 15 seconds on a 2.4GHz P4.
Modern compilers are slow because they're not engineered to be fast, not because of the languages they compile.
I looked for a random compiler that isn't gcc, tcc's page implies linux has at some point been compiled with it. I am sure many C language compilers can compile it, though for many of them it would be a lot of work...(edit: assuming things are done badly/incompatibly in some of the code) wp says Clang did it in 25 October 2010 too.
A glance shows they're still (L)GPL, I'm not sure if reinstalling would not be able to handle the decoding necessary. Home media on my shitty old laptop that has a majority of these titles when they want to follow the Android model rather than the computer.
TCC. A glance shows they're still (L)GPL, I'm not sure which flavour they were at back then so perhaps reason prevailed and no change was made.
Not aiming to name and shame, but rather to point out that disrespect for contributors' copyrights is more common than supposed.
> http://bellard.org/tcc/ ...
I tried today... mips processors are known... The configure script needed a bit of tweaking to run at all. It trues to use gcc by default (hard coded), so I made trials with our poor gcc and the native cc, all with the same result (only gcc shown):
> =>make
> gcc -o tcc.o -c tcc.c -I. -Wall -g -O2 -fno-strict-aliasing
> In file included from tcc.h:75,
> from tcc.c:24:
> elf.h:25: inttypes.h: No such file or directory
> make: *** [tcc.o] Error 1
:-( So, in the top-level tcc directory:
> =>find . -type f -name "inttypes*"
> ./win32/include/inttypes.h
Double :-(
Either the configuration didn't run correctly or nobody ever tried this. At this point's it's obvious that a lot more digging is needed: I don't think that using an include file meant for windows on a UNIX machine can do any good, not before I am perfectly sure about what exactly I'm doing. I give up for now.
> older GCC
That we have tried before. All failed at some point. Alas... We didn't try to compile an older GCC with this poorly working GCC-2.7, only with the native cc. Heh :-) Maybe there's a chance :-) Thanks for giving me the idea :-)
I guess the now well-known inverse square root approximation counts here. Or applying bayesian statistics to spam detection. Or making a linux kernel boot from sources in 15 seconds.
Also, TCC is specifically designed to do this sort of thing. It's a very fast compiler, too, though the executable won't run nearly as fast as GCC's output.
#!/usr/bin/tcc -run #include <stdio.h> // ...
Not exactly a distro but interesting none the less: TCCBOOT is a boot loader able to compile and boot a Linux kernel directly from its source code. There's an iso you can boot in a VM. Really impressive.
Many alternatives? I'd say no. Clang/LLVM is the only comparable open-source option. ICC is probably the only mainstream commercial alternative that is still being updated -- AMD stopped working on the codebase they once inherited. pcc and tcc aren't comparable to the big two and haven't been updated in the last couple of years.
GCC is a very powerful compiler, but it requires quite a lot of setup, especially since it relies on MingW. If you just want a quick and dirty C compiler for Windows, there is TinyC. Just download the Windows version and unpack the archive somewhere. That's pretty much all you need to get started.
If you want to do serious development, I recommend the Code::Blocks IDE with GCC/MingW.
I remember something called tcc... http://bellard.org/tcc/ downloadable here http://download.savannah.gnu.org/releases/tinycc/tcc-0.9.26.tar.bz2 in source code format, although I am not sure it would run on mips...
You could also try an older version of GCC.
> 1.8 million lines of C++ in Webkit. 1.8 million lines!!
Rendering stuff is not as complex as those rendering engine would have you believe.
You should take a look at the Gezira rendering engine (see page 8-9). All Cairo's functionality in 450 lines. Not the fastest thing around, but scarily scalable, and perfectly capable of rendering reasonably complex documents in real time on a modern laptop.
Cairo on the other hand is nearly 200 thousand lines (I just did a grep job). Nearly 500 times bigger than Gezira. You could try and assert that Gezira is cheating, but the language it is written in, Nile, is part of a self-hosting compiler collection that is implemented in a couple thousand lines, tops. Even tcc needs about 60 thousand lines.
Now that was just the rendering part. I expect the "let's implement a huge, messy, ad-hoc, backward-half-compatible standard" to be less simple. If on the other hand we didn't need this HTML/CSS compatibility, a full featured browser would hardly take more than a 10, maybe 20 thousand lines of code. I understand the need for backward compatibility, but the price is huge.
There's nothing simple about writing a C compiler. You might look at fbcc for an example. It produces assembly for a fictional virtual machine, so it's not a true native compiler. But that just means that it's a lot simpler than a real compiler, such as his later work tcc. And of course there's a multitude of additional open source C compilers: gcc, clang, OpenWatcom, bcc, FAUcc, TenDRA. I'm not sure how instructive it would be to look at their code, though.
Agreed, though it can be interesting to note the number of clock cycles per unit of input too. The TCC compiler is supposed to compile 859000 lines of C per second, though that was probably measured in 2004 since the version of TCC that is measured is from 2004. Sure, D is not C, etc., but I think a compiler that is designed to emit machine code in the most straightforward non optimizing way possible can be a lot faster than the current D compiler.
for short, one-file C programming I absolutely love tcc. It lets you do this:
#!tcc -run #include<stdio.h> int main() { printf("Well, that's convenient!"); return 0; }