tcc-doc.texi 36 KB

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