tcc-doc.texi 33 KB

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