File:  [gforth] / gforth / engine / threaded.h
Revision 1.28: download - view: text, annotated - select for diffs
Sat Jan 22 21:06:03 2005 UTC (15 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
preparation for gcc PR 15242 workaround
minor changes

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

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