Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. <HTML>
  2. <HEAD>
  3. <!-- Created by texi2html 1.56k from tcc-doc.texi on 18 June 2005 -->
  4.  
  5. <TITLE>Tiny C Compiler Reference Documentation</TITLE>
  6. </HEAD>
  7. <BODY>
  8. <H1>Tiny C Compiler Reference Documentation</H1>
  9. <P>
  10. <P><HR><P>
  11. <H1>Table of Contents</H1>
  12. <UL>
  13. <LI><A NAME="TOC1" HREF="tcc-doc.html#SEC1">1. Introduction</A>
  14. <LI><A NAME="TOC2" HREF="tcc-doc.html#SEC2">2. Command line invocation</A>
  15. <UL>
  16. <LI><A NAME="TOC3" HREF="tcc-doc.html#SEC3">2.1 Quick start</A>
  17. <LI><A NAME="TOC4" HREF="tcc-doc.html#SEC4">2.2 Option summary</A>
  18. </UL>
  19. <LI><A NAME="TOC5" HREF="tcc-doc.html#SEC5">3. C language support</A>
  20. <UL>
  21. <LI><A NAME="TOC6" HREF="tcc-doc.html#SEC6">3.1 ANSI C</A>
  22. <LI><A NAME="TOC7" HREF="tcc-doc.html#SEC7">3.2 ISOC99 extensions</A>
  23. <LI><A NAME="TOC8" HREF="tcc-doc.html#SEC8">3.3 GNU C extensions</A>
  24. <LI><A NAME="TOC9" HREF="tcc-doc.html#SEC9">3.4 TinyCC extensions</A>
  25. </UL>
  26. <LI><A NAME="TOC10" HREF="tcc-doc.html#SEC10">4. TinyCC Assembler</A>
  27. <UL>
  28. <LI><A NAME="TOC11" HREF="tcc-doc.html#SEC11">4.1 Syntax</A>
  29. <LI><A NAME="TOC12" HREF="tcc-doc.html#SEC12">4.2 Expressions</A>
  30. <LI><A NAME="TOC13" HREF="tcc-doc.html#SEC13">4.3 Labels</A>
  31. <LI><A NAME="TOC14" HREF="tcc-doc.html#SEC14">4.4 Directives</A>
  32. <LI><A NAME="TOC15" HREF="tcc-doc.html#SEC15">4.5 X86 Assembler</A>
  33. </UL>
  34. <LI><A NAME="TOC16" HREF="tcc-doc.html#SEC16">5. TinyCC Linker</A>
  35. <UL>
  36. <LI><A NAME="TOC17" HREF="tcc-doc.html#SEC17">5.1 ELF file generation</A>
  37. <LI><A NAME="TOC18" HREF="tcc-doc.html#SEC18">5.2 ELF file loader</A>
  38. <LI><A NAME="TOC19" HREF="tcc-doc.html#SEC19">5.3 PE-i386 file generation</A>
  39. <LI><A NAME="TOC20" HREF="tcc-doc.html#SEC20">5.4 GNU Linker Scripts</A>
  40. </UL>
  41. <LI><A NAME="TOC21" HREF="tcc-doc.html#SEC21">6. TinyCC Memory and Bound checks</A>
  42. <LI><A NAME="TOC22" HREF="tcc-doc.html#SEC22">7. The <CODE>libtcc</CODE> library</A>
  43. <LI><A NAME="TOC23" HREF="tcc-doc.html#SEC23">8. Developer's guide</A>
  44. <UL>
  45. <LI><A NAME="TOC24" HREF="tcc-doc.html#SEC24">8.1 File reading</A>
  46. <LI><A NAME="TOC25" HREF="tcc-doc.html#SEC25">8.2 Lexer</A>
  47. <LI><A NAME="TOC26" HREF="tcc-doc.html#SEC26">8.3 Parser</A>
  48. <LI><A NAME="TOC27" HREF="tcc-doc.html#SEC27">8.4 Types</A>
  49. <LI><A NAME="TOC28" HREF="tcc-doc.html#SEC28">8.5 Symbols</A>
  50. <LI><A NAME="TOC29" HREF="tcc-doc.html#SEC29">8.6 Sections</A>
  51. <LI><A NAME="TOC30" HREF="tcc-doc.html#SEC30">8.7 Code generation</A>
  52. <UL>
  53. <LI><A NAME="TOC31" HREF="tcc-doc.html#SEC31">8.7.1 Introduction</A>
  54. <LI><A NAME="TOC32" HREF="tcc-doc.html#SEC32">8.7.2 The value stack</A>
  55. <LI><A NAME="TOC33" HREF="tcc-doc.html#SEC33">8.7.3 Manipulating the value stack</A>
  56. <LI><A NAME="TOC34" HREF="tcc-doc.html#SEC34">8.7.4 CPU dependent code generation</A>
  57. </UL>
  58. <LI><A NAME="TOC35" HREF="tcc-doc.html#SEC35">8.8 Optimizations done</A>
  59. </UL>
  60. <LI><A NAME="TOC36" HREF="tcc-doc.html#SEC36">Concept Index</A>
  61. </UL>
  62. <P><HR><P>
  63.  
  64.  
  65. <H1><A NAME="SEC1" HREF="tcc-doc.html#TOC1">1. Introduction</A></H1>
  66.  
  67. <P>
  68. TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C
  69. compilers, it is meant to be self-relying: you do not need an
  70. external assembler or linker because TCC does that for you.
  71.  
  72.  
  73. <P>
  74. TCC compiles so <EM>fast</EM> that even for big projects <CODE>Makefile</CODE>s may
  75. not be necessary.
  76.  
  77.  
  78. <P>
  79. TCC not only supports ANSI C, but also most of the new ISO C99
  80. standard and many GNUC extensions including inline assembly.
  81.  
  82.  
  83. <P>
  84. TCC can also be used to make <EM>C scripts</EM>, i.e. pieces of C source
  85. that you run as a Perl or Python script. Compilation is so fast that
  86. your script will be as fast as if it was an executable.
  87.  
  88.  
  89. <P>
  90. TCC can also automatically generate memory and bound checks
  91. (see section <A HREF="tcc-doc.html#SEC21">6. TinyCC Memory and Bound checks</A>) while allowing all C pointers operations. TCC can do
  92. these checks even if non patched libraries are used.
  93.  
  94.  
  95. <P>
  96. With <CODE>libtcc</CODE>, you can use TCC as a backend for dynamic code
  97. generation (see section <A HREF="tcc-doc.html#SEC22">7. The <CODE>libtcc</CODE> library</A>).
  98.  
  99.  
  100. <P>
  101. TCC mainly supports the i386 target on Linux and Windows. There are alpha
  102. ports for the ARM (<CODE>arm-tcc</CODE>) and the TMS320C67xx targets
  103. (<CODE>c67-tcc</CODE>). More information about the ARM port is available at
  104. <A HREF="http://lists.gnu.org/archive/html/tinycc-devel/2003-10/msg00044.html">http://lists.gnu.org/archive/html/tinycc-devel/2003-10/msg00044.html</A>.
  105.  
  106.  
  107.  
  108.  
  109. <H1><A NAME="SEC2" HREF="tcc-doc.html#TOC2">2. Command line invocation</A></H1>
  110.  
  111. <P>
  112. [This manual documents version 0.9.23 of the Tiny C Compiler]
  113.  
  114.  
  115.  
  116.  
  117. <H2><A NAME="SEC3" HREF="tcc-doc.html#TOC3">2.1 Quick start</A></H2>
  118.  
  119.  
  120. <PRE>
  121. usage: tcc [options] [<VAR>infile1</VAR> <VAR>infile2</VAR>...] [<SAMP>`-run'</SAMP> <VAR>infile</VAR> <VAR>args</VAR>...]
  122. </PRE>
  123.  
  124. <P>
  125. TCC options are a very much like gcc options. The main difference is that TCC
  126. can also execute directly the resulting program and give it runtime
  127. arguments.
  128.  
  129.  
  130. <P>
  131. Here are some examples to understand the logic:
  132.  
  133.  
  134. <DL COMPACT>
  135.  
  136. <DT><CODE><SAMP>`tcc -run a.c'</SAMP></CODE>
  137. <DD>
  138. Compile <TT>`a.c'</TT> and execute it directly
  139.  
  140. <DT><CODE><SAMP>`tcc -run a.c arg1'</SAMP></CODE>
  141. <DD>
  142. Compile a.c and execute it directly. arg1 is given as first argument to
  143. the <CODE>main()</CODE> of a.c.
  144.  
  145. <DT><CODE><SAMP>`tcc a.c -run b.c arg1'</SAMP></CODE>
  146. <DD>
  147. Compile <TT>`a.c'</TT> and <TT>`b.c'</TT>, link them together and execute them. arg1 is given
  148. as first argument to the <CODE>main()</CODE> of the resulting program. Because
  149. multiple C files are specified, <SAMP>`--'</SAMP> are necessary to clearly separate the
  150. program arguments from the TCC options.
  151.  
  152. <DT><CODE><SAMP>`tcc -o myprog a.c b.c'</SAMP></CODE>
  153. <DD>
  154. Compile <TT>`a.c'</TT> and <TT>`b.c'</TT>, link them and generate the executable <TT>`myprog'</TT>.
  155.  
  156. <DT><CODE><SAMP>`tcc -o myprog a.o b.o'</SAMP></CODE>
  157. <DD>
  158. link <TT>`a.o'</TT> and <TT>`b.o'</TT> together and generate the executable <TT>`myprog'</TT>.
  159.  
  160. <DT><CODE><SAMP>`tcc -c a.c'</SAMP></CODE>
  161. <DD>
  162. Compile <TT>`a.c'</TT> and generate object file <TT>`a.o'</TT>.
  163.  
  164. <DT><CODE><SAMP>`tcc -c asmfile.S'</SAMP></CODE>
  165. <DD>
  166. Preprocess with C preprocess and assemble <TT>`asmfile.S'</TT> and generate
  167. object file <TT>`asmfile.o'</TT>.
  168.  
  169. <DT><CODE><SAMP>`tcc -c asmfile.s'</SAMP></CODE>
  170. <DD>
  171. Assemble (but not preprocess) <TT>`asmfile.s'</TT> and generate object file
  172. <TT>`asmfile.o'</TT>.
  173.  
  174. <DT><CODE><SAMP>`tcc -r -o ab.o a.c b.c'</SAMP></CODE>
  175. <DD>
  176. Compile <TT>`a.c'</TT> and <TT>`b.c'</TT>, link them together and generate the object file <TT>`ab.o'</TT>.
  177.  
  178. </DL>
  179.  
  180. <P>
  181. Scripting:
  182.  
  183.  
  184. <P>
  185. TCC can be invoked from <EM>scripts</EM>, just as shell scripts. You just
  186. need to add <CODE>#!/usr/local/bin/tcc -run</CODE> at the start of your C source:
  187.  
  188.  
  189.  
  190. <PRE>
  191. #!/usr/local/bin/tcc -run
  192. #include &#60;stdio.h&#62;
  193.  
  194. int main()
  195. {
  196.     printf("Hello World\n");
  197.     return 0;
  198. }
  199. </PRE>
  200.  
  201.  
  202.  
  203. <H2><A NAME="SEC4" HREF="tcc-doc.html#TOC4">2.2 Option summary</A></H2>
  204.  
  205. <P>
  206. General Options:
  207.  
  208.  
  209. <DL COMPACT>
  210.  
  211. <DT><SAMP>`-v'</SAMP>
  212. <DD>
  213. Display current TCC version.
  214.  
  215. <DT><SAMP>`-c'</SAMP>
  216. <DD>
  217. Generate an object file (<SAMP>`-o'</SAMP> option must also be given).
  218.  
  219. <DT><SAMP>`-o outfile'</SAMP>
  220. <DD>
  221. Put object file, executable, or dll into output file <TT>`outfile'</TT>.
  222.  
  223. <DT><SAMP>`-Bdir'</SAMP>
  224. <DD>
  225. Set the path where the tcc internal libraries can be found (default is
  226. <TT>`PREFIX/lib/tcc'</TT>).
  227.  
  228. <DT><SAMP>`-bench'</SAMP>
  229. <DD>
  230. Output compilation statistics.
  231.  
  232. <DT><SAMP>`-run source [args...]'</SAMP>
  233. <DD>
  234. Compile file <VAR>source</VAR> and run it with the command line arguments
  235. <VAR>args</VAR>. In order to be able to give more than one argument to a
  236. script, several TCC options can be given <EM>after</EM> the
  237. <SAMP>`-run'</SAMP> option, separated by spaces. Example:
  238.  
  239.  
  240. <PRE>
  241. tcc "-run -L/usr/X11R6/lib -lX11" ex4.c
  242. </PRE>
  243.  
  244. In a script, it gives the following header:
  245.  
  246.  
  247. <PRE>
  248. #!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11
  249. #include &#60;stdlib.h&#62;
  250. int main(int argc, char **argv)
  251. {
  252.     ...
  253. }
  254. </PRE>
  255.  
  256. </DL>
  257.  
  258. <P>
  259. Preprocessor options:
  260.  
  261.  
  262. <DL COMPACT>
  263.  
  264. <DT><SAMP>`-Idir'</SAMP>
  265. <DD>
  266. Specify an additional include path. Include paths are searched in the
  267. order they are specified.
  268.  
  269. System include paths are always searched after. The default system
  270. include paths are: <TT>`/usr/local/include'</TT>, <TT>`/usr/include'</TT>
  271. and <TT>`PREFIX/lib/tcc/include'</TT>. (<TT>`PREFIX'</TT> is usually
  272. <TT>`/usr'</TT> or <TT>`/usr/local'</TT>).
  273.  
  274. <DT><SAMP>`-Dsym[=val]'</SAMP>
  275. <DD>
  276. Define preprocessor symbol <SAMP>`sym'</SAMP> to
  277. val. If val is not present, its value is <SAMP>`1'</SAMP>. Function-like macros can
  278. also be defined: <SAMP>`-DF(a)=a+1'</SAMP>
  279.  
  280. <DT><SAMP>`-Usym'</SAMP>
  281. <DD>
  282. Undefine preprocessor symbol <SAMP>`sym'</SAMP>.
  283. </DL>
  284.  
  285. <P>
  286. Compilation flags:
  287.  
  288.  
  289. <P>
  290. Note: each of the following warning options has a negative form beginning with
  291. <SAMP>`-fno-'</SAMP>.
  292.  
  293.  
  294. <DL COMPACT>
  295.  
  296. <DT><SAMP>`-funsigned-char'</SAMP>
  297. <DD>
  298. Let the <CODE>char</CODE> type be unsigned.
  299.  
  300. <DT><SAMP>`-fsigned-char'</SAMP>
  301. <DD>
  302. Let the <CODE>char</CODE> type be signed.
  303.  
  304. <DT><SAMP>`-fno-common'</SAMP>
  305. <DD>
  306. Do not generate common symbols for uninitialized data.
  307.  
  308. <DT><SAMP>`-fleading-underscore'</SAMP>
  309. <DD>
  310. Add a leading underscore at the beginning of each C symbol.
  311.  
  312. </DL>
  313.  
  314. <P>
  315. Warning options:
  316.  
  317.  
  318. <DL COMPACT>
  319.  
  320. <DT><SAMP>`-w'</SAMP>
  321. <DD>
  322. Disable all warnings.
  323.  
  324. </DL>
  325.  
  326. <P>
  327. Note: each of the following warning options has a negative form beginning with
  328. <SAMP>`-Wno-'</SAMP>.
  329.  
  330.  
  331. <DL COMPACT>
  332.  
  333. <DT><SAMP>`-Wimplicit-function-declaration'</SAMP>
  334. <DD>
  335. Warn about implicit function declaration.
  336.  
  337. <DT><SAMP>`-Wunsupported'</SAMP>
  338. <DD>
  339. Warn about unsupported GCC features that are ignored by TCC.
  340.  
  341. <DT><SAMP>`-Wwrite-strings'</SAMP>
  342. <DD>
  343. Make string constants be of type <CODE>const char *</CODE> instead of <CODE>char
  344. *</CODE>.
  345.  
  346. <DT><SAMP>`-Werror'</SAMP>
  347. <DD>
  348. Abort compilation if warnings are issued.
  349.  
  350. <DT><SAMP>`-Wall'</SAMP>
  351. <DD>
  352. Activate all warnings, except <SAMP>`-Werror'</SAMP>, <SAMP>`-Wunusupported'</SAMP> and
  353. <SAMP>`-Wwrite-strings'</SAMP>.
  354.  
  355. </DL>
  356.  
  357. <P>
  358. Linker options:
  359.  
  360.  
  361. <DL COMPACT>
  362.  
  363. <DT><SAMP>`-Ldir'</SAMP>
  364. <DD>
  365. Specify an additional static library path for the <SAMP>`-l'</SAMP> option. The
  366. default library paths are <TT>`/usr/local/lib'</TT>, <TT>`/usr/lib'</TT> and <TT>`/lib'</TT>.
  367.  
  368. <DT><SAMP>`-lxxx'</SAMP>
  369. <DD>
  370. Link your program with dynamic library libxxx.so or static library
  371. libxxx.a. The library is searched in the paths specified by the
  372. <SAMP>`-L'</SAMP> option.
  373.  
  374. <DT><SAMP>`-shared'</SAMP>
  375. <DD>
  376. Generate a shared library instead of an executable (<SAMP>`-o'</SAMP> option
  377. must also be given).
  378.  
  379. <DT><SAMP>`-static'</SAMP>
  380. <DD>
  381. Generate a statically linked executable (default is a shared linked
  382. executable) (<SAMP>`-o'</SAMP> option must also be given).
  383.  
  384. <DT><SAMP>`-rdynamic'</SAMP>
  385. <DD>
  386. Export global symbols to the dynamic linker. It is useful when a library
  387. opened with <CODE>dlopen()</CODE> needs to access executable symbols.
  388.  
  389. <DT><SAMP>`-r'</SAMP>
  390. <DD>
  391. Generate an object file combining all input files (<SAMP>`-o'</SAMP> option must
  392. also be given).
  393.  
  394. <DT><SAMP>`-Wl,-Ttext,address'</SAMP>
  395. <DD>
  396. Set the start of the .text section to <VAR>address</VAR>.
  397.  
  398. <DT><SAMP>`-Wl,--oformat,fmt'</SAMP>
  399. <DD>
  400. Use <VAR>fmt</VAR> as output format. The supported output formats are:
  401. <DL COMPACT>
  402.  
  403. <DT><CODE>elf32-i386</CODE>
  404. <DD>
  405. ELF output format (default)
  406. <DT><CODE>binary</CODE>
  407. <DD>
  408. Binary image (only for executable output)
  409. <DT><CODE>coff</CODE>
  410. <DD>
  411. COFF output format (only for executable output for TMS320C67xx target)
  412. </DL>
  413.  
  414. </DL>
  415.  
  416. <P>
  417. Debugger options:
  418.  
  419.  
  420. <DL COMPACT>
  421.  
  422. <DT><SAMP>`-g'</SAMP>
  423. <DD>
  424. Generate run time debug information so that you get clear run time
  425. error messages: <CODE> test.c:68: in function 'test5()': dereferencing
  426. invalid pointer</CODE> instead of the laconic <CODE>Segmentation
  427. fault</CODE>.
  428.  
  429. <DT><SAMP>`-b'</SAMP>
  430. <DD>
  431. Generate additional support code to check
  432. memory allocations and array/pointer bounds. <SAMP>`-g'</SAMP> is implied. Note
  433. that the generated code is slower and bigger in this case.
  434.  
  435. <DT><SAMP>`-bt N'</SAMP>
  436. <DD>
  437. Display N callers in stack traces. This is useful with <SAMP>`-g'</SAMP> or
  438. <SAMP>`-b'</SAMP>.
  439.  
  440. </DL>
  441.  
  442. <P>
  443. Note: GCC options <SAMP>`-Ox'</SAMP>, <SAMP>`-fx'</SAMP> and <SAMP>`-mx'</SAMP> are
  444. ignored.
  445.  
  446.  
  447.  
  448.  
  449. <H1><A NAME="SEC5" HREF="tcc-doc.html#TOC5">3. C language support</A></H1>
  450.  
  451.  
  452.  
  453. <H2><A NAME="SEC6" HREF="tcc-doc.html#TOC6">3.1 ANSI C</A></H2>
  454.  
  455. <P>
  456. TCC implements all the ANSI C standard, including structure bit fields
  457. and floating point numbers (<CODE>long double</CODE>, <CODE>double</CODE>, and
  458. <CODE>float</CODE> fully supported).
  459.  
  460.  
  461.  
  462.  
  463. <H2><A NAME="SEC7" HREF="tcc-doc.html#TOC7">3.2 ISOC99 extensions</A></H2>
  464.  
  465. <P>
  466. TCC implements many features of the new C standard: ISO C99. Currently
  467. missing items are: complex and imaginary numbers and variable length
  468. arrays.
  469.  
  470.  
  471. <P>
  472. Currently implemented ISOC99 features:
  473.  
  474.  
  475.  
  476. <UL>
  477.  
  478. <LI>64 bit <CODE>long long</CODE> types are fully supported.
  479.  
  480. <LI>The boolean type <CODE>_Bool</CODE> is supported.
  481.  
  482. <LI><CODE>__func__</CODE> is a string variable containing the current
  483.  
  484. function name.
  485.  
  486. <LI>Variadic macros: <CODE>__VA_ARGS__</CODE> can be used for
  487.  
  488.   function-like macros:
  489.  
  490. <PRE>
  491.    #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__)
  492. </PRE>
  493.  
  494. <CODE>dprintf</CODE> can then be used with a variable number of parameters.
  495.  
  496. <LI>Declarations can appear anywhere in a block (as in C++).
  497.  
  498. <LI>Array and struct/union elements can be initialized in any order by
  499.  
  500.  using designators:
  501.  
  502. <PRE>
  503.    struct { int x, y; } st[10] = { [0].x = 1, [0].y = 2 };
  504.  
  505.    int tab[10] = { 1, 2, [5] = 5, [9] = 9};
  506. </PRE>
  507.  
  508.    
  509. <LI>Compound initializers are supported:
  510.  
  511.  
  512. <PRE>
  513.    int *p = (int []){ 1, 2, 3 };
  514. </PRE>
  515.  
  516. to initialize a pointer pointing to an initialized array. The same
  517. works for structures and strings.
  518.  
  519. <LI>Hexadecimal floating point constants are supported:
  520.  
  521.  
  522. <PRE>
  523.          double d = 0x1234p10;
  524. </PRE>
  525.  
  526. is the same as writing
  527.  
  528. <PRE>
  529.          double d = 4771840.0;
  530. </PRE>
  531.  
  532. <LI><CODE>inline</CODE> keyword is ignored.
  533.  
  534. <LI><CODE>restrict</CODE> keyword is ignored.
  535.  
  536. </UL>
  537.  
  538.  
  539.  
  540. <H2><A NAME="SEC8" HREF="tcc-doc.html#TOC8">3.3 GNU C extensions</A></H2>
  541. <P>
  542. <A NAME="IDX1"></A>
  543. <A NAME="IDX2"></A>
  544. <A NAME="IDX3"></A>
  545. <A NAME="IDX4"></A>
  546. <A NAME="IDX5"></A>
  547. <A NAME="IDX6"></A>
  548. <A NAME="IDX7"></A>
  549.  
  550.  
  551. <P>
  552. TCC implements some GNU C extensions:
  553.  
  554.  
  555.  
  556. <UL>
  557.  
  558. <LI>array designators can be used without '=':
  559.  
  560.  
  561. <PRE>
  562.    int a[10] = { [0] 1, [5] 2, 3, 4 };
  563. </PRE>
  564.  
  565. <LI>Structure field designators can be a label:
  566.  
  567.  
  568. <PRE>
  569.    struct { int x, y; } st = { x: 1, y: 1};
  570. </PRE>
  571.  
  572. instead of
  573.  
  574. <PRE>
  575.    struct { int x, y; } st = { .x = 1, .y = 1};
  576. </PRE>
  577.  
  578. <LI><CODE>\e</CODE> is ASCII character 27.
  579.  
  580. <LI>case ranges : ranges can be used in <CODE>case</CODE>s:
  581.  
  582.  
  583. <PRE>
  584.    switch(a) {
  585.    case 1 ... 9:
  586.          printf("range 1 to 9\n");
  587.          break;
  588.    default:
  589.          printf("unexpected\n");
  590.          break;
  591.    }
  592. </PRE>
  593.  
  594. <LI>The keyword <CODE>__attribute__</CODE> is handled to specify variable or
  595.  
  596. function attributes. The following attributes are supported:
  597.  
  598. <UL>
  599.  
  600. <LI><CODE>aligned(n)</CODE>: align a variable or a structure field to n bytes
  601.  
  602. (must be a power of two).
  603.  
  604. <LI><CODE>packed</CODE>: force alignment of a variable or a structure field to
  605.  
  606.  1.
  607.  
  608. <LI><CODE>section(name)</CODE>: generate function or data in assembly section
  609.  
  610. name (name is a string containing the section name) instead of the default
  611. section.
  612.  
  613. <LI><CODE>unused</CODE>: specify that the variable or the function is unused.
  614.  
  615. <LI><CODE>cdecl</CODE>: use standard C calling convention (default).
  616.  
  617. <LI><CODE>stdcall</CODE>: use Pascal-like calling convention.
  618.  
  619. <LI><CODE>regparm(n)</CODE>: use fast i386 calling convention. <VAR>n</VAR> must be
  620.  
  621. between 1 and 3. The first <VAR>n</VAR> function parameters are respectively put in
  622. registers <CODE>%eax</CODE>, <CODE>%edx</CODE> and <CODE>%ecx</CODE>.
  623.  
  624. </UL>
  625.  
  626. Here are some examples:
  627.  
  628. <PRE>
  629.    int a __attribute__ ((aligned(8), section(".mysection")));
  630. </PRE>
  631.  
  632. align variable <CODE>a</CODE> to 8 bytes and put it in section <CODE>.mysection</CODE>.
  633.  
  634.  
  635. <PRE>
  636.    int my_add(int a, int b) __attribute__ ((section(".mycodesection")))
  637.    {
  638.        return a + b;
  639.    }
  640. </PRE>
  641.  
  642. generate function <CODE>my_add</CODE> in section <CODE>.mycodesection</CODE>.
  643.  
  644. <LI>GNU style variadic macros:
  645.  
  646.  
  647. <PRE>
  648.    #define dprintf(fmt, args...) printf(fmt, ## args)
  649.  
  650.    dprintf("no arg\n");
  651.    dprintf("one arg %d\n", 1);
  652. </PRE>
  653.  
  654. <LI><CODE>__FUNCTION__</CODE> is interpreted as C99 <CODE>__func__</CODE>
  655.  
  656. (so it has not exactly the same semantics as string literal GNUC
  657. where it is a string literal).
  658.  
  659. <LI>The <CODE>__alignof__</CODE> keyword can be used as <CODE>sizeof</CODE>
  660.  
  661. to get the alignment of a type or an expression.
  662.  
  663. <LI>The <CODE>typeof(x)</CODE> returns the type of <CODE>x</CODE>.
  664.  
  665. <CODE>x</CODE> is an expression or a type.
  666.  
  667. <LI>Computed gotos: <CODE>&#38;&#38;label</CODE> returns a pointer of type
  668.  
  669. <CODE>void *</CODE> on the goto label <CODE>label</CODE>. <CODE>goto *expr</CODE> can be
  670. used to jump on the pointer resulting from <CODE>expr</CODE>.
  671.  
  672. <LI>Inline assembly with asm instruction:
  673.  
  674. <A NAME="IDX8"></A>
  675. <A NAME="IDX9"></A>
  676. <A NAME="IDX10"></A>
  677.  
  678. <PRE>
  679. static inline void * my_memcpy(void * to, const void * from, size_t n)
  680. {
  681. int d0, d1, d2;
  682. __asm__ __volatile__(
  683.        "rep ; movsl\n\t"
  684.        "testb $2,%b4\n\t"
  685.        "je 1f\n\t"
  686.        "movsw\n"
  687.        "1:\ttestb $1,%b4\n\t"
  688.        "je 2f\n\t"
  689.        "movsb\n"
  690.        "2:"
  691.        : "=&#38;c" (d0), "=&#38;D" (d1), "=&#38;S" (d2)
  692.        :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
  693.        : "memory");
  694. return (to);
  695. }
  696. </PRE>
  697.  
  698. <A NAME="IDX11"></A>
  699. TCC includes its own x86 inline assembler with a <CODE>gas</CODE>-like (GNU
  700. assembler) syntax. No intermediate files are generated. GCC 3.x named
  701. operands are supported.
  702.  
  703. <LI><CODE>__builtin_types_compatible_p()</CODE> and <CODE>__builtin_constant_p()</CODE>
  704.  
  705. are supported.
  706.  
  707. <LI><CODE>#pragma pack</CODE> is supported for win32 compatibility.
  708.  
  709. </UL>
  710.  
  711.  
  712.  
  713. <H2><A NAME="SEC9" HREF="tcc-doc.html#TOC9">3.4 TinyCC extensions</A></H2>
  714.  
  715.  
  716. <UL>
  717.  
  718. <LI><CODE>__TINYC__</CODE> is a predefined macro to <CODE>1</CODE> to
  719.  
  720. indicate that you use TCC.
  721.  
  722. <LI><CODE>#!</CODE> at the start of a line is ignored to allow scripting.
  723.  
  724. <LI>Binary digits can be entered (<CODE>0b101</CODE> instead of
  725.  
  726. <CODE>5</CODE>).
  727.  
  728. <LI><CODE>__BOUNDS_CHECKING_ON</CODE> is defined if bound checking is activated.
  729.  
  730. </UL>
  731.  
  732.  
  733.  
  734. <H1><A NAME="SEC10" HREF="tcc-doc.html#TOC10">4. TinyCC Assembler</A></H1>
  735.  
  736. <P>
  737. Since version 0.9.16, TinyCC integrates its own assembler. TinyCC
  738. assembler supports a gas-like syntax (GNU assembler). You can
  739. desactivate assembler support if you want a smaller TinyCC executable
  740. (the C compiler does not rely on the assembler).
  741.  
  742.  
  743. <P>
  744. TinyCC Assembler is used to handle files with <TT>`.S'</TT> (C
  745. preprocessed assembler) and <TT>`.s'</TT> extensions. It is also used to
  746. handle the GNU inline assembler with the <CODE>asm</CODE> keyword.
  747.  
  748.  
  749.  
  750.  
  751. <H2><A NAME="SEC11" HREF="tcc-doc.html#TOC11">4.1 Syntax</A></H2>
  752.  
  753. <P>
  754. TinyCC Assembler supports most of the gas syntax. The tokens are the
  755. same as C.
  756.  
  757.  
  758.  
  759. <UL>
  760.  
  761. <LI>C and C++ comments are supported.
  762.  
  763. <LI>Identifiers are the same as C, so you cannot use '.' or '$'.
  764.  
  765. <LI>Only 32 bit integer numbers are supported.
  766.  
  767. </UL>
  768.  
  769.  
  770.  
  771. <H2><A NAME="SEC12" HREF="tcc-doc.html#TOC12">4.2 Expressions</A></H2>
  772.  
  773.  
  774. <UL>
  775.  
  776. <LI>Integers in decimal, octal and hexa are supported.
  777.  
  778. <LI>Unary operators: +, -, ~.
  779.  
  780. <LI>Binary operators in decreasing priority order:
  781.  
  782.  
  783. <OL>
  784. <LI>*, /, %
  785.  
  786. <LI>&#38;, |, ^
  787.  
  788. <LI>+, -
  789.  
  790. </OL>
  791.  
  792. <LI>A value is either an absolute number or a label plus an offset.
  793.  
  794. All operators accept absolute values except '+' and '-'. '+' or '-' can be
  795. used to add an offset to a label. '-' supports two labels only if they
  796. are the same or if they are both defined and in the same section.
  797.  
  798. </UL>
  799.  
  800.  
  801.  
  802. <H2><A NAME="SEC13" HREF="tcc-doc.html#TOC13">4.3 Labels</A></H2>
  803.  
  804.  
  805. <UL>
  806.  
  807. <LI>All labels are considered as local, except undefined ones.
  808.  
  809. <LI>Numeric labels can be used as local <CODE>gas</CODE>-like labels.
  810.  
  811. They can be defined several times in the same source. Use 'b'
  812. (backward) or 'f' (forward) as suffix to reference them:
  813.  
  814.  
  815. <PRE>
  816. 1:
  817.      jmp 1b /* jump to '1' label before */
  818.      jmp 1f /* jump to '1' label after */
  819. 1:
  820. </PRE>
  821.  
  822. </UL>
  823.  
  824.  
  825.  
  826. <H2><A NAME="SEC14" HREF="tcc-doc.html#TOC14">4.4 Directives</A></H2>
  827. <P>
  828. <A NAME="IDX12"></A>
  829. <A NAME="IDX13"></A>
  830. <A NAME="IDX14"></A>
  831. <A NAME="IDX15"></A>
  832. <A NAME="IDX16"></A>
  833. <A NAME="IDX17"></A>
  834. <A NAME="IDX18"></A>
  835. <A NAME="IDX19"></A>
  836. <A NAME="IDX20"></A>
  837. <A NAME="IDX21"></A>
  838. <A NAME="IDX22"></A>
  839. <A NAME="IDX23"></A>
  840. <A NAME="IDX24"></A>
  841. <A NAME="IDX25"></A>
  842. <A NAME="IDX26"></A>
  843. <A NAME="IDX27"></A>
  844. <A NAME="IDX28"></A>
  845. <A NAME="IDX29"></A>
  846. <A NAME="IDX30"></A>
  847. <A NAME="IDX31"></A>
  848. <A NAME="IDX32"></A>
  849. <A NAME="IDX33"></A>
  850. <A NAME="IDX34"></A>
  851.  
  852.  
  853. <P>
  854. All directives are preceeded by a '.'. The following directives are
  855. supported:
  856.  
  857.  
  858.  
  859. <UL>
  860. <LI>.align n[,value]
  861.  
  862. <LI>.skip n[,value]
  863.  
  864. <LI>.space n[,value]
  865.  
  866. <LI>.byte value1[,...]
  867.  
  868. <LI>.word value1[,...]
  869.  
  870. <LI>.short value1[,...]
  871.  
  872. <LI>.int value1[,...]
  873.  
  874. <LI>.long value1[,...]
  875.  
  876. <LI>.quad immediate_value1[,...]
  877.  
  878. <LI>.globl symbol
  879.  
  880. <LI>.global symbol
  881.  
  882. <LI>.section section
  883.  
  884. <LI>.text
  885.  
  886. <LI>.data
  887.  
  888. <LI>.bss
  889.  
  890. <LI>.fill repeat[,size[,value]]
  891.  
  892. <LI>.org n
  893.  
  894. <LI>.previous
  895.  
  896. <LI>.string string[,...]
  897.  
  898. <LI>.asciz string[,...]
  899.  
  900. <LI>.ascii string[,...]
  901.  
  902. </UL>
  903.  
  904.  
  905.  
  906. <H2><A NAME="SEC15" HREF="tcc-doc.html#TOC15">4.5 X86 Assembler</A></H2>
  907. <P>
  908. <A NAME="IDX35"></A>
  909.  
  910.  
  911. <P>
  912. All X86 opcodes are supported. Only ATT syntax is supported (source
  913. then destination operand order). If no size suffix is given, TinyCC
  914. tries to guess it from the operand sizes.
  915.  
  916.  
  917. <P>
  918. Currently, MMX opcodes are supported but not SSE ones.
  919.  
  920.  
  921.  
  922.  
  923. <H1><A NAME="SEC16" HREF="tcc-doc.html#TOC16">5. TinyCC Linker</A></H1>
  924. <P>
  925. <A NAME="IDX36"></A>
  926.  
  927.  
  928.  
  929.  
  930. <H2><A NAME="SEC17" HREF="tcc-doc.html#TOC17">5.1 ELF file generation</A></H2>
  931. <P>
  932. <A NAME="IDX37"></A>
  933.  
  934.  
  935. <P>
  936. TCC can directly output relocatable ELF files (object files),
  937. executable ELF files and dynamic ELF libraries without relying on an
  938. external linker.
  939.  
  940.  
  941. <P>
  942. Dynamic ELF libraries can be output but the C compiler does not generate
  943. position independent code (PIC). It means that the dynamic library
  944. code generated by TCC cannot be factorized among processes yet.
  945.  
  946.  
  947. <P>
  948. TCC linker eliminates unreferenced object code in libraries. A single pass is
  949. done on the object and library list, so the order in which object files and
  950. libraries are specified is important (same constraint as GNU ld). No grouping
  951. options (<SAMP>`--start-group'</SAMP> and <SAMP>`--end-group'</SAMP>) are supported.
  952.  
  953.  
  954.  
  955.  
  956. <H2><A NAME="SEC18" HREF="tcc-doc.html#TOC18">5.2 ELF file loader</A></H2>
  957.  
  958. <P>
  959. TCC can load ELF object files, archives (.a files) and dynamic
  960. libraries (.so).
  961.  
  962.  
  963.  
  964.  
  965. <H2><A NAME="SEC19" HREF="tcc-doc.html#TOC19">5.3 PE-i386 file generation</A></H2>
  966. <P>
  967. <A NAME="IDX38"></A>
  968.  
  969.  
  970. <P>
  971. TCC for Windows supports the native Win32 executable file format (PE-i386). It
  972. generates both EXE and DLL files. DLL symbols can be imported thru DEF files
  973. generated with the <CODE>tiny_impdef</CODE> tool.
  974.  
  975.  
  976. <P>
  977. Currently TCC for Windows cannot generate nor read PE object files, so ELF
  978. object files are used for that purpose. It can be a problem if
  979. interoperability with MSVC is needed. Moreover, no leading underscore is
  980. currently generated in the ELF symbols.
  981.  
  982.  
  983.  
  984.  
  985. <H2><A NAME="SEC20" HREF="tcc-doc.html#TOC20">5.4 GNU Linker Scripts</A></H2>
  986. <P>
  987. <A NAME="IDX39"></A>
  988. <A NAME="IDX40"></A>
  989. <A NAME="IDX41"></A>
  990. <A NAME="IDX42"></A>
  991. <A NAME="IDX43"></A>
  992. <A NAME="IDX44"></A>
  993.  
  994.  
  995. <P>
  996. Because on many Linux systems some dynamic libraries (such as
  997. <TT>`/usr/lib/libc.so'</TT>) are in fact GNU ld link scripts (horrible!),
  998. the TCC linker also supports a subset of GNU ld scripts.
  999.  
  1000.  
  1001. <P>
  1002. The <CODE>GROUP</CODE> and <CODE>FILE</CODE> commands are supported. <CODE>OUTPUT_FORMAT</CODE>
  1003. and <CODE>TARGET</CODE> are ignored.
  1004.  
  1005.  
  1006. <P>
  1007. Example from <TT>`/usr/lib/libc.so'</TT>:
  1008.  
  1009. <PRE>
  1010. /* GNU ld script
  1011.   Use the shared library, but some functions are only in
  1012.   the static library, so try that secondarily.  */
  1013. GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )
  1014. </PRE>
  1015.  
  1016.  
  1017.  
  1018. <H1><A NAME="SEC21" HREF="tcc-doc.html#TOC21">6. TinyCC Memory and Bound checks</A></H1>
  1019. <P>
  1020. <A NAME="IDX45"></A>
  1021. <A NAME="IDX46"></A>
  1022.  
  1023.  
  1024. <P>
  1025. This feature is activated with the <SAMP>`-b'</SAMP> (see section <A HREF="tcc-doc.html#SEC2">2. Command line invocation</A>).
  1026.  
  1027.  
  1028. <P>
  1029. Note that pointer size is <EM>unchanged</EM> and that code generated
  1030. with bound checks is <EM>fully compatible</EM> with unchecked
  1031. code. When a pointer comes from unchecked code, it is assumed to be
  1032. valid. Even very obscure C code with casts should work correctly.
  1033.  
  1034.  
  1035. <P>
  1036. For more information about the ideas behind this method, see
  1037. <A HREF="http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html">http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html</A>.
  1038.  
  1039.  
  1040. <P>
  1041. Here are some examples of caught errors:
  1042.  
  1043.  
  1044. <DL COMPACT>
  1045.  
  1046. <DT>Invalid range with standard string function:
  1047. <DD>
  1048.  
  1049. <PRE>
  1050. {
  1051.     char tab[10];
  1052.     memset(tab, 0, 11);
  1053. }
  1054. </PRE>
  1055.  
  1056. <DT>Out of bounds-error in global or local arrays:
  1057. <DD>
  1058.  
  1059. <PRE>
  1060. {
  1061.     int tab[10];
  1062.     for(i=0;i&#60;11;i++) {
  1063.         sum += tab[i];
  1064.     }
  1065. }
  1066. </PRE>
  1067.  
  1068. <DT>Out of bounds-error in malloc'ed data:
  1069. <DD>
  1070.  
  1071. <PRE>
  1072. {
  1073.    int *tab;
  1074.    tab = malloc(20 * sizeof(int));
  1075.    for(i=0;i&#60;21;i++) {
  1076.        sum += tab4[i];
  1077.    }
  1078.    free(tab);
  1079. }
  1080. </PRE>
  1081.  
  1082. <DT>Access of freed memory:
  1083. <DD>
  1084.  
  1085. <PRE>
  1086. {
  1087.    int *tab;
  1088.    tab = malloc(20 * sizeof(int));
  1089.    free(tab);
  1090.    for(i=0;i&#60;20;i++) {
  1091.        sum += tab4[i];
  1092.    }
  1093. }
  1094. </PRE>
  1095.  
  1096. <DT>Double free:
  1097. <DD>
  1098.  
  1099. <PRE>
  1100. {
  1101.    int *tab;
  1102.    tab = malloc(20 * sizeof(int));
  1103.    free(tab);
  1104.    free(tab);
  1105. }
  1106. </PRE>
  1107.  
  1108. </DL>
  1109.  
  1110.  
  1111.  
  1112. <H1><A NAME="SEC22" HREF="tcc-doc.html#TOC22">7. The <CODE>libtcc</CODE> library</A></H1>
  1113.  
  1114. <P>
  1115. The <CODE>libtcc</CODE> library enables you to use TCC as a backend for
  1116. dynamic code generation.
  1117.  
  1118.  
  1119. <P>
  1120. Read the <TT>`libtcc.h'</TT> to have an overview of the API. Read
  1121. <TT>`libtcc_test.c'</TT> to have a very simple example.
  1122.  
  1123.  
  1124. <P>
  1125. The idea consists in giving a C string containing the program you want
  1126. to compile directly to <CODE>libtcc</CODE>. Then you can access to any global
  1127. symbol (function or variable) defined.
  1128.  
  1129.  
  1130.  
  1131.  
  1132. <H1><A NAME="SEC23" HREF="tcc-doc.html#TOC23">8. Developer's guide</A></H1>
  1133.  
  1134. <P>
  1135. This chapter gives some hints to understand how TCC works. You can skip
  1136. it if you do not intend to modify the TCC code.
  1137.  
  1138.  
  1139.  
  1140.  
  1141. <H2><A NAME="SEC24" HREF="tcc-doc.html#TOC24">8.1 File reading</A></H2>
  1142.  
  1143. <P>
  1144. The <CODE>BufferedFile</CODE> structure contains the context needed to read a
  1145. file, including the current line number. <CODE>tcc_open()</CODE> opens a new
  1146. file and <CODE>tcc_close()</CODE> closes it. <CODE>inp()</CODE> returns the next
  1147. character.
  1148.  
  1149.  
  1150.  
  1151.  
  1152. <H2><A NAME="SEC25" HREF="tcc-doc.html#TOC25">8.2 Lexer</A></H2>
  1153.  
  1154. <P>
  1155. <CODE>next()</CODE> reads the next token in the current
  1156. file. <CODE>next_nomacro()</CODE> reads the next token without macro
  1157. expansion.
  1158.  
  1159.  
  1160. <P>
  1161. <CODE>tok</CODE> contains the current token (see <CODE>TOK_xxx</CODE>)
  1162. constants. Identifiers and keywords are also keywords. <CODE>tokc</CODE>
  1163. contains additional infos about the token (for example a constant value
  1164. if number or string token).
  1165.  
  1166.  
  1167.  
  1168.  
  1169. <H2><A NAME="SEC26" HREF="tcc-doc.html#TOC26">8.3 Parser</A></H2>
  1170.  
  1171. <P>
  1172. The parser is hardcoded (yacc is not necessary). It does only one pass,
  1173. except:
  1174.  
  1175.  
  1176.  
  1177. <UL>
  1178.  
  1179. <LI>For initialized arrays with unknown size, a first pass
  1180.  
  1181. is done to count the number of elements.
  1182.  
  1183. <LI>For architectures where arguments are evaluated in
  1184.  
  1185. reverse order, a first pass is done to reverse the argument order.
  1186.  
  1187. </UL>
  1188.  
  1189.  
  1190.  
  1191. <H2><A NAME="SEC27" HREF="tcc-doc.html#TOC27">8.4 Types</A></H2>
  1192.  
  1193. <P>
  1194. The types are stored in a single 'int' variable. It was choosen in the
  1195. first stages of development when tcc was much simpler. Now, it may not
  1196. be the best solution.
  1197.  
  1198.  
  1199.  
  1200. <PRE>
  1201. #define VT_INT        0  /* integer type */
  1202. #define VT_BYTE       1  /* signed byte type */
  1203. #define VT_SHORT      2  /* short type */
  1204. #define VT_VOID       3  /* void type */
  1205. #define VT_PTR        4  /* pointer */
  1206. #define VT_ENUM       5  /* enum definition */
  1207. #define VT_FUNC       6  /* function type */
  1208. #define VT_STRUCT     7  /* struct/union definition */
  1209. #define VT_FLOAT      8  /* IEEE float */
  1210. #define VT_DOUBLE     9  /* IEEE double */
  1211. #define VT_LDOUBLE   10  /* IEEE long double */
  1212. #define VT_BOOL      11  /* ISOC99 boolean type */
  1213. #define VT_LLONG     12  /* 64 bit integer */
  1214. #define VT_LONG      13  /* long integer (NEVER USED as type, only
  1215.                             during parsing) */
  1216. #define VT_BTYPE      0x000f /* mask for basic type */
  1217. #define VT_UNSIGNED   0x0010  /* unsigned type */
  1218. #define VT_ARRAY      0x0020  /* array type (also has VT_PTR) */
  1219. #define VT_BITFIELD   0x0040  /* bitfield modifier */
  1220.  
  1221. #define VT_STRUCT_SHIFT 16   /* structure/enum name shift (16 bits left) */
  1222. </PRE>
  1223.  
  1224. <P>
  1225. When a reference to another type is needed (for pointers, functions and
  1226. structures), the <CODE>32 - VT_STRUCT_SHIFT</CODE> high order bits are used to
  1227. store an identifier reference.
  1228.  
  1229.  
  1230. <P>
  1231. The <CODE>VT_UNSIGNED</CODE> flag can be set for chars, shorts, ints and long
  1232. longs.
  1233.  
  1234.  
  1235. <P>
  1236. Arrays are considered as pointers <CODE>VT_PTR</CODE> with the flag
  1237. <CODE>VT_ARRAY</CODE> set.
  1238.  
  1239.  
  1240. <P>
  1241. The <CODE>VT_BITFIELD</CODE> flag can be set for chars, shorts, ints and long
  1242. longs. If it is set, then the bitfield position is stored from bits
  1243. VT_STRUCT_SHIFT to VT_STRUCT_SHIFT + 5 and the bit field size is stored
  1244. from bits VT_STRUCT_SHIFT + 6 to VT_STRUCT_SHIFT + 11.
  1245.  
  1246.  
  1247. <P>
  1248. <CODE>VT_LONG</CODE> is never used except during parsing.
  1249.  
  1250.  
  1251. <P>
  1252. During parsing, the storage of an object is also stored in the type
  1253. integer:
  1254.  
  1255.  
  1256.  
  1257. <PRE>
  1258. #define VT_EXTERN  0x00000080  /* extern definition */
  1259. #define VT_STATIC  0x00000100  /* static variable */
  1260. #define VT_TYPEDEF 0x00000200  /* typedef definition */
  1261. </PRE>
  1262.  
  1263.  
  1264.  
  1265. <H2><A NAME="SEC28" HREF="tcc-doc.html#TOC28">8.5 Symbols</A></H2>
  1266.  
  1267. <P>
  1268. All symbols are stored in hashed symbol stacks. Each symbol stack
  1269. contains <CODE>Sym</CODE> structures.
  1270.  
  1271.  
  1272. <P>
  1273. <CODE>Sym.v</CODE> contains the symbol name (remember
  1274. an idenfier is also a token, so a string is never necessary to store
  1275. it). <CODE>Sym.t</CODE> gives the type of the symbol. <CODE>Sym.r</CODE> is usually
  1276. the register in which the corresponding variable is stored. <CODE>Sym.c</CODE> is
  1277. usually a constant associated to the symbol.
  1278.  
  1279.  
  1280. <P>
  1281. Four main symbol stacks are defined:
  1282.  
  1283.  
  1284. <DL COMPACT>
  1285.  
  1286. <DT><CODE>define_stack</CODE>
  1287. <DD>
  1288. for the macros (<CODE>#define</CODE>s).
  1289.  
  1290. <DT><CODE>global_stack</CODE>
  1291. <DD>
  1292. for the global variables, functions and types.
  1293.  
  1294. <DT><CODE>local_stack</CODE>
  1295. <DD>
  1296. for the local variables, functions and types.
  1297.  
  1298. <DT><CODE>global_label_stack</CODE>
  1299. <DD>
  1300. for the local labels (for <CODE>goto</CODE>).
  1301.  
  1302. <DT><CODE>label_stack</CODE>
  1303. <DD>
  1304. for GCC block local labels (see the <CODE>__label__</CODE> keyword).
  1305.  
  1306. </DL>
  1307.  
  1308. <P>
  1309. <CODE>sym_push()</CODE> is used to add a new symbol in the local symbol
  1310. stack. If no local symbol stack is active, it is added in the global
  1311. symbol stack.
  1312.  
  1313.  
  1314. <P>
  1315. <CODE>sym_pop(st,b)</CODE> pops symbols from the symbol stack <VAR>st</VAR> until
  1316. the symbol <VAR>b</VAR> is on the top of stack. If <VAR>b</VAR> is NULL, the stack
  1317. is emptied.
  1318.  
  1319.  
  1320. <P>
  1321. <CODE>sym_find(v)</CODE> return the symbol associated to the identifier
  1322. <VAR>v</VAR>. The local stack is searched first from top to bottom, then the
  1323. global stack.
  1324.  
  1325.  
  1326.  
  1327.  
  1328. <H2><A NAME="SEC29" HREF="tcc-doc.html#TOC29">8.6 Sections</A></H2>
  1329.  
  1330. <P>
  1331. The generated code and datas are written in sections. The structure
  1332. <CODE>Section</CODE> contains all the necessary information for a given
  1333. section. <CODE>new_section()</CODE> creates a new section. ELF file semantics
  1334. is assumed for each section.
  1335.  
  1336.  
  1337. <P>
  1338. The following sections are predefined:
  1339.  
  1340.  
  1341. <DL COMPACT>
  1342.  
  1343. <DT><CODE>text_section</CODE>
  1344. <DD>
  1345. is the section containing the generated code. <VAR>ind</VAR> contains the
  1346. current position in the code section.
  1347.  
  1348. <DT><CODE>data_section</CODE>
  1349. <DD>
  1350. contains initialized data
  1351.  
  1352. <DT><CODE>bss_section</CODE>
  1353. <DD>
  1354. contains uninitialized data
  1355.  
  1356. <DT><CODE>bounds_section</CODE>
  1357. <DD>
  1358. <DT><CODE>lbounds_section</CODE>
  1359. <DD>
  1360. are used when bound checking is activated
  1361.  
  1362. <DT><CODE>stab_section</CODE>
  1363. <DD>
  1364. <DT><CODE>stabstr_section</CODE>
  1365. <DD>
  1366. are used when debugging is actived to store debug information
  1367.  
  1368. <DT><CODE>symtab_section</CODE>
  1369. <DD>
  1370. <DT><CODE>strtab_section</CODE>
  1371. <DD>
  1372. contain the exported symbols (currently only used for debugging).
  1373.  
  1374. </DL>
  1375.  
  1376.  
  1377.  
  1378. <H2><A NAME="SEC30" HREF="tcc-doc.html#TOC30">8.7 Code generation</A></H2>
  1379. <P>
  1380. <A NAME="IDX47"></A>
  1381.  
  1382.  
  1383.  
  1384.  
  1385. <H3><A NAME="SEC31" HREF="tcc-doc.html#TOC31">8.7.1 Introduction</A></H3>
  1386.  
  1387. <P>
  1388. The TCC code generator directly generates linked binary code in one
  1389. pass. It is rather unusual these days (see gcc for example which
  1390. generates text assembly), but it can be very fast and surprisingly
  1391. little complicated.
  1392.  
  1393.  
  1394. <P>
  1395. The TCC code generator is register based. Optimization is only done at
  1396. the expression level. No intermediate representation of expression is
  1397. kept except the current values stored in the <EM>value stack</EM>.
  1398.  
  1399.  
  1400. <P>
  1401. On x86, three temporary registers are used. When more registers are
  1402. needed, one register is spilled into a new temporary variable on the stack.
  1403.  
  1404.  
  1405.  
  1406.  
  1407. <H3><A NAME="SEC32" HREF="tcc-doc.html#TOC32">8.7.2 The value stack</A></H3>
  1408. <P>
  1409. <A NAME="IDX48"></A>
  1410.  
  1411.  
  1412. <P>
  1413. When an expression is parsed, its value is pushed on the value stack
  1414. (<VAR>vstack</VAR>). The top of the value stack is <VAR>vtop</VAR>. Each value
  1415. stack entry is the structure <CODE>SValue</CODE>.
  1416.  
  1417.  
  1418. <P>
  1419. <CODE>SValue.t</CODE> is the type. <CODE>SValue.r</CODE> indicates how the value is
  1420. currently stored in the generated code. It is usually a CPU register
  1421. index (<CODE>REG_xxx</CODE> constants), but additional values and flags are
  1422. defined:
  1423.  
  1424.  
  1425.  
  1426. <PRE>
  1427. #define VT_CONST     0x00f0
  1428. #define VT_LLOCAL    0x00f1
  1429. #define VT_LOCAL     0x00f2
  1430. #define VT_CMP       0x00f3
  1431. #define VT_JMP       0x00f4
  1432. #define VT_JMPI      0x00f5
  1433. #define VT_LVAL      0x0100
  1434. #define VT_SYM       0x0200
  1435. #define VT_MUSTCAST  0x0400
  1436. #define VT_MUSTBOUND 0x0800
  1437. #define VT_BOUNDED   0x8000
  1438. #define VT_LVAL_BYTE     0x1000
  1439. #define VT_LVAL_SHORT    0x2000
  1440. #define VT_LVAL_UNSIGNED 0x4000
  1441. #define VT_LVAL_TYPE     (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
  1442. </PRE>
  1443.  
  1444. <DL COMPACT>
  1445.  
  1446. <DT><CODE>VT_CONST</CODE>
  1447. <DD>
  1448. indicates that the value is a constant. It is stored in the union
  1449. <CODE>SValue.c</CODE>, depending on its type.
  1450.  
  1451. <DT><CODE>VT_LOCAL</CODE>
  1452. <DD>
  1453. indicates a local variable pointer at offset <CODE>SValue.c.i</CODE> in the
  1454. stack.
  1455.  
  1456. <DT><CODE>VT_CMP</CODE>
  1457. <DD>
  1458. indicates that the value is actually stored in the CPU flags (i.e. the
  1459. value is the consequence of a test). The value is either 0 or 1. The
  1460. actual CPU flags used is indicated in <CODE>SValue.c.i</CODE>.
  1461.  
  1462. If any code is generated which destroys the CPU flags, this value MUST be
  1463. put in a normal register.
  1464.  
  1465. <DT><CODE>VT_JMP</CODE>
  1466. <DD>
  1467. <DT><CODE>VT_JMPI</CODE>
  1468. <DD>
  1469. indicates that the value is the consequence of a conditional jump. For VT_JMP,
  1470. it is 1 if the jump is taken, 0 otherwise. For VT_JMPI it is inverted.
  1471.  
  1472. These values are used to compile the <CODE>||</CODE> and <CODE>&#38;&#38;</CODE> logical
  1473. operators.
  1474.  
  1475. If any code is generated, this value MUST be put in a normal
  1476. register. Otherwise, the generated code won't be executed if the jump is
  1477. taken.
  1478.  
  1479. <DT><CODE>VT_LVAL</CODE>
  1480. <DD>
  1481. is a flag indicating that the value is actually an lvalue (left value of
  1482. an assignment). It means that the value stored is actually a pointer to
  1483. the wanted value.
  1484.  
  1485. Understanding the use <CODE>VT_LVAL</CODE> is very important if you want to
  1486. understand how TCC works.
  1487.  
  1488. <DT><CODE>VT_LVAL_BYTE</CODE>
  1489. <DD>
  1490. <DT><CODE>VT_LVAL_SHORT</CODE>
  1491. <DD>
  1492. <DT><CODE>VT_LVAL_UNSIGNED</CODE>
  1493. <DD>
  1494. if the lvalue has an integer type, then these flags give its real
  1495. type. The type alone is not enough in case of cast optimisations.
  1496.  
  1497. <DT><CODE>VT_LLOCAL</CODE>
  1498. <DD>
  1499. is a saved lvalue on the stack. <CODE>VT_LLOCAL</CODE> should be eliminated
  1500. ASAP because its semantics are rather complicated.
  1501.  
  1502. <DT><CODE>VT_MUSTCAST</CODE>
  1503. <DD>
  1504. indicates that a cast to the value type must be performed if the value
  1505. is used (lazy casting).
  1506.  
  1507. <DT><CODE>VT_SYM</CODE>
  1508. <DD>
  1509. indicates that the symbol <CODE>SValue.sym</CODE> must be added to the constant.
  1510.  
  1511. <DT><CODE>VT_MUSTBOUND</CODE>
  1512. <DD>
  1513. <DT><CODE>VT_BOUNDED</CODE>
  1514. <DD>
  1515. are only used for optional bound checking.
  1516.  
  1517. </DL>
  1518.  
  1519.  
  1520.  
  1521. <H3><A NAME="SEC33" HREF="tcc-doc.html#TOC33">8.7.3 Manipulating the value stack</A></H3>
  1522. <P>
  1523. <A NAME="IDX49"></A>
  1524.  
  1525.  
  1526. <P>
  1527. <CODE>vsetc()</CODE> and <CODE>vset()</CODE> pushes a new value on the value
  1528. stack. If the previous <VAR>vtop</VAR> was stored in a very unsafe place(for
  1529. example in the CPU flags), then some code is generated to put the
  1530. previous <VAR>vtop</VAR> in a safe storage.
  1531.  
  1532.  
  1533. <P>
  1534. <CODE>vpop()</CODE> pops <VAR>vtop</VAR>. In some cases, it also generates cleanup
  1535. code (for example if stacked floating point registers are used as on
  1536. x86).
  1537.  
  1538.  
  1539. <P>
  1540. The <CODE>gv(rc)</CODE> function generates code to evaluate <VAR>vtop</VAR> (the
  1541. top value of the stack) into registers. <VAR>rc</VAR> selects in which
  1542. register class the value should be put. <CODE>gv()</CODE> is the <EM>most
  1543. important function</EM> of the code generator.
  1544.  
  1545.  
  1546. <P>
  1547. <CODE>gv2()</CODE> is the same as <CODE>gv()</CODE> but for the top two stack
  1548. entries.
  1549.  
  1550.  
  1551.  
  1552.  
  1553. <H3><A NAME="SEC34" HREF="tcc-doc.html#TOC34">8.7.4 CPU dependent code generation</A></H3>
  1554. <P>
  1555. <A NAME="IDX50"></A>
  1556. See the <TT>`i386-gen.c'</TT> file to have an example.
  1557.  
  1558.  
  1559. <DL COMPACT>
  1560.  
  1561. <DT><CODE>load()</CODE>
  1562. <DD>
  1563. must generate the code needed to load a stack value into a register.
  1564.  
  1565. <DT><CODE>store()</CODE>
  1566. <DD>
  1567. must generate the code needed to store a register into a stack value
  1568. lvalue.
  1569.  
  1570. <DT><CODE>gfunc_start()</CODE>
  1571. <DD>
  1572. <DT><CODE>gfunc_param()</CODE>
  1573. <DD>
  1574. <DT><CODE>gfunc_call()</CODE>
  1575. <DD>
  1576. should generate a function call
  1577.  
  1578. <DT><CODE>gfunc_prolog()</CODE>
  1579. <DD>
  1580. <DT><CODE>gfunc_epilog()</CODE>
  1581. <DD>
  1582. should generate a function prolog/epilog.
  1583.  
  1584. <DT><CODE>gen_opi(op)</CODE>
  1585. <DD>
  1586. must generate the binary integer operation <VAR>op</VAR> on the two top
  1587. entries of the stack which are guaranted to contain integer types.
  1588.  
  1589. The result value should be put on the stack.
  1590.  
  1591. <DT><CODE>gen_opf(op)</CODE>
  1592. <DD>
  1593. same as <CODE>gen_opi()</CODE> for floating point operations. The two top
  1594. entries of the stack are guaranted to contain floating point values of
  1595. same types.
  1596.  
  1597. <DT><CODE>gen_cvt_itof()</CODE>
  1598. <DD>
  1599. integer to floating point conversion.
  1600.  
  1601. <DT><CODE>gen_cvt_ftoi()</CODE>
  1602. <DD>
  1603. floating point to integer conversion.
  1604.  
  1605. <DT><CODE>gen_cvt_ftof()</CODE>
  1606. <DD>
  1607. floating point to floating point of different size conversion.
  1608.  
  1609. <DT><CODE>gen_bounded_ptr_add()</CODE>
  1610. <DD>
  1611. <DT><CODE>gen_bounded_ptr_deref()</CODE>
  1612. <DD>
  1613. are only used for bounds checking.
  1614.  
  1615. </DL>
  1616.  
  1617.  
  1618.  
  1619. <H2><A NAME="SEC35" HREF="tcc-doc.html#TOC35">8.8 Optimizations done</A></H2>
  1620. <P>
  1621. <A NAME="IDX51"></A>
  1622. <A NAME="IDX52"></A>
  1623. <A NAME="IDX53"></A>
  1624. <A NAME="IDX54"></A>
  1625. <A NAME="IDX55"></A>
  1626. <A NAME="IDX56"></A>
  1627. <A NAME="IDX57"></A>
  1628. Constant propagation is done for all operations. Multiplications and
  1629. divisions are optimized to shifts when appropriate. Comparison
  1630. operators are optimized by maintaining a special cache for the
  1631. processor flags. &#38;&#38;, || and ! are optimized by maintaining a special
  1632. 'jump target' value. No other jump optimization is currently performed
  1633. because it would require to store the code in a more abstract fashion.
  1634.  
  1635.  
  1636.  
  1637.  
  1638. <H1><A NAME="SEC36" HREF="tcc-doc.html#TOC36">Concept Index</A></H1>
  1639. <P>
  1640. Jump to:
  1641. <A HREF="#cindex__">_</A>
  1642. -
  1643. <A HREF="#cindex_a">a</A>
  1644. -
  1645. <A HREF="#cindex_b">b</A>
  1646. -
  1647. <A HREF="#cindex_c">c</A>
  1648. -
  1649. <A HREF="#cindex_d">d</A>
  1650. -
  1651. <A HREF="#cindex_e">e</A>
  1652. -
  1653. <A HREF="#cindex_f">f</A>
  1654. -
  1655. <A HREF="#cindex_g">g</A>
  1656. -
  1657. <A HREF="#cindex_i">i</A>
  1658. -
  1659. <A HREF="#cindex_j">j</A>
  1660. -
  1661. <A HREF="#cindex_l">l</A>
  1662. -
  1663. <A HREF="#cindex_m">m</A>
  1664. -
  1665. <A HREF="#cindex_o">o</A>
  1666. -
  1667. <A HREF="#cindex_p">p</A>
  1668. -
  1669. <A HREF="#cindex_q">q</A>
  1670. -
  1671. <A HREF="#cindex_r">r</A>
  1672. -
  1673. <A HREF="#cindex_s">s</A>
  1674. -
  1675. <A HREF="#cindex_t">t</A>
  1676. -
  1677. <A HREF="#cindex_u">u</A>
  1678. -
  1679. <A HREF="#cindex_v">v</A>
  1680. -
  1681. <A HREF="#cindex_w">w</A>
  1682. <P>
  1683. <H2><A NAME="cindex__">_</A></H2>
  1684. <DIR>
  1685. <LI><A HREF="tcc-doc.html#IDX10">__asm__</A>
  1686. </DIR>
  1687. <H2><A NAME="cindex_a">a</A></H2>
  1688. <DIR>
  1689. <LI><A HREF="tcc-doc.html#IDX14">align directive</A>
  1690. <LI><A HREF="tcc-doc.html#IDX1">aligned attribute</A>
  1691. <LI><A HREF="tcc-doc.html#IDX34">ascii directive</A>
  1692. <LI><A HREF="tcc-doc.html#IDX33">asciz directive</A>
  1693. <LI><A HREF="tcc-doc.html#IDX35">assembler</A>
  1694. <LI><A HREF="tcc-doc.html#IDX12">assembler directives</A>
  1695. <LI><A HREF="tcc-doc.html#IDX9">assembly, inline</A>
  1696. </DIR>
  1697. <H2><A NAME="cindex_b">b</A></H2>
  1698. <DIR>
  1699. <LI><A HREF="tcc-doc.html#IDX45">bound checks</A>
  1700. <LI><A HREF="tcc-doc.html#IDX28">bss directive</A>
  1701. <LI><A HREF="tcc-doc.html#IDX17">byte directive</A>
  1702. </DIR>
  1703. <H2><A NAME="cindex_c">c</A></H2>
  1704. <DIR>
  1705. <LI><A HREF="tcc-doc.html#IDX55">caching processor flags</A>
  1706. <LI><A HREF="tcc-doc.html#IDX5">cdecl attribute</A>
  1707. <LI><A HREF="tcc-doc.html#IDX47">code generation</A>
  1708. <LI><A HREF="tcc-doc.html#IDX54">comparison operators</A>
  1709. <LI><A HREF="tcc-doc.html#IDX52">constant propagation</A>
  1710. <LI><A HREF="tcc-doc.html#IDX50">CPU dependent</A>
  1711. </DIR>
  1712. <H2><A NAME="cindex_d">d</A></H2>
  1713. <DIR>
  1714. <LI><A HREF="tcc-doc.html#IDX27">data directive</A>
  1715. <LI><A HREF="tcc-doc.html#IDX13">directives, assembler</A>
  1716. </DIR>
  1717. <H2><A NAME="cindex_e">e</A></H2>
  1718. <DIR>
  1719. <LI><A HREF="tcc-doc.html#IDX37">ELF</A>
  1720. </DIR>
  1721. <H2><A NAME="cindex_f">f</A></H2>
  1722. <DIR>
  1723. <LI><A HREF="tcc-doc.html#IDX42">FILE, linker command</A>
  1724. <LI><A HREF="tcc-doc.html#IDX29">fill directive</A>
  1725. <LI><A HREF="tcc-doc.html#IDX56">flags, caching</A>
  1726. </DIR>
  1727. <H2><A NAME="cindex_g">g</A></H2>
  1728. <DIR>
  1729. <LI><A HREF="tcc-doc.html#IDX11">gas</A>
  1730. <LI><A HREF="tcc-doc.html#IDX24">global directive</A>
  1731. <LI><A HREF="tcc-doc.html#IDX23">globl directive</A>
  1732. <LI><A HREF="tcc-doc.html#IDX41">GROUP, linker command</A>
  1733. </DIR>
  1734. <H2><A NAME="cindex_i">i</A></H2>
  1735. <DIR>
  1736. <LI><A HREF="tcc-doc.html#IDX8">inline assembly</A>
  1737. <LI><A HREF="tcc-doc.html#IDX20">int directive</A>
  1738. </DIR>
  1739. <H2><A NAME="cindex_j">j</A></H2>
  1740. <DIR>
  1741. <LI><A HREF="tcc-doc.html#IDX57">jump optimization</A>
  1742. </DIR>
  1743. <H2><A NAME="cindex_l">l</A></H2>
  1744. <DIR>
  1745. <LI><A HREF="tcc-doc.html#IDX36">linker</A>
  1746. <LI><A HREF="tcc-doc.html#IDX40">linker scripts</A>
  1747. <LI><A HREF="tcc-doc.html#IDX21">long directive</A>
  1748. </DIR>
  1749. <H2><A NAME="cindex_m">m</A></H2>
  1750. <DIR>
  1751. <LI><A HREF="tcc-doc.html#IDX46">memory checks</A>
  1752. </DIR>
  1753. <H2><A NAME="cindex_o">o</A></H2>
  1754. <DIR>
  1755. <LI><A HREF="tcc-doc.html#IDX51">optimizations</A>
  1756. <LI><A HREF="tcc-doc.html#IDX30">org directive</A>
  1757. <LI><A HREF="tcc-doc.html#IDX43">OUTPUT_FORMAT, linker command</A>
  1758. </DIR>
  1759. <H2><A NAME="cindex_p">p</A></H2>
  1760. <DIR>
  1761. <LI><A HREF="tcc-doc.html#IDX2">packed attribute</A>
  1762. <LI><A HREF="tcc-doc.html#IDX38">PE-i386</A>
  1763. <LI><A HREF="tcc-doc.html#IDX31">previous directive</A>
  1764. </DIR>
  1765. <H2><A NAME="cindex_q">q</A></H2>
  1766. <DIR>
  1767. <LI><A HREF="tcc-doc.html#IDX22">quad directive</A>
  1768. </DIR>
  1769. <H2><A NAME="cindex_r">r</A></H2>
  1770. <DIR>
  1771. <LI><A HREF="tcc-doc.html#IDX7">regparm attribute</A>
  1772. </DIR>
  1773. <H2><A NAME="cindex_s">s</A></H2>
  1774. <DIR>
  1775. <LI><A HREF="tcc-doc.html#IDX39">scripts, linker</A>
  1776. <LI><A HREF="tcc-doc.html#IDX3">section attribute</A>
  1777. <LI><A HREF="tcc-doc.html#IDX25">section directive</A>
  1778. <LI><A HREF="tcc-doc.html#IDX19">short directive</A>
  1779. <LI><A HREF="tcc-doc.html#IDX15">skip directive</A>
  1780. <LI><A HREF="tcc-doc.html#IDX16">space directive</A>
  1781. <LI><A HREF="tcc-doc.html#IDX6">stdcall attribute</A>
  1782. <LI><A HREF="tcc-doc.html#IDX53">strength reduction</A>
  1783. <LI><A HREF="tcc-doc.html#IDX32">string directive</A>
  1784. </DIR>
  1785. <H2><A NAME="cindex_t">t</A></H2>
  1786. <DIR>
  1787. <LI><A HREF="tcc-doc.html#IDX44">TARGET, linker command</A>
  1788. <LI><A HREF="tcc-doc.html#IDX26">text directive</A>
  1789. </DIR>
  1790. <H2><A NAME="cindex_u">u</A></H2>
  1791. <DIR>
  1792. <LI><A HREF="tcc-doc.html#IDX4">unused attribute</A>
  1793. </DIR>
  1794. <H2><A NAME="cindex_v">v</A></H2>
  1795. <DIR>
  1796. <LI><A HREF="tcc-doc.html#IDX49">value stack</A>
  1797. <LI><A HREF="tcc-doc.html#IDX48">value stack, introduction</A>
  1798. </DIR>
  1799. <H2><A NAME="cindex_w">w</A></H2>
  1800. <DIR>
  1801. <LI><A HREF="tcc-doc.html#IDX18">word directive</A>
  1802. </DIR>
  1803.  
  1804.  
  1805. <P><HR><P>
  1806. This document was generated on 18 June 2005 using
  1807. <A HREF="http://wwwinfo.cern.ch/dis/texi2html/">texi2html</A>&nbsp;1.56k.
  1808. </BODY>
  1809. </HTML>
  1810.