File:  [gforth] / gforth / engine / threaded.h
Revision 1.37: download - view: text, annotated - select for diffs
Mon Dec 31 17:34:59 2007 UTC (12 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright years

    1: /* This file defines a number of threading schemes.
    2: 
    3:   Copyright (C) 1995, 1996,1997,1999,2003,2004,2005,2007 Free Software Foundation, Inc.
    4: 
    5:   This file is part of Gforth.
    6: 
    7:   Gforth is free software; you can redistribute it and/or
    8:   modify it under the terms of the GNU General Public License
    9:   as published by the Free Software Foundation; either version 2
   10:   of the License, or (at your option) any later version.
   11: 
   12:   This program is distributed in the hope that it will be useful,
   13:   but WITHOUT ANY WARRANTY; without even the implied warranty of
   14:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15:   GNU General Public License for more details.
   16: 
   17:   You should have received a copy of the GNU General Public License
   18:   along with this program; if not, write to the Free Software
   19:   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: 
   22:   This files defines macros for threading. Many sets of macros are
   23:   defined. Functionally they have only one difference: Some implement
   24:   direct threading, some indirect threading. The other differences are
   25:   just variations to help GCC generate faster code for various
   26:   machines.
   27: 
   28:   (Well, to tell the truth, there actually is another functional
   29:   difference in some pathological cases: e.g., a '!' stores into the
   30:   cell where the next executed word comes from; or, the next word
   31:   executed comes from the top-of-stack. These differences are one of
   32:   the reasons why GCC cannot produce the right variation by itself. We
   33:   chose disallowing such practices and using the added implementation
   34:   freedom to achieve a significant speedup, because these practices
   35:   are not common in Forth (I have never heard of or seen anyone using
   36:   them), and it is easy to circumvent problems: A control flow change
   37:   will flush any prefetched words; you may want to do a "0
   38:   drop" before that to write back the top-of-stack cache.)
   39: 
   40:   These macro sets are used in the following ways: After translation
   41:   to C a typical primitive looks like
   42: 
   43:   ...
   44:   {
   45:   DEF_CA
   46:   other declarations
   47:   NEXT_P0;
   48:   main part of the primitive
   49:   NEXT_P1;
   50:   store results to stack
   51:   NEXT_P2;
   52:   }
   53: 
   54:   DEF_CA and all the NEXT_P* together must implement NEXT; In the main
   55:   part the instruction pointer can be read with IP, changed with
   56:   INC_IP(const_inc), and the cell right behind the presently executing
   57:   word (i.e. the value of *IP) is accessed with NEXT_INST.
   58: 
   59:   If a primitive does not fall through the main part, it has to do the
   60:   rest by itself. If it changes ip, it has to redo NEXT_P0 (perhaps we
   61:   should define a macro SET_IP).
   62: 
   63:   Some primitives (execute, dodefer) do not end with NEXT, but with
   64:   EXEC(.). If NEXT_P0 has been called earlier, it has to perform
   65:   "ip=IP;" to ensure that ip has the right value (NEXT_P0 may change
   66:   it).
   67: 
   68:   Finally, there is NEXT1_P1 and NEXT1_P2, which are parts of EXEC
   69:   (EXEC(XT) could be defined as "cfa=XT; NEXT1_P1; NEXT1_P2;" (is this
   70:   true?)) and are used for making docol faster.
   71: 
   72:   We can define the ways in which these macros are used with a regular
   73:   expression:
   74: 
   75:   For a primitive
   76: 
   77:   DEF_CA NEXT_P0 ( IP | INC_IP | NEXT_INST | ip=...; NEXT_P0 ) * ( NEXT_P1 NEXT_P2 | EXEC(...) )
   78: 
   79:   For a run-time routine, e.g., docol:
   80:   PFA1(cfa) ( NEXT_P0 NEXT | cfa=...; NEXT1_P1; NEXT1_P2 | EXEC(...) )
   81: 
   82:   This comment does not yet describe all the dependences that the
   83:   macros have to satisfy.
   84: 
   85:   To organize the former ifdef chaos, each path is separated
   86:   This gives a quite impressive number of paths, but you clearly
   87:   find things that go together.
   88: 
   89:   It should be possible to organize the whole thing in a way that
   90:   contains less redundancy and allows a simpler description.
   91: 
   92: */
   93: 
   94: #if !defined(GCC_PR15242_WORKAROUND)
   95: #if __GNUC__ == 3
   96: /* various gcc-3.x version have problems (including PR15242) that are
   97:    solved with this workaround */
   98: #define GCC_PR15242_WORKAROUND 1
   99: #else
  100: /* other gcc versions are better off without the workaround for
  101:    primitives that are not relocatable */
  102: #define GCC_PR15242_WORKAROUND 0
  103: #endif
  104: #endif
  105: 
  106: #if GCC_PR15242_WORKAROUND
  107: #define DO_GOTO goto before_goto
  108: #else
  109: #define DO_GOTO goto *real_ca
  110: #endif
  111: 
  112: #ifndef GOTO_ALIGN
  113: #define GOTO_ALIGN
  114: #endif
  115: 
  116: #define GOTO(target) do {(real_ca=(target));} while(0)
  117: #define NEXT_P2 do {NEXT_P1_5; DO_GOTO;} while(0)
  118: #define EXEC(XT) do { real_ca=EXEC1(XT); DO_GOTO;} while (0)
  119: #define VM_JUMP(target) do {GOTO(target);} while (0)
  120: #define NEXT do {DEF_CA NEXT_P1; NEXT_P2;} while(0)
  121: #define FIRST_NEXT_P2 NEXT_P1_5; GOTO_ALIGN; \
  122: before_goto: goto *real_ca; after_goto:
  123: #define FIRST_NEXT do {DEF_CA NEXT_P1; FIRST_NEXT_P2;} while(0)
  124: #define IPTOS NEXT_INST
  125: 
  126: 
  127: #ifdef DOUBLY_INDIRECT
  128: # ifndef DEBUG_DITC
  129: #  define DEBUG_DITC 0
  130: # endif
  131: /* define to 1 if you want to check consistency */
  132: #  define NEXT_P0	do {cfa1=cfa; cfa=*ip;} while(0)
  133: #  define CFA		cfa1
  134: #  define MORE_VARS     Xt cfa1;
  135: #  define IP		(ip)
  136: #  define SET_IP(p)	do {ip=(p); cfa=*ip;} while(0)
  137: #  define NEXT_INST	(cfa)
  138: #  define INC_IP(const_inc)	do {cfa=IP[const_inc]; ip+=(const_inc);} while(0)
  139: #  define DEF_CA	Label ca;
  140: #  define NEXT_P1	do {\
  141:   if (DEBUG_DITC && (cfa<=vm_prims+DOESJUMP || cfa>=vm_prims+npriminfos)) \
  142:     fprintf(stderr,"NEXT encountered prim %p at ip=%p\n", cfa, ip); \
  143:   ip++;} while(0)
  144: #  define NEXT_P1_5	do {ca=**cfa; GOTO(ca);} while(0)
  145: #  define EXEC1(XT)	({DEF_CA cfa=(XT);\
  146:   if (DEBUG_DITC && (cfa>vm_prims+DOESJUMP && cfa<vm_prims+npriminfos)) \
  147:     fprintf(stderr,"EXEC encountered xt %p at ip=%p, vm_prims=%p, xts=%p\n", cfa, ip, vm_prims, xts); \
  148:  ca=**cfa; ca;})
  149: 
  150: #elif defined(NO_IP)
  151: 
  152: #define NEXT_P0
  153: #  define CFA		cfa
  154: #define SET_IP(target)	assert(0)
  155: #define INC_IP(n)	((void)0)
  156: #define DEF_CA
  157: #define NEXT_P1
  158: #define NEXT_P1_5		do {goto *next_code;} while(0)
  159: /* set next_code to the return address before performing EXEC */
  160: /* original: */
  161: /* #define EXEC1(XT)	do {cfa=(XT); goto **cfa;} while(0) */
  162: /* fake, to make syntax check work */
  163: #define EXEC1(XT)	({cfa=(XT); *cfa;})
  164: 
  165: #else  /* !defined(DOUBLY_INDIRECT) && !defined(NO_IP) */
  166: 
  167: #if defined(DIRECT_THREADED)
  168: 
  169: /* This lets the compiler know that cfa is dead before; we place it at
  170:    "goto *"s that perform direct threaded dispatch (i.e., not EXECUTE
  171:    etc.), and thus do not reach doers, which would use cfa; the only
  172:    way to a doer is through EXECUTE etc., which set the cfa
  173:    themselves.
  174: 
  175:    Some of these direct threaded schemes use "cfa" to hold the code
  176:    address in normal direct threaded code.  Of course we cannot use
  177:    KILLS there.
  178: 
  179:    KILLS works by having an empty asm instruction, and claiming to the
  180:    compiler that it writes to cfa.
  181: 
  182:    KILLS is optional.  You can write
  183: 
  184: #define KILLS
  185: 
  186:    and lose just a little performance.
  187: */
  188: #define KILLS asm("":"=X"(cfa));
  189: 
  190: #warning direct threading scheme 8: cfa dead, i386 hack
  191: #  define NEXT_P0
  192: #  define CFA		cfa
  193: #  define IP		(ip)
  194: #  define SET_IP(p)	do {ip=(p); NEXT_P0;} while(0)
  195: #  define NEXT_INST	(*IP)
  196: #  define INC_IP(const_inc)	do { ip+=(const_inc);} while(0)
  197: #  define DEF_CA
  198: #  define NEXT_P1	(ip++)
  199: #  define NEXT_P1_5	do {KILLS GOTO(*(ip-1));} while(0)
  200: #  define EXEC1(XT)	({cfa=(XT); *cfa;})
  201: 
  202: /* direct threaded */
  203: #else
  204: /* indirect THREADED  */
  205: 
  206: #warning indirect threading scheme 8: low latency,cisc
  207: #  define NEXT_P0
  208: #  define CFA		cfa
  209: #  define IP		(ip)
  210: #  define SET_IP(p)	do {ip=(p); NEXT_P0;} while(0)
  211: #  define NEXT_INST	(*ip)
  212: #  define INC_IP(const_inc)	do {ip+=(const_inc);} while(0)
  213: #  define DEF_CA
  214: #  define NEXT_P1
  215: #  define NEXT_P1_5	do {cfa=*ip++; GOTO(*cfa);} while(0)
  216: #  define EXEC1(XT)	({cfa=(XT); *cfa;})
  217: 
  218: /* indirect threaded */
  219: #endif
  220: 
  221: #endif /* !defined(DOUBLY_INDIRECT) && !defined(NO_IP) */
  222: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>