Introduction
************

TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C compilers, it is meant to be self-suffisant: you do not need an external assembler or linker because TCC does that for you.

TCC compiles so fast that even for big projects Makefiles may not be necessary.

TCC not only supports ANSI C, but also most of the new ISO C99 standard and many GNUC extensions.

TCC can also be used to make C scripts, i.e. pieces of C source that you run as a Perl or Python script. Compilation is so fast that your script will be as fast as if it was an executable.

TCC can also automatically generate memory and bound checks (see bounds) while allowing all C pointers operations. TCC can do these checks even if non patched libraries are used.

With libtcc, you can use TCC as a backend for dynamic code generation (see libtcc).

Command line invocation
***********************

     usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]
                [-i infile] infile [infile_args...]

-Idir
     Specify an additionnal include path. The default ones are: /usr/include, prefix/lib/tcc/include (prefix is usually /usr or /usr/local).

-Dsym[=val]
     Define preprocessor symbol 'sym' to val. If val is not present, its value is '1'. Function-like macros can also be defined: '-DF(a)=a+1'

-Usym
     Undefine preprocessor symbol 'sym'.

-lxxx
     Dynamically link your program with library libxxx.so. Standard library paths are checked, including those specified with LD_LIBRARY_PATH.

-g
     Generate run time debug information so that you get clear run time error messages:  test.c:68: in function 'test5()': dereferencing invalid pointer instead of the laconic Segmentation fault.

-b
     Generate additionnal support code to check memory allocations and array/pointer bounds. '-g' is implied. Note that the generated code is slower and bigger in this case.

-i file
     Compile C source 'file' before main C source. With this command, multiple C files can be compiled and linked together.

Note: the -o file option to generate an ELF executable is currently unsupported.

C language support
******************

ANSI C
======

TCC implements all the ANSI C standard, including structure bit fields and floating point numbers (long double, double, and float fully supported). The following limitations are known:

  * The preprocessor tokens are the same as C. It means that in some rare cases, preprocessed numbers are not handled exactly as in ANSI C. This approach has the advantage of being simpler and FAST!

ISOC99 extensions
=================

TCC implements many features of the new C standard: ISO C99. Currently missing items are: complex and imaginary numbers and variable length arrays.

Currently implemented ISOC99 features:

  * 64 bit 'long long' types are fully supported.

  * The boolean type '_Bool' is supported.

  * '__func__' is a string variable containing the current function name.

  * Variadic macros: __VA_ARGS__ can be used for function-like macros:

              #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)

     dprintf can then be used with a variable number of parameters.

  * Declarations can appear anywhere in a block (as in C++).

  * Array and struct/union elements can be initialized in any order by using designators:

              struct { int x, y; } st[10] = { [0].x = 1, [0].y = 2 };

              int tab[10] = { 1, 2, [5] = 5, [9] = 9};

  * Compound initializers are supported:

              int *p = (int []){ 1, 2, 3 };

     to initialize a pointer pointing to an initialized array. The same works for structures and strings.

  * Hexadecimal floating point constants are supported:

                    double d = 0x1234p10;

     is the same as writing

                    double d = 4771840.0;

  * 'inline' keyword is ignored.

  * 'restrict' keyword is ignored.

GNU C extensions
================

TCC implements some GNU C extensions:

  * array designators can be used without '=':

              int a[10] = { [0] 1, [5] 2, 3, 4 };

  * Structure field designators can be a label:

              struct { int x, y; } st = { x: 1, y: 1};

     instead of

              struct { int x, y; } st = { .x = 1, .y = 1};

  * '\e' is ASCII character 27.

  * case ranges : ranges can be used in cases:

              switch(a) {
              case 1 ... 9:
                    printf("range 1 to 9\n");
                    break;
              default:
                    printf("unexpected\n");
                    break;
              }

  * The keyword __attribute__ is handled to specify variable or function attributes. The following attributes are supported:

       * aligned(n): align data to n bytes (must be a power of two).

       * section(name): generate function or data in assembly section name (name is a string containing the section name) instead of the default section.

       * unused: specify that the variable or the function is unused.

       * cdecl: use standard C calling convention.

       * stdcall: use Pascal-like calling convention.

     Here are some examples:

              int a __attribute__ ((aligned(8), section(".mysection")));

     align variable 'a' to 8 bytes and put it in section .mysection.

              int my_add(int a, int b) __attribute__ ((section(".mycodesection")))
              {
                  return a + b;
              }

     generate function 'my_add' in section .mycodesection.

  * GNU style variadic macros:

              #define dprintf(fmt, args...) printf(fmt, ## args)

              dprintf("no arg\n");
              dprintf("one arg %d\n", 1);

TinyCC extensions
=================

  * __TINYC__ is a predefined macro to '1' to indicate that you use TCC.

  * '#!' at the start of a line is ignored to allow scripting.

  * Binary digits can be entered ('0b101' instead of '5').

  * __BOUNDS_CHECKING_ON is defined if bound checking is activated.

TinyCC Memory and Bound checks
******************************

This feature is activated with the '-b' (see invoke).

Note that pointer size is unchanged and that code generated with bound checks is fully compatible with unchecked code. When a pointer comes from unchecked code, it is assumed to be valid. Even very obscure C code with casts should work correctly.

To have more information about the ideas behind this method, check at http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html.

Here are some examples of catched errors:

Invalid range with standard string function:

          {
              char tab[10];
              memset(tab, 0, 11);
          }

Bound error in global or local arrays:

          {
              int tab[10];
              for(i=0;i<11;i++) {
                  sum += tab[i];
              }
          }

Bound error in allocated data:

          {
              int *tab;
              tab = malloc(20 * sizeof(int));
              for(i=0;i<21;i++) {
                  sum += tab4[i];
              }
              free(tab);
          }

Access to a freed region:

          {
              int *tab;
              tab = malloc(20 * sizeof(int));
              free(tab);
              for(i=0;i<20;i++) {
                  sum += tab4[i];
              }
          }

Freeing an already freed region:

          {
              int *tab;
              tab = malloc(20 * sizeof(int));
              free(tab);
              free(tab);
          }

The libtcc library
******************

The libtcc library enables you to use TCC as a backend for dynamic code generation.

Read the libtcc.h to have an overview of the API. Read libtcc_test.c to have a very simple example.

The idea consists in giving a C string containing the program you want to compile directly to libtcc. Then the main() function of the compiled string can be launched.

Developper's guide
******************

This chapter gives some hints to understand how TCC works. You can skip it if you do not intend to modify the TCC code.

File reading
============

The BufferedFile structure contains the context needed to read a file, including the current line number. tcc_open() opens a new file and tcc_close() closes it. inp() returns the next character.

Lexer
=====

next() reads the next token in the current file. next_nomacro() reads the next token without macro expansion.

tok contains the current token (see TOK_xxx) constants. Identifiers and keywords are also keywords. tokc contains additionnal infos about the token (for example a constant value if number or string token).

Parser
======

The parser is hardcoded (yacc is not necessary). It does only one pass, except:

  * For initialized arrays with unknown size, a first pass is done to count the number of elements.

  * For architectures where arguments are evaluated in reverse order, a first pass is done to reverse the argument order.

Types
=====

The types are stored in a single 'int' variable. It was choosen in the first stages of development when tcc was much simpler. Now, it may not be the best solution.

     #define VT_INT        0  /* integer type */
     #define VT_BYTE       1  /* signed byte type */
     #define VT_SHORT      2  /* short type */
     #define VT_VOID       3  /* void type */
     #define VT_PTR        4  /* pointer */
     #define VT_ENUM       5  /* enum definition */
     #define VT_FUNC       6  /* function type */
     #define VT_STRUCT     7  /* struct/union definition */
     #define VT_FLOAT      8  /* IEEE float */
     #define VT_DOUBLE     9  /* IEEE double */
     #define VT_LDOUBLE   10  /* IEEE long double */
     #define VT_BOOL      11  /* ISOC99 boolean type */
     #define VT_LLONG     12  /* 64 bit integer */
     #define VT_LONG      13  /* long integer (NEVER USED as type, only
                                 during parsing) */
     #define VT_BTYPE      0x000f /* mask for basic type */
     #define VT_UNSIGNED   0x0010  /* unsigned type */
     #define VT_ARRAY      0x0020  /* array type (also has VT_PTR) */
     #define VT_BITFIELD   0x0040  /* bitfield modifier */

     #define VT_STRUCT_SHIFT 16   /* structure/enum name shift (16 bits left) */

When a reference to another type is needed (for pointers, functions and structures), the 32 - VT_STRUCT_SHIFT high order bits are used to store an identifier reference.

The VT_UNSIGNED flag can be set for chars, shorts, ints and long longs.

Arrays are considered as pointers VT_PTR with the flag VT_ARRAY set.

The VT_BITFIELD flag can be set for chars, shorts, ints and long longs. If it is set, then the bitfield position is stored from bits VT_STRUCT_SHIFT to VT_STRUCT_SHIFT + 5 and the bit field size is stored from bits VT_STRUCT_SHIFT + 6 to VT_STRUCT_SHIFT + 11.

VT_LONG is never used except during parsing.

During parsing, the storage of an object is also stored in the type integer:

     #define VT_EXTERN  0x00000080  /* extern definition */
     #define VT_STATIC  0x00000100  /* static variable */
     #define VT_TYPEDEF 0x00000200  /* typedef definition */

Symbols
=======

All symbols are stored in hashed symbol stacks. Each symbol stack contains Sym structures.

Sym.v contains the symbol name (remember an idenfier is also a token, so a string is never necessary to store it). Sym.t gives the type of the symbol. Sym.r is usually the register in which the corresponding variable is stored. Sym.c is usually a constant associated to the symbol.

Four main symbol stacks are defined:

define_stack
     for the macros (#defines).

global_stack
     for the global variables, functions and types.

extern_stack
     for the external symbols shared between files.

local_stack
     for the local variables, functions and types.

label_stack
     for the local labels (for goto).

sym_push() is used to add a new symbol in the local symbol stack. If no local symbol stack is active, it is added in the global symbol stack.

sym_pop(st,b) pops symbols from the symbol stack ST until the symbol B is on the top of stack. If B is NULL, the stack is emptied.

sym_find(v) return the symbol associated to the identifier V. The local stack is searched first from top to bottom, then the global stack.

Sections
========

The generated code and datas are written in sections. The structure Section contains all the necessary information for a given section. new_section() creates a new section. ELF file semantics is assumed for each section.

The following sections are predefined:

text_section
     is the section containing the generated code. IND contains the current position in the code section.

data_section
     contains initialized data

bss_section
     contains uninitialized data

bounds_section
lbounds_section
     are used when bound checking is activated

stab_section
stabstr_section
     are used when debugging is actived to store debug information

symtab_section
strtab_section
     contain the exported symbols (currently only used for debugging).

Code generation
===============

Introduction
------------

The TCC code generator directly generates linked binary code in one pass. It is rather unusual these days (see gcc for example which generates text assembly), but it allows to be very fast and surprisingly not so complicated.

The TCC code generator is register based. Optimization is only done at the expression level. No intermediate representation of expression is kept except the current values stored in the value stack.

On x86, three temporary registers are used. When more registers are needed, one register is flushed in a new local variable.

The value stack
---------------

When an expression is parsed, its value is pushed on the value stack (VSTACK). The top of the value stack is VTOP. Each value stack entry is the structure SValue.

SValue.t is the type. SValue.r indicates how the value is currently stored in the generated code. It is usually a CPU register index (REG_xxx constants), but additionnal values and flags are defined:

     #define VT_CONST     0x00f0  /* constant in vc
                                   (must be first non register value) */
     #define VT_LLOCAL    0x00f1  /* lvalue, offset on stack */
     #define VT_LOCAL     0x00f2  /* offset on stack */
     #define VT_CMP       0x00f3  /* the value is stored in processor flags (in vc) */
     #define VT_JMP       0x00f4  /* value is the consequence of jmp true (even) */
     #define VT_JMPI      0x00f5  /* value is the consequence of jmp false (odd) */
     #define VT_LVAL      0x0100  /* var is an lvalue */
     #define VT_FORWARD   0x0200  /* value is forward reference */
     #define VT_MUSTCAST  0x0400  /* value must be casted to be correct (used for
                                     char/short stored in integer registers) */
     #define VT_MUSTBOUND 0x0800  /* bound checking must be done before
                                     dereferencing value */
     #define VT_BOUNDED   0x8000  /* value is bounded. The address of the
                                     bounding function call point is in vc */
     #define VT_LVAL_BYTE     0x1000  /* lvalue is a byte */
     #define VT_LVAL_SHORT    0x2000  /* lvalue is a short */
     #define VT_LVAL_UNSIGNED 0x4000  /* lvalue is unsigned */
     #define VT_LVAL_TYPE     (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)

VT_CONST
     indicates that the value is a constant. It is stored in the union SValue.c, depending on its type.

VT_LOCAL
     indicates a local variable pointer at offset SValue.c.i in the stack.

VT_CMP
     indicates that the value is actually stored in the CPU flags (i.e. the value is the consequence of a test). The value is either 0 or 1. The actual CPU flags used is indicated in SValue.c.i.

VT_JMP
VT_JMPI
     indicates that the value is the consequence of a jmp. For VT_JMP, it is 1 if the jump is taken, 0 otherwise. For VT_JMPI it is inverted.

     These values are used to compile the || and && logical operators.

VT_LVAL
     is a flag indicating that the value is actually an lvalue (left value of an assignment). It means that the value stored is actually a pointer to the wanted value.

     Understanding the use VT_LVAL is very important if you want to understand how TCC works.

VT_LVAL_BYTE
VT_LVAL_SHORT
VT_LVAL_UNSIGNED
     if the lvalue has an integer type, then these flags give its real type. The type alone is not suffisant in case of cast optimisations.

VT_LLOCAL
     is a saved lvalue on the stack. VT_LLOCAL should be suppressed ASAP because its semantics are rather complicated.

VT_MUSTCAST
     indicates that a cast to the value type must be performed if the value is used (lazy casting).

VT_FORWARD
     indicates that the value is a forward reference to a variable or a function.

VT_MUSTBOUND
VT_BOUNDED
     are only used for optional bound checking.

Manipulating the value stack
----------------------------

vsetc() and vset() pushes a new value on the value stack. If the previous vtop was stored in a very unsafe place(for example in the CPU flags), then some code is generated to put the previous vtop in a safe storage.

vpop() pops vtop. In some cases, it also generates cleanup code (for example if stacked floating point registers are used as on x86).

The gv(rc) function generates code to evaluate vtop (the top value of the stack) into registers. RC selects in which register class the value should be put. gv() is the most important function of the code generator.

gv2() is the same as gv() but for the top two stack entries.

CPU dependent code generation
-----------------------------

See the i386-gen.c file to have an example.

load()
     must generate the code needed to load a stack value into a register.

store()
     must generate the code needed to store a register into a stack value lvalue.

gfunc_start()
gfunc_param()
gfunc_call()
     should generate a function call

gfunc_prolog()
gfunc_epilog()
     should generate a function prolog/epilog.

gen_opi(op)
     must generate the binary integer operation OP on the two top entries of the stack which are guaranted to contain integer types.

     The result value should be put on the stack.

gen_opf(op)
     same as gen_opi() for floating point operations. The two top entries of the stack are guaranted to contain floating point values of same types.

gen_cvt_itof()
     integer to floating point conversion.

gen_cvt_ftoi()
     floating point to integer conversion.

gen_cvt_ftof()
     floating point to floating point of different size conversion.

gen_bounded_ptr_add()

gen_bounded_ptr_deref()
     are only used for bound checking.

Optimizations done
==================

Constant propagation is done for all operations. Multiplications and divisions are optimized to shifts when appropriate. Comparison operators are optimized by maintaining a special cache for the processor flags. &&, || and ! are optimized by maintaining a special 'jump target' value. No other jump optimization is currently performed because it would require to store the code in a more abstract fashion.