📄 tcc-doc.texi
字号:
\input texinfo @c -*- texinfo -*-@c %**start of header@setfilename tcc-doc.info@settitle Tiny C Compiler Reference Documentation@c %**end of header@include config.texi@iftex@titlepage@afourpaper@sp 7@center @titlefont{Tiny C Compiler Reference Documentation}@sp 3@end titlepage@headings double@end iftex@c @ifhtml@contents@c @end ifhtml@ifnothtml@node Top, Introduction, (dir), (dir)@top Tiny C Compiler Reference DocumentationThis manual documents version @value{VERSION} of the Tiny C Compiler.@menu* Introduction:: Introduction to tcc.* Invoke:: Invocation of tcc (command line, options).* Bounds:: Automatic bounds-checking of C code.* Libtcc:: The libtcc library.@end menu@end ifnothtml@node Introduction@chapter IntroductionTinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other Ccompilers, it is meant to be self-relying: you do not need anexternal assembler or linker because TCC does that for you.TCC compiles so @emph{fast} that even for big projects @code{Makefile}s maynot be necessary. TCC not only supports ANSI C, but also most of the new ISO C99standard and many GNUC extensions including inline assembly.TCC can also be used to make @emph{C scripts}, i.e. pieces of C sourcethat you run as a Perl or Python script. Compilation is so fast thatyour script will be as fast as if it was an executable.TCC can also automatically generate memory and bound checks(@pxref{Bounds}) while allowing all C pointers operations. TCC can dothese checks even if non patched libraries are used.With @code{libtcc}, you can use TCC as a backend for dynamic codegeneration (@pxref{Libtcc}).@node Invoke@chapter Command line invocation[This manual documents version @value{VERSION} of the Tiny C Compiler]@section Quick start@example@c man begin SYNOPSISusage: tcc [options] [@var{infile1} @var{infile2}@dots{}] [@option{-run} @var{infile} @var{args}@dots{}]@c man end@end example@noindent@c man begin DESCRIPTIONTCC options are a very much like gcc options. The main difference is that TCCcan also execute directly the resulting program and give it runtimearguments.Here are some examples to understand the logic:@table @code@item @samp{tcc -run a.c}Compile @file{a.c} and execute it directly@item @samp{tcc -run a.c arg1}Compile a.c and execute it directly. arg1 is given as first argument tothe @code{main()} of a.c.@item @samp{tcc a.c -run b.c arg1}Compile @file{a.c} and @file{b.c}, link them together and execute them. arg1 is givenas first argument to the @code{main()} of the resulting program. Becausemultiple C files are specified, @option{--} are necessary to clearly separate theprogram arguments from the TCC options.@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}.@item @samp{tcc -o myprog a.o b.o}link @file{a.o} and @file{b.o} together and generate the executable @file{myprog}.@item @samp{tcc -c a.c}Compile @file{a.c} and generate object file @file{a.o}.@item @samp{tcc -c asmfile.S}Preprocess with C preprocess and assemble @file{asmfile.S} and generateobject file @file{asmfile.o}.@item @samp{tcc -c asmfile.s}Assemble (but not preprocess) @file{asmfile.s} and generate object file@file{asmfile.o}.@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}.@end tableScripting:TCC can be invoked from @emph{scripts}, just as shell scripts. You justneed to add @code{#!/usr/local/bin/tcc -run} at the start of your C source:@example#!/usr/local/bin/tcc -run#include <stdio.h>int main() @{ printf("Hello World\n"); return 0;@}@end example@c man end@section Option summaryGeneral Options:@c man begin OPTIONS@table @option@item -vDisplay current TCC version.@item -cGenerate an object file (@option{-o} option must also be given).@item -o outfilePut object file, executable, or dll into output file @file{outfile}.@item -BdirSet the path where the tcc internal libraries can be found (default is@file{PREFIX/lib/tcc}).@item -benchOutput compilation statistics.@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 ascript, several TCC options can be given @emph{after} the@option{-run} option, separated by spaces. Example:@exampletcc "-run -L/usr/X11R6/lib -lX11" ex4.c@end exampleIn a script, it gives the following header:@example#!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11#include <stdlib.h>int main(int argc, char **argv)@{ ...@}@end example@end tablePreprocessor options:@table @option@item -IdirSpecify an additional include path. Include paths are searched in theorder they are specified.System include paths are always searched after. The default systeminclude paths are: @file{/usr/local/include}, @file{/usr/include}and @file{PREFIX/lib/tcc/include}. (@file{PREFIX} is usually@file{/usr} or @file{/usr/local}).@item -Dsym[=val]Define preprocessor symbol @samp{sym} toval. If val is not present, its value is @samp{1}. Function-like macros canalso be defined: @option{-DF(a)=a+1}@item -UsymUndefine preprocessor symbol @samp{sym}.@end tableWarning options:@table @option@item -wDisable all warnings.@end tableNote: each of the following warning options has a negative form beginning with@option{-Wno-}.@table @option@item -WunsupportedWarn about unsupported GCC features that are ignored by TCC.@item -Wwrite-stringsMake string constants being of type @code{const char *} intead of @code{char*}.@item -WerrorAbort compilation if warnings are issued.@item -Wall Activate all warnings, except @option{-Werror}, @option{-Wunusupported} and@option{-Wwrite-strings} (currently not useful).@end tableLinker options:@table @option@item -LdirSpecify an additional static library path for the @option{-l} option. Thedefault library paths are @file{/usr/local/lib}, @file{/usr/lib} and @file{/lib}.@item -lxxxLink your program with dynamic library libxxx.so or static librarylibxxx.a. The library is searched in the paths specified by the@option{-L} option.@item -sharedGenerate a shared library instead of an executable (@option{-o} optionmust also be given).@item -staticGenerate a statically linked executable (default is a shared linkedexecutable) (@option{-o} option must also be given).@item -rdynamicExport global symbols to the dynamic linker. It is useful when a libraryopened with @code{dlopen()} needs to access executable symbols.@item -rGenerate an object file combining all input files (@option{-o} option mustalso be given).@end tableDebugger options:@table @option@item -gGenerate run time debug information so that you get clear run timeerror messages: @code{ test.c:68: in function 'test5()': dereferencinginvalid pointer} instead of the laconic @code{Segmentationfault}.@item -bGenerate additional support code to checkmemory allocations and array/pointer bounds. @option{-g} is implied. Notethat the generated code is slower and bigger in this case.@item -bt NDisplay N callers in stack traces. This is useful with @option{-g} or@option{-b}.@end tableNote: GCC options @option{-Ox}, @option{-fx} and @option{-mx} areignored.@c man end@ignore@setfilename tcc@settitle Tiny C Compiler@c man begin SEEALSOgcc(1)@c man end@c man begin AUTHORFabrice Bellard@c man end@end ignore@chapter C language support@section ANSI CTCC implements all the ANSI C standard, including structure bit fieldsand floating point numbers (@code{long double}, @code{double}, and@code{float} fully supported).@section ISOC99 extensionsTCC implements many features of the new C standard: ISO C99. Currentlymissing items are: complex and imaginary numbers and variable lengtharrays.Currently implemented ISOC99 features:@itemize@item 64 bit @code{long long} types are fully supported.@item The boolean type @code{_Bool} is supported.@item @code{__func__} is a string variable containing the currentfunction name.@item Variadic macros: @code{__VA_ARGS__} can be used for function-like macros:@example #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)@end example@noindent@code{dprintf} can then be used with a variable number of parameters.@item Declarations can appear anywhere in a block (as in C++).@item Array and struct/union elements can be initialized in any order by using designators:@example struct @{ int x, y; @} st[10] = @{ [0].x = 1, [0].y = 2 @}; int tab[10] = @{ 1, 2, [5] = 5, [9] = 9@};@end example @item Compound initializers are supported:@example int *p = (int [])@{ 1, 2, 3 @};@end exampleto initialize a pointer pointing to an initialized array. The sameworks for structures and strings.@item Hexadecimal floating point constants are supported:@example double d = 0x1234p10;@end example@noindentis the same as writing @example double d = 4771840.0;@end example@item @code{inline} keyword is ignored.@item @code{restrict} keyword is ignored.@end itemize@section GNU C extensionsTCC implements some GNU C extensions:@itemize@item array designators can be used without '=': @example int a[10] = @{ [0] 1, [5] 2, 3, 4 @};@end example@item Structure field designators can be a label: @example struct @{ int x, y; @} st = @{ x: 1, y: 1@};@end exampleinstead of@example struct @{ int x, y; @} st = @{ .x = 1, .y = 1@};@end example@item @code{\e} is ASCII character 27.@item case ranges : ranges can be used in @code{case}s:@example switch(a) @{ case 1 @dots{} 9: printf("range 1 to 9\n"); break; default: printf("unexpected\n"); break; @}@end example@item The keyword @code{__attribute__} is handled to specify variable orfunction attributes. The following attributes are supported: @itemize @item @code{aligned(n)}: align data to n bytes (must be a power of two). @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. @item @code{unused}: specify that the variable or the function is unused. @item @code{cdecl}: use standard C calling convention. @item @code{stdcall}: use Pascal-like calling convention. @end itemizeHere are some examples:@example int a __attribute__ ((aligned(8), section(".mysection")));@end example@noindentalign variable @code{a} to 8 bytes and put it in section @code{.mysection}.@example int my_add(int a, int b) __attribute__ ((section(".mycodesection"))) @{ return a + b; @}@end example@noindentgenerate function @code{my_add} in section @code{.mycodesection}.@item GNU style variadic macros:@example #define dprintf(fmt, args@dots{}) printf(fmt, ## args) dprintf("no arg\n"); dprintf("one arg %d\n", 1);@end example@item @code{__FUNCTION__} is interpreted as C99 @code{__func__} (so it has not exactly the same semantics as string literal GNUCwhere it is a string literal).@item The @code{__alignof__} keyword can be used as @code{sizeof} to get the alignment of a type or an expression.@item The @code{typeof(x)} returns the type of @code{x}. @code{x} is an expression or a type.@item Computed gotos: @code{&&label} returns a pointer of type @code{void *} on the goto label @code{label}. @code{goto *expr} can beused to jump on the pointer resulting from @code{expr}.@item Inline assembly with asm instruction:@cindex inline assembly@cindex assembly, inline@cindex __asm__@examplestatic inline void * my_memcpy(void * to, const void * from, size_t n)@{int d0, d1, d2;__asm__ __volatile__( "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");return (to);@}@end example@noindent@cindex gasTCC includes its own x86 inline assembler with a @code{gas}-like (GNUassembler) syntax. No intermediate files are generated. GCC 3.x namedoperands are supported.@item @code{__builtin_types_compatible_p()} and @code{__builtin_constant_p()} are supported.@end itemize@section TinyCC extensions@itemize@item @code{__TINYC__} is a predefined macro to @code{1} toindicate that you use TCC.@item @code{#!} at the start of a line is ignored to allow scripting.@item Binary digits can be entered (@code{0b101} instead of@code{5}).@item @code{__BOUNDS_CHECKING_ON} is defined if bound checking is activated.@end itemize@chapter TinyCC AssemblerSince version 0.9.16, TinyCC integrates its own assembler. TinyCCassembler supports a gas-like syntax (GNU assembler). You candesactivate assembler support if you want a smaller TinyCC executable(the C compiler does not rely on the assembler).TinyCC Assembler is used to handle files with @file{.S} (Cpreprocessed assembler) and @file{.s} extensions. It is also used tohandle the GNU inline assembler with the @code{asm} keyword.@section SyntaxTinyCC Assembler supports most of the gas syntax. The tokens are thesame 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@item A value is either an absolute number or a label plus an offset. All operators accept absolute values except '+' and '-'. '+' or '-' can beused to add an offset to a label. '-' supports two labels only if theyare 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.@item Numeric labels can be used as local @code{gas}-like labels. They can be defined several times in the same source. Use 'b'(backward) or 'f' (forward) as suffix to reference them:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -