1

How small can we make a compiler for any (one) language like C, Assembly etc ? Would it be possible to make it about 300-400kb ?

david blaine
  • 5,683
  • 12
  • 46
  • 55
  • 1
    A "compiler for any language" would certainly not fit in 400kb. – JJJ Dec 01 '12 at 10:00
  • @Juhana How do we prove that ? Can you also tell me what is the smallest compiler made for C ? – david blaine Dec 01 '12 at 10:02
  • 1
    There are some compilers that take only a few kB. It depends on the language you want to process... – arkascha Dec 01 '12 at 10:05
  • Sorry, I was being unnecessarily sarcastic. If you mean "one compiler that can compile any (all) languages", then it's impossible to make. If you mean "is there a compiler that is smaller than 400kb (for any single language)", then yes, most compilers are smaller than that. If you mean "can a compiler for any given (single) language be made smaller than 400kb", the answer is probably yes. – JJJ Dec 01 '12 at 10:05
  • 2
    Googling for this lead me to this SO post : http://stackoverflow.com/questions/1630417/what-is-the-conceptually-smallest-compiler-that-can-compile-itself I think your question is completely language dependant. Like `How small can you make a C compiler..?` I assume someone will be able to measurably answer a question like that. – gideon Dec 01 '12 at 10:05
  • 1
    I'd expect a compiler for a simple language like Forth could be extremely tiny. – Barmar Dec 01 '12 at 10:43
  • 1
    BTW, the first computer I used, the PDP-8, only had 4K 12-bit words (and no virtual memory). It had compilers for Fortran, BASIC, and FOCAL. – Barmar Dec 01 '12 at 10:49
  • @Barmar - Never knew about forth until now. Seems to be exotic and esoteric. Why do you think it could be extremely tiny ? Could it be the tiniest if yes, why ? – david blaine Dec 01 '12 at 12:09
  • Forth has very simple syntax, and not many built-in operations. It's basically just a fancy RPN calculator. – Barmar Dec 01 '12 at 17:51
  • 1
    Back when C was invented, it was first implemented on the PDP-11. The PDP-11 only supports a program (like the compiler) using 64 KB of code and 64 KB of data. That may be one reason the original compiler was split up into so many passes (preprocessor, compiler, optimizer, assembler, linker). – brian beuning Dec 02 '12 at 20:22

1 Answers1

3

Yes, this is certainly possible.

Check this out: TCC. Fully working C compiler executable in 135KB (for Win32). After upx it becomes just 72KB.

mvp
  • 111,019
  • 13
  • 122
  • 148
  • Will such small compilers be as useful as the ones made by borland, eclipse or netbeans ? That is, can they be trusted make commercial software. – david blaine Dec 01 '12 at 10:22
  • Why not - but it may be not heavily optimized. For example, TCC can compile and boot Linux kernel, even at boot time: http://bellard.org/tcc/tccboot.html. and talking about quality compilers from borland - I think most software is compiled by open source `gcc` and `msvc`, and Borland is probably not even in low digits – mvp Dec 01 '12 at 10:25