File:  [gforth] / gforth / engine / threaded.h
Revision 1.19: download - view: text, annotated - select for diffs
Thu Jan 2 21:40:22 2003 UTC (17 years, 9 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed look problem
Added benchmarking of all engines

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

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