History

spell fixes + texinfo format fixes by Peter Lund

bellard committed 23 years ago

Commit 3605c501 parent d5751371 file · +213 −132Browse files

Unified · Split

tcc-doc.texi+213−132
@@ -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 Documentation4@settitle Tiny C Compiler Reference Documentation
5@c %**end of header
6
7@include version.texi
8
9@ifinfo
10Bla bla bla
11@end ifinfo
12
13@iftex
4@titlepage14@titlepage
15@afourpaper
5@sp 716@sp 7
6@center @titlefont{Tiny C Compiler Reference Documentation}17@center @titlefont{Tiny C Compiler Reference Documentation}
7@sp 318@sp 3
8@end titlepage19@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
31This 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 Introduction42@chapter Introduction
11 43
12TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C44TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C
13compilers, it is meant to be self-suffisant: you do not need an45compilers, it is meant to be self-relying: you do not need an
14external assembler or linker because TCC does that for you.46external assembler or linker because TCC does that for you.
15 47
16TCC compiles so @emph{fast} that even for big projects @code{Makefile}s may48TCC 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
24your script will be as fast as if it was an executable.56your script will be as fast as if it was an executable.
25 57
26TCC can also automatically generate memory and bound checks58TCC can also automatically generate memory and bound checks
27(@xref{bounds}) while allowing all C pointers operations. TCC can do59(@pxref{Bounds}) while allowing all C pointers operations. TCC can do
28these checks even if non patched libraries are used.60these checks even if non patched libraries are used.
29 61
30With @code{libtcc}, you can use TCC as a backend for dynamic code62With @code{libtcc}, you can use TCC as a backend for dynamic code
31generation (@xref{libtcc}).63generation (@pxref{Libtcc}).
32 64
33@node invoke65@node Invoke
34@chapter Command line invocation66@chapter Command line invocation
35 67
36@section Quick start68@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 example74@end example
43 75
44TCC options are a very much like gcc. The main difference is that TCC76@noindent
77TCC options are a very much like gcc options. The main difference is that TCC
45can also execute directly the resulting program and give it runtime78can also execute directly the resulting program and give it runtime
46arguments.79arguments.
47 80
48Here are some examples to understand the logic:81Here are some examples to understand the logic:
49 82
50@table @code83@table @code
51@item tcc a.c84@item @samp{tcc a.c}
52Compile a.c and execute it directly85Compile @file{a.c} and execute it directly
53 86
54@item tcc a.c arg187@item @samp{tcc a.c arg1}
55Compile a.c and execute it directly. arg1 is given as first argument to88Compile a.c and execute it directly. arg1 is given as first argument to
56the @code{main()} of a.c.89the @code{main()} of a.c.
57 90
58@item tcc -- a.c b.c -- arg191@item @samp{tcc -- a.c b.c -- arg1}
59Compile a.c and b.c, link them together and execute them. arg1 is given92Compile @file{a.c} and @file{b.c}, link them together and execute them. arg1 is given
60as first argument to the @code{main()} of the resulting program. Because93as first argument to the @code{main()} of the resulting program. Because
61multiple C files are specified, @code{--} are necessary to clearly separate the94multiple C files are specified, @option{--} are necessary to clearly separate the
62program arguments from the TCC options.95program arguments from the TCC options.
63 96
64@item tcc -o myprog a.c b.c97@item @samp{tcc -o myprog a.c b.c}
65Compile a.c and b.c, link them and generate the executable myprog.98Compile @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.o100@item @samp{tcc -o myprog a.o b.o}
68link a.o and b.o together and generate the executable myprog.101link @file{a.o} and @file{b.o} together and generate the executable @file{myprog}.
69 102
70@item tcc -c a.c103@item @samp{tcc -c a.c}
71Compile a.c and generate object file a.o104Compile @file{a.c} and generate object file @file{a.o}.
72 105
73@item tcc -c asmfile.S106@item @samp{tcc -c asmfile.S}
74Preprocess with C preprocess and assemble asmfile.S and generate107Preprocess with C preprocess and assemble @file{asmfile.S} and generate
75object file asmfile.o.108object file @file{asmfile.o}.
76 109
77@item tcc -c asmfile.s110@item @samp{tcc -c asmfile.s}
78Assemble (but not preprocess) asmfile.s and generate object file111Assemble (but not preprocess) @file{asmfile.s} and generate object file
79asmfile.o.112@file{asmfile.o}.
80 113
81@item tcc -r -o ab.o a.c b.c114@item @samp{tcc -r -o ab.o a.c b.c}
82Compile a.c and b.c, link them together and generate the object file ab.o.115Compile @file{a.c} and @file{b.c}, link them together and generate the object file @file{ab.o}.
83 116
84@end table117@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
95int main() 128int main()
96{129@{
97 printf("Hello World\n");130 printf("Hello World\n");
98 return 0;131 return 0;
99}132@}
100@end example133@end example
101 134
102@section Option summary135@section Option summary
103 136
104General Options:137General Options:
105 138
106@table @samp139@table @option
107@item -c140@item -c
108Generate an object file (@samp{-o} option must also be given).141Generate an object file (@option{-o} option must also be given).
109 142
110@item -o outfile143@item -o outfile
111Put object file, executable, or dll into output file @file{outfile}.144Put object file, executable, or dll into output file @file{outfile}.
@@ -120,54 +153,54 @@ Output compilation statistics. · 8 unmodified lines
120 153
121Preprocessor options:154Preprocessor options:
122 155
123@table @samp156@table @option
124@item -Idir157@item -Idir
125Specify an additionnal include path. Include paths are searched in the158Specify an additional include path. Include paths are searched in the
126order they are specified.159order they are specified.
127 160
128System include paths are always searched after. The default system161System include paths are always searched after. The default system
129include paths are: @file{/usr/local/include}, @file{/usr/include}162include paths are: @file{/usr/local/include}, @file{/usr/include}
130and @file{PREFIX/lib/tcc/include}. (@code{PREFIX} is usually163and @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]
134Define preprocessor symbol 'sym' to167Define preprocessor symbol @samp{sym} to
135val. If val is not present, its value is '1'. Function-like macros can168val. If val is not present, its value is @samp{1}. Function-like macros can
136also be defined: @code{'-DF(a)=a+1'}169also be defined: @option{-DF(a)=a+1}
137 170
138@item -Usym171@item -Usym
139Undefine preprocessor symbol 'sym'.172Undefine preprocessor symbol @samp{sym}.
140@end table173@end table
141 174
142Linker options:175Linker options:
143 176
144@table @samp177@table @option
145@item -Ldir178@item -Ldir
146Specify an additionnal static library path for the @samp{-l} option. The179Specify an additional static library path for the @option{-l} option. The
147default library paths are @file{/usr/local/lib}, @file{/usr/lib} and @file{/lib}.180default library paths are @file{/usr/local/lib}, @file{/usr/lib} and @file{/lib}.
148 181
149@item -lxxx182@item -lxxx
150Link your program with dynamic library libxxx.so or static library183Link your program with dynamic library libxxx.so or static library
151libxxx.a. The library is searched in the paths specified by the184libxxx.a. The library is searched in the paths specified by the
152@samp{-L} option.185@option{-L} option.
153 186
154@item -shared187@item -shared
155Generate a shared library instead of an executable (@samp{-o} option188Generate a shared library instead of an executable (@option{-o} option
156must also be given).189must also be given).
157 190
158@item -static191@item -static
159Generate a statically linked executable (default is a shared linked192Generate a statically linked executable (default is a shared linked
160executable) (@samp{-o} option must also be given).193executable) (@option{-o} option must also be given).
161 194
162@item -r195@item -r
163Generate an object file combining all input files (@samp{-o} option must196Generate an object file combining all input files (@option{-o} option must
164also be given).197also be given).
165 198
166@end table199@end table
167 200
168Debugger options:201Debugger options:
169 202
170@table @samp203@table @option
171@item -g204@item -g
172Generate run time debug information so that you get clear run time205Generate run time debug information so that you get clear run time
173error messages: @code{ test.c:68: in function 'test5()': dereferencing206error messages: @code{ test.c:68: in function 'test5()': dereferencing
@@ -175,17 +208,17 @@ invalid pointer} instead of the laconic @code{Segmentation · 1 unmodified line
175fault}.208fault}.
176 209
177@item -b210@item -b
178Generate additionnal support code to check211Generate additional support code to check
179memory allocations and array/pointer bounds. @samp{-g} is implied. Note212memory allocations and array/pointer bounds. @option{-g} is implied. Note
180that the generated code is slower and bigger in this case.213that the generated code is slower and bigger in this case.
181 214
182@item -bt N215@item -bt N
183Display N callers in stack traces. This is useful with @samp{-g} or216Display N callers in stack traces. This is useful with @option{-g} or
184@samp{-b}.217@option{-b}.
185 218
186@end table219@end table
187 220
188Note: GCC options @samp{-Ox}, @samp{-Wx}, @samp{-fx} and @samp{-mx} are221Note: GCC options @option{-Ox}, @option{-Wx}, @option{-fx} and @option{-mx} are
189ignored.222ignored.
190 223
191@chapter C language support224@chapter C language support
@@ -206,11 +239,11 @@ Currently implemented ISOC99 features: · 14 unmodified lines
206 239
207@itemize240@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 current246@item @code{__func__} is a string variable containing the current
214function name.247function name.
215 248
216@item Variadic macros: @code{__VA_ARGS__} can be used for249@item Variadic macros: @code{__VA_ARGS__} can be used for
@@ -218,6 +251,8 @@ function name. · 1 unmodified line
218@example251@example
219 #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)252 #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)
220@end example253@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 by260@item Array and struct/union elements can be initialized in any order by
226 using designators:261 using designators:
227@example262@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 example266@end example
232 267
233@item Compound initializers are supported:268@item Compound initializers are supported:
234@example269@example
235 int *p = (int []){ 1, 2, 3 };270 int *p = (int [])@{ 1, 2, 3 @};
236@end example271@end example
237to initialize a pointer pointing to an initialized array. The same272to initialize a pointer pointing to an initialized array. The same
238works for structures and strings.273works for structures and strings.
@@ -241,14 +276,16 @@ works for structures and strings. · 2 unmodified lines
241@example276@example
242 double d = 0x1234p10;277 double d = 0x1234p10;
243@end example278@end example
279
280@noindent
244is the same as writing 281is the same as writing
245@example282@example
246 double d = 4771840.0;283 double d = 4771840.0;
247@end example284@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 itemize289@end itemize
253 290
254@section GNU C extensions291@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@example298@example
262 int a[10] = { [0] 1, [5] 2, 3, 4 };299 int a[10] = @{ [0] 1, [5] 2, 3, 4 @};
263@end example300@end example
264 301
265@item Structure field designators can be a label: 302@item Structure field designators can be a label:
266@example303@example
267 struct { int x, y; } st = { x: 1, y: 1};304 struct @{ int x, y; @} st = @{ x: 1, y: 1@};
268@end example305@end example
269instead of306instead of
270@example307@example
271 struct { int x, y; } st = { .x = 1, .y = 1};308 struct @{ int x, y; @} st = @{ .x = 1, .y = 1@};
272@end example309@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@example314@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 example323@end example
287 324
288@item The keyword @code{__attribute__} is handled to specify variable or325@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 example345@end example
309 346
310align variable @code{'a'} to 8 bytes and put it in section @code{.mysection}.347@noindent
348align variable @code{a} to 8 bytes and put it in section @code{.mysection}.
311 349
312@example350@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 example355@end example
318 356
319generate function @code{'my_add'} in section @code{.mycodesection}.357@noindent
358generate function @code{my_add} in section @code{.mycodesection}.
320 359
321@item GNU style variadic macros:360@item GNU style variadic macros:
322@example361@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
341used to jump on the pointer resulting from @code{expr}.380used 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@example386@example
345static inline void * my_memcpy(void * to, const void * from, size_t n)387static inline void * my_memcpy(void * to, const void * from, size_t n)
346{388@{
347int d0, d1, d2;389int 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");
360return (to);402return (to);
361}403@}
362@end example404@end example
363 405
406@noindent
407@cindex gas
364TCC includes its own x86 inline assembler with a @code{gas}-like (GNU408TCC includes its own x86 inline assembler with a @code{gas}-like (GNU
365assembler) syntax. No intermediate files are generated. GCC 3.x named409assembler) syntax. No intermediate files are generated. GCC 3.x named
366operands are supported.410operands are supported.
@@ -371,13 +415,13 @@ operands are supported. · 4 unmodified lines
371 415
372@itemize416@itemize
373 417
374@item @code{__TINYC__} is a predefined macro to @code{'1'} to418@item @code{__TINYC__} is a predefined macro to @code{1} to
375indicate that you use TCC.419indicate 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 of423@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 itemize496@end itemize
453 497
454@section Directives498@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
456All directives are preceeded by a '.'. The following directives are510All directives are preceeded by a '.'. The following directives are
457supported:511supported:
@@ -468,6 +522,7 @@ supported: · 10 unmodified lines
468@end itemize522@end itemize
469 523
470@section X86 Assembler524@section X86 Assembler
525@cindex assembler
471 526
472All X86 opcodes are supported. Only ATT syntax is supported (source527All X86 opcodes are supported. Only ATT syntax is supported (source
473then destination operand order). If no size suffix is given, TinyCC528then 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
476Currently, MMX opcodes are supported but not SSE ones.531Currently, MMX opcodes are supported but not SSE ones.
477 532
478@chapter TinyCC Linker533@chapter TinyCC Linker
534@cindex linker
479 535
480@section ELF file generation536@section ELF file generation
537@cindex ELF
481 538
482TCC can directly output relocatable ELF files (object files),539TCC can directly output relocatable ELF files (object files),
483executable ELF files and dynamic ELF libraries without relying on an540executable ELF files and dynamic ELF libraries without relying on an
484external linker.541external linker.
485 542
486Dynamic ELF libraries can be output but the C compiler does not generate543Dynamic ELF libraries can be output but the C compiler does not generate
487position independant code (PIC) code. It means that the dynamic librairy544position independent code (PIC). It means that the dynamic librairy
488code generated by TCC cannot be factorized among processes yet.545code generated by TCC cannot be factorized among processes yet.
489 546
490TCC linker cannot currently suppress unused object code. But TCC547TCC linker cannot currently eliminate unused object code. But TCC
491will soon integrate a novel feature not found in GNU tools: unused code548will soon integrate a novel feature not found in GNU tools: unused code
492will be suppressed at the function or variable level, provided you only549will be eliminated at the function or variable level, provided you only
493use TCC to compile your files.550use TCC to compile your files.
494 551
495@section ELF file loader552@section ELF file loader
@@ -498,10 +555,14 @@ TCC can load ELF object files, archives (.a files) and dynamic · 2 unmodified lines
498libraries (.so).555libraries (.so).
499 556
500@section GNU Linker Scripts557@section GNU Linker Scripts
558@cindex scripts, linker
559@cindex linker scripts
560@cindex GROUP, linker command
561@cindex FILE, linker command
501 562
502Because on many Linux systems some dynamic libraries (such as563Because 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!),
504TCC linker also support a subset of GNU ld scripts.565the TCC linker also supports a subset of GNU ld scripts.
505 566
506The @code{GROUP} and @code{FILE} commands are supported.567The @code{GROUP} and @code{FILE} commands are supported.
507 568
@@ -513,78 +574,80 @@ Example from @file{/usr/lib/libc.so}: · 5 unmodified lines
513GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )574GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )
514@end example575@end example
515 576
516@node bounds577@node Bounds
517@chapter TinyCC Memory and Bound checks578@chapter TinyCC Memory and Bound checks
579@cindex bound checks
580@cindex memory checks
518 581
519This feature is activated with the @code{'-b'} (@xref{invoke}).582This feature is activated with the @option{-b} (@pxref{Invoke}).
520 583
521Note that pointer size is @emph{unchanged} and that code generated584Note that pointer size is @emph{unchanged} and that code generated
522with bound checks is @emph{fully compatible} with unchecked585with bound checks is @emph{fully compatible} with unchecked
523code. When a pointer comes from unchecked code, it is assumed to be586code. When a pointer comes from unchecked code, it is assumed to be
524valid. Even very obscure C code with casts should work correctly.587valid. Even very obscure C code with casts should work correctly.
525 588
526To have more information about the ideas behind this method, check at 589For 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
529Here are some examples of catched errors:592Here are some examples of caught errors:
530 593
531@table @asis594@table @asis
532 595
533@item Invalid range with standard string function:596@item Invalid range with standard string function:
534@example597@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 example602@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@example605@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 example612@end example
550 613
551@item Bound error in allocated data:614@item Out of bounds-error in malloc'ed data:
552@example615@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 example624@end example
562 625
563@item Access to a freed region:626@item Access of freed memory:
564@example627@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 example636@end example
574 637
575@item Freeing an already freed region:638@item Double free:
576@example639@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 example646@end example
584 647
585@end table648@end table
586 649
587@node libtcc650@node Libtcc
588@chapter The @code{libtcc} library651@chapter The @code{libtcc} library
589 652
590The @code{libtcc} library enables you to use TCC as a backend for653The @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
597to compile directly to @code{libtcc}. Then you can access to any global660to compile directly to @code{libtcc}. Then you can access to any global
598symbol (function or variable) defined.661symbol (function or variable) defined.
599 662
600@chapter Developper's guide663@chapter Developer's guide
601 664
602This chapter gives some hints to understand how TCC works. You can skip665This chapter gives some hints to understand how TCC works. You can skip
603it if you do not intend to modify the TCC code.666it 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})
619constants. Identifiers and keywords are also keywords. @code{tokc}682constants. Identifiers and keywords are also keywords. @code{tokc}
620contains additionnal infos about the token (for example a constant value683contains additional infos about the token (for example a constant value
621if number or string token).684if number or string token).
622 685
623@section Parser686@section Parser
@@ -771,22 +834,24 @@ contain the exported symbols (currently only used for debugging). · 147 unmodified lines
771@end table834@end table
772 835
773@section Code generation836@section Code generation
837@cindex code generation
774 838
775@subsection Introduction839@subsection Introduction
776 840
777The TCC code generator directly generates linked binary code in one841The TCC code generator directly generates linked binary code in one
778pass. It is rather unusual these days (see gcc for example which842pass. It is rather unusual these days (see gcc for example which
779generates text assembly), but it allows to be very fast and surprisingly843generates text assembly), but it can be very fast and surprisingly
780not so complicated.844little complicated.
781 845
782The TCC code generator is register based. Optimization is only done at846The TCC code generator is register based. Optimization is only done at
783the expression level. No intermediate representation of expression is847the expression level. No intermediate representation of expression is
784kept except the current values stored in the @emph{value stack}.848kept except the current values stored in the @emph{value stack}.
785 849
786On x86, three temporary registers are used. When more registers are850On x86, three temporary registers are used. When more registers are
787needed, one register is flushed in a new local variable.851needed, one register is spilled into a new temporary variable on the stack.
788 852
789@subsection The value stack853@subsection The value stack
854@cindex value stack, introduction
790 855
791When an expression is parsed, its value is pushed on the value stack856When 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 value857(@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 is860@code{SValue.t} is the type. @code{SValue.r} indicates how the value is
796currently stored in the generated code. It is usually a CPU register861currently stored in the generated code. It is usually a CPU register
797index (@code{REG_xxx} constants), but additionnal values and flags are862index (@code{REG_xxx} constants), but additional values and flags are
798defined:863defined:
799 864
800@example865@example
@@ -835,8 +900,8 @@ put in a normal register. · 34 unmodified lines
835 900
836@item VT_JMP901@item VT_JMP
837@itemx VT_JMPI902@itemx VT_JMPI
838indicates that the value is the consequence of a jmp. For VT_JMP, it is903indicates that the value is the consequence of a conditional jump. For VT_JMP,
8391 if the jump is taken, 0 otherwise. For VT_JMPI it is inverted.904it is 1 if the jump is taken, 0 otherwise. For VT_JMPI it is inverted.
840 905
841These values are used to compile the @code{||} and @code{&&} logical906These values are used to compile the @code{||} and @code{&&} logical
842operators.907operators.
@@ -857,10 +922,10 @@ understand how TCC works. · 14 unmodified lines
857@itemx VT_LVAL_SHORT922@itemx VT_LVAL_SHORT
858@itemx VT_LVAL_UNSIGNED923@itemx VT_LVAL_UNSIGNED
859if the lvalue has an integer type, then these flags give its real924if the lvalue has an integer type, then these flags give its real
860type. The type alone is not suffisant in case of cast optimisations.925type. The type alone is not enough in case of cast optimisations.
861 926
862@item VT_LLOCAL927@item VT_LLOCAL
863is a saved lvalue on the stack. @code{VT_LLOCAL} should be suppressed928is a saved lvalue on the stack. @code{VT_LLOCAL} should be eliminated
864ASAP because its semantics are rather complicated.929ASAP because its semantics are rather complicated.
865 930
866@item VT_MUSTCAST931@item VT_MUSTCAST
@@ -877,17 +942,18 @@ are only used for optional bound checking. · 10 unmodified lines
877@end table942@end table
878 943
879@subsection Manipulating the value stack944@subsection Manipulating the value stack
945@cindex value stack
880 946
881@code{vsetc()} and @code{vset()} pushes a new value on the value947@code{vsetc()} and @code{vset()} pushes a new value on the value
882stack. If the previous @code{vtop} was stored in a very unsafe place(for948stack. If the previous @var{vtop} was stored in a very unsafe place(for
883example in the CPU flags), then some code is generated to put the949example in the CPU flags), then some code is generated to put the
884previous @code{vtop} in a safe storage.950previous @var{vtop} in a safe storage.
885 951
886@code{vpop()} pops @code{vtop}. In some cases, it also generates cleanup952@code{vpop()} pops @var{vtop}. In some cases, it also generates cleanup
887code (for example if stacked floating point registers are used as on953code (for example if stacked floating point registers are used as on
888x86).954x86).
889 955
890The @code{gv(rc)} function generates code to evaluate @code{vtop} (the956The @code{gv(rc)} function generates code to evaluate @var{vtop} (the
891top value of the stack) into registers. @var{rc} selects in which957top value of the stack) into registers. @var{rc} selects in which
892register class the value should be put. @code{gv()} is the @emph{most958register class the value should be put. @code{gv()} is the @emph{most
893important function} of the code generator.959important function} of the code generator.
@@ -896,7 +962,7 @@ important function} of the code generator. · 2 unmodified lines
896entries.962entries.
897 963
898@subsection CPU dependent code generation964@subsection CPU dependent code generation
899 965@cindex CPU dependent
900See the @file{i386-gen.c} file to have an example.966See the @file{i386-gen.c} file to have an example.
901 967
902@table @code968@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()
942are only used for bound checking.1008are only used for bounds checking.
943 1009
944@end table1010@end table
945 1011
946@section Optimizations done1012@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
948Constant propagation is done for all operations. Multiplications and1020Constant propagation is done for all operations. Multiplications and
949divisions are optimized to shifts when appropriate. Comparison1021divisions are optimized to shifts when appropriate. Comparison
950operators are optimized by maintaining a special cache for the1022operators 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 performed1024'jump target' value. No other jump optimization is currently performed
953because it would require to store the code in a more abstract fashion.1025because 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:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t