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

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

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