forked from TinyCC/tinycc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtinycc-docs.html
1326 lines (1009 loc) · 59.4 KB
/
tinycc-docs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html lang="en">
<head>
<title>Tiny C Compiler Reference Documentation</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Tiny C Compiler Reference Documentation">
<meta name="generator" content="makeinfo 4.8">
<link title="Top" rel="top" href="#Top">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="contents">
<h2>Table of Contents</h2>
<ul>
<li><a name="toc_Top" href="#Top">Tiny C Compiler Reference Documentation</a>
<li><a name="toc_Introduction" href="#Introduction">1 Introduction</a>
<li><a name="toc_Invoke" href="#Invoke">2 Command line invocation</a>
<ul>
<li><a href="#Invoke">2.1 Quick start</a>
<li><a href="#Invoke">2.2 Option summary</a>
</li></ul>
<li><a name="toc_Clang" href="#Clang">3 C language support</a>
<ul>
<li><a href="#Clang">3.1 ANSI C</a>
<li><a href="#Clang">3.2 ISOC99 extensions</a>
<li><a href="#Clang">3.3 GNU C extensions</a>
<li><a href="#Clang">3.4 TinyCC extensions</a>
</li></ul>
<li><a name="toc_asm" href="#asm">4 TinyCC Assembler</a>
<ul>
<li><a href="#asm">4.1 Syntax</a>
<li><a href="#asm">4.2 Expressions</a>
<li><a href="#asm">4.3 Labels</a>
<li><a href="#asm">4.4 Directives</a>
<li><a href="#asm">4.5 X86 Assembler</a>
</li></ul>
<li><a name="toc_linker" href="#linker">5 TinyCC Linker</a>
<ul>
<li><a href="#linker">5.1 ELF file generation</a>
<li><a href="#linker">5.2 ELF file loader</a>
<li><a href="#linker">5.3 PE-i386 file generation</a>
<li><a href="#linker">5.4 GNU Linker Scripts</a>
</li></ul>
<li><a name="toc_Bounds" href="#Bounds">6 TinyCC Memory and Bound checks</a>
<li><a name="toc_Libtcc" href="#Libtcc">7 The <code>libtcc</code> library</a>
<li><a name="toc_devel" href="#devel">8 Developer's guide</a>
<ul>
<li><a href="#devel">8.1 File reading</a>
<li><a href="#devel">8.2 Lexer</a>
<li><a href="#devel">8.3 Parser</a>
<li><a href="#devel">8.4 Types</a>
<li><a href="#devel">8.5 Symbols</a>
<li><a href="#devel">8.6 Sections</a>
<li><a href="#devel">8.7 Code generation</a>
<ul>
<li><a href="#devel">8.7.1 Introduction</a>
<li><a href="#devel">8.7.2 The value stack</a>
<li><a href="#devel">8.7.3 Manipulating the value stack</a>
<li><a href="#devel">8.7.4 CPU dependent code generation</a>
</li></ul>
<li><a href="#devel">8.8 Optimizations done</a>
</li></ul>
<li><a name="toc_devel" href="#devel">Concept Index</a>
</li></ul>
</div>
<div class="node">
<p><hr>
<a name="Top"></a>
Next: <a rel="next" accesskey="n" href="#Introduction">Introduction</a>,
Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
</div>
<h2 class="unnumbered">Tiny C Compiler Reference Documentation</h2>
<p>This manual documents version 0.9.29 of the Tiny C Compiler.
<ul class="menu">
<li><a accesskey="1" href="#Introduction">Introduction</a>: Introduction to tcc.
<li><a accesskey="2" href="#Invoke">Invoke</a>: Invocation of tcc (command line, options).
<li><a accesskey="3" href="#Clang">Clang</a>: ANSI C and extensions.
<li><a accesskey="4" href="#asm">asm</a>: Assembler syntax.
<li><a accesskey="5" href="#linker">linker</a>: Output file generation and supported targets.
<li><a accesskey="6" href="#Bounds">Bounds</a>: Automatic bounds-checking of C code.
<li><a accesskey="7" href="#Libtcc">Libtcc</a>: The libtcc library.
<li><a accesskey="8" href="#devel">devel</a>: Guide for Developers.
</ul>
<div class="node">
<p><hr>
<a name="Introduction"></a>
Next: <a rel="next" accesskey="n" href="#Invoke">Invoke</a>,
Previous: <a rel="previous" accesskey="p" href="#Top">Top</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">1 Introduction</h2>
<p>TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C
compilers, it is meant to be self-relying: you do not need an
external 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 may
not be necessary.
<p>TCC not only supports ANSI C, but also most of the new ISO C99
standard and many GNUC extensions including inline assembly.
<p>TCC can also be used to make <em>C scripts</em>, i.e. pieces of C source
that you run as a Perl or Python script. Compilation is so fast that
your script will be as fast as if it was an executable.
<p>TCC can also automatically generate memory and bound checks
(see <a href="#Bounds">Bounds</a>) while allowing all C pointers operations. TCC can do
these checks even if non patched libraries are used.
<p>With <code>libtcc</code>, you can use TCC as a backend for dynamic code
generation (see <a href="#Libtcc">Libtcc</a>).
<p>TCC mainly supports the i386 target on Linux and Windows. There are alpha
ports for the ARM (<code>arm-tcc</code>) and the TMS320C67xx targets
(<code>c67-tcc</code>). More information 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>.
<p>For usage on Windows, see also <a href="tcc-win32.txt">tcc-win32.txt</a>.
<div class="node">
<p><hr>
<a name="Invoke"></a>
Next: <a rel="next" accesskey="n" href="#Clang">Clang</a>,
Previous: <a rel="previous" accesskey="p" href="#Introduction">Introduction</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">2 Command line invocation</h2>
<h3 class="section">2.1 Quick start</h3>
<pre class="example"> <!-- man begin SYNOPSIS -->
usage: tcc [options] [<var>infile1</var> <var>infile2</var>...] [<samp><span class="option">-run</span></samp> <var>infile</var> <var>args</var>...]
<!-- man end -->
</pre>
<p class="noindent"><!-- man begin DESCRIPTION -->
TCC options are a very much like gcc options. The main difference is that TCC
can also execute directly the resulting program and give it runtime
arguments.
<p>Here are some examples to understand the logic:
<dl>
<dt><code>`<samp></code><span class="samp">tcc -run a.c</span><code></samp>'</code><dd>Compile <samp><span class="file">a.c</span></samp> and execute it directly
<br><dt><code>`<samp></code><span class="samp">tcc -run a.c arg1</span><code></samp>'</code><dd>Compile a.c and execute it directly. arg1 is given as first argument to
the <code>main()</code> of a.c.
<br><dt><code>`<samp></code><span class="samp">tcc a.c -run b.c arg1</span><code></samp>'</code><dd>Compile <samp><span class="file">a.c</span></samp> and <samp><span class="file">b.c</span></samp>, link them together and execute them. arg1 is given
as first argument to the <code>main()</code> of the resulting program.
<br><dt><code>`<samp></code><span class="samp">tcc -o myprog a.c b.c</span><code></samp>'</code><dd>Compile <samp><span class="file">a.c</span></samp> and <samp><span class="file">b.c</span></samp>, link them and generate the executable <samp><span class="file">myprog</span></samp>.
<br><dt><code>`<samp></code><span class="samp">tcc -o myprog a.o b.o</span><code></samp>'</code><dd>link <samp><span class="file">a.o</span></samp> and <samp><span class="file">b.o</span></samp> together and generate the executable <samp><span class="file">myprog</span></samp>.
<br><dt><code>`<samp></code><span class="samp">tcc -c a.c</span><code></samp>'</code><dd>Compile <samp><span class="file">a.c</span></samp> and generate object file <samp><span class="file">a.o</span></samp>.
<br><dt><code>`<samp></code><span class="samp">tcc -c asmfile.S</span><code></samp>'</code><dd>Preprocess with C preprocess and assemble <samp><span class="file">asmfile.S</span></samp> and generate
object file <samp><span class="file">asmfile.o</span></samp>.
<br><dt><code>`<samp></code><span class="samp">tcc -c asmfile.s</span><code></samp>'</code><dd>Assemble (but not preprocess) <samp><span class="file">asmfile.s</span></samp> and generate object file
<samp><span class="file">asmfile.o</span></samp>.
<br><dt><code>`<samp></code><span class="samp">tcc -r -o ab.o a.c b.c</span><code></samp>'</code><dd>Compile <samp><span class="file">a.c</span></samp> and <samp><span class="file">b.c</span></samp>, link them together and generate the object file <samp><span class="file">ab.o</span></samp>.
</dl>
<p>Scripting:
<p>TCC can be invoked from <em>scripts</em>, just as shell scripts. You just
need to add <code>#!/usr/local/bin/tcc -run</code> at the start of your C source:
<pre class="example"> #!/usr/local/bin/tcc -run
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
</pre>
<p>TCC can read C source code from <em>standard input</em> when <samp><span class="option">-</span></samp> is used in
place of <samp><span class="option">infile</span></samp>. Example:
<pre class="example"> echo 'main(){puts("hello");}' | tcc -run -
</pre>
<!-- man end -->
<h3 class="section">2.2 Option summary</h3>
<p>General Options:
<!-- man begin OPTIONS -->
<dl>
<dt><samp><span class="option">-c</span></samp><dd>Generate an object file.
<br><dt><samp><span class="option">-o outfile</span></samp><dd>Put object file, executable, or dll into output file <samp><span class="file">outfile</span></samp>.
<br><dt><samp><span class="option">-run source [args...]</span></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 a
script, several TCC options can be given <em>after</em> the
<samp><span class="option">-run</span></samp> option, separated by spaces:
<pre class="example"> tcc "-run -L/usr/X11R6/lib -lX11" ex4.c
</pre>
<p>In a script, it gives the following header:
<pre class="example"> #!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11
</pre>
<br><dt><samp><span class="option">-v</span></samp><dd>Display TCC version.
<br><dt><samp><span class="option">-vv</span></samp><dd>Show included files. As sole argument, print search dirs. -vvv shows tries too.
<br><dt><samp><span class="option">-bench</span></samp><dd>Display compilation statistics.
</dl>
<p>Preprocessor options:
<dl>
<dt><samp><span class="option">-Idir</span></samp><dd>Specify an additional include path. Include paths are searched in the
order they are specified.
<p>System include paths are always searched after. The default system
include paths are: <samp><span class="file">/usr/local/include</span></samp>, <samp><span class="file">/usr/include</span></samp>
and <samp><span class="file">PREFIX/lib/tcc/include</span></samp>. (<samp><span class="file">PREFIX</span></samp> is usually
<samp><span class="file">/usr</span></samp> or <samp><span class="file">/usr/local</span></samp>).
<br><dt><samp><span class="option">-Dsym[=val]</span></samp><dd>Define preprocessor symbol `<samp><span class="samp">sym</span></samp>' to
val. If val is not present, its value is `<samp><span class="samp">1</span></samp>'. Function-like macros can
also be defined: <samp><span class="option">-DF(a)=a+1</span></samp>
<br><dt><samp><span class="option">-Usym</span></samp><dd>Undefine preprocessor symbol `<samp><span class="samp">sym</span></samp>'.
<br><dt><samp><span class="option">-E</span></samp><dd>Preprocess only, to stdout or file (with -o).
</dl>
<p>Compilation flags:
<p>Note: each of the following options has a negative form beginning with
<samp><span class="option">-fno-</span></samp>.
<dl>
<dt><samp><span class="option">-funsigned-char</span></samp><dd>Let the <code>char</code> type be unsigned.
<br><dt><samp><span class="option">-fsigned-char</span></samp><dd>Let the <code>char</code> type be signed.
<br><dt><samp><span class="option">-fno-common</span></samp><dd>Do not generate common symbols for uninitialized data.
<br><dt><samp><span class="option">-fleading-underscore</span></samp><dd>Add a leading underscore at the beginning of each C symbol.
<br><dt><samp><span class="option">-fms-extensions</span></samp><dd>Allow a MS C compiler extensions to the language. Currently this
assumes a nested named structure declaration without an identifier
behaves like an unnamed one.
<br><dt><samp><span class="option">-fdollars-in-identifiers</span></samp><dd>Allow dollar signs in identifiers
<br><dt><samp><span class="option">-ftest-coverage</span></samp><dd>Create code coverage code. After running the resulting code an executable.tcov
or sofile.tcov file is generated with code coverage.
</dl>
<p>Warning options:
<dl>
<dt><samp><span class="option">-w</span></samp><dd>Disable all warnings.
</dl>
<p>Note: each of the following warning options has a negative form beginning with
<samp><span class="option">-Wno-</span></samp>.
<dl>
<dt><samp><span class="option">-Wimplicit-function-declaration</span></samp><dd>Warn about implicit function declaration.
<br><dt><samp><span class="option">-Wunsupported</span></samp><dd>Warn about unsupported GCC features that are ignored by TCC.
<br><dt><samp><span class="option">-Wwrite-strings</span></samp><dd>Make string constants be of type <code>const char *</code> instead of <code>char
*</code>.
<br><dt><samp><span class="option">-Werror</span></samp><dd>Abort compilation if a warning is issued. Can be given an option to enable
the specified warning and turn it into an error, for example
<samp><span class="option">-Werror=unsupported</span></samp>.
<br><dt><samp><span class="option">-Wall</span></samp><dd>Activate some useful warnings.
</dl>
<p>Linker options:
<dl>
<dt><samp><span class="option">-Ldir</span></samp><dd>Specify an additional static library path for the <samp><span class="option">-l</span></samp> option. The
default library paths are <samp><span class="file">/usr/local/lib</span></samp>, <samp><span class="file">/usr/lib</span></samp> and <samp><span class="file">/lib</span></samp>.
<br><dt><samp><span class="option">-lxxx</span></samp><dd>Link your program with dynamic library libxxx.so or static library
libxxx.a. The library is searched in the paths specified by the
<samp><span class="option">-L</span></samp> option and <samp><span class="env">LIBRARY_PATH</span></samp> variable.
<br><dt><samp><span class="option">-Bdir</span></samp><dd>Set the path where the tcc internal libraries (and include files) can be
found (default is <samp><span class="file">PREFIX/lib/tcc</span></samp>).
<br><dt><samp><span class="option">-shared</span></samp><dd>Generate a shared library instead of an executable.
<br><dt><samp><span class="option">-soname name</span></samp><dd>set name for shared library to be used at runtime
<br><dt><samp><span class="option">-static</span></samp><dd>Generate a statically linked executable (default is a shared linked
executable).
<br><dt><samp><span class="option">-rdynamic</span></samp><dd>Export global symbols to the dynamic linker. It is useful when a library
opened with <code>dlopen()</code> needs to access executable symbols.
<br><dt><samp><span class="option">-r</span></samp><dd>Generate an object file combining all input files.
<br><dt><samp><span class="option">-Wl,-rpath=path</span></samp><dd>Put custom search path for dynamic libraries into executable.
<br><dt><samp><span class="option">-Wl,--enable-new-dtags</span></samp><dd>When putting a custom search path for dynamic libraries into the executable,
create the new ELF dynamic tag DT_RUNPATH instead of the old legacy DT_RPATH.
<br><dt><samp><span class="option">-Wl,--oformat=fmt</span></samp><dd>Use <var>fmt</var> as output format. The supported output formats are:
<dl>
<dt><code>elf32-i386</code><dd>ELF output format (default)
<br><dt><code>binary</code><dd>Binary image (only for executable output)
<br><dt><code>coff</code><dd>COFF output format (only for executable output for TMS320C67xx target)
</dl>
<br><dt><samp><span class="option">-Wl,--export-all-symbols</span></samp><br><dt><samp><span class="option">-Wl,--export-dynamic</span></samp><dd>Export global symbols to the dynamic linker. It is useful when a library
opened with <code>dlopen()</code> needs to access executable symbols.
<br><dt><samp><span class="option">-Wl,-subsystem=console/gui/wince/...</span></samp><dd>Set type for PE (Windows) executables.
<br><dt><samp><span class="option">-Wl,-[Ttext=# | section-alignment=# | file-alignment=# | image-base=# | stack=#]</span></samp><dd>Modify executable layout.
<br><dt><samp><span class="option">-Wl,-Bsymbolic</span></samp><dd>Set DT_SYMBOLIC tag.
<br><dt><samp><span class="option">-Wl,-(no-)whole-archive</span></samp><dd>Turn on/off linking of all objects in archives.
</dl>
<p>Debugger options:
<dl>
<dt><samp><span class="option">-g</span></samp><dd>Generate run time stab debug information so that you get clear run time
error messages: <code> test.c:68: in function 'test5()': dereferencing
invalid pointer</code> instead of the laconic <code>Segmentation
fault</code>.
<br><dt><samp><span class="option">-gdwarf[-x]</span></samp><dd>Generate run time dwarf debug information instead of stab debug information.
<br><dt><samp><span class="option">-b</span></samp><dd>Generate additional support code to check memory allocations and array/pointer
bounds (see <a href="#Bounds">Bounds</a>). <samp><span class="option">-g</span></samp> is implied.
<br><dt><samp><span class="option">-bt[N]</span></samp><dd>Display N callers in stack traces. This is useful with <samp><span class="option">-g</span></samp> or <samp><span class="option">-b</span></samp>.
When activated, <code>__TCC_BACKTRACE__</code> is defined.
<p>With executables, additional support for stack traces is included. A function
<code> int tcc_backtrace(const char *fmt, ...); </code>
is provided to trigger a stack trace with a message on demand.
</dl>
<p>Misc options:
<dl>
<dt><samp><span class="option">-M</span></samp><dd>Just output makefile fragment with dependencies
<br><dt><samp><span class="option">-MM</span></samp><dd>Like -M except mention only user header files, not system header files.
<br><dt><samp><span class="option">-MD</span></samp><dd>Generate makefile fragment with dependencies.
<br><dt><samp><span class="option">-MMD</span></samp><dd>Like -MD except mention only user header files, not system header files.
<br><dt><samp><span class="option">-MF depfile</span></samp><dd>Use <samp><span class="file">depfile</span></samp> as output for -MD.
<br><dt><samp><span class="option">-print-search-dirs</span></samp><dd>Print the configured installation directory and a list of library
and include directories tcc will search.
<br><dt><samp><span class="option">-dumpversion</span></samp><dd>Print version.
</dl>
<p>Target specific options:
<dl>
<dt><samp><span class="option">-mms-bitfields</span></samp><dd>Use an algorithm for bitfield alignment consistent with MSVC. Default is
gcc's algorithm.
<br><dt><samp><span class="option">-mfloat-abi (ARM only)</span></samp><dd>Select the float ABI. Possible values: <code>softfp</code> and <code>hard</code>
<br><dt><samp><span class="option">-mno-sse</span></samp><dd>Do not use sse registers on x86_64
<br><dt><samp><span class="option">-m32, -m64</span></samp><dd>Pass command line to the i386/x86_64 cross compiler.
</dl>
<p>Note: GCC options <samp><span class="option">-Ox</span></samp>, <samp><span class="option">-fx</span></samp> and <samp><span class="option">-mx</span></samp> are
ignored.
<!-- man end -->
<!-- man begin ENVIRONMENT -->
<p>Environment variables that affect how tcc operates.
<dl>
<dt><samp><span class="option">CPATH</span></samp><br><dt><samp><span class="option">C_INCLUDE_PATH</span></samp><dd>A colon-separated list of directories searched for include files,
directories given with <samp><span class="option">-I</span></samp> are searched first.
<br><dt><samp><span class="option">LIBRARY_PATH</span></samp><dd>A colon-separated list of directories searched for libraries for the
<samp><span class="option">-l</span></samp> option, directories given with <samp><span class="option">-L</span></samp> are searched first.
</dl>
<!-- man end -->
<div class="node">
<p><hr>
<a name="Clang"></a>
Next: <a rel="next" accesskey="n" href="#asm">asm</a>,
Previous: <a rel="previous" accesskey="p" href="#Invoke">Invoke</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">3 C language support</h2>
<h3 class="section">3.1 ANSI C</h3>
<p>TCC implements all the ANSI C standard, including structure bit fields
and floating point numbers (<code>long double</code>, <code>double</code>, and
<code>float</code> fully supported).
<h3 class="section">3.2 ISOC99 extensions</h3>
<p>TCC implements many features of the new C standard: ISO C99. Currently
missing items are: complex and imaginary numbers.
<p>Currently implemented ISOC99 features:
<ul>
<li>variable length arrays.
<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 current
function name.
<li>Variadic macros: <code>__VA_ARGS__</code> can be used for
function-like macros:
<pre class="example"> #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)
</pre>
<p class="noindent"><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 class="example"> 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 class="example"> int *p = (int []){ 1, 2, 3 };
</pre>
<p>to initialize a pointer pointing to an initialized array. The same
works for structures and strings.
<li>Hexadecimal floating point constants are supported:
<pre class="example"> double d = 0x1234p10;
</pre>
<p class="noindent">is the same as writing
<pre class="example"> double d = 4771840.0;
</pre>
<li><code>inline</code> keyword is ignored.
<li><code>restrict</code> keyword is ignored.
</ul>
<h3 class="section">3.3 GNU C extensions</h3>
<p>TCC implements some GNU C extensions:
<ul>
<li>array designators can be used without '=':
<pre class="example"> int a[10] = { [0] 1, [5] 2, 3, 4 };
</pre>
<li>Structure field designators can be a label:
<pre class="example"> struct { int x, y; } st = { x: 1, y: 1};
</pre>
<p>instead of
<pre class="example"> 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 class="example"> switch(a) {
case 1 ... 9:
printf("range 1 to 9\n");
break;
default:
printf("unexpected\n");
break;
}
</pre>
<p><a name="index-aligned-attribute-1"></a><a name="index-packed-attribute-2"></a><a name="index-section-attribute-3"></a><a name="index-unused-attribute-4"></a><a name="index-cdecl-attribute-5"></a><a name="index-stdcall-attribute-6"></a><a name="index-regparm-attribute-7"></a><a name="index-dllexport-attribute-8"></a><a name="index-nodecorate-attribute-9"></a>
<li>The keyword <code>__attribute__</code> is handled to specify variable or
function attributes. The following attributes are supported:
<ul>
<li><code>aligned(n)</code>: align a variable or a structure field to n bytes
(must be a power of two).
<li><code>packed</code>: force alignment of a variable or a structure field to
1.
<li><code>section(name)</code>: generate function or data in assembly section
name (name is a string containing the section name) instead of the default
section.
<li><code>unused</code>: specify that the variable or the function is unused.
<li><code>cdecl</code>: use standard C calling convention (default).
<li><code>stdcall</code>: use Pascal-like calling convention.
<li><code>regparm(n)</code>: use fast i386 calling convention. <var>n</var> must be
between 1 and 3. The first <var>n</var> function parameters are respectively put in
registers <code>%eax</code>, <code>%edx</code> and <code>%ecx</code>.
<li><code>dllexport</code>: export function from dll/executable (win32 only)
<li><code>nodecorate</code>: do not apply any decorations that would otherwise be applied when exporting function from dll/executable (win32 only)
</ul>
<p>Here are some examples:
<pre class="example"> int a __attribute__ ((aligned(8), section(".mysection")));
</pre>
<p class="noindent">align variable <code>a</code> to 8 bytes and put it in section <code>.mysection</code>.
<pre class="example"> int my_add(int a, int b) __attribute__ ((section(".mycodesection")))
{
return a + b;
}
</pre>
<p class="noindent">generate function <code>my_add</code> in section <code>.mycodesection</code>.
<li>GNU style variadic macros:
<pre class="example"> #define dprintf(fmt, args...) printf(fmt, ## args)
dprintf("no arg\n");
dprintf("one arg %d\n", 1);
</pre>
<li><code>__FUNCTION__</code> is interpreted as C99 <code>__func__</code>
(so it has not exactly the same semantics as string literal GNUC
where it is a string literal).
<li>The <code>__alignof__</code> keyword can be used as <code>sizeof</code>
to get the alignment of a type or an expression.
<li>The <code>typeof(x)</code> returns the type of <code>x</code>.
<code>x</code> is an expression or a type.
<li>Computed gotos: <code>&&label</code> returns a pointer of type
<code>void *</code> on the goto label <code>label</code>. <code>goto *expr</code> can be
used to jump on the pointer resulting from <code>expr</code>.
<li>Inline assembly with asm instruction:
<a name="index-inline-assembly-10"></a><a name="index-assembly_002c-inline-11"></a><a name="index-g_t_005f_005fasm_005f_005f-12"></a>
<pre class="example"> static 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);
}
</pre>
<p class="noindent"><a name="index-gas-13"></a>TCC includes its own x86 inline assembler with a <code>gas</code>-like (GNU
assembler) syntax. No intermediate files are generated. GCC 3.x named
operands are supported.
<li><code>__builtin_types_compatible_p()</code> and <code>__builtin_constant_p()</code>
are supported.
<li><code>#pragma pack</code> is supported for win32 compatibility.
</ul>
<h3 class="section">3.4 TinyCC extensions</h3>
<ul>
<li><code>__TINYC__</code> is a predefined macro to indicate that you use TCC.
<li><code>#!</code> at the start of a line is ignored to allow scripting.
<li>Binary digits can be entered (<code>0b101</code> instead of
<code>5</code>).
</ul>
<div class="node">
<p><hr>
<a name="asm"></a>
Next: <a rel="next" accesskey="n" href="#linker">linker</a>,
Previous: <a rel="previous" accesskey="p" href="#Clang">Clang</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">4 TinyCC Assembler</h2>
<p>Since version 0.9.16, TinyCC integrates its own assembler. TinyCC
assembler supports a gas-like syntax (GNU assembler). You can
deactivate assembler support if you want a smaller TinyCC executable
(the C compiler does not rely on the assembler).
<p>TinyCC Assembler is used to handle files with <samp><span class="file">.S</span></samp> (C
preprocessed assembler) and <samp><span class="file">.s</span></samp> extensions. It is also used to
handle the GNU inline assembler with the <code>asm</code> keyword.
<h3 class="section">4.1 Syntax</h3>
<p>TinyCC Assembler supports most of the gas syntax. The tokens are the
same as C.
<ul>
<li>C and C++ comments are supported.
<li>Identifiers are the same as C, so you cannot use '.' or '$'.
<li>Only 32 bit integer numbers are supported.
</ul>
<h3 class="section">4.2 Expressions</h3>
<ul>
<li>Integers in decimal, octal and hexa are supported.
<li>Unary operators: +, -, ~.
<li>Binary operators in decreasing priority order:
<ol type=1 start=1>
<li>*, /, %
<li>&, |, ^
<li>+, -
</ol>
<li>A value is either an absolute number or a label plus an offset.
All operators accept absolute values except '+' and '-'. '+' or '-' can be
used to add an offset to a label. '-' supports two labels only if they
are the same or if they are both defined and in the same section.
</ul>
<h3 class="section">4.3 Labels</h3>
<ul>
<li>All labels are considered as local, except undefined ones.
<li>Numeric labels can be used as local <code>gas</code>-like labels.
They can be defined several times in the same source. Use 'b'
(backward) or 'f' (forward) as suffix to reference them:
<pre class="example"> 1:
jmp 1b /* jump to '1' label before */
jmp 1f /* jump to '1' label after */
1:
</pre>
</ul>
<h3 class="section">4.4 Directives</h3>
<p><a name="index-assembler-directives-14"></a><a name="index-directives_002c-assembler-15"></a><a name="index-align-directive-16"></a><a name="index-skip-directive-17"></a><a name="index-space-directive-18"></a><a name="index-byte-directive-19"></a><a name="index-word-directive-20"></a><a name="index-short-directive-21"></a><a name="index-int-directive-22"></a><a name="index-long-directive-23"></a><a name="index-quad-directive-24"></a><a name="index-globl-directive-25"></a><a name="index-global-directive-26"></a><a name="index-section-directive-27"></a><a name="index-text-directive-28"></a><a name="index-data-directive-29"></a><a name="index-bss-directive-30"></a><a name="index-fill-directive-31"></a><a name="index-org-directive-32"></a><a name="index-previous-directive-33"></a><a name="index-string-directive-34"></a><a name="index-asciz-directive-35"></a><a name="index-ascii-directive-36"></a>
All directives are preceded by a '.'. The following directives are
supported:
<ul>
<li>.align n[,value]
<li>.skip n[,value]
<li>.space n[,value]
<li>.byte value1[,...]
<li>.word value1[,...]
<li>.short value1[,...]
<li>.int value1[,...]
<li>.long value1[,...]
<li>.quad immediate_value1[,...]
<li>.globl symbol
<li>.global symbol
<li>.section section
<li>.text
<li>.data
<li>.bss
<li>.fill repeat[,size[,value]]
<li>.org n
<li>.previous
<li>.string string[,...]
<li>.asciz string[,...]
<li>.ascii string[,...]
</ul>
<h3 class="section">4.5 X86 Assembler</h3>
<p><a name="index-assembler-37"></a>
All X86 opcodes are supported. Only ATT syntax is supported (source
then destination operand order). If no size suffix is given, TinyCC
tries to guess it from the operand sizes.
<p>Currently, MMX opcodes are supported but not SSE ones.
<div class="node">
<p><hr>
<a name="linker"></a>
Next: <a rel="next" accesskey="n" href="#Bounds">Bounds</a>,
Previous: <a rel="previous" accesskey="p" href="#asm">asm</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">5 TinyCC Linker</h2>
<p><a name="index-linker-38"></a>
<h3 class="section">5.1 ELF file generation</h3>
<p><a name="index-ELF-39"></a>
TCC can directly output relocatable ELF files (object files),
executable ELF files and dynamic ELF libraries without relying on an
external linker.
<p>Dynamic ELF libraries can be output but the C compiler does not generate
position independent code (PIC). It means that the dynamic library
code generated by TCC cannot be factorized among processes yet.
<p>TCC linker eliminates unreferenced object code in libraries. A single pass is
done on the object and library list, so the order in which object files and
libraries are specified is important (same constraint as GNU ld). No grouping
options (<samp><span class="option">--start-group</span></samp> and <samp><span class="option">--end-group</span></samp>) are supported.
<h3 class="section">5.2 ELF file loader</h3>
<p>TCC can load ELF object files, archives (.a files) and dynamic
libraries (.so).
<h3 class="section">5.3 PE-i386 file generation</h3>
<p><a name="index-PE_002di386-40"></a>
TCC for Windows supports the native Win32 executable file format (PE-i386). It
generates EXE files (console and gui) and DLL files.
<p>For usage on Windows, see also tcc-win32.txt.
<h3 class="section">5.4 GNU Linker Scripts</h3>
<p><a name="index-scripts_002c-linker-41"></a><a name="index-linker-scripts-42"></a><a name="index-GROUP_002c-linker-command-43"></a><a name="index-FILE_002c-linker-command-44"></a><a name="index-OUTPUT_005fFORMAT_002c-linker-command-45"></a><a name="index-TARGET_002c-linker-command-46"></a>
Because on many Linux systems some dynamic libraries (such as
<samp><span class="file">/usr/lib/libc.so</span></samp>) are in fact GNU ld link scripts (horrible!),
the TCC linker also supports a subset of GNU ld scripts.
<p>The <code>GROUP</code> and <code>FILE</code> commands are supported. <code>OUTPUT_FORMAT</code>
and <code>TARGET</code> are ignored.
<p>Example from <samp><span class="file">/usr/lib/libc.so</span></samp>:
<pre class="example"> /* GNU ld script
Use the shared library, but some functions are only in
the static library, so try that secondarily. */
GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )
</pre>
<div class="node">
<p><hr>
<a name="Bounds"></a>
Next: <a rel="next" accesskey="n" href="#Libtcc">Libtcc</a>,
Previous: <a rel="previous" accesskey="p" href="#linker">linker</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">6 TinyCC Memory and Bound checks</h2>
<p><a name="index-bound-checks-47"></a><a name="index-memory-checks-48"></a>
This feature is activated with the <samp><span class="option">-b</span></samp> option (see <a href="#Invoke">Invoke</a>).
Here are some examples of caught errors:
<dl>
<dt>Invalid range with standard string function:<dd>
<pre class="example"> {
char tab[10];
memset(tab, 0, 11);
}
</pre>
<br><dt>Out of bounds-error in global or local arrays:<dd>
<pre class="example"> {
int tab[10];
for(i=0;i<11;i++) {
sum += tab[i];
}
}
</pre>
<br><dt>Out of bounds-error in malloc'ed data:<dd>
<pre class="example"> {
int *tab;
tab = malloc(20 * sizeof(int));
for(i=0;i<21;i++) {
sum += tab[i];
}
free(tab);
}
</pre>
<br><dt>Access of freed memory:<dd>
<pre class="example"> {
int *tab;
tab = malloc(20 * sizeof(int));
free(tab);
for(i=0;i<20;i++) {
sum += tab[i];
}
}
</pre>
<br><dt>Double free:<dd>
<pre class="example"> {
int *tab;
tab = malloc(20 * sizeof(int));
free(tab);
free(tab);
}
</pre>
</dl>
<p>TCC defines <code>__TCC_BCHECK__</code> if activated.
<p>There are five environment variables that can be used to control the behavior:
<ul>
<li>TCC_BOUNDS_WARN_POINTER_ADD
- Print warning when pointer add creates an illegal pointer.
<li>TCC_BOUNDS_PRINT_CALLS
- Print bound checking calls. Can be used for debugging.
<li>TCC_BOUNDS_PRINT_HEAP
- Print heap objects that are not freed at exit of program.
<li>TCC_BOUNDS_PRINT_STATISTIC
- Print statistic information at exit of program.
<li>TCC_BOUNDS_NEVER_FATAL
- Try to continue in case of a bound checking error.
</ul>
<p>Also, a function <code>__bounds_checking(x)</code> can be used to turn off/on bounds
checking from usercode (see below).
<p>Notes:
<ul>
<li>Only available on i386 (linux and windows), x86_64 (linux and windows),
arm, arm64 and riscv64 for the moment.
<li>The generated code is slower and bigger.
<li>The bound checking code is not included in shared libraries. The main
executable should always be compiled with the <samp><span class="option">-b</span></samp>.
<li>Pointer size is <em>unchanged</em> and code generated with bound checks is
<em>fully compatible</em> with unchecked code. When a pointer comes from
unchecked code, it is assumed to be valid. Even very obscure C code with
casts should work correctly.
<li>Signal handlers are not compatible with bounds checking. The
bounds checking code disables checking in signal/sigaction handlers.
The fork() function call in a multi threaded application is also a problem.
The bound checking code fixes this for the child process.
<li>The reason that signals and fork have problems is that we use locking
inside the bounds checking code.
Inside a signal handler we can not use locks. Also in a multi threaded
application after a fork the child process can have the lock set
by another thread.
<li>The BOUNDS_CHECKING_OFF and BOUNDS_CHECKING_ON can also be used to
disable bounds checking for some code.
<li>The __bounds_checking call adds a value to a thread local value.
The value starts at 0. If the value is not 0 the code is not checked
for bounds checking errors.
</ul>
<pre class="example"> #ifdef __TCC_BCHECK__
extern void __bounds_checking (int x);
# define BOUNDS_CHECKING_OFF __bounds_checking(1)
# define BOUNDS_CHECKING_ON __bounds_checking(-1)
#else
# define BOUNDS_CHECKING_OFF
# define BOUNDS_CHECKING_ON
#endif
</pre>
<p>For more information about the ideas behind this method, see
<a href="http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html">http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html</a>.
<div class="node">
<p><hr>
<a name="Libtcc"></a>
Next: <a rel="next" accesskey="n" href="#devel">devel</a>,
Previous: <a rel="previous" accesskey="p" href="#Bounds">Bounds</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">7 The <code>libtcc</code> library</h2>
<p>The <code>libtcc</code> library enables you to use TCC as a backend for
dynamic code generation.
<p>Read the <samp><span class="file">libtcc.h</span></samp> to have an overview of the API. Read
<samp><span class="file">libtcc_test.c</span></samp> to have a very simple example.
<p>The idea consists in giving a C string containing the program you want
to compile directly to <code>libtcc</code>. Then you can access to any global
symbol (function or variable) defined.
<div class="node">
<p><hr>
<a name="devel"></a>
Previous: <a rel="previous" accesskey="p" href="#Libtcc">Libtcc</a>,
Up: <a rel="up" accesskey="u" href="#Top">Top</a>
</div>
<h2 class="chapter">8 Developer's guide</h2>
<p>This chapter gives some hints to understand how TCC works. You can skip
it if you do not intend to modify the TCC code.
<h3 class="section">8.1 File reading</h3>
<p>The <code>BufferedFile</code> structure contains the context needed to read a
file, including the current line number. <code>tcc_open()</code> opens a new
file and <code>tcc_close()</code> closes it. <code>inp()</code> returns the next
character.
<h3 class="section">8.2 Lexer</h3>
<p><code>next()</code> reads the next token in the current
file. <code>next_nomacro()</code> reads the next token without macro
expansion.
<p><code>tok</code> contains the current token (see <code>TOK_xxx</code>)
constants. Identifiers and keywords are also keywords. <code>tokc</code>
contains additional infos about the token (for example a constant value
if number or string token).
<h3 class="section">8.3 Parser</h3>
<p>The parser is hardcoded (yacc is not necessary). It does only one pass,
except:
<ul>
<li>For initialized arrays with unknown size, a first pass
is done to count the number of elements.
<li>For architectures where arguments are evaluated in
reverse order, a first pass is done to reverse the argument order.
</ul>
<h3 class="section">8.4 Types</h3>
<p>The types are stored in a single 'int' variable. It was chosen in the
first stages of development when tcc was much simpler. Now, it may not
be the best solution.
<pre class="example"> #define VT_INT 0 /* integer type */
#define VT_BYTE 1 /* signed byte type */
#define VT_SHORT 2 /* short type */
#define VT_VOID 3 /* void type */
#define VT_PTR 4 /* pointer */
#define VT_ENUM 5 /* enum definition */
#define VT_FUNC 6 /* function type */
#define VT_STRUCT 7 /* struct/union definition */
#define VT_FLOAT 8 /* IEEE float */
#define VT_DOUBLE 9 /* IEEE double */
#define VT_LDOUBLE 10 /* IEEE long double */
#define VT_BOOL 11 /* ISOC99 boolean type */