Jump to content

Tiny C Compiler

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 141.117.54.13 (talk) at 18:02, 17 May 2011 (→‎Uses). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Tiny C Compiler
Developer(s)Fabrice Bellard
Stable release
0.9.25 / May 20, 2009 (2009-05-20)
Repository
Written inC and Assembly
Operating systemLinux, Unix, Windows
TypeC compiler
LicenseGNU Lesser General Public License
Websitehttp://bellard.org/tcc/

The Tiny C Compiler (aka TCC, tCc, or TinyCC) is an x86 and x86-64 C compiler created by Fabrice Bellard. It is designed to work for slow computers with little disk space (e.g. on rescue disks). Windows operating system support has been added in version 0.9.23 (17 Jun 2005). TCC is distributed under the GNU Lesser General Public License (LGPL).

TCC claims to implement all of ANSI C (C89/C90),[1] much of the new ISO C99 standard, and many GNU C extensions including inline assembly.

Features

TCC has a number of features which differentiate it from other current C compilers:

  • Its small file size (about 100 KB for the x86 TCC executable) and memory footprint allow it to be used directly from a single 1.44 M floppy disk, such as a rescue disk.
  • TCC is intended to produce native x86 and x86-64 code very quickly; according to Bellard, it compiles, assembles and links the Links web browser about 9 times faster than GCC does.[1]
  • TCC has a number of compiler-specific language features intended to improve its practicality, such as an optional memory and bound checker, for improved code stability.
  • TCC allows programs to be run automatically at compile time using a command-line switch. This allows programs to be run as a shell script under Unix-like systems which support the shebang interpreter directive syntax.

Compiled program performance

Although the TCC compiler itself is exceptionally fast and produces very small executables, there is an inherent trade off between this size of the compiler and the performance of the code that TCC produces.

TCC does perform a few optimizations, such as constant propagation for all operations, multiplications and divisions are optimized to shifts when appropriate, and comparison operators are specially optimized (by maintaining a special cache for the processor flags). It also does some simple register allocation, which prevents many extraneous save/load pairs inside a single statement.

But in general, TCC's implementation emphasizes smallness instead of optimally-performing results. TCC generates code in a single pass, and does not perform most of the optimizations performed by other compilers such as gcc. TCC compiles every statement on its own, and at the end of each statement register values are written back to the stack and must be re-read even if the next line uses the values in registers (creating extraneous save/load pairs between statements). TCC uses only some of the available registers (e.g., on x86 it never uses ebx, esi, or edi because they need to be preserved across function calls).[2]

Here are two benchmark examples:

  • Rough benchmarks of a recursive Fibonacci algorithm on a 1.8 GHz Intel Centrino laptop with 512MB RAM yields a noticeable difference in results between Microsoft Visual C++ compiler 13.10.3052 and TCC. To calculate the 49th Fibonacci number, it took a TCC-compiled program approximately 110 seconds whereas the same program compiled by VC++ took approximately 93 seconds. Here, TCC takes 18% longer.[citation needed]
  • With a tcc modified to compile gcc, running cc1 (the gcc C compiler) on itself required 518 seconds when compiled using GCC 3.4.2, 558 seconds using GCC 2.95.3, 545 using Microsoft C compiler, and 1145 seconds using tcc. The level of optimization in each compiler was -O1 or similar.[3]

Uses

Well-known uses of tcc include:

  • TCCBOOT, a hack where TCC loads and boots a Linux kernel from source in about 10 seconds. That is to say, it is a "boot loader" which reads Linux kernel source code from disk, writes executable instructions to memory, and begins running it. This did require changes to the Linux build process.
  • TCC was used to demonstrate a defense against the trusting trust attack[4]
  • TCC has been used to compile gcc, though various patches were required to make this work [2]
  • Cinpy is a Python library that allows you to implement functions with C in Python modules. The functions are compiled with TCC in runtime. The results are made callable in Python through the ctypes library.
  • Comes installed on Javascript Linux (also by Bellard).

History

TCC has its origins in the Obfuscated Tiny C Compiler (OTCC), a program Bellard wrote to win the International Obfuscated C Code Contest (IOCCC) in 2001. Since that time, Bellard expanded and un-obfuscated the program to produce tcc.[5]

Current status

TCC has an active mailing list, and Fabrice Bellard's current version is available through CVS. However, official tcc development slowed due to Bellard's work on other projects.

Rob Landley created a fork of tcc that incorporated various patches from others, using the Mercurial SCM; Landley's Mercurial branch showed its current status while the project was active. The project was discontinued on October 4, 2007 and recontinued as a fork on October 27, 2007 [3] then discontinued until further notice [4] on September 5, 2008.

Various others have distributed patches or download sites of various improved versions of tcc, such as Dave Dodge's collection of unofficial tcc patches, Debian and kfreebsd downstream patches, and grischka's gcc patches.[3] grischka's Public Git Hosting contains a mob branch with recent contributions, including a shared build, cross-compilers, and SELinux compatibility.

TCC 0.9.23 is the subject of vulnerability number CVE-2006-0635, which is also Open Source Vulnerability Database vulnerability 22956. The report is that TCC "contains a flaw that may have security implications on programs compiled with it. The compiler fails to return unsigned values for the sizeof operator, resulting in potential integer overflows in the objects it compiles." In version 0.9.24 of TCC this vulnerability was closed.

November 1, 2010 - A recent mailing list suggestion to extend TCC to include a subset of C++ including the class, public/protected/private, inheritance, member functions and variables, along with virtual function support has been proposed. Discussion is ongoing about whether or not it will be included.

See also

References

  1. ^ Tiny C Compiler Reference Documentation accessed on 2008-08-07
  2. ^ Glöckner, Daniel. Re: Tinycc-devel (no subject), September 8, 2006.
  3. ^ a b grischka, GCC by TCC (some fixes), 29 Sep 2005
  4. ^ Wheeler, David A. Countering Trusting Trust through Diverse Double-Compiling. ACSAC.
  5. ^ Bellard, Fabrice. Obfuscated Tiny C Compiler