tcc 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +0,0 @@
1
- /* Automatically generated by configure - do not modify */
2
- #ifndef CONFIG_TCCDIR
3
- # define CONFIG_TCCDIR "/home/quark/src/github/quark-zju/libtcc-ruby/ext/tcc/build/lib/tcc"
4
- #endif
5
- #define GCC_MAJOR 4
6
- #define GCC_MINOR 8
7
- #define HOST_X86_64 1
8
- #define TCC_VERSION "0.9.26"
@@ -1,28 +0,0 @@
1
- # Automatically generated by configure - do not modify
2
- prefix=/home/quark/src/github/quark-zju/libtcc-ruby/ext/tcc/build
3
- bindir=$(DESTDIR)/home/quark/src/github/quark-zju/libtcc-ruby/ext/tcc/build/bin
4
- tccdir=$(DESTDIR)/home/quark/src/github/quark-zju/libtcc-ruby/ext/tcc/build/lib/tcc
5
- libdir=$(DESTDIR)/home/quark/src/github/quark-zju/libtcc-ruby/ext/tcc/build/lib
6
- ln_libdir=/home/quark/src/github/quark-zju/libtcc-ruby/ext/tcc/build/lib
7
- includedir=$(DESTDIR)/home/quark/src/github/quark-zju/libtcc-ruby/ext/tcc/build/include
8
- mandir=$(DESTDIR)/home/quark/src/github/quark-zju/libtcc-ruby/ext/tcc/build/share/man
9
- infodir=$(DESTDIR)/home/quark/src/github/quark-zju/libtcc-ruby/ext/tcc/build/share/info
10
- docdir=$(DESTDIR)/home/quark/src/github/quark-zju/libtcc-ruby/ext/tcc/build/share/doc/tcc
11
- CC=gcc
12
- GCC_MAJOR=4
13
- GCC_MINOR=8
14
- HOST_CC=gcc
15
- AR=ar
16
- STRIP=strip -s -R .comment -R .note
17
- CFLAGS=-Wall -g -O2
18
- LDFLAGS=
19
- LIBSUF=.a
20
- EXESUF=
21
- NATIVE_DEFINES+=-DCONFIG_LDDIR="\"lib64\""
22
- ARCH=x86-64
23
- TARGETOS=Linux
24
- DISABLE_STATIC=yes
25
- VERSION=0.9.26
26
- SRC_PATH=/home/quark/src/github/quark-zju/libtcc-ruby/ext/tcc/tcc-0.9.26
27
- top_srcdir=$(TOP)
28
- top_builddir=$(TOP)
@@ -1 +0,0 @@
1
- @set VERSION 0.9.26
@@ -1,1151 +0,0 @@
1
- This is tcc-doc.info, produced by makeinfo version 5.2 from
2
- tcc-doc.texi.
3
-
4
- INFO-DIR-SECTION Software development
5
- START-INFO-DIR-ENTRY
6
- * TCC: (tcc-doc). The Tiny C Compiler.
7
- END-INFO-DIR-ENTRY
8
-
9
- 
10
- File: tcc-doc.info, Node: Top, Next: Introduction, Prev: (dir), Up: (dir)
11
-
12
- Tiny C Compiler Reference Documentation
13
- ***************************************
14
-
15
- This manual documents version 0.9.26 of the Tiny C Compiler.
16
-
17
- * Menu:
18
-
19
- * Introduction:: Introduction to tcc.
20
- * Invoke:: Invocation of tcc (command line, options).
21
- * Clang:: ANSI C and extensions.
22
- * asm:: Assembler syntax.
23
- * linker:: Output file generation and supported targets.
24
- * Bounds:: Automatic bounds-checking of C code.
25
- * Libtcc:: The libtcc library.
26
- * devel:: Guide for Developers.
27
-
28
- 
29
- File: tcc-doc.info, Node: Introduction, Next: Invoke, Up: Top
30
-
31
- 1 Introduction
32
- **************
33
-
34
- TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C
35
- compilers, it is meant to be self-relying: you do not need an external
36
- assembler or linker because TCC does that for you.
37
-
38
- TCC compiles so _fast_ that even for big projects 'Makefile's may not
39
- be necessary.
40
-
41
- TCC not only supports ANSI C, but also most of the new ISO C99
42
- standard and many GNUC extensions including inline assembly.
43
-
44
- TCC can also be used to make _C scripts_, i.e. pieces of C source
45
- that you run as a Perl or Python script. Compilation is so fast that
46
- your script will be as fast as if it was an executable.
47
-
48
- TCC can also automatically generate memory and bound checks (*note
49
- Bounds::) while allowing all C pointers operations. TCC can do these
50
- checks even if non patched libraries are used.
51
-
52
- With 'libtcc', you can use TCC as a backend for dynamic code
53
- generation (*note Libtcc::).
54
-
55
- TCC mainly supports the i386 target on Linux and Windows. There are
56
- alpha ports for the ARM ('arm-tcc') and the TMS320C67xx targets
57
- ('c67-tcc'). More information about the ARM port is available at
58
- <http://lists.gnu.org/archive/html/tinycc-devel/2003-10/msg00044.html>.
59
-
60
- For usage on Windows, see also <tcc-win32.txt>.
61
-
62
- 
63
- File: tcc-doc.info, Node: Invoke, Next: Clang, Prev: Introduction, Up: Top
64
-
65
- 2 Command line invocation
66
- *************************
67
-
68
- 2.1 Quick start
69
- ===============
70
-
71
- usage: tcc [options] [INFILE1 INFILE2...] [-run INFILE ARGS...]
72
-
73
- TCC options are a very much like gcc options. The main difference is
74
- that TCC can also execute directly the resulting program and give it
75
- runtime arguments.
76
-
77
- Here are some examples to understand the logic:
78
-
79
- ''tcc -run a.c''
80
- Compile 'a.c' and execute it directly
81
-
82
- ''tcc -run a.c arg1''
83
- Compile a.c and execute it directly. arg1 is given as first
84
- argument to the 'main()' of a.c.
85
-
86
- ''tcc a.c -run b.c arg1''
87
- Compile 'a.c' and 'b.c', link them together and execute them. arg1
88
- is given as first argument to the 'main()' of the resulting
89
- program.
90
-
91
- ''tcc -o myprog a.c b.c''
92
- Compile 'a.c' and 'b.c', link them and generate the executable
93
- 'myprog'.
94
-
95
- ''tcc -o myprog a.o b.o''
96
- link 'a.o' and 'b.o' together and generate the executable 'myprog'.
97
-
98
- ''tcc -c a.c''
99
- Compile 'a.c' and generate object file 'a.o'.
100
-
101
- ''tcc -c asmfile.S''
102
- Preprocess with C preprocess and assemble 'asmfile.S' and generate
103
- object file 'asmfile.o'.
104
-
105
- ''tcc -c asmfile.s''
106
- Assemble (but not preprocess) 'asmfile.s' and generate object file
107
- 'asmfile.o'.
108
-
109
- ''tcc -r -o ab.o a.c b.c''
110
- Compile 'a.c' and 'b.c', link them together and generate the object
111
- file 'ab.o'.
112
-
113
- Scripting:
114
-
115
- TCC can be invoked from _scripts_, just as shell scripts. You just
116
- need to add '#!/usr/local/bin/tcc -run' at the start of your C source:
117
-
118
- #!/usr/local/bin/tcc -run
119
- #include <stdio.h>
120
-
121
- int main()
122
- {
123
- printf("Hello World\n");
124
- return 0;
125
- }
126
-
127
- TCC can read C source code from _standard input_ when '-' is used in
128
- place of 'infile'. Example:
129
-
130
- echo 'main(){puts("hello");}' | tcc -run -
131
-
132
- 2.2 Option summary
133
- ==================
134
-
135
- General Options:
136
-
137
- '-c'
138
- Generate an object file.
139
-
140
- '-o outfile'
141
- Put object file, executable, or dll into output file 'outfile'.
142
-
143
- '-run source [args...]'
144
- Compile file SOURCE and run it with the command line arguments
145
- ARGS. In order to be able to give more than one argument to a
146
- script, several TCC options can be given _after_ the '-run' option,
147
- separated by spaces:
148
- tcc "-run -L/usr/X11R6/lib -lX11" ex4.c
149
- In a script, it gives the following header:
150
- #!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11
151
-
152
- '-dumpversion'
153
- Print only the compiler version and nothing else.
154
-
155
- '-v'
156
- Display TCC version.
157
-
158
- '-vv'
159
- Show included files. As sole argument, print search dirs (as
160
- below).
161
-
162
- '-bench'
163
- Display compilation statistics.
164
-
165
- '-print-search-dirs'
166
- Print the configured installation directory and a list of library
167
- and include directories tcc will search.
168
-
169
- Preprocessor options:
170
-
171
- '-Idir'
172
- Specify an additional include path. Include paths are searched in
173
- the order they are specified.
174
-
175
- System include paths are always searched after. The default system
176
- include paths are: '/usr/local/include', '/usr/include' and
177
- 'PREFIX/lib/tcc/include'. ('PREFIX' is usually '/usr' or
178
- '/usr/local').
179
-
180
- '-Dsym[=val]'
181
- Define preprocessor symbol 'sym' to val. If val is not present,
182
- its value is '1'. Function-like macros can also be defined:
183
- '-DF(a)=a+1'
184
-
185
- '-Usym'
186
- Undefine preprocessor symbol 'sym'.
187
-
188
- Compilation flags:
189
-
190
- Note: each of the following warning options has a negative form
191
- beginning with '-fno-'.
192
-
193
- '-funsigned-char'
194
- Let the 'char' type be unsigned.
195
-
196
- '-fsigned-char'
197
- Let the 'char' type be signed.
198
-
199
- '-fno-common'
200
- Do not generate common symbols for uninitialized data.
201
-
202
- '-fleading-underscore'
203
- Add a leading underscore at the beginning of each C symbol.
204
-
205
- Warning options:
206
-
207
- '-w'
208
- Disable all warnings.
209
-
210
- Note: each of the following warning options has a negative form
211
- beginning with '-Wno-'.
212
-
213
- '-Wimplicit-function-declaration'
214
- Warn about implicit function declaration.
215
-
216
- '-Wunsupported'
217
- Warn about unsupported GCC features that are ignored by TCC.
218
-
219
- '-Wwrite-strings'
220
- Make string constants be of type 'const char *' instead of 'char
221
- *'.
222
-
223
- '-Werror'
224
- Abort compilation if warnings are issued.
225
-
226
- '-Wall'
227
- Activate all warnings, except '-Werror', '-Wunusupported' and
228
- '-Wwrite-strings'.
229
-
230
- Linker options:
231
-
232
- '-Ldir'
233
- Specify an additional static library path for the '-l' option. The
234
- default library paths are '/usr/local/lib', '/usr/lib' and '/lib'.
235
-
236
- '-lxxx'
237
- Link your program with dynamic library libxxx.so or static library
238
- libxxx.a. The library is searched in the paths specified by the
239
- '-L' option.
240
-
241
- '-Bdir'
242
- Set the path where the tcc internal libraries (and include files)
243
- can be found (default is 'PREFIX/lib/tcc').
244
-
245
- '-shared'
246
- Generate a shared library instead of an executable.
247
-
248
- '-soname name'
249
- set name for shared library to be used at runtime
250
-
251
- '-static'
252
- Generate a statically linked executable (default is a shared linked
253
- executable).
254
-
255
- '-rdynamic'
256
- Export global symbols to the dynamic linker. It is useful when a
257
- library opened with 'dlopen()' needs to access executable symbols.
258
-
259
- '-r'
260
- Generate an object file combining all input files.
261
-
262
- '-Wl,-rpath=path'
263
- Put custom seatch path for dynamic libraries into executable.
264
-
265
- '-Wl,--oformat=fmt'
266
- Use FMT as output format. The supported output formats are:
267
- 'elf32-i386'
268
- ELF output format (default)
269
- 'binary'
270
- Binary image (only for executable output)
271
- 'coff'
272
- COFF output format (only for executable output for TMS320C67xx
273
- target)
274
-
275
- '-Wl,-subsystem=console/gui/wince/...'
276
- Set type for PE (Windows) executables.
277
-
278
- '-Wl,-[Ttext=# | section-alignment=# | file-alignment=# | image-base=# | stack=#]'
279
- Modify executable layout.
280
-
281
- '-Wl,-Bsymbolic'
282
- Set DT_SYMBOLIC tag.
283
-
284
- Debugger options:
285
-
286
- '-g'
287
- Generate run time debug information so that you get clear run time
288
- error messages: ' test.c:68: in function 'test5()': dereferencing
289
- invalid pointer' instead of the laconic 'Segmentation fault'.
290
-
291
- '-b'
292
- Generate additional support code to check memory allocations and
293
- array/pointer bounds. '-g' is implied. Note that the generated
294
- code is slower and bigger in this case.
295
-
296
- Note: '-b' is only available on i386 for the moment.
297
-
298
- '-bt N'
299
- Display N callers in stack traces. This is useful with '-g' or
300
- '-b'.
301
-
302
- Misc options:
303
-
304
- '-MD'
305
- Generate makefile fragment with dependencies.
306
-
307
- '-MF depfile'
308
- Use 'depfile' as output for -MD.
309
-
310
- Note: GCC options '-Ox', '-fx' and '-mx' are ignored.
311
-
312
- 
313
- File: tcc-doc.info, Node: Clang, Next: asm, Prev: Invoke, Up: Top
314
-
315
- 3 C language support
316
- ********************
317
-
318
- 3.1 ANSI C
319
- ==========
320
-
321
- TCC implements all the ANSI C standard, including structure bit fields
322
- and floating point numbers ('long double', 'double', and 'float' fully
323
- supported).
324
-
325
- 3.2 ISOC99 extensions
326
- =====================
327
-
328
- TCC implements many features of the new C standard: ISO C99. Currently
329
- missing items are: complex and imaginary numbers and variable length
330
- arrays.
331
-
332
- Currently implemented ISOC99 features:
333
-
334
- * 64 bit 'long long' types are fully supported.
335
-
336
- * The boolean type '_Bool' is supported.
337
-
338
- * '__func__' is a string variable containing the current function
339
- name.
340
-
341
- * Variadic macros: '__VA_ARGS__' can be used for function-like
342
- macros:
343
- #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)
344
-
345
- 'dprintf' can then be used with a variable number of parameters.
346
-
347
- * Declarations can appear anywhere in a block (as in C++).
348
-
349
- * Array and struct/union elements can be initialized in any order by
350
- using designators:
351
- struct { int x, y; } st[10] = { [0].x = 1, [0].y = 2 };
352
-
353
- int tab[10] = { 1, 2, [5] = 5, [9] = 9};
354
-
355
- * Compound initializers are supported:
356
- int *p = (int []){ 1, 2, 3 };
357
- to initialize a pointer pointing to an initialized array. The same
358
- works for structures and strings.
359
-
360
- * Hexadecimal floating point constants are supported:
361
- double d = 0x1234p10;
362
-
363
- is the same as writing
364
- double d = 4771840.0;
365
-
366
- * 'inline' keyword is ignored.
367
-
368
- * 'restrict' keyword is ignored.
369
-
370
- 3.3 GNU C extensions
371
- ====================
372
-
373
- TCC implements some GNU C extensions:
374
-
375
- * array designators can be used without '=':
376
- int a[10] = { [0] 1, [5] 2, 3, 4 };
377
-
378
- * Structure field designators can be a label:
379
- struct { int x, y; } st = { x: 1, y: 1};
380
- instead of
381
- struct { int x, y; } st = { .x = 1, .y = 1};
382
-
383
- * '\e' is ASCII character 27.
384
-
385
- * case ranges : ranges can be used in 'case's:
386
- switch(a) {
387
- case 1 ... 9:
388
- printf("range 1 to 9\n");
389
- break;
390
- default:
391
- printf("unexpected\n");
392
- break;
393
- }
394
-
395
- * The keyword '__attribute__' is handled to specify variable or
396
- function attributes. The following attributes are supported:
397
-
398
- * 'aligned(n)': align a variable or a structure field to n bytes
399
- (must be a power of two).
400
-
401
- * 'packed': force alignment of a variable or a structure field
402
- to 1.
403
-
404
- * 'section(name)': generate function or data in assembly section
405
- name (name is a string containing the section name) instead of
406
- the default section.
407
-
408
- * 'unused': specify that the variable or the function is unused.
409
-
410
- * 'cdecl': use standard C calling convention (default).
411
-
412
- * 'stdcall': use Pascal-like calling convention.
413
-
414
- * 'regparm(n)': use fast i386 calling convention. N must be
415
- between 1 and 3. The first N function parameters are
416
- respectively put in registers '%eax', '%edx' and '%ecx'.
417
-
418
- * 'dllexport': export function from dll/executable (win32 only)
419
-
420
- Here are some examples:
421
- int a __attribute__ ((aligned(8), section(".mysection")));
422
-
423
- align variable 'a' to 8 bytes and put it in section '.mysection'.
424
-
425
- int my_add(int a, int b) __attribute__ ((section(".mycodesection")))
426
- {
427
- return a + b;
428
- }
429
-
430
- generate function 'my_add' in section '.mycodesection'.
431
-
432
- * GNU style variadic macros:
433
- #define dprintf(fmt, args...) printf(fmt, ## args)
434
-
435
- dprintf("no arg\n");
436
- dprintf("one arg %d\n", 1);
437
-
438
- * '__FUNCTION__' is interpreted as C99 '__func__' (so it has not
439
- exactly the same semantics as string literal GNUC where it is a
440
- string literal).
441
-
442
- * The '__alignof__' keyword can be used as 'sizeof' to get the
443
- alignment of a type or an expression.
444
-
445
- * The 'typeof(x)' returns the type of 'x'. 'x' is an expression or a
446
- type.
447
-
448
- * Computed gotos: '&&label' returns a pointer of type 'void *' on the
449
- goto label 'label'. 'goto *expr' can be used to jump on the
450
- pointer resulting from 'expr'.
451
-
452
- * Inline assembly with asm instruction:
453
- static inline void * my_memcpy(void * to, const void * from, size_t n)
454
- {
455
- int d0, d1, d2;
456
- __asm__ __volatile__(
457
- "rep ; movsl\n\t"
458
- "testb $2,%b4\n\t"
459
- "je 1f\n\t"
460
- "movsw\n"
461
- "1:\ttestb $1,%b4\n\t"
462
- "je 2f\n\t"
463
- "movsb\n"
464
- "2:"
465
- : "=&c" (d0), "=&D" (d1), "=&S" (d2)
466
- :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
467
- : "memory");
468
- return (to);
469
- }
470
-
471
- TCC includes its own x86 inline assembler with a 'gas'-like (GNU
472
- assembler) syntax. No intermediate files are generated. GCC 3.x
473
- named operands are supported.
474
-
475
- * '__builtin_types_compatible_p()' and '__builtin_constant_p()' are
476
- supported.
477
-
478
- * '#pragma pack' is supported for win32 compatibility.
479
-
480
- 3.4 TinyCC extensions
481
- =====================
482
-
483
- * '__TINYC__' is a predefined macro to '1' to indicate that you use
484
- TCC.
485
-
486
- * '#!' at the start of a line is ignored to allow scripting.
487
-
488
- * Binary digits can be entered ('0b101' instead of '5').
489
-
490
- * '__BOUNDS_CHECKING_ON' is defined if bound checking is activated.
491
-
492
- 
493
- File: tcc-doc.info, Node: asm, Next: linker, Prev: Clang, Up: Top
494
-
495
- 4 TinyCC Assembler
496
- ******************
497
-
498
- Since version 0.9.16, TinyCC integrates its own assembler. TinyCC
499
- assembler supports a gas-like syntax (GNU assembler). You can
500
- desactivate assembler support if you want a smaller TinyCC executable
501
- (the C compiler does not rely on the assembler).
502
-
503
- TinyCC Assembler is used to handle files with '.S' (C preprocessed
504
- assembler) and '.s' extensions. It is also used to handle the GNU
505
- inline assembler with the 'asm' keyword.
506
-
507
- 4.1 Syntax
508
- ==========
509
-
510
- TinyCC Assembler supports most of the gas syntax. The tokens are the
511
- same as C.
512
-
513
- * C and C++ comments are supported.
514
-
515
- * Identifiers are the same as C, so you cannot use '.' or '$'.
516
-
517
- * Only 32 bit integer numbers are supported.
518
-
519
- 4.2 Expressions
520
- ===============
521
-
522
- * Integers in decimal, octal and hexa are supported.
523
-
524
- * Unary operators: +, -, ~.
525
-
526
- * Binary operators in decreasing priority order:
527
-
528
- 1. *, /, %
529
- 2. &, |, ^
530
- 3. +, -
531
-
532
- * A value is either an absolute number or a label plus an offset.
533
- All operators accept absolute values except '+' and '-'. '+' or
534
- '-' can be used to add an offset to a label. '-' supports two
535
- labels only if they are the same or if they are both defined and in
536
- the same section.
537
-
538
- 4.3 Labels
539
- ==========
540
-
541
- * All labels are considered as local, except undefined ones.
542
-
543
- * Numeric labels can be used as local 'gas'-like labels. They can be
544
- defined several times in the same source. Use 'b' (backward) or
545
- 'f' (forward) as suffix to reference them:
546
-
547
- 1:
548
- jmp 1b /* jump to '1' label before */
549
- jmp 1f /* jump to '1' label after */
550
- 1:
551
-
552
- 4.4 Directives
553
- ==============
554
-
555
- All directives are preceeded by a '.'. The following directives are
556
- supported:
557
-
558
- * .align n[,value]
559
- * .skip n[,value]
560
- * .space n[,value]
561
- * .byte value1[,...]
562
- * .word value1[,...]
563
- * .short value1[,...]
564
- * .int value1[,...]
565
- * .long value1[,...]
566
- * .quad immediate_value1[,...]
567
- * .globl symbol
568
- * .global symbol
569
- * .section section
570
- * .text
571
- * .data
572
- * .bss
573
- * .fill repeat[,size[,value]]
574
- * .org n
575
- * .previous
576
- * .string string[,...]
577
- * .asciz string[,...]
578
- * .ascii string[,...]
579
-
580
- 4.5 X86 Assembler
581
- =================
582
-
583
- All X86 opcodes are supported. Only ATT syntax is supported (source
584
- then destination operand order). If no size suffix is given, TinyCC
585
- tries to guess it from the operand sizes.
586
-
587
- Currently, MMX opcodes are supported but not SSE ones.
588
-
589
- 
590
- File: tcc-doc.info, Node: linker, Next: Bounds, Prev: asm, Up: Top
591
-
592
- 5 TinyCC Linker
593
- ***************
594
-
595
- 5.1 ELF file generation
596
- =======================
597
-
598
- TCC can directly output relocatable ELF files (object files), executable
599
- ELF files and dynamic ELF libraries without relying on an external
600
- linker.
601
-
602
- Dynamic ELF libraries can be output but the C compiler does not
603
- generate position independent code (PIC). It means that the dynamic
604
- library code generated by TCC cannot be factorized among processes yet.
605
-
606
- TCC linker eliminates unreferenced object code in libraries. A
607
- single pass is done on the object and library list, so the order in
608
- which object files and libraries are specified is important (same
609
- constraint as GNU ld). No grouping options ('--start-group' and
610
- '--end-group') are supported.
611
-
612
- 5.2 ELF file loader
613
- ===================
614
-
615
- TCC can load ELF object files, archives (.a files) and dynamic libraries
616
- (.so).
617
-
618
- 5.3 PE-i386 file generation
619
- ===========================
620
-
621
- TCC for Windows supports the native Win32 executable file format
622
- (PE-i386). It generates EXE files (console and gui) and DLL files.
623
-
624
- For usage on Windows, see also tcc-win32.txt.
625
-
626
- 5.4 GNU Linker Scripts
627
- ======================
628
-
629
- Because on many Linux systems some dynamic libraries (such as
630
- '/usr/lib/libc.so') are in fact GNU ld link scripts (horrible!), the TCC
631
- linker also supports a subset of GNU ld scripts.
632
-
633
- The 'GROUP' and 'FILE' commands are supported. 'OUTPUT_FORMAT' and
634
- 'TARGET' are ignored.
635
-
636
- Example from '/usr/lib/libc.so':
637
- /* GNU ld script
638
- Use the shared library, but some functions are only in
639
- the static library, so try that secondarily. */
640
- GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )
641
-
642
- 
643
- File: tcc-doc.info, Node: Bounds, Next: Libtcc, Prev: linker, Up: Top
644
-
645
- 6 TinyCC Memory and Bound checks
646
- ********************************
647
-
648
- This feature is activated with the '-b' (*note Invoke::).
649
-
650
- Note that pointer size is _unchanged_ and that code generated with
651
- bound checks is _fully compatible_ with unchecked code. When a pointer
652
- comes from unchecked code, it is assumed to be valid. Even very obscure
653
- C code with casts should work correctly.
654
-
655
- For more information about the ideas behind this method, see
656
- <http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html>.
657
-
658
- Here are some examples of caught errors:
659
-
660
- Invalid range with standard string function:
661
- {
662
- char tab[10];
663
- memset(tab, 0, 11);
664
- }
665
-
666
- Out of bounds-error in global or local arrays:
667
- {
668
- int tab[10];
669
- for(i=0;i<11;i++) {
670
- sum += tab[i];
671
- }
672
- }
673
-
674
- Out of bounds-error in malloc'ed data:
675
- {
676
- int *tab;
677
- tab = malloc(20 * sizeof(int));
678
- for(i=0;i<21;i++) {
679
- sum += tab4[i];
680
- }
681
- free(tab);
682
- }
683
-
684
- Access of freed memory:
685
- {
686
- int *tab;
687
- tab = malloc(20 * sizeof(int));
688
- free(tab);
689
- for(i=0;i<20;i++) {
690
- sum += tab4[i];
691
- }
692
- }
693
-
694
- Double free:
695
- {
696
- int *tab;
697
- tab = malloc(20 * sizeof(int));
698
- free(tab);
699
- free(tab);
700
- }
701
-
702
- 
703
- File: tcc-doc.info, Node: Libtcc, Next: devel, Prev: Bounds, Up: Top
704
-
705
- 7 The 'libtcc' library
706
- **********************
707
-
708
- The 'libtcc' library enables you to use TCC as a backend for dynamic
709
- code generation.
710
-
711
- Read the 'libtcc.h' to have an overview of the API. Read
712
- 'libtcc_test.c' to have a very simple example.
713
-
714
- The idea consists in giving a C string containing the program you
715
- want to compile directly to 'libtcc'. Then you can access to any global
716
- symbol (function or variable) defined.
717
-
718
- 
719
- File: tcc-doc.info, Node: devel, Prev: Libtcc, Up: Top
720
-
721
- 8 Developer's guide
722
- *******************
723
-
724
- This chapter gives some hints to understand how TCC works. You can skip
725
- it if you do not intend to modify the TCC code.
726
-
727
- 8.1 File reading
728
- ================
729
-
730
- The 'BufferedFile' structure contains the context needed to read a file,
731
- including the current line number. 'tcc_open()' opens a new file and
732
- 'tcc_close()' closes it. 'inp()' returns the next character.
733
-
734
- 8.2 Lexer
735
- =========
736
-
737
- 'next()' reads the next token in the current file. 'next_nomacro()'
738
- reads the next token without macro expansion.
739
-
740
- 'tok' contains the current token (see 'TOK_xxx') constants.
741
- Identifiers and keywords are also keywords. 'tokc' contains additional
742
- infos about the token (for example a constant value if number or string
743
- token).
744
-
745
- 8.3 Parser
746
- ==========
747
-
748
- The parser is hardcoded (yacc is not necessary). It does only one pass,
749
- except:
750
-
751
- * For initialized arrays with unknown size, a first pass is done to
752
- count the number of elements.
753
-
754
- * For architectures where arguments are evaluated in reverse order, a
755
- first pass is done to reverse the argument order.
756
-
757
- 8.4 Types
758
- =========
759
-
760
- The types are stored in a single 'int' variable. It was choosen in the
761
- first stages of development when tcc was much simpler. Now, it may not
762
- be the best solution.
763
-
764
- #define VT_INT 0 /* integer type */
765
- #define VT_BYTE 1 /* signed byte type */
766
- #define VT_SHORT 2 /* short type */
767
- #define VT_VOID 3 /* void type */
768
- #define VT_PTR 4 /* pointer */
769
- #define VT_ENUM 5 /* enum definition */
770
- #define VT_FUNC 6 /* function type */
771
- #define VT_STRUCT 7 /* struct/union definition */
772
- #define VT_FLOAT 8 /* IEEE float */
773
- #define VT_DOUBLE 9 /* IEEE double */
774
- #define VT_LDOUBLE 10 /* IEEE long double */
775
- #define VT_BOOL 11 /* ISOC99 boolean type */
776
- #define VT_LLONG 12 /* 64 bit integer */
777
- #define VT_LONG 13 /* long integer (NEVER USED as type, only
778
- during parsing) */
779
- #define VT_BTYPE 0x000f /* mask for basic type */
780
- #define VT_UNSIGNED 0x0010 /* unsigned type */
781
- #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
782
- #define VT_VLA 0x20000 /* VLA type (also has VT_PTR and VT_ARRAY) */
783
- #define VT_BITFIELD 0x0040 /* bitfield modifier */
784
- #define VT_CONSTANT 0x0800 /* const modifier */
785
- #define VT_VOLATILE 0x1000 /* volatile modifier */
786
- #define VT_SIGNED 0x2000 /* signed type */
787
-
788
- #define VT_STRUCT_SHIFT 18 /* structure/enum name shift (14 bits left) */
789
-
790
- When a reference to another type is needed (for pointers, functions
791
- and structures), the '32 - VT_STRUCT_SHIFT' high order bits are used to
792
- store an identifier reference.
793
-
794
- The 'VT_UNSIGNED' flag can be set for chars, shorts, ints and long
795
- longs.
796
-
797
- Arrays are considered as pointers 'VT_PTR' with the flag 'VT_ARRAY'
798
- set. Variable length arrays are considered as special arrays and have
799
- flag 'VT_VLA' set instead of 'VT_ARRAY'.
800
-
801
- The 'VT_BITFIELD' flag can be set for chars, shorts, ints and long
802
- longs. If it is set, then the bitfield position is stored from bits
803
- VT_STRUCT_SHIFT to VT_STRUCT_SHIFT + 5 and the bit field size is stored
804
- from bits VT_STRUCT_SHIFT + 6 to VT_STRUCT_SHIFT + 11.
805
-
806
- 'VT_LONG' is never used except during parsing.
807
-
808
- During parsing, the storage of an object is also stored in the type
809
- integer:
810
-
811
- #define VT_EXTERN 0x00000080 /* extern definition */
812
- #define VT_STATIC 0x00000100 /* static variable */
813
- #define VT_TYPEDEF 0x00000200 /* typedef definition */
814
- #define VT_INLINE 0x00000400 /* inline definition */
815
- #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
816
- #define VT_EXPORT 0x00008000 /* win32: data exported from dll */
817
- #define VT_WEAK 0x00010000 /* win32: data exported from dll */
818
-
819
- 8.5 Symbols
820
- ===========
821
-
822
- All symbols are stored in hashed symbol stacks. Each symbol stack
823
- contains 'Sym' structures.
824
-
825
- 'Sym.v' contains the symbol name (remember an idenfier is also a
826
- token, so a string is never necessary to store it). 'Sym.t' gives the
827
- type of the symbol. 'Sym.r' is usually the register in which the
828
- corresponding variable is stored. 'Sym.c' is usually a constant
829
- associated to the symbol like its address for normal symbols, and the
830
- number of entries for symbols representing arrays. Variable length
831
- array types use 'Sym.c' as a location on the stack which holds the
832
- runtime sizeof for the type.
833
-
834
- Four main symbol stacks are defined:
835
-
836
- 'define_stack'
837
- for the macros ('#define's).
838
-
839
- 'global_stack'
840
- for the global variables, functions and types.
841
-
842
- 'local_stack'
843
- for the local variables, functions and types.
844
-
845
- 'global_label_stack'
846
- for the local labels (for 'goto').
847
-
848
- 'label_stack'
849
- for GCC block local labels (see the '__label__' keyword).
850
-
851
- 'sym_push()' is used to add a new symbol in the local symbol stack.
852
- If no local symbol stack is active, it is added in the global symbol
853
- stack.
854
-
855
- 'sym_pop(st,b)' pops symbols from the symbol stack ST until the
856
- symbol B is on the top of stack. If B is NULL, the stack is emptied.
857
-
858
- 'sym_find(v)' return the symbol associated to the identifier V. The
859
- local stack is searched first from top to bottom, then the global stack.
860
-
861
- 8.6 Sections
862
- ============
863
-
864
- The generated code and datas are written in sections. The structure
865
- 'Section' contains all the necessary information for a given section.
866
- 'new_section()' creates a new section. ELF file semantics is assumed
867
- for each section.
868
-
869
- The following sections are predefined:
870
-
871
- 'text_section'
872
- is the section containing the generated code. IND contains the
873
- current position in the code section.
874
-
875
- 'data_section'
876
- contains initialized data
877
-
878
- 'bss_section'
879
- contains uninitialized data
880
-
881
- 'bounds_section'
882
- 'lbounds_section'
883
- are used when bound checking is activated
884
-
885
- 'stab_section'
886
- 'stabstr_section'
887
- are used when debugging is actived to store debug information
888
-
889
- 'symtab_section'
890
- 'strtab_section'
891
- contain the exported symbols (currently only used for debugging).
892
-
893
- 8.7 Code generation
894
- ===================
895
-
896
- 8.7.1 Introduction
897
- ------------------
898
-
899
- The TCC code generator directly generates linked binary code in one
900
- pass. It is rather unusual these days (see gcc for example which
901
- generates text assembly), but it can be very fast and surprisingly
902
- little complicated.
903
-
904
- The TCC code generator is register based. Optimization is only done
905
- at the expression level. No intermediate representation of expression
906
- is kept except the current values stored in the _value stack_.
907
-
908
- On x86, three temporary registers are used. When more registers are
909
- needed, one register is spilled into a new temporary variable on the
910
- stack.
911
-
912
- 8.7.2 The value stack
913
- ---------------------
914
-
915
- When an expression is parsed, its value is pushed on the value stack
916
- (VSTACK). The top of the value stack is VTOP. Each value stack entry
917
- is the structure 'SValue'.
918
-
919
- 'SValue.t' is the type. 'SValue.r' indicates how the value is
920
- currently stored in the generated code. It is usually a CPU register
921
- index ('REG_xxx' constants), but additional values and flags are
922
- defined:
923
-
924
- #define VT_CONST 0x00f0
925
- #define VT_LLOCAL 0x00f1
926
- #define VT_LOCAL 0x00f2
927
- #define VT_CMP 0x00f3
928
- #define VT_JMP 0x00f4
929
- #define VT_JMPI 0x00f5
930
- #define VT_LVAL 0x0100
931
- #define VT_SYM 0x0200
932
- #define VT_MUSTCAST 0x0400
933
- #define VT_MUSTBOUND 0x0800
934
- #define VT_BOUNDED 0x8000
935
- #define VT_LVAL_BYTE 0x1000
936
- #define VT_LVAL_SHORT 0x2000
937
- #define VT_LVAL_UNSIGNED 0x4000
938
- #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
939
-
940
- 'VT_CONST'
941
- indicates that the value is a constant. It is stored in the union
942
- 'SValue.c', depending on its type.
943
-
944
- 'VT_LOCAL'
945
- indicates a local variable pointer at offset 'SValue.c.i' in the
946
- stack.
947
-
948
- 'VT_CMP'
949
- indicates that the value is actually stored in the CPU flags (i.e.
950
- the value is the consequence of a test). The value is either 0 or
951
- 1. The actual CPU flags used is indicated in 'SValue.c.i'.
952
-
953
- If any code is generated which destroys the CPU flags, this value
954
- MUST be put in a normal register.
955
-
956
- 'VT_JMP'
957
- 'VT_JMPI'
958
- indicates that the value is the consequence of a conditional jump.
959
- For VT_JMP, it is 1 if the jump is taken, 0 otherwise. For VT_JMPI
960
- it is inverted.
961
-
962
- These values are used to compile the '||' and '&&' logical
963
- operators.
964
-
965
- If any code is generated, this value MUST be put in a normal
966
- register. Otherwise, the generated code won't be executed if the
967
- jump is taken.
968
-
969
- 'VT_LVAL'
970
- is a flag indicating that the value is actually an lvalue (left
971
- value of an assignment). It means that the value stored is
972
- actually a pointer to the wanted value.
973
-
974
- Understanding the use 'VT_LVAL' is very important if you want to
975
- understand how TCC works.
976
-
977
- 'VT_LVAL_BYTE'
978
- 'VT_LVAL_SHORT'
979
- 'VT_LVAL_UNSIGNED'
980
- if the lvalue has an integer type, then these flags give its real
981
- type. The type alone is not enough in case of cast optimisations.
982
-
983
- 'VT_LLOCAL'
984
- is a saved lvalue on the stack. 'VT_LLOCAL' should be eliminated
985
- ASAP because its semantics are rather complicated.
986
-
987
- 'VT_MUSTCAST'
988
- indicates that a cast to the value type must be performed if the
989
- value is used (lazy casting).
990
-
991
- 'VT_SYM'
992
- indicates that the symbol 'SValue.sym' must be added to the
993
- constant.
994
-
995
- 'VT_MUSTBOUND'
996
- 'VT_BOUNDED'
997
- are only used for optional bound checking.
998
-
999
- 8.7.3 Manipulating the value stack
1000
- ----------------------------------
1001
-
1002
- 'vsetc()' and 'vset()' pushes a new value on the value stack. If the
1003
- previous VTOP was stored in a very unsafe place(for example in the CPU
1004
- flags), then some code is generated to put the previous VTOP in a safe
1005
- storage.
1006
-
1007
- 'vpop()' pops VTOP. In some cases, it also generates cleanup code
1008
- (for example if stacked floating point registers are used as on x86).
1009
-
1010
- The 'gv(rc)' function generates code to evaluate VTOP (the top value
1011
- of the stack) into registers. RC selects in which register class the
1012
- value should be put. 'gv()' is the _most important function_ of the
1013
- code generator.
1014
-
1015
- 'gv2()' is the same as 'gv()' but for the top two stack entries.
1016
-
1017
- 8.7.4 CPU dependent code generation
1018
- -----------------------------------
1019
-
1020
- See the 'i386-gen.c' file to have an example.
1021
-
1022
- 'load()'
1023
- must generate the code needed to load a stack value into a
1024
- register.
1025
-
1026
- 'store()'
1027
- must generate the code needed to store a register into a stack
1028
- value lvalue.
1029
-
1030
- 'gfunc_start()'
1031
- 'gfunc_param()'
1032
- 'gfunc_call()'
1033
- should generate a function call
1034
-
1035
- 'gfunc_prolog()'
1036
- 'gfunc_epilog()'
1037
- should generate a function prolog/epilog.
1038
-
1039
- 'gen_opi(op)'
1040
- must generate the binary integer operation OP on the two top
1041
- entries of the stack which are guaranted to contain integer types.
1042
-
1043
- The result value should be put on the stack.
1044
-
1045
- 'gen_opf(op)'
1046
- same as 'gen_opi()' for floating point operations. The two top
1047
- entries of the stack are guaranted to contain floating point values
1048
- of same types.
1049
-
1050
- 'gen_cvt_itof()'
1051
- integer to floating point conversion.
1052
-
1053
- 'gen_cvt_ftoi()'
1054
- floating point to integer conversion.
1055
-
1056
- 'gen_cvt_ftof()'
1057
- floating point to floating point of different size conversion.
1058
-
1059
- 'gen_bounded_ptr_add()'
1060
- 'gen_bounded_ptr_deref()'
1061
- are only used for bounds checking.
1062
-
1063
- 8.8 Optimizations done
1064
- ======================
1065
-
1066
- Constant propagation is done for all operations. Multiplications and
1067
- divisions are optimized to shifts when appropriate. Comparison
1068
- operators are optimized by maintaining a special cache for the processor
1069
- flags. &&, || and ! are optimized by maintaining a special 'jump
1070
- target' value. No other jump optimization is currently performed
1071
- because it would require to store the code in a more abstract fashion.
1072
-
1073
- Concept Index
1074
- *************
1075
-
1076
- [index]
1077
- * Menu:
1078
-
1079
- * __asm__: Clang. (line 140)
1080
- * align directive: asm. (line 63)
1081
- * aligned attribute: Clang. (line 83)
1082
- * ascii directive: asm. (line 63)
1083
- * asciz directive: asm. (line 63)
1084
- * assembler: asm. (line 91)
1085
- * assembler directives: asm. (line 63)
1086
- * assembly, inline: Clang. (line 140)
1087
- * bound checks: Bounds. (line 6)
1088
- * bss directive: asm. (line 63)
1089
- * byte directive: asm. (line 63)
1090
- * caching processor flags: devel. (line 348)
1091
- * cdecl attribute: Clang. (line 83)
1092
- * code generation: devel. (line 177)
1093
- * comparison operators: devel. (line 348)
1094
- * constant propagation: devel. (line 348)
1095
- * CPU dependent: devel. (line 302)
1096
- * data directive: asm. (line 63)
1097
- * directives, assembler: asm. (line 63)
1098
- * dllexport attribute: Clang. (line 83)
1099
- * ELF: linker. (line 9)
1100
- * FILE, linker command: linker. (line 40)
1101
- * fill directive: asm. (line 63)
1102
- * flags, caching: devel. (line 348)
1103
- * gas: Clang. (line 159)
1104
- * global directive: asm. (line 63)
1105
- * globl directive: asm. (line 63)
1106
- * GROUP, linker command: linker. (line 40)
1107
- * inline assembly: Clang. (line 140)
1108
- * int directive: asm. (line 63)
1109
- * jump optimization: devel. (line 348)
1110
- * linker: linker. (line 5)
1111
- * linker scripts: linker. (line 40)
1112
- * long directive: asm. (line 63)
1113
- * memory checks: Bounds. (line 6)
1114
- * optimizations: devel. (line 348)
1115
- * org directive: asm. (line 63)
1116
- * OUTPUT_FORMAT, linker command: linker. (line 40)
1117
- * packed attribute: Clang. (line 83)
1118
- * PE-i386: linker. (line 32)
1119
- * previous directive: asm. (line 63)
1120
- * quad directive: asm. (line 63)
1121
- * regparm attribute: Clang. (line 83)
1122
- * scripts, linker: linker. (line 40)
1123
- * section attribute: Clang. (line 83)
1124
- * section directive: asm. (line 63)
1125
- * short directive: asm. (line 63)
1126
- * skip directive: asm. (line 63)
1127
- * space directive: asm. (line 63)
1128
- * stdcall attribute: Clang. (line 83)
1129
- * strength reduction: devel. (line 348)
1130
- * string directive: asm. (line 63)
1131
- * TARGET, linker command: linker. (line 40)
1132
- * text directive: asm. (line 63)
1133
- * unused attribute: Clang. (line 83)
1134
- * value stack: devel. (line 284)
1135
- * value stack, introduction: devel. (line 197)
1136
- * word directive: asm. (line 63)
1137
-
1138
-
1139
- 
1140
- Tag Table:
1141
- Node: Top207
1142
- Node: Introduction927
1143
- Node: Invoke2242
1144
- Node: Clang8984
1145
- Node: asm14690
1146
- Node: linker17278
1147
- Node: Bounds19012
1148
- Node: Libtcc20578
1149
- Node: devel21078
1150
- 
1151
- End Tag Table