tinycc/tcc-doc.texi

323 lines
8.0 KiB
Plaintext
Raw Normal View History

2001-12-17 05:23:42 +08:00
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title> Tiny C Compiler Reference Documentation </title>
</head>
<body>
<center>
<h1>
Tiny C Compiler Reference Documentation
</h1>
</center>
<h2>Introduction</h2>
2002-01-06 03:50:17 +08:00
TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C
2001-12-17 05:23:42 +08:00
compilers, it is meant to be self-suffisant: you do not need an
external assembler or linker because TCC does that for you.
<P>
2002-01-06 03:50:17 +08:00
TCC compiles so <em>fast</em> that even for big projects <tt>Makefile</tt>s may
2001-12-17 05:23:42 +08:00
not be necessary.
<P>
2002-01-06 03:50:17 +08:00
TCC not only supports ANSI C, but also most of the new ISO C99
standard and many GNUC extensions.
<P>
2001-12-17 05:23:42 +08:00
TCC can also be used to make <I>C scripts</I>,
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.
2002-01-06 03:50:17 +08:00
<P>
TCC can also automatically generate <A HREF="#bounds">memory and bound
checks</A> while allowing all C pointers operations. TCC can do these
checks even if non patched libraries are used.
</P>
2001-12-17 05:23:42 +08:00
2002-01-06 03:50:17 +08:00
<h2>Full ANSI C support</h2>
2001-12-17 05:23:42 +08:00
2002-01-06 03:50:17 +08:00
TCC implements all the ANSI C standard, including structure bit fields
and floating point numbers (<tt>long double</tt>, <tt>double</tt>, and
<tt>float</tt> fully supported). The following limitations are known:
2001-12-17 05:23:42 +08:00
<ul>
<li> 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!
</ul>
<h2>ISOC99 extensions</h2>
TCC implements many features of the new C standard: ISO C99. Currently
2002-01-06 03:50:17 +08:00
missing items are: complex and imaginary numbers and variable length
2001-12-17 05:23:42 +08:00
arrays.
Currently implemented ISOC99 features:
<ul>
2002-01-06 03:50:17 +08:00
<li> 64 bit <tt>'long long'</tt> types are fully supported.
2001-12-17 05:23:42 +08:00
2002-01-06 03:50:17 +08:00
<li> The boolean type <tt>'_Bool'</tt> is supported.
2001-12-17 05:23:42 +08:00
<li> <tt>'__func__'</tt> is a string variable containing the current
function name.
<li> Variadic macros: <tt>__VA_ARGS__</tt> can be used for
function-like macros:
<PRE>
#define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)
</PRE>
<tt>dprintf</tt> can then be used with a variable number of parameters.
2002-01-06 03:50:17 +08:00
<li> Declarations can appear anywhere in a block (as in C++).
2001-12-17 05:23:42 +08:00
<li> Array and struct/union elements can be initialized in any order by
using designators:
<PRE>
struct { int x, y; } st[10] = { [0].x = 1, [0].y = 2 };
int tab[10] = { 1, 2, [5] = 5, [9] = 9};
</PRE>
<li> Compound initializers are supported:
<PRE>
int *p = (int []){ 1, 2, 3 };
</PRE>
to initialize a pointer pointing to an initialized array. The same
works for structures and strings.
<li> Hexadecimal floating point constants are supported:
<PRE>
double d = 0x1234p10;
</PRE>
is the same as writing
<PRE>
double d = 4771840.0;
</PRE>
2002-01-06 03:50:17 +08:00
<li> <tt>'inline'</tt> keyword is ignored.
<li> <tt>'restrict'</tt> keyword is ignored.
2001-12-17 05:23:42 +08:00
</ul>
<h2>GNU C extensions</h2>
2002-01-06 03:50:17 +08:00
TCC implements some GNU C extensions:
2001-12-17 05:23:42 +08:00
<ul>
<li> array designators can be used without '=':
<PRE>
int a[10] = { [0] 1, [5] 2, 3, 4 };
</PRE>
<li> Structure field designators can be a label:
<PRE>
struct { int x, y; } st = { x: 1, y: 1};
</PRE>
instead of
<PRE>
struct { int x, y; } st = { .x = 1, .y = 1};
</PRE>
<li> <tt>'\e'</tt> is ASCII character 27.
2002-01-06 03:50:17 +08:00
<li> case ranges : ranges can be used in <tt>case</tt>s:
<PRE>
switch(a) {
case 1 ... 9:
printf("range 1 to 9\n");
break;
default:
printf("unexpected\n");
break;
}
</PRE>
<li> The keyword <tt>__attribute__</tt> is handled to specify variable or
function attributes. The following attributes are supported:
<ul>
<li> <tt>aligned(n)</tt>: align data to n bytes (must be a power of two).
<li> <tt>section(name)</tt>: generate function or data in assembly
section name (name is a string containing the section name) instead
of the default section.
<li> <tt>unused</tt>: specify that the variable or the function is unused.
2002-01-27 03:17:06 +08:00
<li> <tt>cdecl</tt>: use standard C calling convention.
<li> <tt>stdcall</tt>: use Pascal-like calling convention.
2002-01-06 03:50:17 +08:00
</ul>
<BR>
Here are some examples:
<PRE>
int a __attribute__ ((aligned(8), section(".mysection")));
</PRE>
<BR>
align variable <tt>'a'</tt> to 8 bytes and put it in section <tt>.mysection</tt>.
<PRE>
int my_add(int a, int b) __attribute__ ((section(".mycodesection")))
{
return a + b;
}
</PRE>
<BR>
generate function <tt>'my_add'</tt> in section <tt>.mycodesection</tt>.
2001-12-17 05:23:42 +08:00
</ul>
<h2>TinyCC extensions</h2>
I have added some extensions I find interesting:
<ul>
<li> <tt>__TINYC__</tt> is a predefined macro to <tt>'1'</tt> to
indicate that you use TCC.
<li> <tt>'#!'</tt> at the start of a line is ignored to allow scripting.
<li> Binary digits can be entered (<tt>'0b101'</tt> instead of
<tt>'5'</tt>).
2002-01-06 03:50:17 +08:00
<li> <tt>__BOUNDS_CHECKING_ON</tt> is defined if bound checking is activated.
2001-12-17 05:23:42 +08:00
</ul>
2002-01-06 03:50:17 +08:00
<h2>TinyCC Memory and Bound checks</h2>
<A NAME="bounds"></a>
This feature is activated with the <A HREF="#invoke"><tt>'-b'</tt>
option</A>.
<P>
Note that pointer size is <em>unchanged</em> and that code generated
with bound checks is <em>fully compatible</em> 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.
</P>
<P> To have more information about the ideas behind this method, <A
HREF="http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html">check
here</A>.
</P>
<P>
Here are some examples of catched errors:
</P>
<TABLE BORDER=1>
<TR>
<TD>
<PRE>
{
char tab[10];
memset(tab, 0, 11);
}
</PRE>
</TD><TD VALIGN=TOP>Invalid range with standard string function</TD>
<TR>
<TD>
<PRE>
{
int tab[10];
for(i=0;i<11;i++) {
sum += tab[i];
}
}
</PRE>
</TD><TD VALIGN=TOP>Bound error in global or local arrays</TD>
<TR>
<TD>
<PRE>
{
int *tab;
tab = malloc(20 * sizeof(int));
for(i=0;i<21;i++) {
sum += tab4[i];
}
free(tab);
}
</PRE>
</TD><TD VALIGN=TOP>Bound error in allocated data</TD>
<TR>
<TD>
<PRE>
{
int *tab;
tab = malloc(20 * sizeof(int));
free(tab);
for(i=0;i<20;i++) {
sum += tab4[i];
}
}
</PRE>
</TD><TD VALIGN=TOP>Access to a freed region</TD>
<TR>
<TD>
<PRE>
{
int *tab;
tab = malloc(20 * sizeof(int));
free(tab);
free(tab);
}
</PRE>
</TD><TD VALIGN=TOP>Freeing an already freed region</TD>
</TABLE>
<h2> Command line invocation </h2>
<A NAME="invoke"></a>
2001-12-17 05:23:42 +08:00
<PRE>
2002-01-06 03:50:17 +08:00
usage: tcc [-Idir] [-Dsym[=val]] [-Usym] [-llib] [-g] [-b]
[-i infile] infile [infile_args...]
2001-12-17 05:23:42 +08:00
</PRE>
<table>
<tr><td>'-Idir'</td>
2002-01-06 03:50:17 +08:00
<td>Specify an additionnal include path. The default ones are:
2001-12-17 05:23:42 +08:00
/usr/include, /usr/lib/tcc, /usr/local/lib/tcc.</td>
2002-01-06 03:50:17 +08:00
<tr><td>'-Dsym[=val]'</td> <td>Define preprocessor symbol 'sym' to
2002-01-27 03:17:06 +08:00
val. If val is not present, its value is '1'. Function-like macros can
also be defined: <tt>'-DF(a)=a+1'</tt></td>
2002-01-06 03:50:17 +08:00
<tr><td>'-Usym'</td> <td>Undefine preprocessor symbol 'sym'.</td>
2001-12-17 05:23:42 +08:00
<tr><td>'-lxxx'</td>
2002-01-06 03:50:17 +08:00
<td>Dynamically link your program with library
2001-12-17 05:23:42 +08:00
libxxx.so. Standard library paths are checked, including those
2002-01-06 03:50:17 +08:00
specified with LD_LIBRARY_PATH.</td>
<tr><td>'-g'</td>
<td>Generate run time debug information so that you get clear run time
error messages: <tt> test.c:68: in function 'test5()': dereferencing
invalid pointer</tt> instead of the laconic <tt>Segmentation
fault</tt>.
</td>
<tr><td>'-b'</td> <td>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.
</td>
2001-12-17 05:23:42 +08:00
<tr><td>'-i file'</td>
2002-01-06 03:50:17 +08:00
<td>Compile C source 'file' before main C source. With this
2001-12-17 05:23:42 +08:00
command, multiple C files can be compiled and linked together.</td>
</table>
2002-01-06 03:50:17 +08:00
<br>
Note: the <tt>'-o file'</tt> option to generate an ELF executable is
currently unsupported.
2001-12-17 05:23:42 +08:00
<hr>
2002-01-06 03:50:17 +08:00
Copyright (c) 2001, 2002 Fabrice Bellard <hr>
Fabrice Bellard - <em> fabrice.bellard at free.fr </em> - <A HREF="http://bellard.org/"> http://bellard.org/ </A> - <A HREF="http://www.tinycc.org/"> http://www.tinycc.org/ </A>
2001-12-17 05:23:42 +08:00
</body>
</html>