Unified · Split
| @@ -1,16 +1,48 @@ | |||
| 1 | \input texinfo @c -*- texinfo -*- | 1 | \input texinfo @c -*- texinfo -*- |
| 2 | 2 | @c %**start of header | |
| 3 | @setfilename tcc-doc.info | ||
| 3 | @settitle Tiny C Compiler Reference Documentation | 4 | @settitle Tiny C Compiler Reference Documentation |
| 5 | @c %**end of header | ||
| 6 | |||
| 7 | @include version.texi | ||
| 8 | |||
| 9 | @ifinfo | ||
| 10 | Bla bla bla | ||
| 11 | @end ifinfo | ||
| 12 | |||
| 13 | @iftex | ||
| 4 | @titlepage | 14 | @titlepage |
| 15 | @afourpaper | ||
| 5 | @sp 7 | 16 | @sp 7 |
| 6 | @center @titlefont{Tiny C Compiler Reference Documentation} | 17 | @center @titlefont{Tiny C Compiler Reference Documentation} |
| 7 | @sp 3 | 18 | @sp 3 |
| 8 | @end titlepage | 19 | @end titlepage |
| 20 | @headings double | ||
| 21 | @end iftex | ||
| 22 | |||
| 23 | @ifnothtml | ||
| 24 | @contents | ||
| 25 | @end ifnothtml | ||
| 26 | |||
| 27 | @ifnottex | ||
| 28 | @node Top, Introduction, (dir), (dir) | ||
| 29 | @top Tiny C Compiler Reference Documentation | ||
| 30 | |||
| 31 | This manual documents version @value{VERSION} of the Tiny C Compiler. | ||
| 9 | 32 | ||
| 33 | @menu | ||
| 34 | * Introduction:: Introduction to tcc. | ||
| 35 | * Invoke:: Invocation of tcc (command line, options). | ||
| 36 | * Bounds:: Automatic bounds-checking of C code. | ||
| 37 | * Libtcc:: bla bla bla. | ||
| 38 | @end menu | ||
| 39 | @end ifnottex | ||
| 40 | |||
| 41 | @node Introduction | ||
| 10 | @chapter Introduction | 42 | @chapter Introduction |
| 11 | 43 | ||
| 12 | TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C | 44 | TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C |
| 13 | compilers, it is meant to be self-suffisant: you do not need an | 45 | compilers, it is meant to be self-relying: you do not need an |
| 14 | external assembler or linker because TCC does that for you. | 46 | external assembler or linker because TCC does that for you. |
| 15 | 47 | ||
| 16 | TCC compiles so @emph{fast} that even for big projects @code{Makefile}s may | 48 | TCC compiles so @emph{fast} that even for big projects @code{Makefile}s may |
| @@ -24,13 +56,13 @@ that you run as a Perl or Python script. Compilation is so fast that · 7 unmodified lines | |||
| 24 | your script will be as fast as if it was an executable. | 56 | your script will be as fast as if it was an executable. |
| 25 | 57 | ||
| 26 | TCC can also automatically generate memory and bound checks | 58 | TCC can also automatically generate memory and bound checks |
| 27 | (@xref{bounds}) while allowing all C pointers operations. TCC can do | 59 | (@pxref{Bounds}) while allowing all C pointers operations. TCC can do |
| 28 | these checks even if non patched libraries are used. | 60 | these checks even if non patched libraries are used. |
| 29 | 61 | ||
| 30 | With @code{libtcc}, you can use TCC as a backend for dynamic code | 62 | With @code{libtcc}, you can use TCC as a backend for dynamic code |
| 31 | generation (@xref{libtcc}). | 63 | generation (@pxref{Libtcc}). |
| 32 | 64 | ||
| 33 | @node invoke | 65 | @node Invoke |
| 34 | @chapter Command line invocation | 66 | @chapter Command line invocation |
| 35 | 67 | ||
| 36 | @section Quick start | 68 | @section Quick start |
| @@ -41,45 +73,46 @@ usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym] · 4 unmodified lines | |||
| 41 | [--] infile1 [infile2... --] [infile_args...] | 73 | [--] infile1 [infile2... --] [infile_args...] |
| 42 | @end example | 74 | @end example |
| 43 | 75 | ||
| 44 | TCC options are a very much like gcc. The main difference is that TCC | 76 | @noindent |
| 77 | TCC options are a very much like gcc options. The main difference is that TCC | ||
| 45 | can also execute directly the resulting program and give it runtime | 78 | can also execute directly the resulting program and give it runtime |
| 46 | arguments. | 79 | arguments. |
| 47 | 80 | ||
| 48 | Here are some examples to understand the logic: | 81 | Here are some examples to understand the logic: |
| 49 | 82 | ||
| 50 | @table @code | 83 | @table @code |
| 51 | @item tcc a.c | 84 | @item @samp{tcc a.c} |
| 52 | Compile a.c and execute it directly | 85 | Compile @file{a.c} and execute it directly |
| 53 | 86 | ||
| 54 | @item tcc a.c arg1 | 87 | @item @samp{tcc a.c arg1} |
| 55 | Compile a.c and execute it directly. arg1 is given as first argument to | 88 | Compile a.c and execute it directly. arg1 is given as first argument to |
| 56 | the @code{main()} of a.c. | 89 | the @code{main()} of a.c. |
| 57 | 90 | ||
| 58 | @item tcc -- a.c b.c -- arg1 | 91 | @item @samp{tcc -- a.c b.c -- arg1} |
| 59 | Compile a.c and b.c, link them together and execute them. arg1 is given | 92 | Compile @file{a.c} and @file{b.c}, link them together and execute them. arg1 is given |
| 60 | as first argument to the @code{main()} of the resulting program. Because | 93 | as first argument to the @code{main()} of the resulting program. Because |
| 61 | multiple C files are specified, @code{--} are necessary to clearly separate the | 94 | multiple C files are specified, @option{--} are necessary to clearly separate the |
| 62 | program arguments from the TCC options. | 95 | program arguments from the TCC options. |
| 63 | 96 | ||
| 64 | @item tcc -o myprog a.c b.c | 97 | @item @samp{tcc -o myprog a.c b.c} |
| 65 | Compile a.c and b.c, link them and generate the executable myprog. | 98 | Compile @file{a.c} and @file{b.c}, link them and generate the executable @file{myprog}. |
| 66 | 99 | ||
| 67 | @item tcc -o myprog a.o b.o | 100 | @item @samp{tcc -o myprog a.o b.o} |
| 68 | link a.o and b.o together and generate the executable myprog. | 101 | link @file{a.o} and @file{b.o} together and generate the executable @file{myprog}. |
| 69 | 102 | ||
| 70 | @item tcc -c a.c | 103 | @item @samp{tcc -c a.c} |
| 71 | Compile a.c and generate object file a.o | 104 | Compile @file{a.c} and generate object file @file{a.o}. |
| 72 | 105 | ||
| 73 | @item tcc -c asmfile.S | 106 | @item @samp{tcc -c asmfile.S} |
| 74 | Preprocess with C preprocess and assemble asmfile.S and generate | 107 | Preprocess with C preprocess and assemble @file{asmfile.S} and generate |
| 75 | object file asmfile.o. | 108 | object file @file{asmfile.o}. |
| 76 | 109 | ||
| 77 | @item tcc -c asmfile.s | 110 | @item @samp{tcc -c asmfile.s} |
| 78 | Assemble (but not preprocess) asmfile.s and generate object file | 111 | Assemble (but not preprocess) @file{asmfile.s} and generate object file |
| 79 | asmfile.o. | 112 | @file{asmfile.o}. |
| 80 | 113 | ||
| 81 | @item tcc -r -o ab.o a.c b.c | 114 | @item @samp{tcc -r -o ab.o a.c b.c} |
| 82 | Compile a.c and b.c, link them together and generate the object file ab.o. | 115 | Compile @file{a.c} and @file{b.c}, link them together and generate the object file @file{ab.o}. |
| 83 | 116 | ||
| 84 | @end table | 117 | @end table |
| 85 | 118 | ||
| @@ -93,19 +126,19 @@ need to add @code{#!/usr/local/bin/tcc} at the start of your C source: · 7 unmodified lines | |||
| 93 | #include <stdio.h> | 126 | #include <stdio.h> |
| 94 | 127 | ||
| 95 | int main() | 128 | int main() |
| 96 | { | 129 | @{ |
| 97 | printf("Hello World\n"); | 130 | printf("Hello World\n"); |
| 98 | return 0; | 131 | return 0; |
| 99 | } | 132 | @} |
| 100 | @end example | 133 | @end example |
| 101 | 134 | ||
| 102 | @section Option summary | 135 | @section Option summary |
| 103 | 136 | ||
| 104 | General Options: | 137 | General Options: |
| 105 | 138 | ||
| 106 | @table @samp | 139 | @table @option |
| 107 | @item -c | 140 | @item -c |
| 108 | Generate an object file (@samp{-o} option must also be given). | 141 | Generate an object file (@option{-o} option must also be given). |
| 109 | 142 | ||
| 110 | @item -o outfile | 143 | @item -o outfile |
| 111 | Put object file, executable, or dll into output file @file{outfile}. | 144 | Put object file, executable, or dll into output file @file{outfile}. |
| @@ -120,54 +153,54 @@ Output compilation statistics. · 8 unmodified lines | |||
| 120 | 153 | ||
| 121 | Preprocessor options: | 154 | Preprocessor options: |
| 122 | 155 | ||
| 123 | @table @samp | 156 | @table @option |
| 124 | @item -Idir | 157 | @item -Idir |
| 125 | Specify an additionnal include path. Include paths are searched in the | 158 | Specify an additional include path. Include paths are searched in the |
| 126 | order they are specified. | 159 | order they are specified. |
| 127 | 160 | ||
| 128 | System include paths are always searched after. The default system | 161 | System include paths are always searched after. The default system |
| 129 | include paths are: @file{/usr/local/include}, @file{/usr/include} | 162 | include paths are: @file{/usr/local/include}, @file{/usr/include} |
| 130 | and @file{PREFIX/lib/tcc/include}. (@code{PREFIX} is usually | 163 | and @file{PREFIX/lib/tcc/include}. (@file{PREFIX} is usually |
| 131 | @file{/usr} or @file{/usr/local}). | 164 | @file{/usr} or @file{/usr/local}). |
| 132 | 165 | ||
| 133 | @item -Dsym[=val] | 166 | @item -Dsym[=val] |
| 134 | Define preprocessor symbol 'sym' to | 167 | Define preprocessor symbol @samp{sym} to |
| 135 | val. If val is not present, its value is '1'. Function-like macros can | 168 | val. If val is not present, its value is @samp{1}. Function-like macros can |
| 136 | also be defined: @code{'-DF(a)=a+1'} | 169 | also be defined: @option{-DF(a)=a+1} |
| 137 | 170 | ||
| 138 | @item -Usym | 171 | @item -Usym |
| 139 | Undefine preprocessor symbol 'sym'. | 172 | Undefine preprocessor symbol @samp{sym}. |
| 140 | @end table | 173 | @end table |
| 141 | 174 | ||
| 142 | Linker options: | 175 | Linker options: |
| 143 | 176 | ||
| 144 | @table @samp | 177 | @table @option |
| 145 | @item -Ldir | 178 | @item -Ldir |
| 146 | Specify an additionnal static library path for the @samp{-l} option. The | 179 | Specify an additional static library path for the @option{-l} option. The |
| 147 | default library paths are @file{/usr/local/lib}, @file{/usr/lib} and @file{/lib}. | 180 | default library paths are @file{/usr/local/lib}, @file{/usr/lib} and @file{/lib}. |
| 148 | 181 | ||
| 149 | @item -lxxx | 182 | @item -lxxx |
| 150 | Link your program with dynamic library libxxx.so or static library | 183 | Link your program with dynamic library libxxx.so or static library |
| 151 | libxxx.a. The library is searched in the paths specified by the | 184 | libxxx.a. The library is searched in the paths specified by the |
| 152 | @samp{-L} option. | 185 | @option{-L} option. |
| 153 | 186 | ||
| 154 | @item -shared | 187 | @item -shared |
| 155 | Generate a shared library instead of an executable (@samp{-o} option | 188 | Generate a shared library instead of an executable (@option{-o} option |
| 156 | must also be given). | 189 | must also be given). |
| 157 | 190 | ||
| 158 | @item -static | 191 | @item -static |
| 159 | Generate a statically linked executable (default is a shared linked | 192 | Generate a statically linked executable (default is a shared linked |
| 160 | executable) (@samp{-o} option must also be given). | 193 | executable) (@option{-o} option must also be given). |
| 161 | 194 | ||
| 162 | @item -r | 195 | @item -r |
| 163 | Generate an object file combining all input files (@samp{-o} option must | 196 | Generate an object file combining all input files (@option{-o} option must |
| 164 | also be given). | 197 | also be given). |
| 165 | 198 | ||
| 166 | @end table | 199 | @end table |
| 167 | 200 | ||
| 168 | Debugger options: | 201 | Debugger options: |
| 169 | 202 | ||
| 170 | @table @samp | 203 | @table @option |
| 171 | @item -g | 204 | @item -g |
| 172 | Generate run time debug information so that you get clear run time | 205 | Generate run time debug information so that you get clear run time |
| 173 | error messages: @code{ test.c:68: in function 'test5()': dereferencing | 206 | error messages: @code{ test.c:68: in function 'test5()': dereferencing |
| @@ -175,17 +208,17 @@ invalid pointer} instead of the laconic @code{Segmentation · 1 unmodified line | |||
| 175 | fault}. | 208 | fault}. |
| 176 | 209 | ||
| 177 | @item -b | 210 | @item -b |
| 178 | Generate additionnal support code to check | 211 | Generate additional support code to check |
| 179 | memory allocations and array/pointer bounds. @samp{-g} is implied. Note | 212 | memory allocations and array/pointer bounds. @option{-g} is implied. Note |
| 180 | that the generated code is slower and bigger in this case. | 213 | that the generated code is slower and bigger in this case. |
| 181 | 214 | ||
| 182 | @item -bt N | 215 | @item -bt N |
| 183 | Display N callers in stack traces. This is useful with @samp{-g} or | 216 | Display N callers in stack traces. This is useful with @option{-g} or |
| 184 | @samp{-b}. | 217 | @option{-b}. |
| 185 | 218 | ||
| 186 | @end table | 219 | @end table |
| 187 | 220 | ||
| 188 | Note: GCC options @samp{-Ox}, @samp{-Wx}, @samp{-fx} and @samp{-mx} are | 221 | Note: GCC options @option{-Ox}, @option{-Wx}, @option{-fx} and @option{-mx} are |
| 189 | ignored. | 222 | ignored. |
| 190 | 223 | ||
| 191 | @chapter C language support | 224 | @chapter C language support |
| @@ -206,11 +239,11 @@ Currently implemented ISOC99 features: · 14 unmodified lines | |||
| 206 | 239 | ||
| 207 | @itemize | 240 | @itemize |
| 208 | 241 | ||
| 209 | @item 64 bit @code{'long long'} types are fully supported. | 242 | @item 64 bit @code{long long} types are fully supported. |
| 210 | 243 | ||
| 211 | @item The boolean type @code{'_Bool'} is supported. | 244 | @item The boolean type @code{_Bool} is supported. |
| 212 | 245 | ||
| 213 | @item @code{'__func__'} is a string variable containing the current | 246 | @item @code{__func__} is a string variable containing the current |
| 214 | function name. | 247 | function name. |
| 215 | 248 | ||
| 216 | @item Variadic macros: @code{__VA_ARGS__} can be used for | 249 | @item Variadic macros: @code{__VA_ARGS__} can be used for |
| @@ -218,6 +251,8 @@ function name. · 1 unmodified line | |||
| 218 | @example | 251 | @example |
| 219 | #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__) | 252 | #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__) |
| 220 | @end example | 253 | @end example |
| 254 | |||
| 255 | @noindent | ||
| 221 | @code{dprintf} can then be used with a variable number of parameters. | 256 | @code{dprintf} can then be used with a variable number of parameters. |
| 222 | 257 | ||
| 223 | @item Declarations can appear anywhere in a block (as in C++). | 258 | @item Declarations can appear anywhere in a block (as in C++). |
| @@ -225,14 +260,14 @@ function name. · 1 unmodified line | |||
| 225 | @item Array and struct/union elements can be initialized in any order by | 260 | @item Array and struct/union elements can be initialized in any order by |
| 226 | using designators: | 261 | using designators: |
| 227 | @example | 262 | @example |
| 228 | struct { int x, y; } st[10] = { [0].x = 1, [0].y = 2 }; | 263 | struct @{ int x, y; @} st[10] = @{ [0].x = 1, [0].y = 2 @}; |
| 229 | 264 | ||
| 230 | int tab[10] = { 1, 2, [5] = 5, [9] = 9}; | 265 | int tab[10] = @{ 1, 2, [5] = 5, [9] = 9@}; |
| 231 | @end example | 266 | @end example |
| 232 | 267 | ||
| 233 | @item Compound initializers are supported: | 268 | @item Compound initializers are supported: |
| 234 | @example | 269 | @example |
| 235 | int *p = (int []){ 1, 2, 3 }; | 270 | int *p = (int [])@{ 1, 2, 3 @}; |
| 236 | @end example | 271 | @end example |
| 237 | to initialize a pointer pointing to an initialized array. The same | 272 | to initialize a pointer pointing to an initialized array. The same |
| 238 | works for structures and strings. | 273 | works for structures and strings. |
| @@ -241,14 +276,16 @@ works for structures and strings. · 2 unmodified lines | |||
| 241 | @example | 276 | @example |
| 242 | double d = 0x1234p10; | 277 | double d = 0x1234p10; |
| 243 | @end example | 278 | @end example |
| 279 | |||
| 280 | @noindent | ||
| 244 | is the same as writing | 281 | is the same as writing |
| 245 | @example | 282 | @example |
| 246 | double d = 4771840.0; | 283 | double d = 4771840.0; |
| 247 | @end example | 284 | @end example |
| 248 | 285 | ||
| 249 | @item @code{'inline'} keyword is ignored. | 286 | @item @code{inline} keyword is ignored. |
| 250 | 287 | ||
| 251 | @item @code{'restrict'} keyword is ignored. | 288 | @item @code{restrict} keyword is ignored. |
| 252 | @end itemize | 289 | @end itemize |
| 253 | 290 | ||
| 254 | @section GNU C extensions | 291 | @section GNU C extensions |
| @@ -259,30 +296,30 @@ TCC implements some GNU C extensions: · 4 unmodified lines | |||
| 259 | 296 | ||
| 260 | @item array designators can be used without '=': | 297 | @item array designators can be used without '=': |
| 261 | @example | 298 | @example |
| 262 | int a[10] = { [0] 1, [5] 2, 3, 4 }; | 299 | int a[10] = @{ [0] 1, [5] 2, 3, 4 @}; |
| 263 | @end example | 300 | @end example |
| 264 | 301 | ||
| 265 | @item Structure field designators can be a label: | 302 | @item Structure field designators can be a label: |
| 266 | @example | 303 | @example |
| 267 | struct { int x, y; } st = { x: 1, y: 1}; | 304 | struct @{ int x, y; @} st = @{ x: 1, y: 1@}; |
| 268 | @end example | 305 | @end example |
| 269 | instead of | 306 | instead of |
| 270 | @example | 307 | @example |
| 271 | struct { int x, y; } st = { .x = 1, .y = 1}; | 308 | struct @{ int x, y; @} st = @{ .x = 1, .y = 1@}; |
| 272 | @end example | 309 | @end example |
| 273 | 310 | ||
| 274 | @item @code{'\e'} is ASCII character 27. | 311 | @item @code{\e} is ASCII character 27. |
| 275 | 312 | ||
| 276 | @item case ranges : ranges can be used in @code{case}s: | 313 | @item case ranges : ranges can be used in @code{case}s: |
| 277 | @example | 314 | @example |
| 278 | switch(a) { | 315 | switch(a) @{ |
| 279 | case 1 ... 9: | 316 | case 1 @dots{} 9: |
| 280 | printf("range 1 to 9\n"); | 317 | printf("range 1 to 9\n"); |
| 281 | break; | 318 | break; |
| 282 | default: | 319 | default: |
| 283 | printf("unexpected\n"); | 320 | printf("unexpected\n"); |
| 284 | break; | 321 | break; |
| 285 | } | 322 | @} |
| 286 | @end example | 323 | @end example |
| 287 | 324 | ||
| 288 | @item The keyword @code{__attribute__} is handled to specify variable or | 325 | @item The keyword @code{__attribute__} is handled to specify variable or |
| @@ -307,20 +344,22 @@ Here are some examples: · 18 unmodified lines | |||
| 307 | int a __attribute__ ((aligned(8), section(".mysection"))); | 344 | int a __attribute__ ((aligned(8), section(".mysection"))); |
| 308 | @end example | 345 | @end example |
| 309 | 346 | ||
| 310 | align variable @code{'a'} to 8 bytes and put it in section @code{.mysection}. | 347 | @noindent |
| 348 | align variable @code{a} to 8 bytes and put it in section @code{.mysection}. | ||
| 311 | 349 | ||
| 312 | @example | 350 | @example |
| 313 | int my_add(int a, int b) __attribute__ ((section(".mycodesection"))) | 351 | int my_add(int a, int b) __attribute__ ((section(".mycodesection"))) |
| 314 | { | 352 | @{ |
| 315 | return a + b; | 353 | return a + b; |
| 316 | } | 354 | @} |
| 317 | @end example | 355 | @end example |
| 318 | 356 | ||
| 319 | generate function @code{'my_add'} in section @code{.mycodesection}. | 357 | @noindent |
| 358 | generate function @code{my_add} in section @code{.mycodesection}. | ||
| 320 | 359 | ||
| 321 | @item GNU style variadic macros: | 360 | @item GNU style variadic macros: |
| 322 | @example | 361 | @example |
| 323 | #define dprintf(fmt, args...) printf(fmt, ## args) | 362 | #define dprintf(fmt, args@dots{}) printf(fmt, ## args) |
| 324 | 363 | ||
| 325 | dprintf("no arg\n"); | 364 | dprintf("no arg\n"); |
| 326 | dprintf("one arg %d\n", 1); | 365 | dprintf("one arg %d\n", 1); |
| @@ -341,26 +380,31 @@ to get the alignment of a type or an expression. · 14 unmodified lines | |||
| 341 | used to jump on the pointer resulting from @code{expr}. | 380 | used to jump on the pointer resulting from @code{expr}. |
| 342 | 381 | ||
| 343 | @item Inline assembly with asm instruction: | 382 | @item Inline assembly with asm instruction: |
| 383 | @cindex inline assembly | ||
| 384 | @cindex assembly, inline | ||
| 385 | @cindex __asm__ | ||
| 344 | @example | 386 | @example |
| 345 | static inline void * my_memcpy(void * to, const void * from, size_t n) | 387 | static inline void * my_memcpy(void * to, const void * from, size_t n) |
| 346 | { | 388 | @{ |
| 347 | int d0, d1, d2; | 389 | int d0, d1, d2; |
| 348 | __asm__ __volatile__( | 390 | __asm__ __volatile__( |
| 349 | "rep ; movsl\n\t" | 391 | "rep ; movsl\n\t" |
| 350 | "testb $2,%b4\n\t" | 392 | "testb $2,%b4\n\t" |
| 351 | "je 1f\n\t" | 393 | "je 1f\n\t" |
| 352 | "movsw\n" | 394 | "movsw\n" |
| 353 | "1:\ttestb $1,%b4\n\t" | 395 | "1:\ttestb $1,%b4\n\t" |
| 354 | "je 2f\n\t" | 396 | "je 2f\n\t" |
| 355 | "movsb\n" | 397 | "movsb\n" |
| 356 | "2:" | 398 | "2:" |
| 357 | : "=&c" (d0), "=&D" (d1), "=&S" (d2) | 399 | : "=&c" (d0), "=&D" (d1), "=&S" (d2) |
| 358 | :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from) | 400 | :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from) |
| 359 | : "memory"); | 401 | : "memory"); |
| 360 | return (to); | 402 | return (to); |
| 361 | } | 403 | @} |
| 362 | @end example | 404 | @end example |
| 363 | 405 | ||
| 406 | @noindent | ||
| 407 | @cindex gas | ||
| 364 | TCC includes its own x86 inline assembler with a @code{gas}-like (GNU | 408 | TCC includes its own x86 inline assembler with a @code{gas}-like (GNU |
| 365 | assembler) syntax. No intermediate files are generated. GCC 3.x named | 409 | assembler) syntax. No intermediate files are generated. GCC 3.x named |
| 366 | operands are supported. | 410 | operands are supported. |
| @@ -371,13 +415,13 @@ operands are supported. · 4 unmodified lines | |||
| 371 | 415 | ||
| 372 | @itemize | 416 | @itemize |
| 373 | 417 | ||
| 374 | @item @code{__TINYC__} is a predefined macro to @code{'1'} to | 418 | @item @code{__TINYC__} is a predefined macro to @code{1} to |
| 375 | indicate that you use TCC. | 419 | indicate that you use TCC. |
| 376 | 420 | ||
| 377 | @item @code{'#!'} at the start of a line is ignored to allow scripting. | 421 | @item @code{#!} at the start of a line is ignored to allow scripting. |
| 378 | 422 | ||
| 379 | @item Binary digits can be entered (@code{'0b101'} instead of | 423 | @item Binary digits can be entered (@code{0b101} instead of |
| 380 | @code{'5'}). | 424 | @code{5}). |
| 381 | 425 | ||
| 382 | @item @code{__BOUNDS_CHECKING_ON} is defined if bound checking is activated. | 426 | @item @code{__BOUNDS_CHECKING_ON} is defined if bound checking is activated. |
| 383 | 427 | ||
| @@ -452,6 +496,16 @@ They can be defined several times in the same source. Use 'b' · 68 unmodified lines | |||
| 452 | @end itemize | 496 | @end itemize |
| 453 | 497 | ||
| 454 | @section Directives | 498 | @section Directives |
| 499 | @cindex assembler directives | ||
| 500 | @cindex directives, assembler | ||
| 501 | @cindex .align | ||
| 502 | @cindex .skip | ||
| 503 | @cindex .space | ||
| 504 | @cindex .byte | ||
| 505 | @cindex .word | ||
| 506 | @cindex .short | ||
| 507 | @cindex .int | ||
| 508 | @cindex .long | ||
| 455 | 509 | ||
| 456 | All directives are preceeded by a '.'. The following directives are | 510 | All directives are preceeded by a '.'. The following directives are |
| 457 | supported: | 511 | supported: |
| @@ -468,6 +522,7 @@ supported: · 10 unmodified lines | |||
| 468 | @end itemize | 522 | @end itemize |
| 469 | 523 | ||
| 470 | @section X86 Assembler | 524 | @section X86 Assembler |
| 525 | @cindex assembler | ||
| 471 | 526 | ||
| 472 | All X86 opcodes are supported. Only ATT syntax is supported (source | 527 | All X86 opcodes are supported. Only ATT syntax is supported (source |
| 473 | then destination operand order). If no size suffix is given, TinyCC | 528 | then destination operand order). If no size suffix is given, TinyCC |
| @@ -476,20 +531,22 @@ tries to guess it from the operand sizes. · 2 unmodified lines | |||
| 476 | Currently, MMX opcodes are supported but not SSE ones. | 531 | Currently, MMX opcodes are supported but not SSE ones. |
| 477 | 532 | ||
| 478 | @chapter TinyCC Linker | 533 | @chapter TinyCC Linker |
| 534 | @cindex linker | ||
| 479 | 535 | ||
| 480 | @section ELF file generation | 536 | @section ELF file generation |
| 537 | @cindex ELF | ||
| 481 | 538 | ||
| 482 | TCC can directly output relocatable ELF files (object files), | 539 | TCC can directly output relocatable ELF files (object files), |
| 483 | executable ELF files and dynamic ELF libraries without relying on an | 540 | executable ELF files and dynamic ELF libraries without relying on an |
| 484 | external linker. | 541 | external linker. |
| 485 | 542 | ||
| 486 | Dynamic ELF libraries can be output but the C compiler does not generate | 543 | Dynamic ELF libraries can be output but the C compiler does not generate |
| 487 | position independant code (PIC) code. It means that the dynamic librairy | 544 | position independent code (PIC). It means that the dynamic librairy |
| 488 | code generated by TCC cannot be factorized among processes yet. | 545 | code generated by TCC cannot be factorized among processes yet. |
| 489 | 546 | ||
| 490 | TCC linker cannot currently suppress unused object code. But TCC | 547 | TCC linker cannot currently eliminate unused object code. But TCC |
| 491 | will soon integrate a novel feature not found in GNU tools: unused code | 548 | will soon integrate a novel feature not found in GNU tools: unused code |
| 492 | will be suppressed at the function or variable level, provided you only | 549 | will be eliminated at the function or variable level, provided you only |
| 493 | use TCC to compile your files. | 550 | use TCC to compile your files. |
| 494 | 551 | ||
| 495 | @section ELF file loader | 552 | @section ELF file loader |
| @@ -498,10 +555,14 @@ TCC can load ELF object files, archives (.a files) and dynamic · 2 unmodified lines | |||
| 498 | libraries (.so). | 555 | libraries (.so). |
| 499 | 556 | ||
| 500 | @section GNU Linker Scripts | 557 | @section GNU Linker Scripts |
| 558 | @cindex scripts, linker | ||
| 559 | @cindex linker scripts | ||
| 560 | @cindex GROUP, linker command | ||
| 561 | @cindex FILE, linker command | ||
| 501 | 562 | ||
| 502 | Because on many Linux systems some dynamic libraries (such as | 563 | Because on many Linux systems some dynamic libraries (such as |
| 503 | @file{/usr/lib/libc.so}) are in fact GNU ld link scripts (horrible!), | 564 | @file{/usr/lib/libc.so}) are in fact GNU ld link scripts (horrible!), |
| 504 | TCC linker also support a subset of GNU ld scripts. | 565 | the TCC linker also supports a subset of GNU ld scripts. |
| 505 | 566 | ||
| 506 | The @code{GROUP} and @code{FILE} commands are supported. | 567 | The @code{GROUP} and @code{FILE} commands are supported. |
| 507 | 568 | ||
| @@ -513,78 +574,80 @@ Example from @file{/usr/lib/libc.so}: · 5 unmodified lines | |||
| 513 | GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a ) | 574 | GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a ) |
| 514 | @end example | 575 | @end example |
| 515 | 576 | ||
| 516 | @node bounds | 577 | @node Bounds |
| 517 | @chapter TinyCC Memory and Bound checks | 578 | @chapter TinyCC Memory and Bound checks |
| 579 | @cindex bound checks | ||
| 580 | @cindex memory checks | ||
| 518 | 581 | ||
| 519 | This feature is activated with the @code{'-b'} (@xref{invoke}). | 582 | This feature is activated with the @option{-b} (@pxref{Invoke}). |
| 520 | 583 | ||
| 521 | Note that pointer size is @emph{unchanged} and that code generated | 584 | Note that pointer size is @emph{unchanged} and that code generated |
| 522 | with bound checks is @emph{fully compatible} with unchecked | 585 | with bound checks is @emph{fully compatible} with unchecked |
| 523 | code. When a pointer comes from unchecked code, it is assumed to be | 586 | code. When a pointer comes from unchecked code, it is assumed to be |
| 524 | valid. Even very obscure C code with casts should work correctly. | 587 | valid. Even very obscure C code with casts should work correctly. |
| 525 | 588 | ||
| 526 | To have more information about the ideas behind this method, check at | 589 | For more information about the ideas behind this method, see |
| 527 | @url{http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html}. | 590 | @url{http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html}. |
| 528 | 591 | ||
| 529 | Here are some examples of catched errors: | 592 | Here are some examples of caught errors: |
| 530 | 593 | ||
| 531 | @table @asis | 594 | @table @asis |
| 532 | 595 | ||
| 533 | @item Invalid range with standard string function: | 596 | @item Invalid range with standard string function: |
| 534 | @example | 597 | @example |
| 535 | { | 598 | @{ |
| 536 | char tab[10]; | 599 | char tab[10]; |
| 537 | memset(tab, 0, 11); | 600 | memset(tab, 0, 11); |
| 538 | } | 601 | @} |
| 539 | @end example | 602 | @end example |
| 540 | 603 | ||
| 541 | @item Bound error in global or local arrays: | 604 | @item Out of bounds-error in global or local arrays: |
| 542 | @example | 605 | @example |
| 543 | { | 606 | @{ |
| 544 | int tab[10]; | 607 | int tab[10]; |
| 545 | for(i=0;i<11;i++) { | 608 | for(i=0;i<11;i++) @{ |
| 546 | sum += tab[i]; | 609 | sum += tab[i]; |
| 547 | } | 610 | @} |
| 548 | } | 611 | @} |
| 549 | @end example | 612 | @end example |
| 550 | 613 | ||
| 551 | @item Bound error in allocated data: | 614 | @item Out of bounds-error in malloc'ed data: |
| 552 | @example | 615 | @example |
| 553 | { | 616 | @{ |
| 554 | int *tab; | 617 | int *tab; |
| 555 | tab = malloc(20 * sizeof(int)); | 618 | tab = malloc(20 * sizeof(int)); |
| 556 | for(i=0;i<21;i++) { | 619 | for(i=0;i<21;i++) @{ |
| 557 | sum += tab4[i]; | 620 | sum += tab4[i]; |
| 558 | } | 621 | @} |
| 559 | free(tab); | 622 | free(tab); |
| 560 | } | 623 | @} |
| 561 | @end example | 624 | @end example |
| 562 | 625 | ||
| 563 | @item Access to a freed region: | 626 | @item Access of freed memory: |
| 564 | @example | 627 | @example |
| 565 | { | 628 | @{ |
| 566 | int *tab; | 629 | int *tab; |
| 567 | tab = malloc(20 * sizeof(int)); | 630 | tab = malloc(20 * sizeof(int)); |
| 568 | free(tab); | 631 | free(tab); |
| 569 | for(i=0;i<20;i++) { | 632 | for(i=0;i<20;i++) @{ |
| 570 | sum += tab4[i]; | 633 | sum += tab4[i]; |
| 571 | } | 634 | @} |
| 572 | } | 635 | @} |
| 573 | @end example | 636 | @end example |
| 574 | 637 | ||
| 575 | @item Freeing an already freed region: | 638 | @item Double free: |
| 576 | @example | 639 | @example |
| 577 | { | 640 | @{ |
| 578 | int *tab; | 641 | int *tab; |
| 579 | tab = malloc(20 * sizeof(int)); | 642 | tab = malloc(20 * sizeof(int)); |
| 580 | free(tab); | 643 | free(tab); |
| 581 | free(tab); | 644 | free(tab); |
| 582 | } | 645 | @} |
| 583 | @end example | 646 | @end example |
| 584 | 647 | ||
| 585 | @end table | 648 | @end table |
| 586 | 649 | ||
| 587 | @node libtcc | 650 | @node Libtcc |
| 588 | @chapter The @code{libtcc} library | 651 | @chapter The @code{libtcc} library |
| 589 | 652 | ||
| 590 | The @code{libtcc} library enables you to use TCC as a backend for | 653 | The @code{libtcc} library enables you to use TCC as a backend for |
| @@ -597,7 +660,7 @@ The idea consists in giving a C string containing the program you want · 6 unmodified lines | |||
| 597 | to compile directly to @code{libtcc}. Then you can access to any global | 660 | to compile directly to @code{libtcc}. Then you can access to any global |
| 598 | symbol (function or variable) defined. | 661 | symbol (function or variable) defined. |
| 599 | 662 | ||
| 600 | @chapter Developper's guide | 663 | @chapter Developer's guide |
| 601 | 664 | ||
| 602 | This chapter gives some hints to understand how TCC works. You can skip | 665 | This chapter gives some hints to understand how TCC works. You can skip |
| 603 | it if you do not intend to modify the TCC code. | 666 | it if you do not intend to modify the TCC code. |
| @@ -617,7 +680,7 @@ expansion. · 13 unmodified lines | |||
| 617 | 680 | ||
| 618 | @code{tok} contains the current token (see @code{TOK_xxx}) | 681 | @code{tok} contains the current token (see @code{TOK_xxx}) |
| 619 | constants. Identifiers and keywords are also keywords. @code{tokc} | 682 | constants. Identifiers and keywords are also keywords. @code{tokc} |
| 620 | contains additionnal infos about the token (for example a constant value | 683 | contains additional infos about the token (for example a constant value |
| 621 | if number or string token). | 684 | if number or string token). |
| 622 | 685 | ||
| 623 | @section Parser | 686 | @section Parser |
| @@ -771,22 +834,24 @@ contain the exported symbols (currently only used for debugging). · 147 unmodified lines | |||
| 771 | @end table | 834 | @end table |
| 772 | 835 | ||
| 773 | @section Code generation | 836 | @section Code generation |
| 837 | @cindex code generation | ||
| 774 | 838 | ||
| 775 | @subsection Introduction | 839 | @subsection Introduction |
| 776 | 840 | ||
| 777 | The TCC code generator directly generates linked binary code in one | 841 | The TCC code generator directly generates linked binary code in one |
| 778 | pass. It is rather unusual these days (see gcc for example which | 842 | pass. It is rather unusual these days (see gcc for example which |
| 779 | generates text assembly), but it allows to be very fast and surprisingly | 843 | generates text assembly), but it can be very fast and surprisingly |
| 780 | not so complicated. | 844 | little complicated. |
| 781 | 845 | ||
| 782 | The TCC code generator is register based. Optimization is only done at | 846 | The TCC code generator is register based. Optimization is only done at |
| 783 | the expression level. No intermediate representation of expression is | 847 | the expression level. No intermediate representation of expression is |
| 784 | kept except the current values stored in the @emph{value stack}. | 848 | kept except the current values stored in the @emph{value stack}. |
| 785 | 849 | ||
| 786 | On x86, three temporary registers are used. When more registers are | 850 | On x86, three temporary registers are used. When more registers are |
| 787 | needed, one register is flushed in a new local variable. | 851 | needed, one register is spilled into a new temporary variable on the stack. |
| 788 | 852 | ||
| 789 | @subsection The value stack | 853 | @subsection The value stack |
| 854 | @cindex value stack, introduction | ||
| 790 | 855 | ||
| 791 | When an expression is parsed, its value is pushed on the value stack | 856 | When an expression is parsed, its value is pushed on the value stack |
| 792 | (@var{vstack}). The top of the value stack is @var{vtop}. Each value | 857 | (@var{vstack}). The top of the value stack is @var{vtop}. Each value |
| @@ -794,7 +859,7 @@ stack entry is the structure @code{SValue}. · 1 unmodified line | |||
| 794 | 859 | ||
| 795 | @code{SValue.t} is the type. @code{SValue.r} indicates how the value is | 860 | @code{SValue.t} is the type. @code{SValue.r} indicates how the value is |
| 796 | currently stored in the generated code. It is usually a CPU register | 861 | currently stored in the generated code. It is usually a CPU register |
| 797 | index (@code{REG_xxx} constants), but additionnal values and flags are | 862 | index (@code{REG_xxx} constants), but additional values and flags are |
| 798 | defined: | 863 | defined: |
| 799 | 864 | ||
| 800 | @example | 865 | @example |
| @@ -835,8 +900,8 @@ put in a normal register. · 34 unmodified lines | |||
| 835 | 900 | ||
| 836 | @item VT_JMP | 901 | @item VT_JMP |
| 837 | @itemx VT_JMPI | 902 | @itemx VT_JMPI |
| 838 | indicates that the value is the consequence of a jmp. For VT_JMP, it is | 903 | indicates that the value is the consequence of a conditional jump. For VT_JMP, |
| 839 | 1 if the jump is taken, 0 otherwise. For VT_JMPI it is inverted. | 904 | it is 1 if the jump is taken, 0 otherwise. For VT_JMPI it is inverted. |
| 840 | 905 | ||
| 841 | These values are used to compile the @code{||} and @code{&&} logical | 906 | These values are used to compile the @code{||} and @code{&&} logical |
| 842 | operators. | 907 | operators. |
| @@ -857,10 +922,10 @@ understand how TCC works. · 14 unmodified lines | |||
| 857 | @itemx VT_LVAL_SHORT | 922 | @itemx VT_LVAL_SHORT |
| 858 | @itemx VT_LVAL_UNSIGNED | 923 | @itemx VT_LVAL_UNSIGNED |
| 859 | if the lvalue has an integer type, then these flags give its real | 924 | if the lvalue has an integer type, then these flags give its real |
| 860 | type. The type alone is not suffisant in case of cast optimisations. | 925 | type. The type alone is not enough in case of cast optimisations. |
| 861 | 926 | ||
| 862 | @item VT_LLOCAL | 927 | @item VT_LLOCAL |
| 863 | is a saved lvalue on the stack. @code{VT_LLOCAL} should be suppressed | 928 | is a saved lvalue on the stack. @code{VT_LLOCAL} should be eliminated |
| 864 | ASAP because its semantics are rather complicated. | 929 | ASAP because its semantics are rather complicated. |
| 865 | 930 | ||
| 866 | @item VT_MUSTCAST | 931 | @item VT_MUSTCAST |
| @@ -877,17 +942,18 @@ are only used for optional bound checking. · 10 unmodified lines | |||
| 877 | @end table | 942 | @end table |
| 878 | 943 | ||
| 879 | @subsection Manipulating the value stack | 944 | @subsection Manipulating the value stack |
| 945 | @cindex value stack | ||
| 880 | 946 | ||
| 881 | @code{vsetc()} and @code{vset()} pushes a new value on the value | 947 | @code{vsetc()} and @code{vset()} pushes a new value on the value |
| 882 | stack. If the previous @code{vtop} was stored in a very unsafe place(for | 948 | stack. If the previous @var{vtop} was stored in a very unsafe place(for |
| 883 | example in the CPU flags), then some code is generated to put the | 949 | example in the CPU flags), then some code is generated to put the |
| 884 | previous @code{vtop} in a safe storage. | 950 | previous @var{vtop} in a safe storage. |
| 885 | 951 | ||
| 886 | @code{vpop()} pops @code{vtop}. In some cases, it also generates cleanup | 952 | @code{vpop()} pops @var{vtop}. In some cases, it also generates cleanup |
| 887 | code (for example if stacked floating point registers are used as on | 953 | code (for example if stacked floating point registers are used as on |
| 888 | x86). | 954 | x86). |
| 889 | 955 | ||
| 890 | The @code{gv(rc)} function generates code to evaluate @code{vtop} (the | 956 | The @code{gv(rc)} function generates code to evaluate @var{vtop} (the |
| 891 | top value of the stack) into registers. @var{rc} selects in which | 957 | top value of the stack) into registers. @var{rc} selects in which |
| 892 | register class the value should be put. @code{gv()} is the @emph{most | 958 | register class the value should be put. @code{gv()} is the @emph{most |
| 893 | important function} of the code generator. | 959 | important function} of the code generator. |
| @@ -896,7 +962,7 @@ important function} of the code generator. · 2 unmodified lines | |||
| 896 | entries. | 962 | entries. |
| 897 | 963 | ||
| 898 | @subsection CPU dependent code generation | 964 | @subsection CPU dependent code generation |
| 899 | 965 | @cindex CPU dependent | |
| 900 | See the @file{i386-gen.c} file to have an example. | 966 | See the @file{i386-gen.c} file to have an example. |
| 901 | 967 | ||
| 902 | @table @code | 968 | @table @code |
| @@ -939,12 +1005,18 @@ floating point to floating point of different size conversion. · 36 unmodified lines | |||
| 939 | 1005 | ||
| 940 | @item gen_bounded_ptr_add() | 1006 | @item gen_bounded_ptr_add() |
| 941 | @item gen_bounded_ptr_deref() | 1007 | @item gen_bounded_ptr_deref() |
| 942 | are only used for bound checking. | 1008 | are only used for bounds checking. |
| 943 | 1009 | ||
| 944 | @end table | 1010 | @end table |
| 945 | 1011 | ||
| 946 | @section Optimizations done | 1012 | @section Optimizations done |
| 947 | 1013 | @cindex optimizations | |
| 1014 | @cindex constant propagation | ||
| 1015 | @cindex strength reduction | ||
| 1016 | @cindex comparison operators | ||
| 1017 | @cindex caching processor flags | ||
| 1018 | @cindex flags, caching | ||
| 1019 | @cindex jump optimization | ||
| 948 | Constant propagation is done for all operations. Multiplications and | 1020 | Constant propagation is done for all operations. Multiplications and |
| 949 | divisions are optimized to shifts when appropriate. Comparison | 1021 | divisions are optimized to shifts when appropriate. Comparison |
| 950 | operators are optimized by maintaining a special cache for the | 1022 | operators are optimized by maintaining a special cache for the |
| @@ -952,3 +1024,12 @@ processor flags. &&, || and ! are optimized by maintaining a special · 1 unmodified line | |||
| 952 | 'jump target' value. No other jump optimization is currently performed | 1024 | 'jump target' value. No other jump optimization is currently performed |
| 953 | because it would require to store the code in a more abstract fashion. | 1025 | because it would require to store the code in a more abstract fashion. |
| 954 | 1026 | ||
| 1027 | @unnumbered Concept Index | ||
| 1028 | @printindex cp | ||
| 1029 | |||
| 1030 | @bye | ||
| 1031 | |||
| 1032 | @c Local variables: | ||
| 1033 | @c fill-column: 78 | ||
| 1034 | @c texinfo-column-for-description: 32 | ||
| 1035 | @c End: | ||