📄 tcc-doc.html
字号:
<HTML><HEAD><!-- Created by texi2html 1.56k from tcc-doc.texi on 8 November 2004 --><TITLE>Tiny C Compiler Reference Documentation</TITLE></HEAD><BODY><H1>Tiny C Compiler Reference Documentation</H1><P><P><HR><P><H1>Table of Contents</H1><UL><LI><A NAME="TOC1" HREF="tcc-doc.html#SEC1">1. Introduction</A><LI><A NAME="TOC2" HREF="tcc-doc.html#SEC2">2. Command line invocation</A><UL><LI><A NAME="TOC3" HREF="tcc-doc.html#SEC3">2.1 Quick start</A><LI><A NAME="TOC4" HREF="tcc-doc.html#SEC4">2.2 Option summary</A></UL><LI><A NAME="TOC5" HREF="tcc-doc.html#SEC5">3. C language support</A><UL><LI><A NAME="TOC6" HREF="tcc-doc.html#SEC6">3.1 ANSI C</A><LI><A NAME="TOC7" HREF="tcc-doc.html#SEC7">3.2 ISOC99 extensions</A><LI><A NAME="TOC8" HREF="tcc-doc.html#SEC8">3.3 GNU C extensions</A><LI><A NAME="TOC9" HREF="tcc-doc.html#SEC9">3.4 TinyCC extensions</A></UL><LI><A NAME="TOC10" HREF="tcc-doc.html#SEC10">4. TinyCC Assembler</A><UL><LI><A NAME="TOC11" HREF="tcc-doc.html#SEC11">4.1 Syntax</A><LI><A NAME="TOC12" HREF="tcc-doc.html#SEC12">4.2 Expressions</A><LI><A NAME="TOC13" HREF="tcc-doc.html#SEC13">4.3 Labels</A><LI><A NAME="TOC14" HREF="tcc-doc.html#SEC14">4.4 Directives</A><LI><A NAME="TOC15" HREF="tcc-doc.html#SEC15">4.5 X86 Assembler</A></UL><LI><A NAME="TOC16" HREF="tcc-doc.html#SEC16">5. TinyCC Linker</A><UL><LI><A NAME="TOC17" HREF="tcc-doc.html#SEC17">5.1 ELF file generation</A><LI><A NAME="TOC18" HREF="tcc-doc.html#SEC18">5.2 ELF file loader</A><LI><A NAME="TOC19" HREF="tcc-doc.html#SEC19">5.3 GNU Linker Scripts</A></UL><LI><A NAME="TOC20" HREF="tcc-doc.html#SEC20">6. TinyCC Memory and Bound checks</A><LI><A NAME="TOC21" HREF="tcc-doc.html#SEC21">7. The <CODE>libtcc</CODE> library</A><LI><A NAME="TOC22" HREF="tcc-doc.html#SEC22">8. Developer's guide</A><UL><LI><A NAME="TOC23" HREF="tcc-doc.html#SEC23">8.1 File reading</A><LI><A NAME="TOC24" HREF="tcc-doc.html#SEC24">8.2 Lexer</A><LI><A NAME="TOC25" HREF="tcc-doc.html#SEC25">8.3 Parser</A><LI><A NAME="TOC26" HREF="tcc-doc.html#SEC26">8.4 Types</A><LI><A NAME="TOC27" HREF="tcc-doc.html#SEC27">8.5 Symbols</A><LI><A NAME="TOC28" HREF="tcc-doc.html#SEC28">8.6 Sections</A><LI><A NAME="TOC29" HREF="tcc-doc.html#SEC29">8.7 Code generation</A><UL><LI><A NAME="TOC30" HREF="tcc-doc.html#SEC30">8.7.1 Introduction</A><LI><A NAME="TOC31" HREF="tcc-doc.html#SEC31">8.7.2 The value stack</A><LI><A NAME="TOC32" HREF="tcc-doc.html#SEC32">8.7.3 Manipulating the value stack</A><LI><A NAME="TOC33" HREF="tcc-doc.html#SEC33">8.7.4 CPU dependent code generation</A></UL><LI><A NAME="TOC34" HREF="tcc-doc.html#SEC34">8.8 Optimizations done</A></UL><LI><A NAME="TOC35" HREF="tcc-doc.html#SEC35">Concept Index</A></UL><P><HR><P><H1><A NAME="SEC1" HREF="tcc-doc.html#TOC1">1. Introduction</A></H1><P>TinyCC (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.<P>TCC compiles so <EM>fast</EM> that even for big projects <CODE>Makefile</CODE>s maynot be necessary. <P>TCC not only supports ANSI C, but also most of the new ISO C99standard and many GNUC extensions including inline assembly.<P>TCC can also be used to make <EM>C scripts</EM>, 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.<P>TCC can also automatically generate memory and bound checks(see section <A HREF="tcc-doc.html#SEC20">6. TinyCC Memory and Bound checks</A>) while allowing all C pointers operations. TCC can dothese checks even if non patched libraries are used.<P>With <CODE>libtcc</CODE>, you can use TCC as a backend for dynamic codegeneration (see section <A HREF="tcc-doc.html#SEC21">7. The <CODE>libtcc</CODE> library</A>).<P>TCC mainly supports the i386 target. There are alpha ports for the ARM(<CODE>arm-tcc</CODE>) and the TMS320C67xx targets (<CODE>c67-tcc</CODE>). Moreinformation about the ARM port is available at<A HREF="http://lists.gnu.org/archive/html/tinycc-devel/2003-10/msg00044.html">http://lists.gnu.org/archive/html/tinycc-devel/2003-10/msg00044.html</A>.<H1><A NAME="SEC2" HREF="tcc-doc.html#TOC2">2. Command line invocation</A></H1><P>[This manual documents version 0.9.22 of the Tiny C Compiler]<H2><A NAME="SEC3" HREF="tcc-doc.html#TOC3">2.1 Quick start</A></H2><PRE>usage: tcc [options] [<VAR>infile1</VAR> <VAR>infile2</VAR>...] [<SAMP>`-run'</SAMP> <VAR>infile</VAR> <VAR>args</VAR>...]</PRE><P>TCC options are a very much like gcc options. The main difference is that TCCcan also execute directly the resulting program and give it runtimearguments.<P>Here are some examples to understand the logic:<DL COMPACT><DT><CODE><SAMP>`tcc -run a.c'</SAMP></CODE><DD>Compile <TT>`a.c'</TT> and execute it directly<DT><CODE><SAMP>`tcc -run a.c arg1'</SAMP></CODE><DD>Compile a.c and execute it directly. arg1 is given as first argument tothe <CODE>main()</CODE> of a.c.<DT><CODE><SAMP>`tcc a.c -run b.c arg1'</SAMP></CODE><DD>Compile <TT>`a.c'</TT> and <TT>`b.c'</TT>, link them together and execute them. arg1 is givenas first argument to the <CODE>main()</CODE> of the resulting program. Becausemultiple C files are specified, <SAMP>`--'</SAMP> are necessary to clearly separate theprogram arguments from the TCC options.<DT><CODE><SAMP>`tcc -o myprog a.c b.c'</SAMP></CODE><DD>Compile <TT>`a.c'</TT> and <TT>`b.c'</TT>, link them and generate the executable <TT>`myprog'</TT>.<DT><CODE><SAMP>`tcc -o myprog a.o b.o'</SAMP></CODE><DD>link <TT>`a.o'</TT> and <TT>`b.o'</TT> together and generate the executable <TT>`myprog'</TT>.<DT><CODE><SAMP>`tcc -c a.c'</SAMP></CODE><DD>Compile <TT>`a.c'</TT> and generate object file <TT>`a.o'</TT>.<DT><CODE><SAMP>`tcc -c asmfile.S'</SAMP></CODE><DD>Preprocess with C preprocess and assemble <TT>`asmfile.S'</TT> and generateobject file <TT>`asmfile.o'</TT>.<DT><CODE><SAMP>`tcc -c asmfile.s'</SAMP></CODE><DD>Assemble (but not preprocess) <TT>`asmfile.s'</TT> and generate object file<TT>`asmfile.o'</TT>.<DT><CODE><SAMP>`tcc -r -o ab.o a.c b.c'</SAMP></CODE><DD>Compile <TT>`a.c'</TT> and <TT>`b.c'</TT>, link them together and generate the object file <TT>`ab.o'</TT>.</DL><P>Scripting:<P>TCC can be invoked from <EM>scripts</EM>, just as shell scripts. You justneed to add <CODE>#!/usr/local/bin/tcc -run</CODE> at the start of your C source:<PRE>#!/usr/local/bin/tcc -run#include <stdio.h>int main() { printf("Hello World\n"); return 0;}</PRE><H2><A NAME="SEC4" HREF="tcc-doc.html#TOC4">2.2 Option summary</A></H2><P>General Options:<DL COMPACT><DT><SAMP>`-v'</SAMP><DD>Display current TCC version.<DT><SAMP>`-c'</SAMP><DD>Generate an object file (<SAMP>`-o'</SAMP> option must also be given).<DT><SAMP>`-o outfile'</SAMP><DD>Put object file, executable, or dll into output file <TT>`outfile'</TT>.<DT><SAMP>`-Bdir'</SAMP><DD>Set the path where the tcc internal libraries can be found (default is<TT>`PREFIX/lib/tcc'</TT>).<DT><SAMP>`-bench'</SAMP><DD>Output compilation statistics.<DT><SAMP>`-run source [args...]'</SAMP><DD>Compile file <VAR>source</VAR> and run it with the command line arguments<VAR>args</VAR>. In order to be able to give more than one argument to ascript, several TCC options can be given <EM>after</EM> the<SAMP>`-run'</SAMP> option, separated by spaces. Example:<PRE>tcc "-run -L/usr/X11R6/lib -lX11" ex4.c</PRE>In a script, it gives the following header:<PRE>#!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11#include <stdlib.h>int main(int argc, char **argv){ ...}</PRE></DL><P>Preprocessor options:<DL COMPACT><DT><SAMP>`-Idir'</SAMP><DD>Specify 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: <TT>`/usr/local/include'</TT>, <TT>`/usr/include'</TT>and <TT>`PREFIX/lib/tcc/include'</TT>. (<TT>`PREFIX'</TT> is usually<TT>`/usr'</TT> or <TT>`/usr/local'</TT>).<DT><SAMP>`-Dsym[=val]'</SAMP><DD>Define preprocessor symbol <SAMP>`sym'</SAMP> toval. If val is not present, its value is <SAMP>`1'</SAMP>. Function-like macros canalso be defined: <SAMP>`-DF(a)=a+1'</SAMP><DT><SAMP>`-Usym'</SAMP><DD>Undefine preprocessor symbol <SAMP>`sym'</SAMP>.</DL><P>Compilation flags:<P>Note: each of the following warning options has a negative form beginning with<SAMP>`-fno-'</SAMP>.<DL COMPACT><DT><SAMP>`-funsigned-char'</SAMP><DD>Let the <CODE>char</CODE> type be unsigned.<DT><SAMP>`-fsigned-char'</SAMP><DD>Let the <CODE>char</CODE> type be signed.<DT><SAMP>`-fno-common'</SAMP><DD>Do not generate common symbols for uninitialized data.</DL><P>Warning options:<DL COMPACT><DT><SAMP>`-w'</SAMP><DD>Disable all warnings.</DL><P>Note: each of the following warning options has a negative form beginning with<SAMP>`-Wno-'</SAMP>.<DL COMPACT><DT><SAMP>`-Wimplicit-function-declaration'</SAMP><DD>Warn about implicit function declaration.<DT><SAMP>`-Wunsupported'</SAMP><DD>Warn about unsupported GCC features that are ignored by TCC.<DT><SAMP>`-Wwrite-strings'</SAMP><DD>Make string constants be of type <CODE>const char *</CODE> instead of <CODE>char*</CODE>.<DT><SAMP>`-Werror'</SAMP><DD>Abort compilation if warnings are issued.<DT><SAMP>`-Wall'</SAMP><DD>Activate all warnings, except <SAMP>`-Werror'</SAMP>, <SAMP>`-Wunusupported'</SAMP> and<SAMP>`-Wwrite-strings'</SAMP>.</DL><P>Linker options:<DL COMPACT><DT><SAMP>`-Ldir'</SAMP><DD>Specify an additional static library path for the <SAMP>`-l'</SAMP> option. Thedefault library paths are <TT>`/usr/local/lib'</TT>, <TT>`/usr/lib'</TT> and <TT>`/lib'</TT>.<DT><SAMP>`-lxxx'</SAMP><DD>Link your program with dynamic library libxxx.so or static librarylibxxx.a. The library is searched in the paths specified by the<SAMP>`-L'</SAMP> option.<DT><SAMP>`-shared'</SAMP><DD>Generate a shared library instead of an executable (<SAMP>`-o'</SAMP> optionmust also be given).<DT><SAMP>`-static'</SAMP><DD>Generate a statically linked executable (default is a shared linkedexecutable) (<SAMP>`-o'</SAMP> option must also be given).<DT><SAMP>`-rdynamic'</SAMP><DD>Export global symbols to the dynamic linker. It is useful when a libraryopened with <CODE>dlopen()</CODE> needs to access executable symbols.<DT><SAMP>`-r'</SAMP><DD>Generate an object file combining all input files (<SAMP>`-o'</SAMP> option mustalso be given).<DT><SAMP>`-Wl,-Ttext,address'</SAMP><DD>Set the start of the .text section to <VAR>address</VAR>.<DT><SAMP>`-Wl,--oformat,fmt'</SAMP><DD>Use <VAR>fmt</VAR> as output format. The supported output formats are:<DL COMPACT><DT><CODE>elf32-i386</CODE><DD>ELF output format (default)<DT><CODE>binary</CODE><DD>Binary image (only for executable output)<DT><CODE>coff</CODE><DD>COFF output format (only for executable output for TMS320C67xx target)</DL></DL><P>Debugger options:<DL COMPACT><DT><SAMP>`-g'</SAMP><DD>Generate run time debug information so that you get clear run timeerror messages: <CODE> test.c:68: in function 'test5()': dereferencinginvalid pointer</CODE> instead of the laconic <CODE>Segmentationfault</CODE>.<DT><SAMP>`-b'</SAMP><DD>Generate additional support code to checkmemory allocations and array/pointer bounds. <SAMP>`-g'</SAMP> is implied. Notethat the generated code is slower and bigger in this case.<DT><SAMP>`-bt N'</SAMP><DD>Display N callers in stack traces. This is useful with <SAMP>`-g'</SAMP> or<SAMP>`-b'</SAMP>.</DL><P>Note: GCC options <SAMP>`-Ox'</SAMP>, <SAMP>`-fx'</SAMP> and <SAMP>`-mx'</SAMP> areignored.<H1><A NAME="SEC5" HREF="tcc-doc.html#TOC5">3. C language support</A></H1><H2><A NAME="SEC6" HREF="tcc-doc.html#TOC6">3.1 ANSI C</A></H2><P>TCC implements all the ANSI C standard, including structure bit fieldsand floating point numbers (<CODE>long double</CODE>, <CODE>double</CODE>, and<CODE>float</CODE> fully supported).<H2><A NAME="SEC7" HREF="tcc-doc.html#TOC7">3.2 ISOC99 extensions</A></H2><P>TCC implements many features of the new C standard: ISO C99. Currentlymissing items are: complex and imaginary numbers and variable lengtharrays.<P>Currently implemented ISOC99 features:<UL><LI>64 bit <CODE>long long</CODE> types are fully supported.<LI>The boolean type <CODE>_Bool</CODE> is supported.<LI><CODE>__func__</CODE> is a string variable containing the currentfunction name.<LI>Variadic macros: <CODE>__VA_ARGS__</CODE> can be used for function-like macros:<PRE> #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)</PRE><CODE>dprintf</CODE> can then be used with a variable number of parameters.<LI>Declarations can appear anywhere in a block (as in C++).<LI>Array and struct/union elements can be initialized in any order by using designators:<PRE> struct { int x, y; } st[10] = { [0].x = 1, [0].y = 2 }; int tab[10] = { 1, 2, [5] = 5, [9] = 9};</PRE> <LI>Compound initializers are supported:<PRE> int *p = (int []){ 1, 2, 3 };</PRE>to initialize a pointer pointing to an initialized array. The sameworks for structures and strings.<LI>Hexadecimal floating point constants are supported:<PRE> double d = 0x1234p10;</PRE>is the same as writing <PRE> double d = 4771840.0;</PRE><LI><CODE>inline</CODE> keyword is ignored.<LI><CODE>restrict</CODE> keyword is ignored.</UL><H2><A NAME="SEC8" HREF="tcc-doc.html#TOC8">3.3 GNU C extensions</A></H2><P>TCC implements some GNU C extensions:<UL><LI>array designators can be used without '=':<PRE> int a[10] = { [0] 1, [5] 2, 3, 4 };</PRE><LI>Structure field designators can be a label:<PRE> struct { int x, y; } st = { x: 1, y: 1};</PRE>instead of<PRE> struct { int x, y; } st = { .x = 1, .y = 1};</PRE><LI><CODE>\e</CODE> is ASCII character 27.<LI>case ranges : ranges can be used in <CODE>case</CODE>s:<PRE> switch(a) { case 1 ... 9: printf("range 1 to 9\n"); break; default:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -