tcc-doc.texi 36 KB

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