2002-07-13 19:21:18 +04:00
|
|
|
\input texinfo @c -*- texinfo -*-
|
2003-04-10 04:03:26 +04:00
|
|
|
@c %**start of header
|
|
|
|
@setfilename tcc-doc.info
|
2002-07-13 19:21:18 +04:00
|
|
|
@settitle Tiny C Compiler Reference Documentation
|
2010-09-10 23:11:45 +04:00
|
|
|
@dircategory Software development
|
|
|
|
@direntry
|
|
|
|
* TCC: (tcc-doc). The Tiny C Compiler.
|
|
|
|
@end direntry
|
2003-04-10 04:03:26 +04:00
|
|
|
@c %**end of header
|
|
|
|
|
2003-04-14 01:56:00 +04:00
|
|
|
@include config.texi
|
2003-04-10 04:03:26 +04:00
|
|
|
|
|
|
|
@iftex
|
2002-07-13 19:21:18 +04:00
|
|
|
@titlepage
|
2003-04-10 04:03:26 +04:00
|
|
|
@afourpaper
|
2002-07-13 19:21:18 +04:00
|
|
|
@sp 7
|
|
|
|
@center @titlefont{Tiny C Compiler Reference Documentation}
|
|
|
|
@sp 3
|
|
|
|
@end titlepage
|
2003-04-10 04:03:26 +04:00
|
|
|
@headings double
|
|
|
|
@end iftex
|
|
|
|
|
|
|
|
@contents
|
|
|
|
|
|
|
|
@node Top, Introduction, (dir), (dir)
|
|
|
|
@top Tiny C Compiler Reference Documentation
|
|
|
|
|
|
|
|
This manual documents version @value{VERSION} of the Tiny C Compiler.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@menu
|
|
|
|
* Introduction:: Introduction to tcc.
|
|
|
|
* Invoke:: Invocation of tcc (command line, options).
|
2009-04-18 16:20:27 +04:00
|
|
|
* Clang:: ANSI C and extensions.
|
|
|
|
* asm:: Assembler syntax.
|
|
|
|
* linker:: Output file generation and supported targets.
|
2003-04-10 04:03:26 +04:00
|
|
|
* Bounds:: Automatic bounds-checking of C code.
|
2003-04-14 01:56:00 +04:00
|
|
|
* Libtcc:: The libtcc library.
|
2009-04-18 16:20:27 +04:00
|
|
|
* devel:: Guide for Developers.
|
2003-04-10 04:03:26 +04:00
|
|
|
@end menu
|
2009-04-18 16:20:27 +04:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
|
|
|
|
@node Introduction
|
2002-07-13 19:21:18 +04:00
|
|
|
@chapter Introduction
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-01-05 22:50:17 +03:00
|
|
|
TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C
|
2003-04-10 04:03:26 +04:00
|
|
|
compilers, it is meant to be self-relying: you do not need an
|
2001-12-17 00:23:42 +03:00
|
|
|
external assembler or linker because TCC does that for you.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
TCC compiles so @emph{fast} that even for big projects @code{Makefile}s may
|
2009-04-18 16:20:27 +04:00
|
|
|
not be necessary.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
2002-01-05 22:50:17 +03:00
|
|
|
TCC not only supports ANSI C, but also most of the new ISO C99
|
2003-01-06 23:19:20 +03:00
|
|
|
standard and many GNUC extensions including inline assembly.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
TCC can also be used to make @emph{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
|
2003-04-10 04:03:26 +04:00
|
|
|
(@pxref{Bounds}) while allowing all C pointers operations. TCC can do
|
2002-07-13 19:21:18 +04:00
|
|
|
these checks even if non patched libraries are used.
|
|
|
|
|
|
|
|
With @code{libtcc}, you can use TCC as a backend for dynamic code
|
2003-04-10 04:03:26 +04:00
|
|
|
generation (@pxref{Libtcc}).
|
2002-07-13 19:21:18 +04:00
|
|
|
|
2005-04-15 03:49:21 +04:00
|
|
|
TCC mainly supports the i386 target on Linux and Windows. There are alpha
|
|
|
|
ports for the ARM (@code{arm-tcc}) and the TMS320C67xx targets
|
|
|
|
(@code{c67-tcc}). More information about the ARM port is available at
|
2004-11-08 23:52:04 +03:00
|
|
|
@url{http://lists.gnu.org/archive/html/tinycc-devel/2003-10/msg00044.html}.
|
2004-10-24 03:11:05 +04:00
|
|
|
|
2013-02-08 22:07:11 +04:00
|
|
|
For usage on Windows, see also @url{tcc-win32.txt}.
|
2009-04-18 16:20:27 +04:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@node Invoke
|
2002-07-13 19:21:18 +04:00
|
|
|
@chapter Command line invocation
|
|
|
|
|
2002-07-16 02:38:56 +04:00
|
|
|
@section Quick start
|
|
|
|
|
|
|
|
@example
|
2003-05-18 22:11:06 +04:00
|
|
|
@c man begin SYNOPSIS
|
2003-05-24 20:12:58 +04:00
|
|
|
usage: tcc [options] [@var{infile1} @var{infile2}@dots{}] [@option{-run} @var{infile} @var{args}@dots{}]
|
2003-05-18 22:11:06 +04:00
|
|
|
@c man end
|
2002-07-16 02:38:56 +04:00
|
|
|
@end example
|
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@noindent
|
2003-05-18 22:11:06 +04:00
|
|
|
@c man begin DESCRIPTION
|
2003-04-10 04:03:26 +04:00
|
|
|
TCC options are a very much like gcc options. The main difference is that TCC
|
2002-07-16 02:38:56 +04:00
|
|
|
can also execute directly the resulting program and give it runtime
|
|
|
|
arguments.
|
|
|
|
|
|
|
|
Here are some examples to understand the logic:
|
|
|
|
|
|
|
|
@table @code
|
2003-04-14 01:56:00 +04:00
|
|
|
@item @samp{tcc -run a.c}
|
2003-04-10 04:03:26 +04:00
|
|
|
Compile @file{a.c} and execute it directly
|
2002-07-16 02:38:56 +04:00
|
|
|
|
2003-04-14 01:56:00 +04:00
|
|
|
@item @samp{tcc -run a.c arg1}
|
2002-07-16 02:38:56 +04:00
|
|
|
Compile a.c and execute it directly. arg1 is given as first argument to
|
|
|
|
the @code{main()} of a.c.
|
|
|
|
|
2003-04-14 01:56:00 +04:00
|
|
|
@item @samp{tcc a.c -run b.c arg1}
|
2003-04-10 04:03:26 +04:00
|
|
|
Compile @file{a.c} and @file{b.c}, link them together and execute them. arg1 is given
|
2015-07-29 23:53:57 +03:00
|
|
|
as first argument to the @code{main()} of the resulting program.
|
|
|
|
@ignore
|
|
|
|
Because multiple C files are specified, @option{--} are necessary to clearly
|
2008-03-31 22:42:56 +04:00
|
|
|
separate the program arguments from the TCC options.
|
|
|
|
@end ignore
|
2002-07-16 02:38:56 +04:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item @samp{tcc -o myprog a.c b.c}
|
|
|
|
Compile @file{a.c} and @file{b.c}, link them and generate the executable @file{myprog}.
|
2002-07-16 02:38:56 +04:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item @samp{tcc -o myprog a.o b.o}
|
|
|
|
link @file{a.o} and @file{b.o} together and generate the executable @file{myprog}.
|
2002-07-16 02:38:56 +04:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item @samp{tcc -c a.c}
|
|
|
|
Compile @file{a.c} and generate object file @file{a.o}.
|
2002-07-16 02:38:56 +04:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item @samp{tcc -c asmfile.S}
|
|
|
|
Preprocess with C preprocess and assemble @file{asmfile.S} and generate
|
|
|
|
object file @file{asmfile.o}.
|
2003-01-06 23:19:20 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item @samp{tcc -c asmfile.s}
|
|
|
|
Assemble (but not preprocess) @file{asmfile.s} and generate object file
|
|
|
|
@file{asmfile.o}.
|
2003-01-06 23:19:20 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item @samp{tcc -r -o ab.o a.c b.c}
|
|
|
|
Compile @file{a.c} and @file{b.c}, link them together and generate the object file @file{ab.o}.
|
2002-08-31 16:44:16 +04:00
|
|
|
|
2002-07-16 02:38:56 +04:00
|
|
|
@end table
|
|
|
|
|
|
|
|
Scripting:
|
|
|
|
|
|
|
|
TCC can be invoked from @emph{scripts}, just as shell scripts. You just
|
2003-04-14 01:56:00 +04:00
|
|
|
need to add @code{#!/usr/local/bin/tcc -run} at the start of your C source:
|
2002-07-16 02:38:56 +04:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2003-05-18 22:11:06 +04:00
|
|
|
#!/usr/local/bin/tcc -run
|
2002-07-16 02:38:56 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
int main()
|
2003-04-10 04:03:26 +04:00
|
|
|
@{
|
2002-07-16 02:38:56 +04:00
|
|
|
printf("Hello World\n");
|
|
|
|
return 0;
|
2003-04-10 04:03:26 +04:00
|
|
|
@}
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2008-03-31 22:42:56 +04:00
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
TCC can read C source code from @emph{standard input} when @option{-} is used in
|
2008-03-31 22:42:56 +04:00
|
|
|
place of @option{infile}. Example:
|
|
|
|
|
|
|
|
@example
|
2009-04-18 16:20:27 +04:00
|
|
|
echo 'main()@{puts("hello");@}' | tcc -run -
|
2008-03-31 22:42:56 +04:00
|
|
|
@end example
|
2003-05-18 22:11:06 +04:00
|
|
|
@c man end
|
2002-07-13 19:21:18 +04:00
|
|
|
|
2002-07-16 02:38:56 +04:00
|
|
|
@section Option summary
|
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
2003-05-18 22:11:06 +04:00
|
|
|
@c man begin OPTIONS
|
2003-04-10 04:03:26 +04:00
|
|
|
@table @option
|
2002-07-16 02:38:56 +04:00
|
|
|
@item -c
|
2011-05-17 23:55:05 +04:00
|
|
|
Generate an object file.
|
2002-07-16 02:38:56 +04:00
|
|
|
|
|
|
|
@item -o outfile
|
|
|
|
Put object file, executable, or dll into output file @file{outfile}.
|
|
|
|
|
2003-10-05 01:29:04 +04:00
|
|
|
@item -run source [args...]
|
|
|
|
Compile file @var{source} and run it with the command line arguments
|
|
|
|
@var{args}. In order to be able to give more than one argument to a
|
|
|
|
script, several TCC options can be given @emph{after} the
|
2013-02-10 23:39:05 +04:00
|
|
|
@option{-run} option, separated by spaces:
|
2003-10-05 01:29:04 +04:00
|
|
|
@example
|
|
|
|
tcc "-run -L/usr/X11R6/lib -lX11" ex4.c
|
|
|
|
@end example
|
|
|
|
In a script, it gives the following header:
|
|
|
|
@example
|
|
|
|
#!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11
|
|
|
|
@end example
|
|
|
|
|
2013-02-10 23:39:05 +04:00
|
|
|
@item -v
|
|
|
|
Display TCC version.
|
|
|
|
|
|
|
|
@item -vv
|
2017-02-13 21:03:29 +03:00
|
|
|
Show included files. As sole argument, print search dirs. -vvv shows tries too.
|
2013-02-10 23:39:05 +04:00
|
|
|
|
|
|
|
@item -bench
|
|
|
|
Display compilation statistics.
|
|
|
|
|
2002-07-16 02:38:56 +04:00
|
|
|
@end table
|
|
|
|
|
|
|
|
Preprocessor options:
|
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@table @option
|
2002-07-13 19:21:18 +04:00
|
|
|
@item -Idir
|
2003-04-10 04:03:26 +04:00
|
|
|
Specify an additional include path. Include paths are searched in the
|
2002-08-18 17:24:19 +04:00
|
|
|
order they are specified.
|
|
|
|
|
|
|
|
System include paths are always searched after. The default system
|
|
|
|
include paths are: @file{/usr/local/include}, @file{/usr/include}
|
2003-04-10 04:03:26 +04:00
|
|
|
and @file{PREFIX/lib/tcc/include}. (@file{PREFIX} is usually
|
2002-08-18 17:24:19 +04:00
|
|
|
@file{/usr} or @file{/usr/local}).
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@item -Dsym[=val]
|
2003-04-10 04:03:26 +04:00
|
|
|
Define preprocessor symbol @samp{sym} to
|
|
|
|
val. If val is not present, its value is @samp{1}. Function-like macros can
|
|
|
|
also be defined: @option{-DF(a)=a+1}
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@item -Usym
|
2003-04-10 04:03:26 +04:00
|
|
|
Undefine preprocessor symbol @samp{sym}.
|
2017-02-13 21:03:29 +03:00
|
|
|
|
|
|
|
@item -E
|
|
|
|
Preprocess only, to stdout or file (with -o).
|
|
|
|
|
2002-07-16 02:38:56 +04:00
|
|
|
@end table
|
2002-07-13 19:21:18 +04:00
|
|
|
|
2003-10-18 00:43:47 +04:00
|
|
|
Compilation flags:
|
|
|
|
|
2014-01-07 13:50:08 +04:00
|
|
|
Note: each of the following options has a negative form beginning with
|
2003-10-18 00:43:47 +04:00
|
|
|
@option{-fno-}.
|
|
|
|
|
|
|
|
@table @option
|
|
|
|
@item -funsigned-char
|
|
|
|
Let the @code{char} type be unsigned.
|
|
|
|
|
|
|
|
@item -fsigned-char
|
|
|
|
Let the @code{char} type be signed.
|
|
|
|
|
2004-10-24 03:11:05 +04:00
|
|
|
@item -fno-common
|
|
|
|
Do not generate common symbols for uninitialized data.
|
|
|
|
|
2005-06-18 02:07:03 +04:00
|
|
|
@item -fleading-underscore
|
|
|
|
Add a leading underscore at the beginning of each C symbol.
|
|
|
|
|
2015-04-10 06:31:58 +03:00
|
|
|
@item -fms-extensions
|
2016-11-22 23:56:00 +03:00
|
|
|
Allow a MS C compiler extensions to the language. Currently this
|
|
|
|
assumes a nested named structure declaration without an identifier
|
|
|
|
behaves like an unnamed one.
|
2015-04-10 06:31:58 +03:00
|
|
|
|
2015-04-12 15:32:03 +03:00
|
|
|
@item -fdollars-in-identifiers
|
2016-11-22 23:56:00 +03:00
|
|
|
Allow dollar signs in identifiers
|
2015-04-12 15:32:03 +03:00
|
|
|
|
2003-10-18 00:43:47 +04:00
|
|
|
@end table
|
|
|
|
|
2003-05-24 20:12:58 +04:00
|
|
|
Warning options:
|
|
|
|
|
2003-10-04 17:04:47 +04:00
|
|
|
@table @option
|
|
|
|
@item -w
|
|
|
|
Disable all warnings.
|
|
|
|
|
|
|
|
@end table
|
|
|
|
|
|
|
|
Note: each of the following warning options has a negative form beginning with
|
|
|
|
@option{-Wno-}.
|
2003-05-24 20:12:58 +04:00
|
|
|
|
|
|
|
@table @option
|
2003-10-18 00:43:47 +04:00
|
|
|
@item -Wimplicit-function-declaration
|
|
|
|
Warn about implicit function declaration.
|
|
|
|
|
2003-05-24 20:12:58 +04:00
|
|
|
@item -Wunsupported
|
|
|
|
Warn about unsupported GCC features that are ignored by TCC.
|
|
|
|
|
|
|
|
@item -Wwrite-strings
|
2003-10-18 00:43:47 +04:00
|
|
|
Make string constants be of type @code{const char *} instead of @code{char
|
2003-05-24 20:12:58 +04:00
|
|
|
*}.
|
|
|
|
|
|
|
|
@item -Werror
|
|
|
|
Abort compilation if warnings are issued.
|
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item -Wall
|
2003-05-24 20:12:58 +04:00
|
|
|
Activate all warnings, except @option{-Werror}, @option{-Wunusupported} and
|
2003-10-18 00:43:47 +04:00
|
|
|
@option{-Wwrite-strings}.
|
2003-05-24 20:12:58 +04:00
|
|
|
|
|
|
|
@end table
|
|
|
|
|
2002-07-16 02:38:56 +04:00
|
|
|
Linker options:
|
2002-07-13 19:21:18 +04:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@table @option
|
2002-07-25 02:13:02 +04:00
|
|
|
@item -Ldir
|
2003-04-10 04:03:26 +04:00
|
|
|
Specify an additional static library path for the @option{-l} option. The
|
2002-07-25 02:13:02 +04:00
|
|
|
default library paths are @file{/usr/local/lib}, @file{/usr/lib} and @file{/lib}.
|
|
|
|
|
2002-07-16 02:38:56 +04:00
|
|
|
@item -lxxx
|
2002-07-25 02:13:02 +04:00
|
|
|
Link your program with dynamic library libxxx.so or static library
|
|
|
|
libxxx.a. The library is searched in the paths specified by the
|
2013-02-20 17:23:44 +04:00
|
|
|
@option{-L} option and @env{LIBRARY_PATH} variable.
|
2002-07-16 02:38:56 +04:00
|
|
|
|
2013-02-10 23:39:05 +04:00
|
|
|
@item -Bdir
|
|
|
|
Set the path where the tcc internal libraries (and include files) can be
|
|
|
|
found (default is @file{PREFIX/lib/tcc}).
|
|
|
|
|
2002-07-16 02:38:56 +04:00
|
|
|
@item -shared
|
2011-05-17 23:55:05 +04:00
|
|
|
Generate a shared library instead of an executable.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
2009-12-20 00:08:15 +03:00
|
|
|
@item -soname name
|
|
|
|
set name for shared library to be used at runtime
|
|
|
|
|
2002-07-16 02:38:56 +04:00
|
|
|
@item -static
|
|
|
|
Generate a statically linked executable (default is a shared linked
|
2011-05-17 23:55:05 +04:00
|
|
|
executable).
|
2002-07-16 02:38:56 +04:00
|
|
|
|
2003-05-24 20:12:58 +04:00
|
|
|
@item -rdynamic
|
|
|
|
Export global symbols to the dynamic linker. It is useful when a library
|
|
|
|
opened with @code{dlopen()} needs to access executable symbols.
|
|
|
|
|
2002-08-31 16:44:16 +04:00
|
|
|
@item -r
|
2011-05-17 23:55:05 +04:00
|
|
|
Generate an object file combining all input files.
|
2002-08-31 16:44:16 +04:00
|
|
|
|
2013-02-10 23:39:05 +04:00
|
|
|
@item -Wl,-rpath=path
|
2016-07-05 16:48:31 +03:00
|
|
|
Put custom search path for dynamic libraries into executable.
|
2004-10-24 03:11:05 +04:00
|
|
|
|
-Wl, --enable-new-dtags for DT_RUNPATH instead of DT_RPATH
Today by accident i had to deal with linker problems of some
software and found an issue that mentioned DT_RUNPATH, which
mentioned that DT_RPATH is legacy and searched for
$LD_LIBRARY_PATH, whereas the newer DT_RUNPATH is searched
thereafter. Completely unencrypted! Well. For what's it worth,
i for one am astonished because of course i want to override
$LD_LIBRARY_PATH, but it surely has its merites, smart people came
to the conclusion, did they.
The attached diff below seems to be sufficient to support
DT_RUNPATH instead of DT_RPATH with tcc(1). But i have no insight
in what --enable-new-dtags is supposed to change in addition, so
i wonder.
Ciao!
--steffen
libtcc.c | 2 ++
tcc-doc.texi | 4 ++++
tcc.h | 1 +
tccelf.c | 3 ++-
4 files changed, 9 insertions(+), 1 deletion(-)
2017-02-16 17:49:02 +03:00
|
|
|
@item -Wl,--enable-new-dtags
|
|
|
|
When putting a custom search path for dynamic libraries into the executable,
|
|
|
|
create the new ELF dynamic tag DT_RUNPATH instead of the old legacy DT_RPATH.
|
|
|
|
|
2013-02-10 23:39:05 +04:00
|
|
|
@item -Wl,--oformat=fmt
|
2004-10-24 03:11:05 +04:00
|
|
|
Use @var{fmt} as output format. The supported output formats are:
|
|
|
|
@table @code
|
|
|
|
@item elf32-i386
|
|
|
|
ELF output format (default)
|
|
|
|
@item binary
|
|
|
|
Binary image (only for executable output)
|
|
|
|
@item coff
|
|
|
|
COFF output format (only for executable output for TMS320C67xx target)
|
|
|
|
@end table
|
|
|
|
|
2018-06-11 19:15:24 +03:00
|
|
|
@item -Wl,--export-all-symbols
|
|
|
|
@item -Wl,--export-dynamic
|
|
|
|
Export global symbols to the dynamic linker. It is useful when a library
|
|
|
|
opened with @code{dlopen()} needs to access executable symbols.
|
|
|
|
|
2013-02-10 23:39:05 +04:00
|
|
|
@item -Wl,-subsystem=console/gui/wince/...
|
|
|
|
Set type for PE (Windows) executables.
|
|
|
|
|
|
|
|
@item -Wl,-[Ttext=# | section-alignment=# | file-alignment=# | image-base=# | stack=#]
|
|
|
|
Modify executable layout.
|
|
|
|
|
|
|
|
@item -Wl,-Bsymbolic
|
|
|
|
Set DT_SYMBOLIC tag.
|
2009-12-20 00:08:15 +03:00
|
|
|
|
2017-02-13 21:03:29 +03:00
|
|
|
@item -Wl,-(no-)whole-archive
|
|
|
|
Turn on/off linking of all objects in archives.
|
|
|
|
|
2002-07-16 02:38:56 +04:00
|
|
|
@end table
|
2002-07-13 19:21:18 +04:00
|
|
|
|
2002-11-03 03:44:38 +03:00
|
|
|
Debugger options:
|
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@table @option
|
2002-11-03 03:44:38 +03:00
|
|
|
@item -g
|
|
|
|
Generate run time debug information so that you get clear run time
|
|
|
|
error messages: @code{ test.c:68: in function 'test5()': dereferencing
|
|
|
|
invalid pointer} instead of the laconic @code{Segmentation
|
|
|
|
fault}.
|
|
|
|
|
|
|
|
@item -b
|
2003-04-10 04:03:26 +04:00
|
|
|
Generate additional support code to check
|
|
|
|
memory allocations and array/pointer bounds. @option{-g} is implied. Note
|
2002-11-03 03:44:38 +03:00
|
|
|
that the generated code is slower and bigger in this case.
|
|
|
|
|
2014-01-06 07:09:06 +04:00
|
|
|
Note: @option{-b} is only available on i386 when using libtcc for the moment.
|
2012-03-13 22:43:43 +04:00
|
|
|
|
2002-11-03 03:44:38 +03:00
|
|
|
@item -bt N
|
2003-04-10 04:03:26 +04:00
|
|
|
Display N callers in stack traces. This is useful with @option{-g} or
|
|
|
|
@option{-b}.
|
2002-11-03 03:44:38 +03:00
|
|
|
|
|
|
|
@end table
|
|
|
|
|
2013-02-10 23:39:05 +04:00
|
|
|
Misc options:
|
|
|
|
|
|
|
|
@table @option
|
|
|
|
@item -MD
|
|
|
|
Generate makefile fragment with dependencies.
|
|
|
|
|
|
|
|
@item -MF depfile
|
|
|
|
Use @file{depfile} as output for -MD.
|
|
|
|
|
2017-02-13 21:03:29 +03:00
|
|
|
@item -print-search-dirs
|
|
|
|
Print the configured installation directory and a list of library
|
|
|
|
and include directories tcc will search.
|
|
|
|
|
|
|
|
@item -dumpversion
|
|
|
|
Print version.
|
|
|
|
|
|
|
|
@end table
|
|
|
|
|
|
|
|
Target specific options:
|
|
|
|
|
|
|
|
@table @option
|
|
|
|
@item -mms-bitfields
|
|
|
|
Use an algorithm for bitfield alignment consistent with MSVC. Default is
|
|
|
|
gcc's algorithm.
|
|
|
|
|
|
|
|
@item -mfloat-abi (ARM only)
|
|
|
|
Select the float ABI. Possible values: @code{softfp} and @code{hard}
|
|
|
|
|
|
|
|
@item -mno-sse
|
2017-02-20 20:58:08 +03:00
|
|
|
Do not use sse registers on x86_64
|
2017-02-13 21:03:29 +03:00
|
|
|
|
|
|
|
@item -m32, -m64
|
2017-02-20 20:58:08 +03:00
|
|
|
Pass command line to the i386/x86_64 cross compiler.
|
2017-02-13 21:03:29 +03:00
|
|
|
|
2013-02-10 23:39:05 +04:00
|
|
|
@end table
|
|
|
|
|
2003-05-24 20:12:58 +04:00
|
|
|
Note: GCC options @option{-Ox}, @option{-fx} and @option{-mx} are
|
2002-11-03 03:44:38 +03:00
|
|
|
ignored.
|
2003-05-18 22:11:06 +04:00
|
|
|
@c man end
|
|
|
|
|
2013-02-20 17:23:44 +04:00
|
|
|
@c man begin ENVIRONMENT
|
|
|
|
Environment variables that affect how tcc operates.
|
|
|
|
|
|
|
|
@table @option
|
|
|
|
|
|
|
|
@item CPATH
|
|
|
|
@item C_INCLUDE_PATH
|
|
|
|
A colon-separated list of directories searched for include files,
|
|
|
|
directories given with @option{-I} are searched first.
|
|
|
|
|
|
|
|
@item LIBRARY_PATH
|
|
|
|
A colon-separated list of directories searched for libraries for the
|
|
|
|
@option{-l} option, directories given with @option{-L} are searched first.
|
|
|
|
|
|
|
|
@end table
|
|
|
|
|
|
|
|
@c man end
|
|
|
|
|
2003-05-18 22:11:06 +04:00
|
|
|
@ignore
|
|
|
|
|
|
|
|
@setfilename tcc
|
|
|
|
@settitle Tiny C Compiler
|
|
|
|
|
|
|
|
@c man begin SEEALSO
|
2013-02-20 17:23:44 +04:00
|
|
|
cpp(1),
|
2003-05-18 22:11:06 +04:00
|
|
|
gcc(1)
|
|
|
|
@c man end
|
|
|
|
|
|
|
|
@c man begin AUTHOR
|
|
|
|
Fabrice Bellard
|
|
|
|
@c man end
|
|
|
|
|
|
|
|
@end ignore
|
2002-11-03 03:44:38 +03:00
|
|
|
|
2009-04-18 16:20:27 +04:00
|
|
|
@node Clang
|
2002-07-13 19:21:18 +04:00
|
|
|
@chapter C language support
|
|
|
|
|
|
|
|
@section ANSI C
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-01-05 22:50:17 +03:00
|
|
|
TCC implements all the ANSI C standard, including structure bit fields
|
2002-07-13 19:21:18 +04:00
|
|
|
and floating point numbers (@code{long double}, @code{double}, and
|
2002-11-03 03:44:38 +03:00
|
|
|
@code{float} fully supported).
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@section ISOC99 extensions
|
2001-12-17 00:23:42 +03:00
|
|
|
|
|
|
|
TCC implements many features of the new C standard: ISO C99. Currently
|
2013-02-17 03:48:51 +04:00
|
|
|
missing items are: complex and imaginary numbers.
|
2001-12-17 00:23:42 +03:00
|
|
|
|
|
|
|
Currently implemented ISOC99 features:
|
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@itemize
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2013-02-17 03:48:51 +04:00
|
|
|
@item variable length arrays.
|
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item 64 bit @code{long long} types are fully supported.
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item The boolean type @code{_Bool} is supported.
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item @code{__func__} is a string variable containing the current
|
2001-12-17 00:23:42 +03:00
|
|
|
function name.
|
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@item Variadic macros: @code{__VA_ARGS__} can be used for
|
2001-12-17 00:23:42 +03:00
|
|
|
function-like macros:
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2001-12-17 00:23:42 +03:00
|
|
|
#define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2003-04-10 04:03:26 +04:00
|
|
|
|
|
|
|
@noindent
|
2002-07-13 19:21:18 +04:00
|
|
|
@code{dprintf} can then be used with a variable number of parameters.
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@item Declarations can appear anywhere in a block (as in C++).
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@item Array and struct/union elements can be initialized in any order by
|
2001-12-17 00:23:42 +03:00
|
|
|
using designators:
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
struct @{ int x, y; @} st[10] = @{ [0].x = 1, [0].y = 2 @};
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
int tab[10] = @{ 1, 2, [5] = 5, [9] = 9@};
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@item Compound initializers are supported:
|
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
int *p = (int [])@{ 1, 2, 3 @};
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2001-12-17 00:23:42 +03:00
|
|
|
to initialize a pointer pointing to an initialized array. The same
|
|
|
|
works for structures and strings.
|
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@item Hexadecimal floating point constants are supported:
|
|
|
|
@example
|
2001-12-17 00:23:42 +03:00
|
|
|
double d = 0x1234p10;
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2003-04-10 04:03:26 +04:00
|
|
|
|
|
|
|
@noindent
|
2015-07-29 23:53:57 +03:00
|
|
|
is the same as writing
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2001-12-17 00:23:42 +03:00
|
|
|
double d = 4771840.0;
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item @code{inline} keyword is ignored.
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item @code{restrict} keyword is ignored.
|
2002-07-13 19:21:18 +04:00
|
|
|
@end itemize
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@section GNU C extensions
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-01-05 22:50:17 +03:00
|
|
|
TCC implements some GNU C extensions:
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@itemize
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item array designators can be used without '=':
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
int a[10] = @{ [0] 1, [5] 2, 3, 4 @};
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item Structure field designators can be a label:
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
struct @{ int x, y; @} st = @{ x: 1, y: 1@};
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2001-12-17 00:23:42 +03:00
|
|
|
instead of
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
struct @{ int x, y; @} st = @{ .x = 1, .y = 1@};
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item @code{\e} is ASCII character 27.
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@item case ranges : ranges can be used in @code{case}s:
|
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
switch(a) @{
|
|
|
|
case 1 @dots{} 9:
|
2002-01-05 22:50:17 +03:00
|
|
|
printf("range 1 to 9\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("unexpected\n");
|
|
|
|
break;
|
2003-04-10 04:03:26 +04:00
|
|
|
@}
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2008-03-31 23:50:58 +04:00
|
|
|
@cindex aligned attribute
|
|
|
|
@cindex packed attribute
|
|
|
|
@cindex section attribute
|
|
|
|
@cindex unused attribute
|
|
|
|
@cindex cdecl attribute
|
|
|
|
@cindex stdcall attribute
|
|
|
|
@cindex regparm attribute
|
|
|
|
@cindex dllexport attribute
|
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@item The keyword @code{__attribute__} is handled to specify variable or
|
2002-01-05 22:50:17 +03:00
|
|
|
function attributes. The following attributes are supported:
|
2002-07-13 19:21:18 +04:00
|
|
|
@itemize
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2004-11-09 00:19:47 +03:00
|
|
|
@item @code{aligned(n)}: align a variable or a structure field to n bytes
|
|
|
|
(must be a power of two).
|
2008-03-31 23:50:58 +04:00
|
|
|
|
2004-11-09 00:19:47 +03:00
|
|
|
@item @code{packed}: force alignment of a variable or a structure field to
|
|
|
|
1.
|
|
|
|
|
|
|
|
@item @code{section(name)}: generate function or data in assembly section
|
|
|
|
name (name is a string containing the section name) instead of the default
|
|
|
|
section.
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@item @code{unused}: specify that the variable or the function is unused.
|
|
|
|
|
2004-11-09 00:19:47 +03:00
|
|
|
@item @code{cdecl}: use standard C calling convention (default).
|
2002-01-26 22:17:06 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@item @code{stdcall}: use Pascal-like calling convention.
|
2002-01-26 22:17:06 +03:00
|
|
|
|
2004-11-09 00:19:47 +03:00
|
|
|
@item @code{regparm(n)}: use fast i386 calling convention. @var{n} must be
|
|
|
|
between 1 and 3. The first @var{n} function parameters are respectively put in
|
|
|
|
registers @code{%eax}, @code{%edx} and @code{%ecx}.
|
|
|
|
|
2008-03-31 23:50:58 +04:00
|
|
|
@item @code{dllexport}: export function from dll/executable (win32 only)
|
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@end itemize
|
2002-01-26 22:17:06 +03:00
|
|
|
|
2002-01-05 22:50:17 +03:00
|
|
|
Here are some examples:
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2002-01-05 22:50:17 +03:00
|
|
|
int a __attribute__ ((aligned(8), section(".mysection")));
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@noindent
|
|
|
|
align variable @code{a} to 8 bytes and put it in section @code{.mysection}.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@example
|
2015-07-29 23:53:57 +03:00
|
|
|
int my_add(int a, int b) __attribute__ ((section(".mycodesection")))
|
2003-04-10 04:03:26 +04:00
|
|
|
@{
|
2002-01-05 22:50:17 +03:00
|
|
|
return a + b;
|
2003-04-10 04:03:26 +04:00
|
|
|
@}
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@noindent
|
|
|
|
generate function @code{my_add} in section @code{.mycodesection}.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@item GNU style variadic macros:
|
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
#define dprintf(fmt, args@dots{}) printf(fmt, ## args)
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
dprintf("no arg\n");
|
|
|
|
dprintf("one arg %d\n", 1);
|
|
|
|
@end example
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item @code{__FUNCTION__} is interpreted as C99 @code{__func__}
|
2002-08-31 16:44:16 +04:00
|
|
|
(so it has not exactly the same semantics as string literal GNUC
|
|
|
|
where it is a string literal).
|
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item The @code{__alignof__} keyword can be used as @code{sizeof}
|
2002-11-03 03:44:38 +03:00
|
|
|
to get the alignment of a type or an expression.
|
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item The @code{typeof(x)} returns the type of @code{x}.
|
2002-11-03 03:44:38 +03:00
|
|
|
@code{x} is an expression or a type.
|
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item Computed gotos: @code{&&label} returns a pointer of type
|
2002-11-03 03:44:38 +03:00
|
|
|
@code{void *} on the goto label @code{label}. @code{goto *expr} can be
|
|
|
|
used to jump on the pointer resulting from @code{expr}.
|
|
|
|
|
2003-01-06 23:19:20 +03:00
|
|
|
@item Inline assembly with asm instruction:
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex inline assembly
|
|
|
|
@cindex assembly, inline
|
|
|
|
@cindex __asm__
|
2003-01-06 23:19:20 +03:00
|
|
|
@example
|
|
|
|
static inline void * my_memcpy(void * to, const void * from, size_t n)
|
2003-04-10 04:03:26 +04:00
|
|
|
@{
|
2003-01-06 23:19:20 +03:00
|
|
|
int d0, d1, d2;
|
|
|
|
__asm__ __volatile__(
|
2003-04-10 04:03:26 +04:00
|
|
|
"rep ; movsl\n\t"
|
|
|
|
"testb $2,%b4\n\t"
|
|
|
|
"je 1f\n\t"
|
|
|
|
"movsw\n"
|
|
|
|
"1:\ttestb $1,%b4\n\t"
|
|
|
|
"je 2f\n\t"
|
|
|
|
"movsb\n"
|
|
|
|
"2:"
|
|
|
|
: "=&c" (d0), "=&D" (d1), "=&S" (d2)
|
|
|
|
:"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
|
|
|
|
: "memory");
|
2003-01-06 23:19:20 +03:00
|
|
|
return (to);
|
2003-04-10 04:03:26 +04:00
|
|
|
@}
|
2003-01-06 23:19:20 +03:00
|
|
|
@end example
|
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@noindent
|
|
|
|
@cindex gas
|
2003-01-06 23:19:20 +03:00
|
|
|
TCC includes its own x86 inline assembler with a @code{gas}-like (GNU
|
|
|
|
assembler) syntax. No intermediate files are generated. GCC 3.x named
|
|
|
|
operands are supported.
|
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item @code{__builtin_types_compatible_p()} and @code{__builtin_constant_p()}
|
2003-05-24 20:12:58 +04:00
|
|
|
are supported.
|
|
|
|
|
2005-06-18 02:07:03 +04:00
|
|
|
@item @code{#pragma pack} is supported for win32 compatibility.
|
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@end itemize
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@section TinyCC extensions
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@itemize
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2013-02-18 18:44:18 +04:00
|
|
|
@item @code{__TINYC__} is a predefined macro to indicate that you use TCC.
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item @code{#!} at the start of a line is ignored to allow scripting.
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item Binary digits can be entered (@code{0b101} instead of
|
|
|
|
@code{5}).
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@item @code{__BOUNDS_CHECKING_ON} is defined if bound checking is activated.
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@end itemize
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2009-04-18 16:20:27 +04:00
|
|
|
@node asm
|
2003-01-06 23:19:20 +03:00
|
|
|
@chapter TinyCC Assembler
|
|
|
|
|
|
|
|
Since version 0.9.16, TinyCC integrates its own assembler. TinyCC
|
|
|
|
assembler supports a gas-like syntax (GNU assembler). You can
|
2017-09-25 04:22:42 +03:00
|
|
|
deactivate assembler support if you want a smaller TinyCC executable
|
2003-01-06 23:19:20 +03:00
|
|
|
(the C compiler does not rely on the assembler).
|
|
|
|
|
|
|
|
TinyCC Assembler is used to handle files with @file{.S} (C
|
|
|
|
preprocessed assembler) and @file{.s} extensions. It is also used to
|
|
|
|
handle the GNU inline assembler with the @code{asm} keyword.
|
|
|
|
|
|
|
|
@section Syntax
|
|
|
|
|
|
|
|
TinyCC Assembler supports most of the gas syntax. The tokens are the
|
|
|
|
same as C.
|
|
|
|
|
|
|
|
@itemize
|
|
|
|
|
|
|
|
@item C and C++ comments are supported.
|
|
|
|
|
|
|
|
@item Identifiers are the same as C, so you cannot use '.' or '$'.
|
|
|
|
|
|
|
|
@item Only 32 bit integer numbers are supported.
|
|
|
|
|
|
|
|
@end itemize
|
|
|
|
|
|
|
|
@section Expressions
|
|
|
|
|
|
|
|
@itemize
|
|
|
|
|
|
|
|
@item Integers in decimal, octal and hexa are supported.
|
|
|
|
|
|
|
|
@item Unary operators: +, -, ~.
|
|
|
|
|
|
|
|
@item Binary operators in decreasing priority order:
|
|
|
|
|
|
|
|
@enumerate
|
|
|
|
@item *, /, %
|
|
|
|
@item &, |, ^
|
|
|
|
@item +, -
|
|
|
|
@end enumerate
|
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item A value is either an absolute number or a label plus an offset.
|
2003-01-06 23:19:20 +03:00
|
|
|
All operators accept absolute values except '+' and '-'. '+' or '-' can be
|
|
|
|
used to add an offset to a label. '-' supports two labels only if they
|
|
|
|
are the same or if they are both defined and in the same section.
|
|
|
|
|
|
|
|
@end itemize
|
|
|
|
|
|
|
|
@section Labels
|
|
|
|
|
|
|
|
@itemize
|
|
|
|
|
|
|
|
@item All labels are considered as local, except undefined ones.
|
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item Numeric labels can be used as local @code{gas}-like labels.
|
2003-01-06 23:19:20 +03:00
|
|
|
They can be defined several times in the same source. Use 'b'
|
|
|
|
(backward) or 'f' (forward) as suffix to reference them:
|
|
|
|
|
|
|
|
@example
|
|
|
|
1:
|
|
|
|
jmp 1b /* jump to '1' label before */
|
|
|
|
jmp 1f /* jump to '1' label after */
|
|
|
|
1:
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@end itemize
|
|
|
|
|
|
|
|
@section Directives
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex assembler directives
|
|
|
|
@cindex directives, assembler
|
2004-11-09 00:19:47 +03:00
|
|
|
@cindex align directive
|
|
|
|
@cindex skip directive
|
|
|
|
@cindex space directive
|
|
|
|
@cindex byte directive
|
|
|
|
@cindex word directive
|
|
|
|
@cindex short directive
|
|
|
|
@cindex int directive
|
|
|
|
@cindex long directive
|
|
|
|
@cindex quad directive
|
|
|
|
@cindex globl directive
|
|
|
|
@cindex global directive
|
|
|
|
@cindex section directive
|
|
|
|
@cindex text directive
|
|
|
|
@cindex data directive
|
|
|
|
@cindex bss directive
|
|
|
|
@cindex fill directive
|
|
|
|
@cindex org directive
|
|
|
|
@cindex previous directive
|
|
|
|
@cindex string directive
|
|
|
|
@cindex asciz directive
|
|
|
|
@cindex ascii directive
|
2003-01-06 23:19:20 +03:00
|
|
|
|
2013-02-20 17:23:44 +04:00
|
|
|
All directives are preceded by a '.'. The following directives are
|
2003-01-06 23:19:20 +03:00
|
|
|
supported:
|
|
|
|
|
|
|
|
@itemize
|
|
|
|
@item .align n[,value]
|
|
|
|
@item .skip n[,value]
|
|
|
|
@item .space n[,value]
|
2004-11-09 00:19:47 +03:00
|
|
|
@item .byte value1[,...]
|
|
|
|
@item .word value1[,...]
|
|
|
|
@item .short value1[,...]
|
|
|
|
@item .int value1[,...]
|
|
|
|
@item .long value1[,...]
|
|
|
|
@item .quad immediate_value1[,...]
|
2004-10-02 18:01:26 +04:00
|
|
|
@item .globl symbol
|
2003-05-24 20:12:58 +04:00
|
|
|
@item .global symbol
|
|
|
|
@item .section section
|
|
|
|
@item .text
|
|
|
|
@item .data
|
|
|
|
@item .bss
|
2004-10-24 03:11:05 +04:00
|
|
|
@item .fill repeat[,size[,value]]
|
|
|
|
@item .org n
|
|
|
|
@item .previous
|
|
|
|
@item .string string[,...]
|
|
|
|
@item .asciz string[,...]
|
|
|
|
@item .ascii string[,...]
|
2003-01-06 23:19:20 +03:00
|
|
|
@end itemize
|
|
|
|
|
|
|
|
@section X86 Assembler
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex assembler
|
2003-01-06 23:19:20 +03:00
|
|
|
|
|
|
|
All X86 opcodes are supported. Only ATT syntax is supported (source
|
|
|
|
then destination operand order). If no size suffix is given, TinyCC
|
|
|
|
tries to guess it from the operand sizes.
|
|
|
|
|
|
|
|
Currently, MMX opcodes are supported but not SSE ones.
|
|
|
|
|
2009-04-18 16:20:27 +04:00
|
|
|
@node linker
|
2002-07-25 02:13:02 +04:00
|
|
|
@chapter TinyCC Linker
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex linker
|
2002-07-25 02:13:02 +04:00
|
|
|
|
|
|
|
@section ELF file generation
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex ELF
|
2002-07-25 02:13:02 +04:00
|
|
|
|
|
|
|
TCC can directly output relocatable ELF files (object files),
|
|
|
|
executable ELF files and dynamic ELF libraries without relying on an
|
|
|
|
external linker.
|
|
|
|
|
2002-08-18 17:24:19 +04:00
|
|
|
Dynamic ELF libraries can be output but the C compiler does not generate
|
2003-10-04 17:04:47 +04:00
|
|
|
position independent code (PIC). It means that the dynamic library
|
2002-08-18 17:24:19 +04:00
|
|
|
code generated by TCC cannot be factorized among processes yet.
|
2002-07-25 02:13:02 +04:00
|
|
|
|
2003-05-24 20:12:58 +04:00
|
|
|
TCC linker eliminates unreferenced object code in libraries. A single pass is
|
|
|
|
done on the object and library list, so the order in which object files and
|
|
|
|
libraries are specified is important (same constraint as GNU ld). No grouping
|
|
|
|
options (@option{--start-group} and @option{--end-group}) are supported.
|
2002-07-25 02:13:02 +04:00
|
|
|
|
|
|
|
@section ELF file loader
|
|
|
|
|
|
|
|
TCC can load ELF object files, archives (.a files) and dynamic
|
|
|
|
libraries (.so).
|
|
|
|
|
2005-06-18 02:07:03 +04:00
|
|
|
@section PE-i386 file generation
|
|
|
|
@cindex PE-i386
|
|
|
|
|
2009-04-18 16:20:27 +04:00
|
|
|
TCC for Windows supports the native Win32 executable file format (PE-i386). It
|
|
|
|
generates EXE files (console and gui) and DLL files.
|
2008-03-31 23:50:58 +04:00
|
|
|
|
2009-04-18 16:20:27 +04:00
|
|
|
For usage on Windows, see also tcc-win32.txt.
|
2005-06-18 02:07:03 +04:00
|
|
|
|
2002-07-25 02:13:02 +04:00
|
|
|
@section GNU Linker Scripts
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex scripts, linker
|
|
|
|
@cindex linker scripts
|
|
|
|
@cindex GROUP, linker command
|
|
|
|
@cindex FILE, linker command
|
2003-05-24 20:12:58 +04:00
|
|
|
@cindex OUTPUT_FORMAT, linker command
|
|
|
|
@cindex TARGET, linker command
|
2002-07-25 02:13:02 +04:00
|
|
|
|
|
|
|
Because on many Linux systems some dynamic libraries (such as
|
|
|
|
@file{/usr/lib/libc.so}) are in fact GNU ld link scripts (horrible!),
|
2003-04-10 04:03:26 +04:00
|
|
|
the TCC linker also supports a subset of GNU ld scripts.
|
2002-07-25 02:13:02 +04:00
|
|
|
|
2003-05-24 20:12:58 +04:00
|
|
|
The @code{GROUP} and @code{FILE} commands are supported. @code{OUTPUT_FORMAT}
|
|
|
|
and @code{TARGET} are ignored.
|
2002-07-25 02:13:02 +04:00
|
|
|
|
|
|
|
Example from @file{/usr/lib/libc.so}:
|
|
|
|
@example
|
|
|
|
/* GNU ld script
|
|
|
|
Use the shared library, but some functions are only in
|
|
|
|
the static library, so try that secondarily. */
|
|
|
|
GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )
|
|
|
|
@end example
|
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@node Bounds
|
2002-07-13 19:21:18 +04:00
|
|
|
@chapter TinyCC Memory and Bound checks
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex bound checks
|
|
|
|
@cindex memory checks
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
This feature is activated with the @option{-b} (@pxref{Invoke}).
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
Note that pointer size is @emph{unchanged} and that code generated
|
|
|
|
with bound checks is @emph{fully compatible} with unchecked
|
2002-01-05 22:50:17 +03:00
|
|
|
code. When a pointer comes from unchecked code, it is assumed to be
|
|
|
|
valid. Even very obscure C code with casts should work correctly.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
For more information about the ideas behind this method, see
|
2002-07-13 19:21:18 +04:00
|
|
|
@url{http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html}.
|
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
Here are some examples of caught errors:
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@table @asis
|
|
|
|
|
|
|
|
@item Invalid range with standard string function:
|
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
@{
|
2002-01-05 22:50:17 +03:00
|
|
|
char tab[10];
|
|
|
|
memset(tab, 0, 11);
|
2003-04-10 04:03:26 +04:00
|
|
|
@}
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item Out of bounds-error in global or local arrays:
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
@{
|
2002-01-05 22:50:17 +03:00
|
|
|
int tab[10];
|
2003-04-10 04:03:26 +04:00
|
|
|
for(i=0;i<11;i++) @{
|
2002-01-05 22:50:17 +03:00
|
|
|
sum += tab[i];
|
2003-04-10 04:03:26 +04:00
|
|
|
@}
|
|
|
|
@}
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item Out of bounds-error in malloc'ed data:
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
@{
|
2002-01-05 22:50:17 +03:00
|
|
|
int *tab;
|
|
|
|
tab = malloc(20 * sizeof(int));
|
2003-04-10 04:03:26 +04:00
|
|
|
for(i=0;i<21;i++) @{
|
2002-01-05 22:50:17 +03:00
|
|
|
sum += tab4[i];
|
2003-04-10 04:03:26 +04:00
|
|
|
@}
|
2002-01-05 22:50:17 +03:00
|
|
|
free(tab);
|
2003-04-10 04:03:26 +04:00
|
|
|
@}
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item Access of freed memory:
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
@{
|
2002-01-05 22:50:17 +03:00
|
|
|
int *tab;
|
|
|
|
tab = malloc(20 * sizeof(int));
|
|
|
|
free(tab);
|
2003-04-10 04:03:26 +04:00
|
|
|
for(i=0;i<20;i++) @{
|
2002-01-05 22:50:17 +03:00
|
|
|
sum += tab4[i];
|
2003-04-10 04:03:26 +04:00
|
|
|
@}
|
|
|
|
@}
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@item Double free:
|
2002-07-13 19:21:18 +04:00
|
|
|
@example
|
2003-04-10 04:03:26 +04:00
|
|
|
@{
|
2002-01-05 22:50:17 +03:00
|
|
|
int *tab;
|
|
|
|
tab = malloc(20 * sizeof(int));
|
|
|
|
free(tab);
|
|
|
|
free(tab);
|
2003-04-10 04:03:26 +04:00
|
|
|
@}
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@end table
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@node Libtcc
|
2002-07-13 19:21:18 +04:00
|
|
|
@chapter The @code{libtcc} library
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
The @code{libtcc} library enables you to use TCC as a backend for
|
2015-07-29 23:53:57 +03:00
|
|
|
dynamic code generation.
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
Read the @file{libtcc.h} to have an overview of the API. Read
|
|
|
|
@file{libtcc_test.c} to have a very simple example.
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
The idea consists in giving a C string containing the program you want
|
2003-04-10 03:52:29 +04:00
|
|
|
to compile directly to @code{libtcc}. Then you can access to any global
|
|
|
|
symbol (function or variable) defined.
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2009-04-18 16:20:27 +04:00
|
|
|
@node devel
|
2003-04-10 04:03:26 +04:00
|
|
|
@chapter Developer's guide
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
This chapter gives some hints to understand how TCC works. You can skip
|
|
|
|
it if you do not intend to modify the TCC code.
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@section File reading
|
2002-01-05 22:50:17 +03:00
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
The @code{BufferedFile} structure contains the context needed to read a
|
|
|
|
file, including the current line number. @code{tcc_open()} opens a new
|
|
|
|
file and @code{tcc_close()} closes it. @code{inp()} returns the next
|
|
|
|
character.
|
|
|
|
|
|
|
|
@section Lexer
|
|
|
|
|
|
|
|
@code{next()} reads the next token in the current
|
|
|
|
file. @code{next_nomacro()} reads the next token without macro
|
|
|
|
expansion.
|
|
|
|
|
|
|
|
@code{tok} contains the current token (see @code{TOK_xxx})
|
|
|
|
constants. Identifiers and keywords are also keywords. @code{tokc}
|
2003-04-10 04:03:26 +04:00
|
|
|
contains additional infos about the token (for example a constant value
|
2002-07-13 19:21:18 +04:00
|
|
|
if number or string token).
|
|
|
|
|
|
|
|
@section Parser
|
|
|
|
|
|
|
|
The parser is hardcoded (yacc is not necessary). It does only one pass,
|
|
|
|
except:
|
|
|
|
|
|
|
|
@itemize
|
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item For initialized arrays with unknown size, a first pass
|
2002-07-13 19:21:18 +04:00
|
|
|
is done to count the number of elements.
|
|
|
|
|
2015-07-29 23:53:57 +03:00
|
|
|
@item For architectures where arguments are evaluated in
|
2002-07-13 19:21:18 +04:00
|
|
|
reverse order, a first pass is done to reverse the argument order.
|
|
|
|
|
|
|
|
@end itemize
|
|
|
|
|
|
|
|
@section Types
|
|
|
|
|
2013-02-20 17:23:44 +04:00
|
|
|
The types are stored in a single 'int' variable. It was chosen in the
|
2002-07-13 19:21:18 +04:00
|
|
|
first stages of development when tcc was much simpler. Now, it may not
|
|
|
|
be the best solution.
|
|
|
|
|
|
|
|
@example
|
|
|
|
#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) */
|
2011-04-06 20:17:03 +04:00
|
|
|
#define VT_VLA 0x20000 /* VLA type (also has VT_PTR and VT_ARRAY) */
|
2011-02-05 04:31:27 +03:00
|
|
|
#define VT_BITFIELD 0x0040 /* bitfield modifier */
|
2011-04-06 20:17:03 +04:00
|
|
|
#define VT_CONSTANT 0x0800 /* const modifier */
|
|
|
|
#define VT_VOLATILE 0x1000 /* volatile modifier */
|
2014-02-06 16:51:47 +04:00
|
|
|
#define VT_DEFSIGN 0x2000 /* signed type */
|
2002-07-13 19:21:18 +04:00
|
|
|
|
2011-04-06 20:17:03 +04:00
|
|
|
#define VT_STRUCT_SHIFT 18 /* structure/enum name shift (14 bits left) */
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
|
|
|
|
|
|
|
When a reference to another type is needed (for pointers, functions and
|
|
|
|
structures), the @code{32 - VT_STRUCT_SHIFT} high order bits are used to
|
|
|
|
store an identifier reference.
|
|
|
|
|
|
|
|
The @code{VT_UNSIGNED} flag can be set for chars, shorts, ints and long
|
|
|
|
longs.
|
|
|
|
|
|
|
|
Arrays are considered as pointers @code{VT_PTR} with the flag
|
2011-04-06 20:17:03 +04:00
|
|
|
@code{VT_ARRAY} set. Variable length arrays are considered as special
|
2011-04-10 10:41:16 +04:00
|
|
|
arrays and have flag @code{VT_VLA} set instead of @code{VT_ARRAY}.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
The @code{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.
|
|
|
|
|
|
|
|
@code{VT_LONG} is never used except during parsing.
|
|
|
|
|
|
|
|
During parsing, the storage of an object is also stored in the type
|
|
|
|
integer:
|
|
|
|
|
|
|
|
@example
|
2011-02-05 04:31:27 +03:00
|
|
|
#define VT_EXTERN 0x00000080 /* extern definition */
|
|
|
|
#define VT_STATIC 0x00000100 /* static variable */
|
|
|
|
#define VT_TYPEDEF 0x00000200 /* typedef definition */
|
2011-04-06 20:17:03 +04:00
|
|
|
#define VT_INLINE 0x00000400 /* inline definition */
|
|
|
|
#define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
|
|
|
|
#define VT_EXPORT 0x00008000 /* win32: data exported from dll */
|
|
|
|
#define VT_WEAK 0x00010000 /* win32: data exported from dll */
|
2002-07-13 19:21:18 +04:00
|
|
|
@end example
|
|
|
|
|
|
|
|
@section Symbols
|
|
|
|
|
|
|
|
All symbols are stored in hashed symbol stacks. Each symbol stack
|
|
|
|
contains @code{Sym} structures.
|
|
|
|
|
|
|
|
@code{Sym.v} contains the symbol name (remember
|
2017-09-25 04:22:42 +03:00
|
|
|
an identifier is also a token, so a string is never necessary to store
|
2002-07-13 19:21:18 +04:00
|
|
|
it). @code{Sym.t} gives the type of the symbol. @code{Sym.r} is usually
|
|
|
|
the register in which the corresponding variable is stored. @code{Sym.c} is
|
2011-04-06 20:17:03 +04:00
|
|
|
usually a constant associated to the symbol like its address for normal
|
|
|
|
symbols, and the number of entries for symbols representing arrays.
|
2011-04-10 10:41:16 +04:00
|
|
|
Variable length array types use @code{Sym.c} as a location on the stack
|
|
|
|
which holds the runtime sizeof for the type.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
Four main symbol stacks are defined:
|
|
|
|
|
|
|
|
@table @code
|
|
|
|
|
|
|
|
@item define_stack
|
|
|
|
for the macros (@code{#define}s).
|
|
|
|
|
|
|
|
@item global_stack
|
|
|
|
for the global variables, functions and types.
|
|
|
|
|
|
|
|
@item local_stack
|
|
|
|
for the local variables, functions and types.
|
|
|
|
|
2003-04-10 03:52:29 +04:00
|
|
|
@item global_label_stack
|
2002-07-13 19:21:18 +04:00
|
|
|
for the local labels (for @code{goto}).
|
|
|
|
|
2003-04-10 03:52:29 +04:00
|
|
|
@item label_stack
|
|
|
|
for GCC block local labels (see the @code{__label__} keyword).
|
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@end table
|
|
|
|
|
|
|
|
@code{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.
|
|
|
|
|
|
|
|
@code{sym_pop(st,b)} pops symbols from the symbol stack @var{st} until
|
|
|
|
the symbol @var{b} is on the top of stack. If @var{b} is NULL, the stack
|
|
|
|
is emptied.
|
|
|
|
|
|
|
|
@code{sym_find(v)} return the symbol associated to the identifier
|
|
|
|
@var{v}. The local stack is searched first from top to bottom, then the
|
|
|
|
global stack.
|
|
|
|
|
|
|
|
@section Sections
|
|
|
|
|
2017-09-25 04:22:42 +03:00
|
|
|
The generated code and data are written in sections. The structure
|
2002-07-13 19:21:18 +04:00
|
|
|
@code{Section} contains all the necessary information for a given
|
|
|
|
section. @code{new_section()} creates a new section. ELF file semantics
|
|
|
|
is assumed for each section.
|
|
|
|
|
|
|
|
The following sections are predefined:
|
|
|
|
|
|
|
|
@table @code
|
|
|
|
|
|
|
|
@item text_section
|
|
|
|
is the section containing the generated code. @var{ind} contains the
|
|
|
|
current position in the code section.
|
|
|
|
|
|
|
|
@item data_section
|
|
|
|
contains initialized data
|
|
|
|
|
|
|
|
@item bss_section
|
|
|
|
contains uninitialized data
|
|
|
|
|
|
|
|
@item bounds_section
|
|
|
|
@itemx lbounds_section
|
|
|
|
are used when bound checking is activated
|
|
|
|
|
|
|
|
@item stab_section
|
|
|
|
@itemx stabstr_section
|
2014-04-07 15:20:49 +04:00
|
|
|
are used when debugging is active to store debug information
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@item symtab_section
|
|
|
|
@itemx strtab_section
|
|
|
|
contain the exported symbols (currently only used for debugging).
|
|
|
|
|
|
|
|
@end table
|
|
|
|
|
|
|
|
@section Code generation
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex code generation
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@subsection Introduction
|
|
|
|
|
|
|
|
The TCC code generator directly generates linked binary code in one
|
|
|
|
pass. It is rather unusual these days (see gcc for example which
|
2003-04-10 04:03:26 +04:00
|
|
|
generates text assembly), but it can be very fast and surprisingly
|
|
|
|
little complicated.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
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 @emph{value stack}.
|
|
|
|
|
|
|
|
On x86, three temporary registers are used. When more registers are
|
2003-04-10 04:03:26 +04:00
|
|
|
needed, one register is spilled into a new temporary variable on the stack.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@subsection The value stack
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex value stack, introduction
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
When an expression is parsed, its value is pushed on the value stack
|
|
|
|
(@var{vstack}). The top of the value stack is @var{vtop}. Each value
|
|
|
|
stack entry is the structure @code{SValue}.
|
|
|
|
|
|
|
|
@code{SValue.t} is the type. @code{SValue.r} indicates how the value is
|
|
|
|
currently stored in the generated code. It is usually a CPU register
|
2003-04-10 04:03:26 +04:00
|
|
|
index (@code{REG_xxx} constants), but additional values and flags are
|
2002-07-13 19:21:18 +04:00
|
|
|
defined:
|
|
|
|
|
|
|
|
@example
|
2002-09-09 02:13:54 +04:00
|
|
|
#define VT_CONST 0x00f0
|
|
|
|
#define VT_LLOCAL 0x00f1
|
|
|
|
#define VT_LOCAL 0x00f2
|
|
|
|
#define VT_CMP 0x00f3
|
|
|
|
#define VT_JMP 0x00f4
|
|
|
|
#define VT_JMPI 0x00f5
|
|
|
|
#define VT_LVAL 0x0100
|
|
|
|
#define VT_SYM 0x0200
|
|
|
|
#define VT_MUSTCAST 0x0400
|
|
|
|
#define VT_MUSTBOUND 0x0800
|
|
|
|
#define VT_BOUNDED 0x8000
|
|
|
|
#define VT_LVAL_BYTE 0x1000
|
|
|
|
#define VT_LVAL_SHORT 0x2000
|
|
|
|
#define VT_LVAL_UNSIGNED 0x4000
|
2002-07-13 19:21:18 +04:00
|
|
|
#define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@table @code
|
|
|
|
|
|
|
|
@item VT_CONST
|
|
|
|
indicates that the value is a constant. It is stored in the union
|
|
|
|
@code{SValue.c}, depending on its type.
|
|
|
|
|
|
|
|
@item VT_LOCAL
|
|
|
|
indicates a local variable pointer at offset @code{SValue.c.i} in the
|
|
|
|
stack.
|
|
|
|
|
|
|
|
@item 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
|
2015-07-29 23:53:57 +03:00
|
|
|
actual CPU flags used is indicated in @code{SValue.c.i}.
|
2002-09-09 02:13:54 +04:00
|
|
|
|
|
|
|
If any code is generated which destroys the CPU flags, this value MUST be
|
|
|
|
put in a normal register.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@item VT_JMP
|
|
|
|
@itemx VT_JMPI
|
2003-04-10 04:03:26 +04:00
|
|
|
indicates that the value is the consequence of a conditional jump. For VT_JMP,
|
|
|
|
it is 1 if the jump is taken, 0 otherwise. For VT_JMPI it is inverted.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
These values are used to compile the @code{||} and @code{&&} logical
|
|
|
|
operators.
|
|
|
|
|
2002-09-09 02:13:54 +04:00
|
|
|
If any code is generated, this value MUST be put in a normal
|
|
|
|
register. Otherwise, the generated code won't be executed if the jump is
|
|
|
|
taken.
|
|
|
|
|
2002-07-13 19:21:18 +04:00
|
|
|
@item 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
|
2015-07-29 23:53:57 +03:00
|
|
|
the wanted value.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
Understanding the use @code{VT_LVAL} is very important if you want to
|
|
|
|
understand how TCC works.
|
|
|
|
|
|
|
|
@item VT_LVAL_BYTE
|
|
|
|
@itemx VT_LVAL_SHORT
|
|
|
|
@itemx VT_LVAL_UNSIGNED
|
|
|
|
if the lvalue has an integer type, then these flags give its real
|
2003-04-10 04:03:26 +04:00
|
|
|
type. The type alone is not enough in case of cast optimisations.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@item VT_LLOCAL
|
2015-02-21 02:01:01 +03:00
|
|
|
is a saved lvalue on the stack. @code{VT_LVAL} must also be set with
|
|
|
|
@code{VT_LLOCAL}. @code{VT_LLOCAL} can arise when a @code{VT_LVAL} in
|
|
|
|
a register has to be saved to the stack, or it can come from an
|
|
|
|
architecture-specific calling convention.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@item VT_MUSTCAST
|
|
|
|
indicates that a cast to the value type must be performed if the value
|
|
|
|
is used (lazy casting).
|
|
|
|
|
2002-09-09 02:13:54 +04:00
|
|
|
@item VT_SYM
|
|
|
|
indicates that the symbol @code{SValue.sym} must be added to the constant.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@item VT_MUSTBOUND
|
|
|
|
@itemx VT_BOUNDED
|
|
|
|
are only used for optional bound checking.
|
|
|
|
|
|
|
|
@end table
|
|
|
|
|
|
|
|
@subsection Manipulating the value stack
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex value stack
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@code{vsetc()} and @code{vset()} pushes a new value on the value
|
2003-04-10 04:03:26 +04:00
|
|
|
stack. If the previous @var{vtop} was stored in a very unsafe place(for
|
2002-07-13 19:21:18 +04:00
|
|
|
example in the CPU flags), then some code is generated to put the
|
2003-04-10 04:03:26 +04:00
|
|
|
previous @var{vtop} in a safe storage.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@code{vpop()} pops @var{vtop}. In some cases, it also generates cleanup
|
2002-07-13 19:21:18 +04:00
|
|
|
code (for example if stacked floating point registers are used as on
|
|
|
|
x86).
|
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
The @code{gv(rc)} function generates code to evaluate @var{vtop} (the
|
2002-07-13 19:21:18 +04:00
|
|
|
top value of the stack) into registers. @var{rc} selects in which
|
|
|
|
register class the value should be put. @code{gv()} is the @emph{most
|
|
|
|
important function} of the code generator.
|
|
|
|
|
|
|
|
@code{gv2()} is the same as @code{gv()} but for the top two stack
|
|
|
|
entries.
|
|
|
|
|
|
|
|
@subsection CPU dependent code generation
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex CPU dependent
|
2002-07-13 19:21:18 +04:00
|
|
|
See the @file{i386-gen.c} file to have an example.
|
|
|
|
|
|
|
|
@table @code
|
|
|
|
|
|
|
|
@item load()
|
|
|
|
must generate the code needed to load a stack value into a register.
|
|
|
|
|
|
|
|
@item store()
|
|
|
|
must generate the code needed to store a register into a stack value
|
|
|
|
lvalue.
|
|
|
|
|
|
|
|
@item gfunc_start()
|
|
|
|
@itemx gfunc_param()
|
|
|
|
@itemx gfunc_call()
|
|
|
|
should generate a function call
|
|
|
|
|
|
|
|
@item gfunc_prolog()
|
|
|
|
@itemx gfunc_epilog()
|
|
|
|
should generate a function prolog/epilog.
|
|
|
|
|
|
|
|
@item gen_opi(op)
|
|
|
|
must generate the binary integer operation @var{op} on the two top
|
2017-09-25 04:22:42 +03:00
|
|
|
entries of the stack which are guaranteed to contain integer types.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
The result value should be put on the stack.
|
|
|
|
|
|
|
|
@item gen_opf(op)
|
|
|
|
same as @code{gen_opi()} for floating point operations. The two top
|
2017-09-25 04:22:42 +03:00
|
|
|
entries of the stack are guaranteed to contain floating point values of
|
2002-07-13 19:21:18 +04:00
|
|
|
same types.
|
|
|
|
|
|
|
|
@item gen_cvt_itof()
|
|
|
|
integer to floating point conversion.
|
|
|
|
|
|
|
|
@item gen_cvt_ftoi()
|
|
|
|
floating point to integer conversion.
|
|
|
|
|
|
|
|
@item gen_cvt_ftof()
|
|
|
|
floating point to floating point of different size conversion.
|
|
|
|
|
|
|
|
@item gen_bounded_ptr_add()
|
|
|
|
@item gen_bounded_ptr_deref()
|
2003-04-10 04:03:26 +04:00
|
|
|
are only used for bounds checking.
|
2002-07-13 19:21:18 +04:00
|
|
|
|
|
|
|
@end table
|
|
|
|
|
|
|
|
@section Optimizations done
|
2003-04-10 04:03:26 +04:00
|
|
|
@cindex optimizations
|
|
|
|
@cindex constant propagation
|
|
|
|
@cindex strength reduction
|
|
|
|
@cindex comparison operators
|
|
|
|
@cindex caching processor flags
|
|
|
|
@cindex flags, caching
|
|
|
|
@cindex jump optimization
|
2002-07-13 19:21:18 +04:00
|
|
|
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.
|
2001-12-17 00:23:42 +03:00
|
|
|
|
2003-04-10 04:03:26 +04:00
|
|
|
@unnumbered Concept Index
|
|
|
|
@printindex cp
|
|
|
|
|
|
|
|
@bye
|
|
|
|
|
|
|
|
@c Local variables:
|
|
|
|
@c fill-column: 78
|
|
|
|
@c texinfo-column-for-description: 32
|
|
|
|
@c End:
|