fix-mixed-struct (patch by Pip Cet)
[tinycc.git] / tcc-doc.texi
Commit [+]AuthorDateLineData
30766d3c bellard2002-07-13 15:21:18 +00001\input texinfo @c -*- texinfo -*-
3605c50b bellard2003-04-10 00:03:26 +00002@c %**start of header
3@setfilename tcc-doc.info
30766d3c bellard2002-07-13 15:21:18 +00004@settitle Tiny C Compiler Reference Documentation
36f74e46
TP
Thomas Preud'homme2010-09-10 21:11:45 +02005@dircategory Software development
6@direntry
7* TCC: (tcc-doc). The Tiny C Compiler.
8@end direntry
3605c50b bellard2003-04-10 00:03:26 +00009@c %**end of header
10
3bbcd307 bellard2003-04-13 21:56:00 +000011@include config.texi
3605c50b bellard2003-04-10 00:03:26 +000012
13@iftex
30766d3c bellard2002-07-13 15:21:18 +000014@titlepage
3605c50b bellard2003-04-10 00:03:26 +000015@afourpaper
30766d3c bellard2002-07-13 15:21:18 +000016@sp 7
17@center @titlefont{Tiny C Compiler Reference Documentation}
18@sp 3
19@end titlepage
3605c50b bellard2003-04-10 00:03:26 +000020@headings double
21@end iftex
22
3605c50b bellard2003-04-10 00:03:26 +000023@contents
3605c50b bellard2003-04-10 00:03:26 +000024
3605c50b bellard2003-04-10 00:03:26 +000025@node Top, Introduction, (dir), (dir)
26@top Tiny C Compiler Reference Documentation
27
28This manual documents version @value{VERSION} of the Tiny C Compiler.
30766d3c bellard2002-07-13 15:21:18 +000029
3605c50b bellard2003-04-10 00:03:26 +000030@menu
31* Introduction:: Introduction to tcc.
32* Invoke:: Invocation of tcc (command line, options).
e8a52a82 grischka2009-04-18 14:20:27 +020033* Clang:: ANSI C and extensions.
34* asm:: Assembler syntax.
35* linker:: Output file generation and supported targets.
3605c50b bellard2003-04-10 00:03:26 +000036* Bounds:: Automatic bounds-checking of C code.
3bbcd307 bellard2003-04-13 21:56:00 +000037* Libtcc:: The libtcc library.
e8a52a82 grischka2009-04-18 14:20:27 +020038* devel:: Guide for Developers.
3605c50b bellard2003-04-10 00:03:26 +000039@end menu
e8a52a82 grischka2009-04-18 14:20:27 +020040
3605c50b bellard2003-04-10 00:03:26 +000041
42@node Introduction
30766d3c bellard2002-07-13 15:21:18 +000043@chapter Introduction
5ee15844 bellard2001-12-16 21:23:42 +000044
ebe9e87c bellard2002-01-05 19:50:17 +000045TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C
3605c50b bellard2003-04-10 00:03:26 +000046compilers, it is meant to be self-relying: you do not need an
5ee15844 bellard2001-12-16 21:23:42 +000047external assembler or linker because TCC does that for you.
30766d3c bellard2002-07-13 15:21:18 +000048
49TCC compiles so @emph{fast} that even for big projects @code{Makefile}s may
e8a52a82 grischka2009-04-18 14:20:27 +020050not be necessary.
30766d3c bellard2002-07-13 15:21:18 +000051
ebe9e87c bellard2002-01-05 19:50:17 +000052TCC not only supports ANSI C, but also most of the new ISO C99
75e743d2 bellard2003-01-06 20:19:20 +000053standard and many GNUC extensions including inline assembly.
30766d3c bellard2002-07-13 15:21:18 +000054
55TCC can also be used to make @emph{C scripts}, i.e. pieces of C source
56that you run as a Perl or Python script. Compilation is so fast that
57your script will be as fast as if it was an executable.
58
59TCC can also automatically generate memory and bound checks
3605c50b bellard2003-04-10 00:03:26 +000060(@pxref{Bounds}) while allowing all C pointers operations. TCC can do
30766d3c bellard2002-07-13 15:21:18 +000061these checks even if non patched libraries are used.
62
63With @code{libtcc}, you can use TCC as a backend for dynamic code
3605c50b bellard2003-04-10 00:03:26 +000064generation (@pxref{Libtcc}).
30766d3c bellard2002-07-13 15:21:18 +000065
4821702f bellard2005-04-14 23:49:21 +000066TCC mainly supports the i386 target on Linux and Windows. There are alpha
67ports for the ARM (@code{arm-tcc}) and the TMS320C67xx targets
68(@code{c67-tcc}). More information about the ARM port is available at
0b8d7f44 bellard2004-11-08 20:52:04 +000069@url{http://lists.gnu.org/archive/html/tinycc-devel/2003-10/msg00044.html}.
2a6c1047 bellard2004-10-23 23:11:05 +000070
d6d7686b grischka2013-02-08 19:07:11 +010071For usage on Windows, see also @url{tcc-win32.txt}.
e8a52a82 grischka2009-04-18 14:20:27 +020072
3605c50b bellard2003-04-10 00:03:26 +000073@node Invoke
30766d3c bellard2002-07-13 15:21:18 +000074@chapter Command line invocation
75
f75e0c6d bellard2002-07-15 22:38:56 +000076@section Quick start
77
78@example
f0d5a74b bellard2003-05-18 18:11:06 +000079@c man begin SYNOPSIS
4e6edcdc bellard2003-05-24 16:12:58 +000080usage: tcc [options] [@var{infile1} @var{infile2}@dots{}] [@option{-run} @var{infile} @var{args}@dots{}]
f0d5a74b bellard2003-05-18 18:11:06 +000081@c man end
f75e0c6d bellard2002-07-15 22:38:56 +000082@end example
83
3605c50b bellard2003-04-10 00:03:26 +000084@noindent
f0d5a74b bellard2003-05-18 18:11:06 +000085@c man begin DESCRIPTION
3605c50b bellard2003-04-10 00:03:26 +000086TCC options are a very much like gcc options. The main difference is that TCC
f75e0c6d bellard2002-07-15 22:38:56 +000087can also execute directly the resulting program and give it runtime
88arguments.
89
90Here are some examples to understand the logic:
91
92@table @code
3bbcd307 bellard2003-04-13 21:56:00 +000093@item @samp{tcc -run a.c}
3605c50b bellard2003-04-10 00:03:26 +000094Compile @file{a.c} and execute it directly
f75e0c6d bellard2002-07-15 22:38:56 +000095
3bbcd307 bellard2003-04-13 21:56:00 +000096@item @samp{tcc -run a.c arg1}
f75e0c6d bellard2002-07-15 22:38:56 +000097Compile a.c and execute it directly. arg1 is given as first argument to
98the @code{main()} of a.c.
99
3bbcd307 bellard2003-04-13 21:56:00 +0000100@item @samp{tcc a.c -run b.c arg1}
3605c50b bellard2003-04-10 00:03:26 +0000101Compile @file{a.c} and @file{b.c}, link them together and execute them. arg1 is given
4d9aaacc grischka2008-03-31 18:42:56 +0000102as first argument to the @code{main()} of the resulting program.
103@ignore
104Because multiple C files are specified, @option{--} are necessary to clearly
105separate the program arguments from the TCC options.
106@end ignore
f75e0c6d bellard2002-07-15 22:38:56 +0000107
3605c50b bellard2003-04-10 00:03:26 +0000108@item @samp{tcc -o myprog a.c b.c}
109Compile @file{a.c} and @file{b.c}, link them and generate the executable @file{myprog}.
f75e0c6d bellard2002-07-15 22:38:56 +0000110
3605c50b bellard2003-04-10 00:03:26 +0000111@item @samp{tcc -o myprog a.o b.o}
112link @file{a.o} and @file{b.o} together and generate the executable @file{myprog}.
f75e0c6d bellard2002-07-15 22:38:56 +0000113
3605c50b bellard2003-04-10 00:03:26 +0000114@item @samp{tcc -c a.c}
115Compile @file{a.c} and generate object file @file{a.o}.
f75e0c6d bellard2002-07-15 22:38:56 +0000116
3605c50b bellard2003-04-10 00:03:26 +0000117@item @samp{tcc -c asmfile.S}
118Preprocess with C preprocess and assemble @file{asmfile.S} and generate
119object file @file{asmfile.o}.
75e743d2 bellard2003-01-06 20:19:20 +0000120
3605c50b bellard2003-04-10 00:03:26 +0000121@item @samp{tcc -c asmfile.s}
122Assemble (but not preprocess) @file{asmfile.s} and generate object file
123@file{asmfile.o}.
75e743d2 bellard2003-01-06 20:19:20 +0000124
3605c50b bellard2003-04-10 00:03:26 +0000125@item @samp{tcc -r -o ab.o a.c b.c}
126Compile @file{a.c} and @file{b.c}, link them together and generate the object file @file{ab.o}.
bc427f5b bellard2002-08-31 12:44:16 +0000127
f75e0c6d bellard2002-07-15 22:38:56 +0000128@end table
129
130Scripting:
131
132TCC can be invoked from @emph{scripts}, just as shell scripts. You just
3bbcd307 bellard2003-04-13 21:56:00 +0000133need to add @code{#!/usr/local/bin/tcc -run} at the start of your C source:
f75e0c6d bellard2002-07-15 22:38:56 +0000134
30766d3c bellard2002-07-13 15:21:18 +0000135@example
f0d5a74b bellard2003-05-18 18:11:06 +0000136#!/usr/local/bin/tcc -run
f75e0c6d bellard2002-07-15 22:38:56 +0000137#include <stdio.h>
138
139int main()
3605c50b bellard2003-04-10 00:03:26 +0000140@{
f75e0c6d bellard2002-07-15 22:38:56 +0000141 printf("Hello World\n");
142 return 0;
3605c50b bellard2003-04-10 00:03:26 +0000143@}
30766d3c bellard2002-07-13 15:21:18 +0000144@end example
4d9aaacc grischka2008-03-31 18:42:56 +0000145
146TCC can read C source code from @emph{standard input} when @option{-} is used in
147place of @option{infile}. Example:
148
149@example
e8a52a82 grischka2009-04-18 14:20:27 +0200150echo 'main()@{puts("hello");@}' | tcc -run -
4d9aaacc grischka2008-03-31 18:42:56 +0000151@end example
f0d5a74b bellard2003-05-18 18:11:06 +0000152@c man end
30766d3c bellard2002-07-13 15:21:18 +0000153
f75e0c6d bellard2002-07-15 22:38:56 +0000154@section Option summary
155
156General Options:
157
f0d5a74b bellard2003-05-18 18:11:06 +0000158@c man begin OPTIONS
3605c50b bellard2003-04-10 00:03:26 +0000159@table @option
f75e0c6d bellard2002-07-15 22:38:56 +0000160@item -c
c449ef2e grischka2011-05-17 21:55:05 +0200161Generate an object file.
f75e0c6d bellard2002-07-15 22:38:56 +0000162
163@item -o outfile
164Put object file, executable, or dll into output file @file{outfile}.
165
b7f12dfb bellard2003-10-04 21:29:04 +0000166@item -run source [args...]
b7f12dfb bellard2003-10-04 21:29:04 +0000167Compile file @var{source} and run it with the command line arguments
168@var{args}. In order to be able to give more than one argument to a
169script, several TCC options can be given @emph{after} the
82965594 grischka2013-02-10 20:39:05 +0100170@option{-run} option, separated by spaces:
b7f12dfb bellard2003-10-04 21:29:04 +0000171@example
172tcc "-run -L/usr/X11R6/lib -lX11" ex4.c
173@end example
b7f12dfb bellard2003-10-04 21:29:04 +0000174In a script, it gives the following header:
b7f12dfb bellard2003-10-04 21:29:04 +0000175@example
176#!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11
b7f12dfb bellard2003-10-04 21:29:04 +0000177@end example
178
b6247d1f
TP
Thomas Preud'homme2014-01-07 15:23:54 +0800179@item -mfloat-abi (ARM only)
180Select the float ABI. Possible values: @code{softfp} and @code{hard}
181
cec76c8b
UJ
Urs Janssen2013-02-15 12:48:33 +0100182@item -dumpversion
183Print only the compiler version and nothing else.
184
82965594 grischka2013-02-10 20:39:05 +0100185@item -v
186Display TCC version.
187
188@item -vv
189Show included files. As sole argument, print search dirs (as below).
190
191@item -bench
192Display compilation statistics.
193
194@item -print-search-dirs
195Print the configured installation directory and a list of library
196and include directories tcc will search.
197
f75e0c6d bellard2002-07-15 22:38:56 +0000198@end table
199
200Preprocessor options:
201
3605c50b bellard2003-04-10 00:03:26 +0000202@table @option
30766d3c bellard2002-07-13 15:21:18 +0000203@item -Idir
3605c50b bellard2003-04-10 00:03:26 +0000204Specify an additional include path. Include paths are searched in the
79686686 bellard2002-08-18 13:24:19 +0000205order they are specified.
206
207System include paths are always searched after. The default system
208include paths are: @file{/usr/local/include}, @file{/usr/include}
3605c50b bellard2003-04-10 00:03:26 +0000209and @file{PREFIX/lib/tcc/include}. (@file{PREFIX} is usually
79686686 bellard2002-08-18 13:24:19 +0000210@file{/usr} or @file{/usr/local}).
30766d3c bellard2002-07-13 15:21:18 +0000211
212@item -Dsym[=val]
3605c50b bellard2003-04-10 00:03:26 +0000213Define preprocessor symbol @samp{sym} to
214val. If val is not present, its value is @samp{1}. Function-like macros can
215also be defined: @option{-DF(a)=a+1}
30766d3c bellard2002-07-13 15:21:18 +0000216
217@item -Usym
3605c50b bellard2003-04-10 00:03:26 +0000218Undefine preprocessor symbol @samp{sym}.
f75e0c6d bellard2002-07-15 22:38:56 +0000219@end table
30766d3c bellard2002-07-13 15:21:18 +0000220
114883e0 bellard2003-10-17 20:43:47 +0000221Compilation flags:
222
58f3b778 Thomas Preud'homme2014-01-07 17:50:08 +0800223Note: each of the following options has a negative form beginning with
114883e0 bellard2003-10-17 20:43:47 +0000224@option{-fno-}.
225
226@table @option
227@item -funsigned-char
228Let the @code{char} type be unsigned.
229
230@item -fsigned-char
231Let the @code{char} type be signed.
232
2a6c1047 bellard2004-10-23 23:11:05 +0000233@item -fno-common
234Do not generate common symbols for uninitialized data.
235
16559cd6 bellard2005-06-17 22:07:03 +0000236@item -fleading-underscore
237Add a leading underscore at the beginning of each C symbol.
238
dec95935 seyko2015-04-10 06:31:58 +0300239@item -fms-extensions
240Allow a MS C compiler extensions to the language. Curretly this
241assume a nested named structure declaration without identifier behave
242like an unnamed one.
243
dcb36587 seyko2015-04-12 15:32:03 +0300244@item -fdollars-in-identifiers
245Allow a dollars in identifiers
246
114883e0 bellard2003-10-17 20:43:47 +0000247@end table
248
4e6edcdc bellard2003-05-24 16:12:58 +0000249Warning options:
250
c5ce5eae bellard2003-10-04 13:04:47 +0000251@table @option
252@item -w
253Disable all warnings.
254
255@end table
256
257Note: each of the following warning options has a negative form beginning with
258@option{-Wno-}.
4e6edcdc bellard2003-05-24 16:12:58 +0000259
260@table @option
114883e0 bellard2003-10-17 20:43:47 +0000261@item -Wimplicit-function-declaration
262Warn about implicit function declaration.
263
4e6edcdc bellard2003-05-24 16:12:58 +0000264@item -Wunsupported
265Warn about unsupported GCC features that are ignored by TCC.
266
267@item -Wwrite-strings
114883e0 bellard2003-10-17 20:43:47 +0000268Make string constants be of type @code{const char *} instead of @code{char
4e6edcdc bellard2003-05-24 16:12:58 +0000269*}.
270
271@item -Werror
272Abort compilation if warnings are issued.
273
274@item -Wall
275Activate all warnings, except @option{-Werror}, @option{-Wunusupported} and
114883e0 bellard2003-10-17 20:43:47 +0000276@option{-Wwrite-strings}.
4e6edcdc bellard2003-05-24 16:12:58 +0000277
278@end table
279
f75e0c6d bellard2002-07-15 22:38:56 +0000280Linker options:
30766d3c bellard2002-07-13 15:21:18 +0000281
3605c50b bellard2003-04-10 00:03:26 +0000282@table @option
840d197e bellard2002-07-24 22:13:02 +0000283@item -Ldir
3605c50b bellard2003-04-10 00:03:26 +0000284Specify an additional static library path for the @option{-l} option. The
840d197e bellard2002-07-24 22:13:02 +0000285default library paths are @file{/usr/local/lib}, @file{/usr/lib} and @file{/lib}.
286
f75e0c6d bellard2002-07-15 22:38:56 +0000287@item -lxxx
840d197e bellard2002-07-24 22:13:02 +0000288Link your program with dynamic library libxxx.so or static library
289libxxx.a. The library is searched in the paths specified by the
243c6990 Urs Janssen2013-02-20 14:23:44 +0100290@option{-L} option and @env{LIBRARY_PATH} variable.
f75e0c6d bellard2002-07-15 22:38:56 +0000291
82965594 grischka2013-02-10 20:39:05 +0100292@item -Bdir
293Set the path where the tcc internal libraries (and include files) can be
294found (default is @file{PREFIX/lib/tcc}).
295
f75e0c6d bellard2002-07-15 22:38:56 +0000296@item -shared
c449ef2e grischka2011-05-17 21:55:05 +0200297Generate a shared library instead of an executable.
30766d3c bellard2002-07-13 15:21:18 +0000298
e81569bc grischka2009-12-19 22:08:15 +0100299@item -soname name
300set name for shared library to be used at runtime
301
f75e0c6d bellard2002-07-15 22:38:56 +0000302@item -static
303Generate a statically linked executable (default is a shared linked
c449ef2e grischka2011-05-17 21:55:05 +0200304executable).
f75e0c6d bellard2002-07-15 22:38:56 +0000305
4e6edcdc bellard2003-05-24 16:12:58 +0000306@item -rdynamic
307Export global symbols to the dynamic linker. It is useful when a library
308opened with @code{dlopen()} needs to access executable symbols.
309
bc427f5b bellard2002-08-31 12:44:16 +0000310@item -r
c449ef2e grischka2011-05-17 21:55:05 +0200311Generate an object file combining all input files.
bc427f5b bellard2002-08-31 12:44:16 +0000312
82965594 grischka2013-02-10 20:39:05 +0100313@item -Wl,-rpath=path
314Put custom seatch path for dynamic libraries into executable.
2a6c1047 bellard2004-10-23 23:11:05 +0000315
82965594 grischka2013-02-10 20:39:05 +0100316@item -Wl,--oformat=fmt
2a6c1047 bellard2004-10-23 23:11:05 +0000317Use @var{fmt} as output format. The supported output formats are:
318@table @code
319@item elf32-i386
320ELF output format (default)
321@item binary
322Binary image (only for executable output)
323@item coff
324COFF output format (only for executable output for TMS320C67xx target)
325@end table
326
82965594 grischka2013-02-10 20:39:05 +0100327@item -Wl,-subsystem=console/gui/wince/...
328Set type for PE (Windows) executables.
329
330@item -Wl,-[Ttext=# | section-alignment=# | file-alignment=# | image-base=# | stack=#]
331Modify executable layout.
332
333@item -Wl,-Bsymbolic
334Set DT_SYMBOLIC tag.
e81569bc grischka2009-12-19 22:08:15 +0100335
f75e0c6d bellard2002-07-15 22:38:56 +0000336@end table
30766d3c bellard2002-07-13 15:21:18 +0000337
6cd2d5d2 bellard2002-11-03 00:44:38 +0000338Debugger options:
339
3605c50b bellard2003-04-10 00:03:26 +0000340@table @option
6cd2d5d2 bellard2002-11-03 00:44:38 +0000341@item -g
342Generate run time debug information so that you get clear run time
343error messages: @code{ test.c:68: in function 'test5()': dereferencing
344invalid pointer} instead of the laconic @code{Segmentation
345fault}.
346
347@item -b
3605c50b bellard2003-04-10 00:03:26 +0000348Generate additional support code to check
349memory allocations and array/pointer bounds. @option{-g} is implied. Note
6cd2d5d2 bellard2002-11-03 00:44:38 +0000350that the generated code is slower and bigger in this case.
351
a01d83d7 Thomas Preud'homme2014-01-06 11:09:06 +0800352Note: @option{-b} is only available on i386 when using libtcc for the moment.
3d25213c Thomas Preud'homme2012-03-13 19:43:43 +0100353
6cd2d5d2 bellard2002-11-03 00:44:38 +0000354@item -bt N
3605c50b bellard2003-04-10 00:03:26 +0000355Display N callers in stack traces. This is useful with @option{-g} or
356@option{-b}.
6cd2d5d2 bellard2002-11-03 00:44:38 +0000357
358@end table
359
82965594 grischka2013-02-10 20:39:05 +0100360Misc options:
361
362@table @option
363@item -MD
364Generate makefile fragment with dependencies.
365
366@item -MF depfile
367Use @file{depfile} as output for -MD.
368
369@end table
370
4e6edcdc bellard2003-05-24 16:12:58 +0000371Note: GCC options @option{-Ox}, @option{-fx} and @option{-mx} are
6cd2d5d2 bellard2002-11-03 00:44:38 +0000372ignored.
f0d5a74b bellard2003-05-18 18:11:06 +0000373@c man end
374
243c6990
UJ
Urs Janssen2013-02-20 14:23:44 +0100375@c man begin ENVIRONMENT
376Environment variables that affect how tcc operates.
377
378@table @option
379
380@item CPATH
381@item C_INCLUDE_PATH
382A colon-separated list of directories searched for include files,
383directories given with @option{-I} are searched first.
384
385@item LIBRARY_PATH
386A colon-separated list of directories searched for libraries for the
387@option{-l} option, directories given with @option{-L} are searched first.
388
389@end table
390
391@c man end
392
f0d5a74b bellard2003-05-18 18:11:06 +0000393@ignore
394
395@setfilename tcc
396@settitle Tiny C Compiler
397
398@c man begin SEEALSO
243c6990 Urs Janssen2013-02-20 14:23:44 +0100399cpp(1),
f0d5a74b bellard2003-05-18 18:11:06 +0000400gcc(1)
401@c man end
402
403@c man begin AUTHOR
404Fabrice Bellard
405@c man end
406
407@end ignore
6cd2d5d2 bellard2002-11-03 00:44:38 +0000408
e8a52a82 grischka2009-04-18 14:20:27 +0200409@node Clang
30766d3c bellard2002-07-13 15:21:18 +0000410@chapter C language support
411
412@section ANSI C
5ee15844 bellard2001-12-16 21:23:42 +0000413
ebe9e87c bellard2002-01-05 19:50:17 +0000414TCC implements all the ANSI C standard, including structure bit fields
30766d3c bellard2002-07-13 15:21:18 +0000415and floating point numbers (@code{long double}, @code{double}, and
6cd2d5d2 bellard2002-11-03 00:44:38 +0000416@code{float} fully supported).
5ee15844 bellard2001-12-16 21:23:42 +0000417
30766d3c bellard2002-07-13 15:21:18 +0000418@section ISOC99 extensions
5ee15844 bellard2001-12-16 21:23:42 +0000419
420TCC implements many features of the new C standard: ISO C99. Currently
0bdbd49e Urs Janssen2013-02-17 00:48:51 +0100421missing items are: complex and imaginary numbers.
5ee15844 bellard2001-12-16 21:23:42 +0000422
423Currently implemented ISOC99 features:
424
30766d3c bellard2002-07-13 15:21:18 +0000425@itemize
5ee15844 bellard2001-12-16 21:23:42 +0000426
0bdbd49e
UJ
Urs Janssen2013-02-17 00:48:51 +0100427@item variable length arrays.
428
3605c50b bellard2003-04-10 00:03:26 +0000429@item 64 bit @code{long long} types are fully supported.
5ee15844 bellard2001-12-16 21:23:42 +0000430
3605c50b bellard2003-04-10 00:03:26 +0000431@item The boolean type @code{_Bool} is supported.
5ee15844 bellard2001-12-16 21:23:42 +0000432
3605c50b bellard2003-04-10 00:03:26 +0000433@item @code{__func__} is a string variable containing the current
5ee15844 bellard2001-12-16 21:23:42 +0000434function name.
435
30766d3c bellard2002-07-13 15:21:18 +0000436@item Variadic macros: @code{__VA_ARGS__} can be used for
5ee15844 bellard2001-12-16 21:23:42 +0000437 function-like macros:
30766d3c bellard2002-07-13 15:21:18 +0000438@example
5ee15844 bellard2001-12-16 21:23:42 +0000439 #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)
30766d3c bellard2002-07-13 15:21:18 +0000440@end example
3605c50b bellard2003-04-10 00:03:26 +0000441
442@noindent
30766d3c bellard2002-07-13 15:21:18 +0000443@code{dprintf} can then be used with a variable number of parameters.
5ee15844 bellard2001-12-16 21:23:42 +0000444
30766d3c bellard2002-07-13 15:21:18 +0000445@item Declarations can appear anywhere in a block (as in C++).
5ee15844 bellard2001-12-16 21:23:42 +0000446
30766d3c bellard2002-07-13 15:21:18 +0000447@item Array and struct/union elements can be initialized in any order by
5ee15844 bellard2001-12-16 21:23:42 +0000448 using designators:
30766d3c bellard2002-07-13 15:21:18 +0000449@example
3605c50b bellard2003-04-10 00:03:26 +0000450 struct @{ int x, y; @} st[10] = @{ [0].x = 1, [0].y = 2 @};
5ee15844 bellard2001-12-16 21:23:42 +0000451
3605c50b bellard2003-04-10 00:03:26 +0000452 int tab[10] = @{ 1, 2, [5] = 5, [9] = 9@};
30766d3c bellard2002-07-13 15:21:18 +0000453@end example
5ee15844 bellard2001-12-16 21:23:42 +0000454
30766d3c bellard2002-07-13 15:21:18 +0000455@item Compound initializers are supported:
456@example
3605c50b bellard2003-04-10 00:03:26 +0000457 int *p = (int [])@{ 1, 2, 3 @};
30766d3c bellard2002-07-13 15:21:18 +0000458@end example
5ee15844 bellard2001-12-16 21:23:42 +0000459to initialize a pointer pointing to an initialized array. The same
460works for structures and strings.
461
30766d3c bellard2002-07-13 15:21:18 +0000462@item Hexadecimal floating point constants are supported:
463@example
5ee15844 bellard2001-12-16 21:23:42 +0000464 double d = 0x1234p10;
30766d3c bellard2002-07-13 15:21:18 +0000465@end example
3605c50b bellard2003-04-10 00:03:26 +0000466
467@noindent
5ee15844 bellard2001-12-16 21:23:42 +0000468is the same as writing
30766d3c bellard2002-07-13 15:21:18 +0000469@example
5ee15844 bellard2001-12-16 21:23:42 +0000470 double d = 4771840.0;
30766d3c bellard2002-07-13 15:21:18 +0000471@end example
ebe9e87c bellard2002-01-05 19:50:17 +0000472
3605c50b bellard2003-04-10 00:03:26 +0000473@item @code{inline} keyword is ignored.
ebe9e87c bellard2002-01-05 19:50:17 +0000474
3605c50b bellard2003-04-10 00:03:26 +0000475@item @code{restrict} keyword is ignored.
30766d3c bellard2002-07-13 15:21:18 +0000476@end itemize
5ee15844 bellard2001-12-16 21:23:42 +0000477
30766d3c bellard2002-07-13 15:21:18 +0000478@section GNU C extensions
5ee15844 bellard2001-12-16 21:23:42 +0000479
ebe9e87c bellard2002-01-05 19:50:17 +0000480TCC implements some GNU C extensions:
5ee15844 bellard2001-12-16 21:23:42 +0000481
30766d3c bellard2002-07-13 15:21:18 +0000482@itemize
5ee15844 bellard2001-12-16 21:23:42 +0000483
30766d3c bellard2002-07-13 15:21:18 +0000484@item array designators can be used without '=':
485@example
3605c50b bellard2003-04-10 00:03:26 +0000486 int a[10] = @{ [0] 1, [5] 2, 3, 4 @};
30766d3c bellard2002-07-13 15:21:18 +0000487@end example
5ee15844 bellard2001-12-16 21:23:42 +0000488
30766d3c bellard2002-07-13 15:21:18 +0000489@item Structure field designators can be a label:
490@example
3605c50b bellard2003-04-10 00:03:26 +0000491 struct @{ int x, y; @} st = @{ x: 1, y: 1@};
30766d3c bellard2002-07-13 15:21:18 +0000492@end example
5ee15844 bellard2001-12-16 21:23:42 +0000493instead of
30766d3c bellard2002-07-13 15:21:18 +0000494@example
3605c50b bellard2003-04-10 00:03:26 +0000495 struct @{ int x, y; @} st = @{ .x = 1, .y = 1@};
30766d3c bellard2002-07-13 15:21:18 +0000496@end example
5ee15844 bellard2001-12-16 21:23:42 +0000497
3605c50b bellard2003-04-10 00:03:26 +0000498@item @code{\e} is ASCII character 27.
5ee15844 bellard2001-12-16 21:23:42 +0000499
30766d3c bellard2002-07-13 15:21:18 +0000500@item case ranges : ranges can be used in @code{case}s:
501@example
3605c50b bellard2003-04-10 00:03:26 +0000502 switch(a) @{
503 case 1 @dots{} 9:
ebe9e87c bellard2002-01-05 19:50:17 +0000504 printf("range 1 to 9\n");
505 break;
506 default:
507 printf("unexpected\n");
508 break;
3605c50b bellard2003-04-10 00:03:26 +0000509 @}
30766d3c bellard2002-07-13 15:21:18 +0000510@end example
ebe9e87c bellard2002-01-05 19:50:17 +0000511
f22e961f grischka2008-03-31 19:50:58 +0000512@cindex aligned attribute
513@cindex packed attribute
514@cindex section attribute
515@cindex unused attribute
516@cindex cdecl attribute
517@cindex stdcall attribute
518@cindex regparm attribute
519@cindex dllexport attribute
520
30766d3c bellard2002-07-13 15:21:18 +0000521@item The keyword @code{__attribute__} is handled to specify variable or
ebe9e87c bellard2002-01-05 19:50:17 +0000522function attributes. The following attributes are supported:
30766d3c bellard2002-07-13 15:21:18 +0000523 @itemize
ebe9e87c bellard2002-01-05 19:50:17 +0000524
6ab11406 bellard2004-11-08 21:19:47 +0000525 @item @code{aligned(n)}: align a variable or a structure field to n bytes
526(must be a power of two).
f22e961f grischka2008-03-31 19:50:58 +0000527
6ab11406 bellard2004-11-08 21:19:47 +0000528 @item @code{packed}: force alignment of a variable or a structure field to
529 1.
530
531 @item @code{section(name)}: generate function or data in assembly section
532name (name is a string containing the section name) instead of the default
533section.
ebe9e87c bellard2002-01-05 19:50:17 +0000534
30766d3c bellard2002-07-13 15:21:18 +0000535 @item @code{unused}: specify that the variable or the function is unused.
536
6ab11406 bellard2004-11-08 21:19:47 +0000537 @item @code{cdecl}: use standard C calling convention (default).
308017d5 bellard2002-01-26 19:17:06 +0000538
30766d3c bellard2002-07-13 15:21:18 +0000539 @item @code{stdcall}: use Pascal-like calling convention.
308017d5 bellard2002-01-26 19:17:06 +0000540
6ab11406 bellard2004-11-08 21:19:47 +0000541 @item @code{regparm(n)}: use fast i386 calling convention. @var{n} must be
542between 1 and 3. The first @var{n} function parameters are respectively put in
543registers @code{%eax}, @code{%edx} and @code{%ecx}.
544
f22e961f grischka2008-03-31 19:50:58 +0000545 @item @code{dllexport}: export function from dll/executable (win32 only)
546
30766d3c bellard2002-07-13 15:21:18 +0000547 @end itemize
308017d5 bellard2002-01-26 19:17:06 +0000548
ebe9e87c bellard2002-01-05 19:50:17 +0000549Here are some examples:
30766d3c bellard2002-07-13 15:21:18 +0000550@example
ebe9e87c bellard2002-01-05 19:50:17 +0000551 int a __attribute__ ((aligned(8), section(".mysection")));
30766d3c bellard2002-07-13 15:21:18 +0000552@end example
ebe9e87c bellard2002-01-05 19:50:17 +0000553
3605c50b bellard2003-04-10 00:03:26 +0000554@noindent
555align variable @code{a} to 8 bytes and put it in section @code{.mysection}.
30766d3c bellard2002-07-13 15:21:18 +0000556
557@example
ebe9e87c bellard2002-01-05 19:50:17 +0000558 int my_add(int a, int b) __attribute__ ((section(".mycodesection")))
3605c50b bellard2003-04-10 00:03:26 +0000559 @{
ebe9e87c bellard2002-01-05 19:50:17 +0000560 return a + b;
3605c50b bellard2003-04-10 00:03:26 +0000561 @}
30766d3c bellard2002-07-13 15:21:18 +0000562@end example
563
3605c50b bellard2003-04-10 00:03:26 +0000564@noindent
565generate function @code{my_add} in section @code{.mycodesection}.
30766d3c bellard2002-07-13 15:21:18 +0000566
567@item GNU style variadic macros:
568@example
3605c50b bellard2003-04-10 00:03:26 +0000569 #define dprintf(fmt, args@dots{}) printf(fmt, ## args)
30766d3c bellard2002-07-13 15:21:18 +0000570
571 dprintf("no arg\n");
572 dprintf("one arg %d\n", 1);
573@end example
5ee15844 bellard2001-12-16 21:23:42 +0000574
bc427f5b bellard2002-08-31 12:44:16 +0000575@item @code{__FUNCTION__} is interpreted as C99 @code{__func__}
576(so it has not exactly the same semantics as string literal GNUC
577where it is a string literal).
578
6cd2d5d2 bellard2002-11-03 00:44:38 +0000579@item The @code{__alignof__} keyword can be used as @code{sizeof}
580to get the alignment of a type or an expression.
581
582@item The @code{typeof(x)} returns the type of @code{x}.
583@code{x} is an expression or a type.
584
585@item Computed gotos: @code{&&label} returns a pointer of type
586@code{void *} on the goto label @code{label}. @code{goto *expr} can be
587used to jump on the pointer resulting from @code{expr}.
588
75e743d2 bellard2003-01-06 20:19:20 +0000589@item Inline assembly with asm instruction:
3605c50b bellard2003-04-10 00:03:26 +0000590@cindex inline assembly
591@cindex assembly, inline
592@cindex __asm__
75e743d2 bellard2003-01-06 20:19:20 +0000593@example
594static inline void * my_memcpy(void * to, const void * from, size_t n)
3605c50b bellard2003-04-10 00:03:26 +0000595@{
75e743d2 bellard2003-01-06 20:19:20 +0000596int d0, d1, d2;
597__asm__ __volatile__(
3605c50b bellard2003-04-10 00:03:26 +0000598 "rep ; movsl\n\t"
599 "testb $2,%b4\n\t"
600 "je 1f\n\t"
601 "movsw\n"
602 "1:\ttestb $1,%b4\n\t"
603 "je 2f\n\t"
604 "movsb\n"
605 "2:"
606 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
607 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
608 : "memory");
75e743d2 bellard2003-01-06 20:19:20 +0000609return (to);
3605c50b bellard2003-04-10 00:03:26 +0000610@}
75e743d2 bellard2003-01-06 20:19:20 +0000611@end example
612
3605c50b bellard2003-04-10 00:03:26 +0000613@noindent
614@cindex gas
75e743d2 bellard2003-01-06 20:19:20 +0000615TCC includes its own x86 inline assembler with a @code{gas}-like (GNU
616assembler) syntax. No intermediate files are generated. GCC 3.x named
617operands are supported.
618
4e6edcdc bellard2003-05-24 16:12:58 +0000619@item @code{__builtin_types_compatible_p()} and @code{__builtin_constant_p()}
620are supported.
621
16559cd6 bellard2005-06-17 22:07:03 +0000622@item @code{#pragma pack} is supported for win32 compatibility.
623
30766d3c bellard2002-07-13 15:21:18 +0000624@end itemize
5ee15844 bellard2001-12-16 21:23:42 +0000625
30766d3c bellard2002-07-13 15:21:18 +0000626@section TinyCC extensions
5ee15844 bellard2001-12-16 21:23:42 +0000627
30766d3c bellard2002-07-13 15:21:18 +0000628@itemize
5ee15844 bellard2001-12-16 21:23:42 +0000629
0db7f616 Urs Janssen2013-02-18 15:44:18 +0100630@item @code{__TINYC__} is a predefined macro to indicate that you use TCC.
5ee15844 bellard2001-12-16 21:23:42 +0000631
3605c50b bellard2003-04-10 00:03:26 +0000632@item @code{#!} at the start of a line is ignored to allow scripting.
5ee15844 bellard2001-12-16 21:23:42 +0000633
3605c50b bellard2003-04-10 00:03:26 +0000634@item Binary digits can be entered (@code{0b101} instead of
635@code{5}).
5ee15844 bellard2001-12-16 21:23:42 +0000636
30766d3c bellard2002-07-13 15:21:18 +0000637@item @code{__BOUNDS_CHECKING_ON} is defined if bound checking is activated.
ebe9e87c bellard2002-01-05 19:50:17 +0000638
30766d3c bellard2002-07-13 15:21:18 +0000639@end itemize
5ee15844 bellard2001-12-16 21:23:42 +0000640
e8a52a82 grischka2009-04-18 14:20:27 +0200641@node asm
75e743d2 bellard2003-01-06 20:19:20 +0000642@chapter TinyCC Assembler
643
644Since version 0.9.16, TinyCC integrates its own assembler. TinyCC
645assembler supports a gas-like syntax (GNU assembler). You can
646desactivate assembler support if you want a smaller TinyCC executable
647(the C compiler does not rely on the assembler).
648
649TinyCC Assembler is used to handle files with @file{.S} (C
650preprocessed assembler) and @file{.s} extensions. It is also used to
651handle the GNU inline assembler with the @code{asm} keyword.
652
653@section Syntax
654
655TinyCC Assembler supports most of the gas syntax. The tokens are the
656same as C.
657
658@itemize
659
660@item C and C++ comments are supported.
661
662@item Identifiers are the same as C, so you cannot use '.' or '$'.
663
664@item Only 32 bit integer numbers are supported.
665
666@end itemize
667
668@section Expressions
669
670@itemize
671
672@item Integers in decimal, octal and hexa are supported.
673
674@item Unary operators: +, -, ~.
675
676@item Binary operators in decreasing priority order:
677
678@enumerate
679@item *, /, %
680@item &, |, ^
681@item +, -
682@end enumerate
683
684@item A value is either an absolute number or a label plus an offset.
685All operators accept absolute values except '+' and '-'. '+' or '-' can be
686used to add an offset to a label. '-' supports two labels only if they
687are the same or if they are both defined and in the same section.
688
689@end itemize
690
691@section Labels
692
693@itemize
694
695@item All labels are considered as local, except undefined ones.
696
697@item Numeric labels can be used as local @code{gas}-like labels.
698They can be defined several times in the same source. Use 'b'
699(backward) or 'f' (forward) as suffix to reference them:
700
701@example
702 1:
703 jmp 1b /* jump to '1' label before */
704 jmp 1f /* jump to '1' label after */
705 1:
706@end example
707
708@end itemize
709
710@section Directives
3605c50b bellard2003-04-10 00:03:26 +0000711@cindex assembler directives
712@cindex directives, assembler
6ab11406 bellard2004-11-08 21:19:47 +0000713@cindex align directive
714@cindex skip directive
715@cindex space directive
716@cindex byte directive
717@cindex word directive
718@cindex short directive
719@cindex int directive
720@cindex long directive
721@cindex quad directive
722@cindex globl directive
723@cindex global directive
724@cindex section directive
725@cindex text directive
726@cindex data directive
727@cindex bss directive
728@cindex fill directive
729@cindex org directive
730@cindex previous directive
731@cindex string directive
732@cindex asciz directive
733@cindex ascii directive
75e743d2 bellard2003-01-06 20:19:20 +0000734
243c6990 Urs Janssen2013-02-20 14:23:44 +0100735All directives are preceded by a '.'. The following directives are
75e743d2 bellard2003-01-06 20:19:20 +0000736supported:
737
738@itemize
739@item .align n[,value]
740@item .skip n[,value]
741@item .space n[,value]
6ab11406 bellard2004-11-08 21:19:47 +0000742@item .byte value1[,...]
743@item .word value1[,...]
744@item .short value1[,...]
745@item .int value1[,...]
746@item .long value1[,...]
747@item .quad immediate_value1[,...]
8da6027e bellard2004-10-02 14:01:26 +0000748@item .globl symbol
4e6edcdc bellard2003-05-24 16:12:58 +0000749@item .global symbol
750@item .section section
751@item .text
752@item .data
753@item .bss
2a6c1047 bellard2004-10-23 23:11:05 +0000754@item .fill repeat[,size[,value]]
755@item .org n
756@item .previous
757@item .string string[,...]
758@item .asciz string[,...]
759@item .ascii string[,...]
75e743d2 bellard2003-01-06 20:19:20 +0000760@end itemize
761
762@section X86 Assembler
3605c50b bellard2003-04-10 00:03:26 +0000763@cindex assembler
75e743d2 bellard2003-01-06 20:19:20 +0000764
765All X86 opcodes are supported. Only ATT syntax is supported (source
766then destination operand order). If no size suffix is given, TinyCC
767tries to guess it from the operand sizes.
768
769Currently, MMX opcodes are supported but not SSE ones.
770
e8a52a82 grischka2009-04-18 14:20:27 +0200771@node linker
840d197e bellard2002-07-24 22:13:02 +0000772@chapter TinyCC Linker
3605c50b bellard2003-04-10 00:03:26 +0000773@cindex linker
840d197e bellard2002-07-24 22:13:02 +0000774
775@section ELF file generation
3605c50b bellard2003-04-10 00:03:26 +0000776@cindex ELF
840d197e bellard2002-07-24 22:13:02 +0000777
778TCC can directly output relocatable ELF files (object files),
779executable ELF files and dynamic ELF libraries without relying on an
780external linker.
781
79686686 bellard2002-08-18 13:24:19 +0000782Dynamic ELF libraries can be output but the C compiler does not generate
c5ce5eae bellard2003-10-04 13:04:47 +0000783position independent code (PIC). It means that the dynamic library
79686686 bellard2002-08-18 13:24:19 +0000784code generated by TCC cannot be factorized among processes yet.
840d197e bellard2002-07-24 22:13:02 +0000785
4e6edcdc bellard2003-05-24 16:12:58 +0000786TCC linker eliminates unreferenced object code in libraries. A single pass is
787done on the object and library list, so the order in which object files and
788libraries are specified is important (same constraint as GNU ld). No grouping
789options (@option{--start-group} and @option{--end-group}) are supported.
840d197e bellard2002-07-24 22:13:02 +0000790
840d197e bellard2002-07-24 22:13:02 +0000791@section ELF file loader
792
793TCC can load ELF object files, archives (.a files) and dynamic
794libraries (.so).
795
16559cd6 bellard2005-06-17 22:07:03 +0000796@section PE-i386 file generation
797@cindex PE-i386
798
e8a52a82 grischka2009-04-18 14:20:27 +0200799TCC for Windows supports the native Win32 executable file format (PE-i386). It
800generates EXE files (console and gui) and DLL files.
f22e961f grischka2008-03-31 19:50:58 +0000801
e8a52a82 grischka2009-04-18 14:20:27 +0200802For usage on Windows, see also tcc-win32.txt.
16559cd6 bellard2005-06-17 22:07:03 +0000803
840d197e bellard2002-07-24 22:13:02 +0000804@section GNU Linker Scripts
3605c50b bellard2003-04-10 00:03:26 +0000805@cindex scripts, linker
806@cindex linker scripts
807@cindex GROUP, linker command
808@cindex FILE, linker command
4e6edcdc bellard2003-05-24 16:12:58 +0000809@cindex OUTPUT_FORMAT, linker command
810@cindex TARGET, linker command
840d197e bellard2002-07-24 22:13:02 +0000811
812Because on many Linux systems some dynamic libraries (such as
813@file{/usr/lib/libc.so}) are in fact GNU ld link scripts (horrible!),
3605c50b bellard2003-04-10 00:03:26 +0000814the TCC linker also supports a subset of GNU ld scripts.
840d197e bellard2002-07-24 22:13:02 +0000815
4e6edcdc bellard2003-05-24 16:12:58 +0000816The @code{GROUP} and @code{FILE} commands are supported. @code{OUTPUT_FORMAT}
817and @code{TARGET} are ignored.
840d197e bellard2002-07-24 22:13:02 +0000818
819Example from @file{/usr/lib/libc.so}:
820@example
821/* GNU ld script
822 Use the shared library, but some functions are only in
823 the static library, so try that secondarily. */
824GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )
825@end example
826
3605c50b bellard2003-04-10 00:03:26 +0000827@node Bounds
30766d3c bellard2002-07-13 15:21:18 +0000828@chapter TinyCC Memory and Bound checks
3605c50b bellard2003-04-10 00:03:26 +0000829@cindex bound checks
830@cindex memory checks
ebe9e87c bellard2002-01-05 19:50:17 +0000831
3605c50b bellard2003-04-10 00:03:26 +0000832This feature is activated with the @option{-b} (@pxref{Invoke}).
30766d3c bellard2002-07-13 15:21:18 +0000833
834Note that pointer size is @emph{unchanged} and that code generated
835with bound checks is @emph{fully compatible} with unchecked
ebe9e87c bellard2002-01-05 19:50:17 +0000836code. When a pointer comes from unchecked code, it is assumed to be
837valid. Even very obscure C code with casts should work correctly.
30766d3c bellard2002-07-13 15:21:18 +0000838
3605c50b bellard2003-04-10 00:03:26 +0000839For more information about the ideas behind this method, see
30766d3c bellard2002-07-13 15:21:18 +0000840@url{http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html}.
841
3605c50b bellard2003-04-10 00:03:26 +0000842Here are some examples of caught errors:
30766d3c bellard2002-07-13 15:21:18 +0000843
844@table @asis
845
846@item Invalid range with standard string function:
847@example
3605c50b bellard2003-04-10 00:03:26 +0000848@{
ebe9e87c bellard2002-01-05 19:50:17 +0000849 char tab[10];
850 memset(tab, 0, 11);
3605c50b bellard2003-04-10 00:03:26 +0000851@}
30766d3c bellard2002-07-13 15:21:18 +0000852@end example
ebe9e87c bellard2002-01-05 19:50:17 +0000853
3605c50b bellard2003-04-10 00:03:26 +0000854@item Out of bounds-error in global or local arrays:
30766d3c bellard2002-07-13 15:21:18 +0000855@example
3605c50b bellard2003-04-10 00:03:26 +0000856@{
ebe9e87c bellard2002-01-05 19:50:17 +0000857 int tab[10];
3605c50b bellard2003-04-10 00:03:26 +0000858 for(i=0;i<11;i++) @{
ebe9e87c bellard2002-01-05 19:50:17 +0000859 sum += tab[i];
3605c50b bellard2003-04-10 00:03:26 +0000860 @}
861@}
30766d3c bellard2002-07-13 15:21:18 +0000862@end example
ebe9e87c bellard2002-01-05 19:50:17 +0000863
3605c50b bellard2003-04-10 00:03:26 +0000864@item Out of bounds-error in malloc'ed data:
30766d3c bellard2002-07-13 15:21:18 +0000865@example
3605c50b bellard2003-04-10 00:03:26 +0000866@{
ebe9e87c bellard2002-01-05 19:50:17 +0000867 int *tab;
868 tab = malloc(20 * sizeof(int));
3605c50b bellard2003-04-10 00:03:26 +0000869 for(i=0;i<21;i++) @{
ebe9e87c bellard2002-01-05 19:50:17 +0000870 sum += tab4[i];
3605c50b bellard2003-04-10 00:03:26 +0000871 @}
ebe9e87c bellard2002-01-05 19:50:17 +0000872 free(tab);
3605c50b bellard2003-04-10 00:03:26 +0000873@}
30766d3c bellard2002-07-13 15:21:18 +0000874@end example
ebe9e87c bellard2002-01-05 19:50:17 +0000875
3605c50b bellard2003-04-10 00:03:26 +0000876@item Access of freed memory:
30766d3c bellard2002-07-13 15:21:18 +0000877@example
3605c50b bellard2003-04-10 00:03:26 +0000878@{
ebe9e87c bellard2002-01-05 19:50:17 +0000879 int *tab;
880 tab = malloc(20 * sizeof(int));
881 free(tab);
3605c50b bellard2003-04-10 00:03:26 +0000882 for(i=0;i<20;i++) @{
ebe9e87c bellard2002-01-05 19:50:17 +0000883 sum += tab4[i];
3605c50b bellard2003-04-10 00:03:26 +0000884 @}
885@}
30766d3c bellard2002-07-13 15:21:18 +0000886@end example
ebe9e87c bellard2002-01-05 19:50:17 +0000887
3605c50b bellard2003-04-10 00:03:26 +0000888@item Double free:
30766d3c bellard2002-07-13 15:21:18 +0000889@example
3605c50b bellard2003-04-10 00:03:26 +0000890@{
ebe9e87c bellard2002-01-05 19:50:17 +0000891 int *tab;
892 tab = malloc(20 * sizeof(int));
893 free(tab);
894 free(tab);
3605c50b bellard2003-04-10 00:03:26 +0000895@}
30766d3c bellard2002-07-13 15:21:18 +0000896@end example
ebe9e87c bellard2002-01-05 19:50:17 +0000897
30766d3c bellard2002-07-13 15:21:18 +0000898@end table
ebe9e87c bellard2002-01-05 19:50:17 +0000899
3605c50b bellard2003-04-10 00:03:26 +0000900@node Libtcc
30766d3c bellard2002-07-13 15:21:18 +0000901@chapter The @code{libtcc} library
5ee15844 bellard2001-12-16 21:23:42 +0000902
30766d3c bellard2002-07-13 15:21:18 +0000903The @code{libtcc} library enables you to use TCC as a backend for
904dynamic code generation.
5ee15844 bellard2001-12-16 21:23:42 +0000905
30766d3c bellard2002-07-13 15:21:18 +0000906Read the @file{libtcc.h} to have an overview of the API. Read
907@file{libtcc_test.c} to have a very simple example.
5ee15844 bellard2001-12-16 21:23:42 +0000908
30766d3c bellard2002-07-13 15:21:18 +0000909The idea consists in giving a C string containing the program you want
d5751376 bellard2003-04-09 23:52:29 +0000910to compile directly to @code{libtcc}. Then you can access to any global
911symbol (function or variable) defined.
ebe9e87c bellard2002-01-05 19:50:17 +0000912
e8a52a82 grischka2009-04-18 14:20:27 +0200913@node devel
3605c50b bellard2003-04-10 00:03:26 +0000914@chapter Developer's guide
5ee15844 bellard2001-12-16 21:23:42 +0000915
30766d3c bellard2002-07-13 15:21:18 +0000916This chapter gives some hints to understand how TCC works. You can skip
917it if you do not intend to modify the TCC code.
ebe9e87c bellard2002-01-05 19:50:17 +0000918
30766d3c bellard2002-07-13 15:21:18 +0000919@section File reading
ebe9e87c bellard2002-01-05 19:50:17 +0000920
30766d3c bellard2002-07-13 15:21:18 +0000921The @code{BufferedFile} structure contains the context needed to read a
922file, including the current line number. @code{tcc_open()} opens a new
923file and @code{tcc_close()} closes it. @code{inp()} returns the next
924character.
925
926@section Lexer
927
928@code{next()} reads the next token in the current
929file. @code{next_nomacro()} reads the next token without macro
930expansion.
931
932@code{tok} contains the current token (see @code{TOK_xxx})
933constants. Identifiers and keywords are also keywords. @code{tokc}
3605c50b bellard2003-04-10 00:03:26 +0000934contains additional infos about the token (for example a constant value
30766d3c bellard2002-07-13 15:21:18 +0000935if number or string token).
936
937@section Parser
938
939The parser is hardcoded (yacc is not necessary). It does only one pass,
940except:
941
942@itemize
943
944@item For initialized arrays with unknown size, a first pass
945is done to count the number of elements.
946
947@item For architectures where arguments are evaluated in
948reverse order, a first pass is done to reverse the argument order.
949
950@end itemize
951
952@section Types
953
243c6990 Urs Janssen2013-02-20 14:23:44 +0100954The types are stored in a single 'int' variable. It was chosen in the
30766d3c bellard2002-07-13 15:21:18 +0000955first stages of development when tcc was much simpler. Now, it may not
956be the best solution.
957
958@example
959#define VT_INT 0 /* integer type */
960#define VT_BYTE 1 /* signed byte type */
961#define VT_SHORT 2 /* short type */
962#define VT_VOID 3 /* void type */
963#define VT_PTR 4 /* pointer */
964#define VT_ENUM 5 /* enum definition */
965#define VT_FUNC 6 /* function type */
966#define VT_STRUCT 7 /* struct/union definition */
967#define VT_FLOAT 8 /* IEEE float */
968#define VT_DOUBLE 9 /* IEEE double */
969#define VT_LDOUBLE 10 /* IEEE long double */
970#define VT_BOOL 11 /* ISOC99 boolean type */
971#define VT_LLONG 12 /* 64 bit integer */
972#define VT_LONG 13 /* long integer (NEVER USED as type, only
973 during parsing) */
974#define VT_BTYPE 0x000f /* mask for basic type */
975#define VT_UNSIGNED 0x0010 /* unsigned type */
976#define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -0700977#define VT_VLA 0x20000 /* VLA type (also has VT_PTR and VT_ARRAY) */
38f6467c Thomas Preud'homme2011-02-05 02:31:27 +0100978#define VT_BITFIELD 0x0040 /* bitfield modifier */
ace0f7f2
JS
Joe Soroka2011-04-06 09:17:03 -0700979#define VT_CONSTANT 0x0800 /* const modifier */
980#define VT_VOLATILE 0x1000 /* volatile modifier */
b0b5165d Thomas Preud'homme2014-02-06 20:51:47 +0800981#define VT_DEFSIGN 0x2000 /* signed type */
30766d3c bellard2002-07-13 15:21:18 +0000982
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -0700983#define VT_STRUCT_SHIFT 18 /* structure/enum name shift (14 bits left) */
30766d3c bellard2002-07-13 15:21:18 +0000984@end example
985
986When a reference to another type is needed (for pointers, functions and
987structures), the @code{32 - VT_STRUCT_SHIFT} high order bits are used to
988store an identifier reference.
989
990The @code{VT_UNSIGNED} flag can be set for chars, shorts, ints and long
991longs.
992
993Arrays are considered as pointers @code{VT_PTR} with the flag
ace0f7f2 Joe Soroka2011-04-06 09:17:03 -0700994@code{VT_ARRAY} set. Variable length arrays are considered as special
1b0f42f8 Joe Soroka2011-04-09 23:41:16 -0700995arrays and have flag @code{VT_VLA} set instead of @code{VT_ARRAY}.
30766d3c bellard2002-07-13 15:21:18 +0000996
997The @code{VT_BITFIELD} flag can be set for chars, shorts, ints and long
998longs. If it is set, then the bitfield position is stored from bits
999VT_STRUCT_SHIFT to VT_STRUCT_SHIFT + 5 and the bit field size is stored
1000from bits VT_STRUCT_SHIFT + 6 to VT_STRUCT_SHIFT + 11.
1001
1002@code{VT_LONG} is never used except during parsing.
1003
1004During parsing, the storage of an object is also stored in the type
1005integer:
1006
1007@example
38f6467c
TP
Thomas Preud'homme2011-02-05 02:31:27 +01001008#define VT_EXTERN 0x00000080 /* extern definition */
1009#define VT_STATIC 0x00000100 /* static variable */
1010#define VT_TYPEDEF 0x00000200 /* typedef definition */
ace0f7f2
JS
Joe Soroka2011-04-06 09:17:03 -07001011#define VT_INLINE 0x00000400 /* inline definition */
1012#define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
1013#define VT_EXPORT 0x00008000 /* win32: data exported from dll */
1014#define VT_WEAK 0x00010000 /* win32: data exported from dll */
30766d3c bellard2002-07-13 15:21:18 +00001015@end example
1016
1017@section Symbols
1018
1019All symbols are stored in hashed symbol stacks. Each symbol stack
1020contains @code{Sym} structures.
1021
1022@code{Sym.v} contains the symbol name (remember
1023an idenfier is also a token, so a string is never necessary to store
1024it). @code{Sym.t} gives the type of the symbol. @code{Sym.r} is usually
1025the register in which the corresponding variable is stored. @code{Sym.c} is
ace0f7f2
JS
Joe Soroka2011-04-06 09:17:03 -07001026usually a constant associated to the symbol like its address for normal
1027symbols, and the number of entries for symbols representing arrays.
1b0f42f8
JS
Joe Soroka2011-04-09 23:41:16 -07001028Variable length array types use @code{Sym.c} as a location on the stack
1029which holds the runtime sizeof for the type.
30766d3c bellard2002-07-13 15:21:18 +00001030
1031Four main symbol stacks are defined:
1032
1033@table @code
1034
1035@item define_stack
1036for the macros (@code{#define}s).
1037
1038@item global_stack
1039for the global variables, functions and types.
1040
30766d3c bellard2002-07-13 15:21:18 +00001041@item local_stack
1042for the local variables, functions and types.
1043
d5751376 bellard2003-04-09 23:52:29 +00001044@item global_label_stack
30766d3c bellard2002-07-13 15:21:18 +00001045for the local labels (for @code{goto}).
1046
d5751376 bellard2003-04-09 23:52:29 +00001047@item label_stack
1048for GCC block local labels (see the @code{__label__} keyword).
1049
30766d3c bellard2002-07-13 15:21:18 +00001050@end table
1051
1052@code{sym_push()} is used to add a new symbol in the local symbol
1053stack. If no local symbol stack is active, it is added in the global
1054symbol stack.
1055
1056@code{sym_pop(st,b)} pops symbols from the symbol stack @var{st} until
1057the symbol @var{b} is on the top of stack. If @var{b} is NULL, the stack
1058is emptied.
1059
1060@code{sym_find(v)} return the symbol associated to the identifier
1061@var{v}. The local stack is searched first from top to bottom, then the
1062global stack.
1063
1064@section Sections
1065
1066The generated code and datas are written in sections. The structure
1067@code{Section} contains all the necessary information for a given
1068section. @code{new_section()} creates a new section. ELF file semantics
1069is assumed for each section.
1070
1071The following sections are predefined:
1072
1073@table @code
1074
1075@item text_section
1076is the section containing the generated code. @var{ind} contains the
1077current position in the code section.
1078
1079@item data_section
1080contains initialized data
1081
1082@item bss_section
1083contains uninitialized data
1084
1085@item bounds_section
1086@itemx lbounds_section
1087are used when bound checking is activated
1088
1089@item stab_section
1090@itemx stabstr_section
d09a46d6 Vincent Lefevre2014-04-07 13:20:49 +02001091are used when debugging is active to store debug information
30766d3c bellard2002-07-13 15:21:18 +00001092
1093@item symtab_section
1094@itemx strtab_section
1095contain the exported symbols (currently only used for debugging).
1096
1097@end table
1098
1099@section Code generation
3605c50b bellard2003-04-10 00:03:26 +00001100@cindex code generation
30766d3c bellard2002-07-13 15:21:18 +00001101
1102@subsection Introduction
1103
1104The TCC code generator directly generates linked binary code in one
1105pass. It is rather unusual these days (see gcc for example which
3605c50b bellard2003-04-10 00:03:26 +00001106generates text assembly), but it can be very fast and surprisingly
1107little complicated.
30766d3c bellard2002-07-13 15:21:18 +00001108
1109The TCC code generator is register based. Optimization is only done at
1110the expression level. No intermediate representation of expression is
1111kept except the current values stored in the @emph{value stack}.
1112
1113On x86, three temporary registers are used. When more registers are
3605c50b bellard2003-04-10 00:03:26 +00001114needed, one register is spilled into a new temporary variable on the stack.
30766d3c bellard2002-07-13 15:21:18 +00001115
1116@subsection The value stack
3605c50b bellard2003-04-10 00:03:26 +00001117@cindex value stack, introduction
30766d3c bellard2002-07-13 15:21:18 +00001118
1119When an expression is parsed, its value is pushed on the value stack
1120(@var{vstack}). The top of the value stack is @var{vtop}. Each value
1121stack entry is the structure @code{SValue}.
1122
1123@code{SValue.t} is the type. @code{SValue.r} indicates how the value is
1124currently stored in the generated code. It is usually a CPU register
3605c50b bellard2003-04-10 00:03:26 +00001125index (@code{REG_xxx} constants), but additional values and flags are
30766d3c bellard2002-07-13 15:21:18 +00001126defined:
1127
1128@example
10f5d44f bellard2002-09-08 22:13:54 +00001129#define VT_CONST 0x00f0
1130#define VT_LLOCAL 0x00f1
1131#define VT_LOCAL 0x00f2
1132#define VT_CMP 0x00f3
1133#define VT_JMP 0x00f4
1134#define VT_JMPI 0x00f5
1135#define VT_LVAL 0x0100
1136#define VT_SYM 0x0200
1137#define VT_MUSTCAST 0x0400
1138#define VT_MUSTBOUND 0x0800
1139#define VT_BOUNDED 0x8000
1140#define VT_LVAL_BYTE 0x1000
1141#define VT_LVAL_SHORT 0x2000
1142#define VT_LVAL_UNSIGNED 0x4000
30766d3c bellard2002-07-13 15:21:18 +00001143#define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
1144@end example
1145
1146@table @code
1147
1148@item VT_CONST
1149indicates that the value is a constant. It is stored in the union
1150@code{SValue.c}, depending on its type.
1151
1152@item VT_LOCAL
1153indicates a local variable pointer at offset @code{SValue.c.i} in the
1154stack.
1155
1156@item VT_CMP
1157indicates that the value is actually stored in the CPU flags (i.e. the
1158value is the consequence of a test). The value is either 0 or 1. The
10f5d44f bellard2002-09-08 22:13:54 +00001159actual CPU flags used is indicated in @code{SValue.c.i}.
1160
1161If any code is generated which destroys the CPU flags, this value MUST be
1162put in a normal register.
30766d3c bellard2002-07-13 15:21:18 +00001163
1164@item VT_JMP
1165@itemx VT_JMPI
3605c50b bellard2003-04-10 00:03:26 +00001166indicates that the value is the consequence of a conditional jump. For VT_JMP,
1167it is 1 if the jump is taken, 0 otherwise. For VT_JMPI it is inverted.
30766d3c bellard2002-07-13 15:21:18 +00001168
1169These values are used to compile the @code{||} and @code{&&} logical
1170operators.
1171
10f5d44f bellard2002-09-08 22:13:54 +00001172If any code is generated, this value MUST be put in a normal
1173register. Otherwise, the generated code won't be executed if the jump is
1174taken.
1175
30766d3c bellard2002-07-13 15:21:18 +00001176@item VT_LVAL
1177is a flag indicating that the value is actually an lvalue (left value of
1178an assignment). It means that the value stored is actually a pointer to
1179the wanted value.
1180
1181Understanding the use @code{VT_LVAL} is very important if you want to
1182understand how TCC works.
1183
1184@item VT_LVAL_BYTE
1185@itemx VT_LVAL_SHORT
1186@itemx VT_LVAL_UNSIGNED
1187if the lvalue has an integer type, then these flags give its real
3605c50b bellard2003-04-10 00:03:26 +00001188type. The type alone is not enough in case of cast optimisations.
30766d3c bellard2002-07-13 15:21:18 +00001189
1190@item VT_LLOCAL
86c850fc
EGE
Edmund Grimley Evans2015-02-20 23:01:01 +00001191is a saved lvalue on the stack. @code{VT_LVAL} must also be set with
1192@code{VT_LLOCAL}. @code{VT_LLOCAL} can arise when a @code{VT_LVAL} in
1193a register has to be saved to the stack, or it can come from an
1194architecture-specific calling convention.
30766d3c bellard2002-07-13 15:21:18 +00001195
1196@item VT_MUSTCAST
1197indicates that a cast to the value type must be performed if the value
1198is used (lazy casting).
1199
10f5d44f bellard2002-09-08 22:13:54 +00001200@item VT_SYM
1201indicates that the symbol @code{SValue.sym} must be added to the constant.
30766d3c bellard2002-07-13 15:21:18 +00001202
1203@item VT_MUSTBOUND
1204@itemx VT_BOUNDED
1205are only used for optional bound checking.
1206
1207@end table
1208
1209@subsection Manipulating the value stack
3605c50b bellard2003-04-10 00:03:26 +00001210@cindex value stack
30766d3c bellard2002-07-13 15:21:18 +00001211
1212@code{vsetc()} and @code{vset()} pushes a new value on the value
3605c50b bellard2003-04-10 00:03:26 +00001213stack. If the previous @var{vtop} was stored in a very unsafe place(for
30766d3c bellard2002-07-13 15:21:18 +00001214example in the CPU flags), then some code is generated to put the
3605c50b bellard2003-04-10 00:03:26 +00001215previous @var{vtop} in a safe storage.
30766d3c bellard2002-07-13 15:21:18 +00001216
3605c50b bellard2003-04-10 00:03:26 +00001217@code{vpop()} pops @var{vtop}. In some cases, it also generates cleanup
30766d3c bellard2002-07-13 15:21:18 +00001218code (for example if stacked floating point registers are used as on
1219x86).
1220
3605c50b bellard2003-04-10 00:03:26 +00001221The @code{gv(rc)} function generates code to evaluate @var{vtop} (the
30766d3c bellard2002-07-13 15:21:18 +00001222top value of the stack) into registers. @var{rc} selects in which
1223register class the value should be put. @code{gv()} is the @emph{most
1224important function} of the code generator.
1225
1226@code{gv2()} is the same as @code{gv()} but for the top two stack
1227entries.
1228
1229@subsection CPU dependent code generation
3605c50b bellard2003-04-10 00:03:26 +00001230@cindex CPU dependent
30766d3c bellard2002-07-13 15:21:18 +00001231See the @file{i386-gen.c} file to have an example.
1232
1233@table @code
1234
1235@item load()
1236must generate the code needed to load a stack value into a register.
1237
1238@item store()
1239must generate the code needed to store a register into a stack value
1240lvalue.
1241
1242@item gfunc_start()
1243@itemx gfunc_param()
1244@itemx gfunc_call()
1245should generate a function call
1246
1247@item gfunc_prolog()
1248@itemx gfunc_epilog()
1249should generate a function prolog/epilog.
1250
1251@item gen_opi(op)
1252must generate the binary integer operation @var{op} on the two top
1253entries of the stack which are guaranted to contain integer types.
1254
1255The result value should be put on the stack.
1256
1257@item gen_opf(op)
1258same as @code{gen_opi()} for floating point operations. The two top
1259entries of the stack are guaranted to contain floating point values of
1260same types.
1261
1262@item gen_cvt_itof()
1263integer to floating point conversion.
1264
1265@item gen_cvt_ftoi()
1266floating point to integer conversion.
1267
1268@item gen_cvt_ftof()
1269floating point to floating point of different size conversion.
1270
1271@item gen_bounded_ptr_add()
1272@item gen_bounded_ptr_deref()
3605c50b bellard2003-04-10 00:03:26 +00001273are only used for bounds checking.
30766d3c bellard2002-07-13 15:21:18 +00001274
1275@end table
1276
1277@section Optimizations done
3605c50b bellard2003-04-10 00:03:26 +00001278@cindex optimizations
1279@cindex constant propagation
1280@cindex strength reduction
1281@cindex comparison operators
1282@cindex caching processor flags
1283@cindex flags, caching
1284@cindex jump optimization
30766d3c bellard2002-07-13 15:21:18 +00001285Constant propagation is done for all operations. Multiplications and
1286divisions are optimized to shifts when appropriate. Comparison
1287operators are optimized by maintaining a special cache for the
1288processor flags. &&, || and ! are optimized by maintaining a special
1289'jump target' value. No other jump optimization is currently performed
1290because it would require to store the code in a more abstract fashion.
5ee15844 bellard2001-12-16 21:23:42 +00001291
3605c50b bellard2003-04-10 00:03:26 +00001292@unnumbered Concept Index
1293@printindex cp
1294
1295@bye
1296
1297@c Local variables:
1298@c fill-column: 78
1299@c texinfo-column-for-description: 32
1300@c End: