File:  [gforth] / gforth / Attic / threading.h
Revision 1.4: download - view: text, annotated - select for diffs
Mon Feb 19 18:57:27 1996 UTC (25 years, 7 months ago) by anton
Branches: MAIN
CVS tags: v0-2-1, v0-2-0, HEAD
a few bug fixes, doc changes

    1: /* This file defines a number of threading schemes.
    2: 
    3:   Copyright (C) 1995, 1996 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., 675 Mass Ave, Cambridge, MA 02139, 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: #ifndef GETCFA
   95: #  define CFA_NEXT
   96: #endif
   97: 
   98: #if defined(DIRECT_THREADED) && defined(AUTO_INCREMENT)\
   99:     && defined(LONG_LATENCY) && defined(CFA_NEXT)
  100: #warning scheme 1
  101: #  define NEXT_P0	({cfa=*ip++;})
  102: #  define IP		(ip-1)
  103: #  define NEXT_INST	(cfa)
  104: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  105: #  define DEF_CA
  106: #  define NEXT_P1
  107: #  define NEXT_P2	({goto *cfa;})
  108: #  define EXEC(XT)	({cfa=(XT); goto *cfa;})
  109: #endif
  110: 
  111: #if defined(DIRECT_THREADED) && defined(AUTO_INCREMENT)\
  112:     && defined(LONG_LATENCY) && !defined(CFA_NEXT)
  113: #warning scheme 2
  114: #  define NEXT_P0	(ip++)
  115: #  define IP		(ip-1)
  116: #  define NEXT_INST	(*(ip-1))
  117: #  define INC_IP(const_inc)	({ ip+=(const_inc);})
  118: #  define DEF_CA
  119: #  define NEXT_P1
  120: #  define NEXT_P2	({goto **(ip-1);})
  121: #  define EXEC(XT)	({goto *(XT);})
  122: #endif
  123: 
  124: 
  125: #if defined(DIRECT_THREADED) && defined(AUTO_INCREMENT)\
  126:     && !defined(LONG_LATENCY) && defined(CFA_NEXT)
  127: #warning scheme 3
  128: #  define NEXT_P0
  129: #  define IP		(ip)
  130: #  define NEXT_INST	(*ip)
  131: #  define INC_IP(const_inc)	({ip+=(const_inc);})
  132: #  define DEF_CA
  133: #  define NEXT_P1	({cfa=*ip++;})
  134: #  define NEXT_P2	({goto *cfa;})
  135: #  define EXEC(XT)	({cfa=(XT); goto *cfa;})
  136: #endif
  137: 
  138: #if defined(DIRECT_THREADED) && defined(AUTO_INCREMENT)\
  139:     && !defined(LONG_LATENCY) && !defined(CFA_NEXT)
  140: #warning scheme 4
  141: #  define NEXT_P0
  142: #  define IP		(ip)
  143: #  define NEXT_INST	(*ip)
  144: #  define INC_IP(const_inc)	({ ip+=(const_inc);})
  145: #  define DEF_CA
  146: #  define NEXT_P1
  147: #  define NEXT_P2	({goto **(ip++);})
  148: #  define EXEC(XT)	({goto *(XT);})
  149: #endif
  150: 
  151: /* without autoincrement */
  152: 
  153: #if defined(DIRECT_THREADED) && !defined(AUTO_INCREMENT)\
  154:     && defined(LONG_LATENCY) && defined(CFA_NEXT)
  155: #warning scheme 5
  156: #  define NEXT_P0	({cfa=*ip;})
  157: #  define IP		(ip)
  158: #  define NEXT_INST	(cfa)
  159: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  160: #  define DEF_CA
  161: #  define NEXT_P1	(ip++)
  162: #  define NEXT_P2	({goto *cfa;})
  163: #  define EXEC(XT)	({cfa=(XT); goto *cfa;})
  164: #endif
  165: 
  166: #if defined(DIRECT_THREADED) && !defined(AUTO_INCREMENT)\
  167:     && defined(LONG_LATENCY) && !defined(CFA_NEXT)
  168: #warning scheme 6
  169: #  define NEXT_P0
  170: #  define IP		(ip)
  171: #  define NEXT_INST	(*ip)
  172: #  define INC_IP(const_inc)	({ip+=(const_inc);})
  173: #  define DEF_CA
  174: #  define NEXT_P1	(ip++)
  175: #  define NEXT_P2	({goto **(ip-1);})
  176: #  define EXEC(XT)	({goto *(XT);})
  177: #endif
  178: 
  179: 
  180: #if defined(DIRECT_THREADED) && !defined(AUTO_INCREMENT)\
  181:     && !defined(LONG_LATENCY) && defined(CFA_NEXT)
  182: #warning scheme 7
  183: #  define NEXT_P0
  184: #  define IP		(ip)
  185: #  define NEXT_INST	(*ip)
  186: #  define INC_IP(const_inc)	({ip+=(const_inc);})
  187: #  define DEF_CA
  188: #  define NEXT_P1	({cfa=*ip++;})
  189: #  define NEXT_P2	({goto *cfa;})
  190: #  define EXEC(XT)	({cfa=(XT); goto *cfa;})
  191: #endif
  192: 
  193: #if defined(DIRECT_THREADED) && !defined(AUTO_INCREMENT)\
  194:     && !defined(LONG_LATENCY) && !defined(CFA_NEXT)
  195: #warning scheme 8
  196: #  define NEXT_P0
  197: #  define IP		(ip)
  198: #  define NEXT_INST	(*IP)
  199: #  define INC_IP(const_inc)	({ ip+=(const_inc);})
  200: #  define DEF_CA
  201: #  define NEXT_P1	(ip++)
  202: #  define NEXT_P2	({goto **(ip-1);})
  203: #  define EXEC(XT)	({goto *(XT);})
  204: #endif
  205: 
  206: /* common settings for direct THREADED */
  207: 
  208: 
  209: /* indirect THREADED  */
  210: 
  211: #if !defined(DIRECT_THREADED) && defined(AUTO_INCREMENT)\
  212:     && defined(LONG_LATENCY) && defined(CISC_NEXT)
  213: #  define NEXT_P0	({cfa=*ip++;})
  214: #  define IP		(ip-1)
  215: #  define NEXT_INST	(cfa)
  216: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  217: #  define DEF_CA
  218: #  define NEXT_P1
  219: #  define NEXT_P2	({goto **cfa;})
  220: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  221: #endif
  222: 
  223: #if !defined(DIRECT_THREADED) && defined(AUTO_INCREMENT)\
  224:     && defined(LONG_LATENCY) && !defined(CISC_NEXT)
  225: #  define NEXT_P0	({cfa=*ip++;})
  226: #  define IP		(ip-1)
  227: #  define NEXT_INST	(cfa)
  228: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  229: #  define DEF_CA	Label ca;
  230: #  define NEXT_P1	({ca=*cfa;})
  231: #  define NEXT_P2	({goto *ca;})
  232: #  define EXEC(XT)	({DEF_CA cfa=(XT); ca=*cfa; goto *ca;})
  233: #endif
  234: 
  235: 
  236: #if !defined(DIRECT_THREADED) && defined(AUTO_INCREMENT)\
  237:     && !defined(LONG_LATENCY) && defined(CISC_NEXT)
  238: #  define NEXT_P0
  239: #  define IP		(ip)
  240: #  define NEXT_INST	(*ip)
  241: #  define INC_IP(const_inc)	({ip+=(const_inc);})
  242: #  define DEF_CA
  243: #  define NEXT_P1
  244: #  define NEXT_P2	({cfa=*ip++; goto **cfa;})
  245: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  246: #endif
  247: 
  248: #if !defined(DIRECT_THREADED) && defined(AUTO_INCREMENT)\
  249:     && !defined(LONG_LATENCY) && !defined(CISC_NEXT)
  250: #  define NEXT_P0	({cfa=*ip++;})
  251: #  define IP		(ip-1)
  252: #  define NEXT_INST	(cfa)
  253: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  254: #  define DEF_CA	Label ca;
  255: #  define NEXT_P1	({ca=*cfa;})
  256: #  define NEXT_P2	({goto *ca;})
  257: #  define EXEC(XT)	({DEF_CA cfa=(XT); ca=*cfa; goto *ca;})
  258: #endif
  259: 
  260: 
  261: /* without autoincrement */
  262: 
  263: #if !defined(DIRECT_THREADED) && !defined(AUTO_INCREMENT)\
  264:     && defined(LONG_LATENCY) && defined(CISC_NEXT)
  265: #  define NEXT_P0	({cfa=*ip;})
  266: #  define IP		(ip)
  267: #  define NEXT_INST	(cfa)
  268: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  269: #  define DEF_CA
  270: #  define NEXT_P1	(ip++)
  271: #  define NEXT_P2	({goto **cfa;})
  272: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  273: #endif
  274: 
  275: #if !defined(DIRECT_THREADED) && !defined(AUTO_INCREMENT)\
  276:     && defined(LONG_LATENCY) && !defined(CISC_NEXT)
  277: #  define NEXT_P0	({cfa=*ip;})
  278: #  define IP		(ip)
  279: #  define NEXT_INST	(cfa)
  280: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  281: #  define DEF_CA	Label ca;
  282: #  define NEXT_P1	({ip++; ca=*cfa;})
  283: #  define NEXT_P2	({goto *ca;})
  284: #  define EXEC(XT)	({DEF_CA cfa=(XT); ca=*cfa; goto *ca;})
  285: #endif
  286: 
  287: 
  288: #if !defined(DIRECT_THREADED) && !defined(AUTO_INCREMENT)\
  289:     && !defined(LONG_LATENCY) && defined(CISC_NEXT)
  290: #  define NEXT_P0
  291: #  define IP		(ip)
  292: #  define NEXT_INST	(*ip)
  293: #  define INC_IP(const_inc)	({ip+=(const_inc);})
  294: #  define DEF_CA
  295: #  define NEXT_P1
  296: #  define NEXT_P2	({cfa=*ip++; goto **cfa;})
  297: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  298: #endif
  299: 
  300: #if !defined(DIRECT_THREADED) && !defined(AUTO_INCREMENT)\
  301:     && !defined(LONG_LATENCY) && !defined(CISC_NEXT)
  302: #  define NEXT_P0	({cfa=*ip;})
  303: #  define IP		(ip)
  304: #  define NEXT_INST	(cfa)
  305: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  306: #  define DEF_CA	Label ca;
  307: #  define NEXT_P1	({ip++; ca=*cfa;})
  308: #  define NEXT_P2	({goto *ca;})
  309: #  define EXEC(XT)	({DEF_CA cfa=(XT); ca=*cfa; goto *ca;})
  310: #endif
  311: 
  312: #define NEXT ({DEF_CA NEXT_P1; NEXT_P2;})
  313: 
  314: #if defined(CISC_NEXT) && !defined(LONG_LATENCY)
  315: # define NEXT1_P1
  316: # ifdef DIRECT_THREADED
  317: #  define NEXT1_P2 ({goto *cfa;})
  318: # else
  319: #  define NEXT1_P2 ({goto **cfa;})
  320: # endif /* DIRECT_THREADED */
  321: #else /* defined(CISC_NEXT) && !defined(LONG_LATENCY) */
  322: # ifdef DIRECT_THREADED
  323: #  define NEXT1_P1
  324: #  define NEXT1_P2 ({goto *cfa;})
  325: # else /* DIRECT_THREADED */
  326: #  define NEXT1_P1 ({ca = *cfa;})
  327: #  define NEXT1_P2 ({goto *ca;})
  328: # endif /* DIRECT_THREADED */
  329: #endif

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