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