📄 tcc-doc.html
字号:
#define VT_SHORT 2 /* short type */#define VT_VOID 3 /* void type */#define VT_PTR 4 /* pointer */#define VT_ENUM 5 /* enum definition */#define VT_FUNC 6 /* function type */#define VT_STRUCT 7 /* struct/union definition */#define VT_FLOAT 8 /* IEEE float */#define VT_DOUBLE 9 /* IEEE double */#define VT_LDOUBLE 10 /* IEEE long double */#define VT_BOOL 11 /* ISOC99 boolean type */#define VT_LLONG 12 /* 64 bit integer */#define VT_LONG 13 /* long integer (NEVER USED as type, only during parsing) */#define VT_BTYPE 0x000f /* mask for basic type */#define VT_UNSIGNED 0x0010 /* unsigned type */#define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */#define VT_BITFIELD 0x0040 /* bitfield modifier */#define VT_STRUCT_SHIFT 16 /* structure/enum name shift (16 bits left) */</PRE><P>When a reference to another type is needed (for pointers, functions andstructures), the <CODE>32 - VT_STRUCT_SHIFT</CODE> high order bits are used tostore an identifier reference.<P>The <CODE>VT_UNSIGNED</CODE> flag can be set for chars, shorts, ints and longlongs.<P>Arrays are considered as pointers <CODE>VT_PTR</CODE> with the flag<CODE>VT_ARRAY</CODE> set.<P>The <CODE>VT_BITFIELD</CODE> flag can be set for chars, shorts, ints and longlongs. If it is set, then the bitfield position is stored from bitsVT_STRUCT_SHIFT to VT_STRUCT_SHIFT + 5 and the bit field size is storedfrom bits VT_STRUCT_SHIFT + 6 to VT_STRUCT_SHIFT + 11.<P><CODE>VT_LONG</CODE> is never used except during parsing.<P>During parsing, the storage of an object is also stored in the typeinteger:<PRE>#define VT_EXTERN 0x00000080 /* extern definition */#define VT_STATIC 0x00000100 /* static variable */#define VT_TYPEDEF 0x00000200 /* typedef definition */</PRE><H2><A NAME="SEC27" HREF="tcc-doc.html#TOC27">8.5 Symbols</A></H2><P>All symbols are stored in hashed symbol stacks. Each symbol stackcontains <CODE>Sym</CODE> structures.<P><CODE>Sym.v</CODE> contains the symbol name (rememberan idenfier is also a token, so a string is never necessary to storeit). <CODE>Sym.t</CODE> gives the type of the symbol. <CODE>Sym.r</CODE> is usuallythe register in which the corresponding variable is stored. <CODE>Sym.c</CODE> isusually a constant associated to the symbol.<P>Four main symbol stacks are defined:<DL COMPACT><DT><CODE>define_stack</CODE><DD>for the macros (<CODE>#define</CODE>s).<DT><CODE>global_stack</CODE><DD>for the global variables, functions and types.<DT><CODE>local_stack</CODE><DD>for the local variables, functions and types.<DT><CODE>global_label_stack</CODE><DD>for the local labels (for <CODE>goto</CODE>).<DT><CODE>label_stack</CODE><DD>for GCC block local labels (see the <CODE>__label__</CODE> keyword).</DL><P><CODE>sym_push()</CODE> is used to add a new symbol in the local symbolstack. If no local symbol stack is active, it is added in the globalsymbol stack.<P><CODE>sym_pop(st,b)</CODE> pops symbols from the symbol stack <VAR>st</VAR> untilthe symbol <VAR>b</VAR> is on the top of stack. If <VAR>b</VAR> is NULL, the stackis emptied.<P><CODE>sym_find(v)</CODE> return the symbol associated to the identifier<VAR>v</VAR>. The local stack is searched first from top to bottom, then theglobal stack.<H2><A NAME="SEC28" HREF="tcc-doc.html#TOC28">8.6 Sections</A></H2><P>The generated code and datas are written in sections. The structure<CODE>Section</CODE> contains all the necessary information for a givensection. <CODE>new_section()</CODE> creates a new section. ELF file semanticsis assumed for each section.<P>The following sections are predefined:<DL COMPACT><DT><CODE>text_section</CODE><DD>is the section containing the generated code. <VAR>ind</VAR> contains thecurrent position in the code section.<DT><CODE>data_section</CODE><DD>contains initialized data<DT><CODE>bss_section</CODE><DD>contains uninitialized data<DT><CODE>bounds_section</CODE><DD><DT><CODE>lbounds_section</CODE><DD>are used when bound checking is activated<DT><CODE>stab_section</CODE><DD><DT><CODE>stabstr_section</CODE><DD>are used when debugging is actived to store debug information<DT><CODE>symtab_section</CODE><DD><DT><CODE>strtab_section</CODE><DD>contain the exported symbols (currently only used for debugging).</DL><H2><A NAME="SEC29" HREF="tcc-doc.html#TOC29">8.7 Code generation</A></H2><P><A NAME="IDX33"></A><H3><A NAME="SEC30" HREF="tcc-doc.html#TOC30">8.7.1 Introduction</A></H3><P>The TCC code generator directly generates linked binary code in onepass. It is rather unusual these days (see gcc for example whichgenerates text assembly), but it can be very fast and surprisinglylittle complicated.<P>The TCC code generator is register based. Optimization is only done atthe expression level. No intermediate representation of expression iskept except the current values stored in the <EM>value stack</EM>.<P>On x86, three temporary registers are used. When more registers areneeded, one register is spilled into a new temporary variable on the stack.<H3><A NAME="SEC31" HREF="tcc-doc.html#TOC31">8.7.2 The value stack</A></H3><P><A NAME="IDX34"></A><P>When an expression is parsed, its value is pushed on the value stack(<VAR>vstack</VAR>). The top of the value stack is <VAR>vtop</VAR>. Each valuestack entry is the structure <CODE>SValue</CODE>.<P><CODE>SValue.t</CODE> is the type. <CODE>SValue.r</CODE> indicates how the value iscurrently stored in the generated code. It is usually a CPU registerindex (<CODE>REG_xxx</CODE> constants), but additional values and flags aredefined:<PRE>#define VT_CONST 0x00f0#define VT_LLOCAL 0x00f1#define VT_LOCAL 0x00f2#define VT_CMP 0x00f3#define VT_JMP 0x00f4#define VT_JMPI 0x00f5#define VT_LVAL 0x0100#define VT_SYM 0x0200#define VT_MUSTCAST 0x0400#define VT_MUSTBOUND 0x0800#define VT_BOUNDED 0x8000#define VT_LVAL_BYTE 0x1000#define VT_LVAL_SHORT 0x2000#define VT_LVAL_UNSIGNED 0x4000#define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)</PRE><DL COMPACT><DT><CODE>VT_CONST</CODE><DD>indicates that the value is a constant. It is stored in the union<CODE>SValue.c</CODE>, depending on its type.<DT><CODE>VT_LOCAL</CODE><DD>indicates a local variable pointer at offset <CODE>SValue.c.i</CODE> in thestack.<DT><CODE>VT_CMP</CODE><DD>indicates that the value is actually stored in the CPU flags (i.e. thevalue is the consequence of a test). The value is either 0 or 1. Theactual CPU flags used is indicated in <CODE>SValue.c.i</CODE>. If any code is generated which destroys the CPU flags, this value MUST beput in a normal register.<DT><CODE>VT_JMP</CODE><DD><DT><CODE>VT_JMPI</CODE><DD>indicates that the value is the consequence of a conditional jump. For VT_JMP,it is 1 if the jump is taken, 0 otherwise. For VT_JMPI it is inverted.These values are used to compile the <CODE>||</CODE> and <CODE>&&</CODE> logicaloperators.If any code is generated, this value MUST be put in a normalregister. Otherwise, the generated code won't be executed if the jump istaken.<DT><CODE>VT_LVAL</CODE><DD>is a flag indicating that the value is actually an lvalue (left value ofan assignment). It means that the value stored is actually a pointer tothe wanted value. Understanding the use <CODE>VT_LVAL</CODE> is very important if you want tounderstand how TCC works.<DT><CODE>VT_LVAL_BYTE</CODE><DD><DT><CODE>VT_LVAL_SHORT</CODE><DD><DT><CODE>VT_LVAL_UNSIGNED</CODE><DD>if the lvalue has an integer type, then these flags give its realtype. The type alone is not enough in case of cast optimisations.<DT><CODE>VT_LLOCAL</CODE><DD>is a saved lvalue on the stack. <CODE>VT_LLOCAL</CODE> should be eliminatedASAP because its semantics are rather complicated.<DT><CODE>VT_MUSTCAST</CODE><DD>indicates that a cast to the value type must be performed if the valueis used (lazy casting).<DT><CODE>VT_SYM</CODE><DD>indicates that the symbol <CODE>SValue.sym</CODE> must be added to the constant.<DT><CODE>VT_MUSTBOUND</CODE><DD><DT><CODE>VT_BOUNDED</CODE><DD>are only used for optional bound checking.</DL><H3><A NAME="SEC32" HREF="tcc-doc.html#TOC32">8.7.3 Manipulating the value stack</A></H3><P><A NAME="IDX35"></A><P><CODE>vsetc()</CODE> and <CODE>vset()</CODE> pushes a new value on the valuestack. If the previous <VAR>vtop</VAR> was stored in a very unsafe place(forexample in the CPU flags), then some code is generated to put theprevious <VAR>vtop</VAR> in a safe storage.<P><CODE>vpop()</CODE> pops <VAR>vtop</VAR>. In some cases, it also generates cleanupcode (for example if stacked floating point registers are used as onx86).<P>The <CODE>gv(rc)</CODE> function generates code to evaluate <VAR>vtop</VAR> (thetop value of the stack) into registers. <VAR>rc</VAR> selects in whichregister class the value should be put. <CODE>gv()</CODE> is the <EM>mostimportant function</EM> of the code generator.<P><CODE>gv2()</CODE> is the same as <CODE>gv()</CODE> but for the top two stackentries.<H3><A NAME="SEC33" HREF="tcc-doc.html#TOC33">8.7.4 CPU dependent code generation</A></H3><P><A NAME="IDX36"></A>See the <TT>`i386-gen.c'</TT> file to have an example.<DL COMPACT><DT><CODE>load()</CODE><DD>must generate the code needed to load a stack value into a register.<DT><CODE>store()</CODE><DD>must generate the code needed to store a register into a stack valuelvalue.<DT><CODE>gfunc_start()</CODE><DD><DT><CODE>gfunc_param()</CODE><DD><DT><CODE>gfunc_call()</CODE><DD>should generate a function call<DT><CODE>gfunc_prolog()</CODE><DD><DT><CODE>gfunc_epilog()</CODE><DD>should generate a function prolog/epilog.<DT><CODE>gen_opi(op)</CODE><DD>must generate the binary integer operation <VAR>op</VAR> on the two topentries of the stack which are guaranted to contain integer types.The result value should be put on the stack.<DT><CODE>gen_opf(op)</CODE><DD>same as <CODE>gen_opi()</CODE> for floating point operations. The two topentries of the stack are guaranted to contain floating point values ofsame types.<DT><CODE>gen_cvt_itof()</CODE><DD>integer to floating point conversion.<DT><CODE>gen_cvt_ftoi()</CODE><DD>floating point to integer conversion.<DT><CODE>gen_cvt_ftof()</CODE><DD>floating point to floating point of different size conversion.<DT><CODE>gen_bounded_ptr_add()</CODE><DD><DT><CODE>gen_bounded_ptr_deref()</CODE><DD>are only used for bounds checking.</DL><H2><A NAME="SEC34" HREF="tcc-doc.html#TOC34">8.8 Optimizations done</A></H2><P><A NAME="IDX37"></A><A NAME="IDX38"></A><A NAME="IDX39"></A><A NAME="IDX40"></A><A NAME="IDX41"></A><A NAME="IDX42"></A><A NAME="IDX43"></A>Constant propagation is done for all operations. Multiplications anddivisions are optimized to shifts when appropriate. Comparisonoperators are optimized by maintaining a special cache for theprocessor flags. &&, || and ! are optimized by maintaining a special'jump target' value. No other jump optimization is currently performedbecause it would require to store the code in a more abstract fashion.<H1><A NAME="SEC35" HREF="tcc-doc.html#TOC35">Concept Index</A></H1><P>Jump to:<A HREF="#cindex_.">.</A>-<A HREF="#cindex__">_</A>-<A HREF="#cindex_a">a</A>-<A HREF="#cindex_b">b</A>-<A HREF="#cindex_c">c</A>-<A HREF="#cindex_d">d</A>-<A HREF="#cindex_e">e</A>-<A HREF="#cindex_f">f</A>-<A HREF="#cindex_g">g</A>-<A HREF="#cindex_i">i</A>-<A HREF="#cindex_j">j</A>-<A HREF="#cindex_l">l</A>-<A HREF="#cindex_m">m</A>-<A HREF="#cindex_o">o</A>-<A HREF="#cindex_s">s</A>-<A HREF="#cindex_t">t</A>-<A HREF="#cindex_v">v</A><P><H2><A NAME="cindex_.">.</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX7">.align</A><LI><A HREF="tcc-doc.html#IDX21">.bss</A><LI><A HREF="tcc-doc.html#IDX10">.byte</A><LI><A HREF="tcc-doc.html#IDX20">.data</A><LI><A HREF="tcc-doc.html#IDX17">.global</A><LI><A HREF="tcc-doc.html#IDX16">.globl</A><LI><A HREF="tcc-doc.html#IDX13">.int</A><LI><A HREF="tcc-doc.html#IDX14">.long</A><LI><A HREF="tcc-doc.html#IDX18">.section</A><LI><A HREF="tcc-doc.html#IDX12">.short</A><LI><A HREF="tcc-doc.html#IDX8">.skip</A><LI><A HREF="tcc-doc.html#IDX9">.space</A><LI><A HREF="tcc-doc.html#IDX15">.string</A><LI><A HREF="tcc-doc.html#IDX19">.text</A><LI><A HREF="tcc-doc.html#IDX11">.word</A></DIR><H2><A NAME="cindex__">_</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX3">__asm__</A></DIR><H2><A NAME="cindex_a">a</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX22">assembler</A><LI><A HREF="tcc-doc.html#IDX5">assembler directives</A><LI><A HREF="tcc-doc.html#IDX2">assembly, inline</A></DIR><H2><A NAME="cindex_b">b</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX31">bound checks</A></DIR><H2><A NAME="cindex_c">c</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX41">caching processor flags</A><LI><A HREF="tcc-doc.html#IDX33">code generation</A><LI><A HREF="tcc-doc.html#IDX40">comparison operators</A><LI><A HREF="tcc-doc.html#IDX38">constant propagation</A><LI><A HREF="tcc-doc.html#IDX36">CPU dependent</A></DIR><H2><A NAME="cindex_d">d</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX6">directives, assembler</A></DIR><H2><A NAME="cindex_e">e</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX24">ELF</A></DIR><H2><A NAME="cindex_f">f</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX28">FILE, linker command</A><LI><A HREF="tcc-doc.html#IDX42">flags, caching</A></DIR><H2><A NAME="cindex_g">g</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX4">gas</A><LI><A HREF="tcc-doc.html#IDX27">GROUP, linker command</A></DIR><H2><A NAME="cindex_i">i</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX1">inline assembly</A></DIR><H2><A NAME="cindex_j">j</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX43">jump optimization</A></DIR><H2><A NAME="cindex_l">l</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX23">linker</A><LI><A HREF="tcc-doc.html#IDX26">linker scripts</A></DIR><H2><A NAME="cindex_m">m</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX32">memory checks</A></DIR><H2><A NAME="cindex_o">o</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX37">optimizations</A><LI><A HREF="tcc-doc.html#IDX29">OUTPUT_FORMAT, linker command</A></DIR><H2><A NAME="cindex_s">s</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX25">scripts, linker</A><LI><A HREF="tcc-doc.html#IDX39">strength reduction</A></DIR><H2><A NAME="cindex_t">t</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX30">TARGET, linker command</A></DIR><H2><A NAME="cindex_v">v</A></H2><DIR><LI><A HREF="tcc-doc.html#IDX35">value stack</A><LI><A HREF="tcc-doc.html#IDX34">value stack, introduction</A></DIR><P><HR><P>This document was generated on 8 November 2004 using<A HREF="http://wwwinfo.cern.ch/dis/texi2html/">texi2html</A> 1.56k.</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -