File:  [gforth] / gforth / engine / threaded.h
Revision 1.16: download - view: text, annotated - select for diffs
Sun Nov 24 13:54:02 2002 UTC (21 years, 5 months ago) by anton
Branches: MAIN
CVS tags: HEAD
new engine gforth-native (works on 386 arch only for now).
  appropriate changes in control-flow instructions in prim
  new primitives SET-NEXT-CODE and CALL2 (not necessary for the other engines)
  new primitives COMPILE-PRIM1 and FINISH-CODE
  prims2x.fs now produces IMMARG(...) macros for initializing immediate args
  prims2x.fs: changes in some of the output-c-tail words (goes with the
     changes in the control-flow words).
  appropriate changes in engine.c
  engine.c: rewrite of check_prims, support for gforth-native (NO_IP)
  threaded.c: support for NO_IP
  various kernel files: started to eliminate return stack manipulations for
    embedding data (e.g. string literals); incomplete.
dynamic superinstructions now use LABEL2 instead of IS_NEXT_JUMP
FORCE_REG has no effect if DOUBLY_INDIRECT (gcc-2.95.1 crashes otherwise;
   it's unclear which change provoked this).

    1: /* This file defines a number of threading schemes.
    2: 
    3:   Copyright (C) 1995, 1996,1997,1999 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: 
   95: 
   96: #ifdef DOUBLY_INDIRECT
   97: #  define NEXT_P0	({cfa=*ip;})
   98: #  define IP		(ip)
   99: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  100: #  define NEXT_INST	(cfa)
  101: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  102: #  define DEF_CA	Label ca;
  103: #  define NEXT_P1	({\
  104:   if (cfa<=vm_prims+DOESJUMP || cfa>=vm_prims+npriminfos) \
  105:     fprintf(stderr,"NEXT encountered prim %p at ip=%p\n", cfa, ip); \
  106:   ip++; ca=**cfa;})
  107: #  define NEXT_P2	({goto *ca;})
  108: #  define EXEC(XT)	({DEF_CA cfa=(XT);\
  109:   if (cfa>vm_prims+DOESJUMP && cfa<vm_prims+npriminfos) \
  110:     fprintf(stderr,"EXEC encountered xt %p at ip=%p, vm_prims=%p, xts=%p\n", cfa, ip, vm_prims, xts); \
  111:  ca=**cfa; goto *ca;})
  112: 
  113: #elif defined(NO_IP)
  114: 
  115: #define NEXT_P0
  116: #define SET_IP(target)	assert(0)
  117: #define INC_IP(n)	((void)0)
  118: #define DEF_CA
  119: #define NEXT_P1
  120: #define NEXT_P2		({goto *next_code;})
  121: /* set next_code to the return address before performing EXEC */
  122: #define EXEC(XT)	({cfa=(XT); goto **cfa;})
  123: 
  124: #else  /* !defined(DOUBLY_INDIRECT) && !defined(NO_IP) */
  125: 
  126: #if defined(DIRECT_THREADED)
  127: 
  128: #if THREADING_SCHEME==1
  129: #warning direct threading scheme 1: autoinc, long latency, cfa live
  130: #  define NEXT_P0	({cfa=*ip++;})
  131: #  define IP		(ip-1)
  132: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  133: #  define NEXT_INST	(cfa)
  134: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  135: #  define DEF_CA
  136: #  define NEXT_P1
  137: #  define NEXT_P2	({goto *cfa;})
  138: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  139: #endif
  140: 
  141: #if THREADING_SCHEME==2
  142: #warning direct threading scheme 2: autoinc, long latency, cfa dead
  143: #  define NEXT_P0	(ip++)
  144: #  define IP		(ip-1)
  145: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  146: #  define NEXT_INST	(*(ip-1))
  147: #  define INC_IP(const_inc)	({ ip+=(const_inc);})
  148: #  define DEF_CA
  149: #  define NEXT_P1
  150: #  define NEXT_P2	({goto **(ip-1);})
  151: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  152: #endif
  153: 
  154: 
  155: #if THREADING_SCHEME==3
  156: #warning direct threading scheme 3: autoinc, low latency, cfa live
  157: #  define NEXT_P0
  158: #  define IP		(ip)
  159: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  160: #  define NEXT_INST	(*ip)
  161: #  define INC_IP(const_inc)	({ip+=(const_inc);})
  162: #  define DEF_CA
  163: #  define NEXT_P1	({cfa=*ip++;})
  164: #  define NEXT_P2	({goto *cfa;})
  165: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  166: #endif
  167: 
  168: #if THREADING_SCHEME==4
  169: #warning direct threading scheme 4: autoinc, low latency, cfa dead
  170: #  define NEXT_P0
  171: #  define IP		(ip)
  172: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  173: #  define NEXT_INST	(*ip)
  174: #  define INC_IP(const_inc)	({ ip+=(const_inc);})
  175: #  define DEF_CA
  176: #  define NEXT_P1
  177: #  define NEXT_P2	({goto **(ip++);})
  178: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  179: #endif
  180: 
  181: #if THREADING_SCHEME==5
  182: #warning direct threading scheme 5: long latency, cfa live
  183: #  define NEXT_P0	({cfa=*ip;})
  184: #  define IP		(ip)
  185: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  186: #  define NEXT_INST	(cfa)
  187: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  188: #  define DEF_CA
  189: #  define NEXT_P1	(ip++)
  190: #  define NEXT_P2	({goto *cfa;})
  191: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  192: #endif
  193: 
  194: #if THREADING_SCHEME==6
  195: #warning direct threading scheme 6: long latency, cfa dead
  196: #  define NEXT_P0
  197: #  define IP		(ip)
  198: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  199: #  define NEXT_INST	(*ip)
  200: #  define INC_IP(const_inc)	({ip+=(const_inc);})
  201: #  define DEF_CA
  202: #  define NEXT_P1	(ip++)
  203: #  define NEXT_P2	({goto **(ip-1);})
  204: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  205: #endif
  206: 
  207: 
  208: #if THREADING_SCHEME==7
  209: #warning direct threading scheme 7: low latency, cfa live
  210: #  define NEXT_P0
  211: #  define IP		(ip)
  212: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  213: #  define NEXT_INST	(*ip)
  214: #  define INC_IP(const_inc)	({ip+=(const_inc);})
  215: #  define DEF_CA
  216: #  define NEXT_P1	({cfa=*ip++;})
  217: #  define NEXT_P2	({goto *cfa;})
  218: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  219: #endif
  220: 
  221: #if THREADING_SCHEME==8
  222: #warning direct threading scheme 8: cfa dead, i386 hack
  223: #  define NEXT_P0
  224: #  define IP		(ip)
  225: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  226: #  define NEXT_INST	(*IP)
  227: #  define INC_IP(const_inc)	({ ip+=(const_inc);})
  228: #  define DEF_CA
  229: #  define NEXT_P1	(ip++)
  230: #  define NEXT_P2	({goto **(ip-1);})
  231: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  232: #endif
  233: 
  234: #if THREADING_SCHEME==9
  235: #warning direct threading scheme 9: Power/PPC hack, long latency
  236: /* Power uses a prepare-to-branch instruction, and the latency between
  237:    this inst and the branch is 5 cycles on a PPC604; so we utilize this
  238:    to do some prefetching in between */
  239: #  define NEXT_P0
  240: #  define IP		ip
  241: #  define SET_IP(p)	({ip=(p); next_cfa=*ip; NEXT_P0;})
  242: #  define NEXT_INST	(next_cfa)
  243: #  define INC_IP(const_inc)	({next_cfa=IP[const_inc]; ip+=(const_inc);})
  244: #  define DEF_CA	
  245: #  define NEXT_P1	({cfa=next_cfa; ip++; next_cfa=*ip;})
  246: #  define NEXT_P2	({goto *cfa;})
  247: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  248: #  define MORE_VARS	Xt next_cfa;
  249: #endif
  250: 
  251: #if THREADING_SCHEME==10
  252: #warning direct threading scheme 10: plain (no attempt at scheduling)
  253: #  define NEXT_P0
  254: #  define IP		(ip)
  255: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  256: #  define NEXT_INST	(*ip)
  257: #  define INC_IP(const_inc)	({ip+=(const_inc);})
  258: #  define DEF_CA
  259: #  define NEXT_P1
  260: #  define NEXT_P2	({cfa=*ip++; goto *cfa;})
  261: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  262: #endif
  263: 
  264: /* direct threaded */
  265: #else
  266: /* indirect THREADED  */
  267: 
  268: #if THREADING_SCHEME==1
  269: #warning indirect threading scheme 1: autoinc, long latency, cisc
  270: #  define NEXT_P0	({cfa=*ip++;})
  271: #  define IP		(ip-1)
  272: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  273: #  define NEXT_INST	(cfa)
  274: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  275: #  define DEF_CA
  276: #  define NEXT_P1
  277: #  define NEXT_P2	({goto **cfa;})
  278: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  279: #endif
  280: 
  281: #if THREADING_SCHEME==2
  282: #warning indirect threading scheme 2: autoinc, long latency
  283: #  define NEXT_P0	({cfa=*ip++;})
  284: #  define IP		(ip-1)
  285: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  286: #  define NEXT_INST	(cfa)
  287: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  288: #  define DEF_CA	Label ca;
  289: #  define NEXT_P1	({ca=*cfa;})
  290: #  define NEXT_P2	({goto *ca;})
  291: #  define EXEC(XT)	({DEF_CA cfa=(XT); ca=*cfa; goto *ca;})
  292: #endif
  293: 
  294: 
  295: #if THREADING_SCHEME==3
  296: #warning indirect threading scheme 3: autoinc, low latency, cisc
  297: #  define NEXT_P0
  298: #  define IP		(ip)
  299: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  300: #  define NEXT_INST	(*ip)
  301: #  define INC_IP(const_inc)	({ip+=(const_inc);})
  302: #  define DEF_CA
  303: #  define NEXT_P1
  304: #  define NEXT_P2	({cfa=*ip++; goto **cfa;})
  305: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  306: #endif
  307: 
  308: #if THREADING_SCHEME==4
  309: #warning indirect threading scheme 4: autoinc, low latency
  310: #  define NEXT_P0	({cfa=*ip++;})
  311: #  define IP		(ip-1)
  312: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  313: #  define NEXT_INST	(cfa)
  314: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  315: #  define DEF_CA	Label ca;
  316: #  define NEXT_P1	({ca=*cfa;})
  317: #  define NEXT_P2	({goto *ca;})
  318: #  define EXEC(XT)	({DEF_CA cfa=(XT); ca=*cfa; goto *ca;})
  319: #endif
  320: 
  321: 
  322: #if THREADING_SCHEME==5
  323: #warning indirect threading scheme 5: long latency, cisc
  324: #  define NEXT_P0	({cfa=*ip;})
  325: #  define IP		(ip)
  326: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  327: #  define NEXT_INST	(cfa)
  328: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  329: #  define DEF_CA
  330: #  define NEXT_P1	(ip++)
  331: #  define NEXT_P2	({goto **cfa;})
  332: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  333: #endif
  334: 
  335: #if THREADING_SCHEME==6
  336: #warning indirect threading scheme 6: long latency
  337: #  define NEXT_P0	({cfa=*ip;})
  338: #  define IP		(ip)
  339: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  340: #  define NEXT_INST	(cfa)
  341: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  342: #  define DEF_CA	Label ca;
  343: #  define NEXT_P1	({ip++; ca=*cfa;})
  344: #  define NEXT_P2	({goto *ca;})
  345: #  define EXEC(XT)	({DEF_CA cfa=(XT); ca=*cfa; goto *ca;})
  346: #endif
  347: 
  348: #if THREADING_SCHEME==7
  349: #warning indirect threading scheme 7: low latency
  350: #  define NEXT_P0	({cfa=*ip;})
  351: #  define IP		(ip)
  352: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  353: #  define NEXT_INST	(cfa)
  354: #  define INC_IP(const_inc)	({cfa=IP[const_inc]; ip+=(const_inc);})
  355: #  define DEF_CA	Label ca;
  356: #  define NEXT_P1	({ip++; ca=*cfa;})
  357: #  define NEXT_P2	({goto *ca;})
  358: #  define EXEC(XT)	({DEF_CA cfa=(XT); ca=*cfa; goto *ca;})
  359: #endif
  360: 
  361: #if THREADING_SCHEME==8
  362: #warning indirect threading scheme 8: low latency,cisc
  363: #  define NEXT_P0
  364: #  define IP		(ip)
  365: #  define SET_IP(p)	({ip=(p); NEXT_P0;})
  366: #  define NEXT_INST	(*ip)
  367: #  define INC_IP(const_inc)	({ip+=(const_inc);})
  368: #  define DEF_CA
  369: #  define NEXT_P1
  370: #  define NEXT_P2	({cfa=*ip++; goto **cfa;})
  371: #  define EXEC(XT)	({cfa=(XT); goto **cfa;})
  372: #endif
  373: 
  374: /* indirect threaded */
  375: #endif
  376: 
  377: #endif /* !defined(DOUBLY_INDIRECT) && !defined(NO_IP) */
  378: 
  379: #define NEXT ({DEF_CA NEXT_P1; NEXT_P2;})
  380: #define IPTOS NEXT_INST

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