Annotation of gforth/engine/threaded.h, revision 1.9

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

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