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., 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:
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 ({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:
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
120: # define CFA_NEXT
121: # define NEXT_P0 ({cfa=*ip++;})
122: # define IP (ip-1)
123: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
132: #if THREADING_SCHEME==2
133: #warning direct threading scheme 2: autoinc, long latency, cfa dead
134: #ifndef GETCFA
135: #error GETCFA must be defined for cfa dead threading
136: #endif
137: # define NEXT_P0 (ip++)
138: # define IP (ip-1)
139: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
149: #if THREADING_SCHEME==3
150: #warning direct threading scheme 3: autoinc, low latency, cfa live
151: # define CFA_NEXT
152: # define NEXT_P0
153: # define IP (ip)
154: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
163: #if THREADING_SCHEME==4
164: #warning direct threading scheme 4: autoinc, low latency, cfa dead
165: #ifndef GETCFA
166: #error GETCFA must be defined for cfa dead threading
167: #endif
168: # define NEXT_P0
169: # define IP (ip)
170: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
179: #if THREADING_SCHEME==5
180: #warning direct threading scheme 5: long latency, cfa live
181: # define CFA_NEXT
182: # define NEXT_P0 ({cfa=*ip;})
183: # define IP (ip)
184: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
193: #if THREADING_SCHEME==6
194: #warning direct threading scheme 6: long latency, cfa dead
195: #ifndef GETCFA
196: #error GETCFA must be defined for cfa dead threading
197: #endif
198: # define NEXT_P0
199: # define IP (ip)
200: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
210: #if THREADING_SCHEME==7
211: #warning direct threading scheme 7: low latency, cfa live
212: # define CFA_NEXT
213: # define NEXT_P0
214: # define IP (ip)
215: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
224: #if THREADING_SCHEME==8
225: #warning direct threading scheme 8: cfa dead, i386 hack
226: #ifndef GETCFA
227: #error GETCFA must be defined for cfa dead threading
228: #endif
229: # define NEXT_P0
230: # define IP (ip)
231: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
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 */
245: # define CFA_NEXT
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);})
251: # define DEF_CA Label ca;
252: # define NEXT_P1 ({ca=next_cfa; cfa=next_cfa; ip++; next_cfa=*ip;})
253: # define NEXT_P2 ({goto *ca;})
254: # define EXEC(XT) ({cfa=(XT); goto *cfa;})
255: # define MORE_VARS Xt next_cfa;
256: #endif
257:
258: #if THREADING_SCHEME==10
259: #warning direct threading scheme 10: plain (no attempt at scheduling)
260: # define CFA_NEXT
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
271:
272: /* direct threaded */
273: #else
274: /* indirect THREADED */
275:
276: #if THREADING_SCHEME==1
277: #warning indirect threading scheme 1: autoinc, long latency, cisc
278: # define CFA_NEXT
279: # define NEXT_P0 ({cfa=*ip++;})
280: # define IP (ip-1)
281: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
290: #if THREADING_SCHEME==2
291: #warning indirect threading scheme 2: autoinc, long latency
292: # define CFA_NEXT
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 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:
305: #if THREADING_SCHEME==3
306: #warning indirect threading scheme 3: autoinc, low latency, cisc
307: # define CFA_NEXT
308: # define NEXT_P0
309: # define IP (ip)
310: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
319: #if THREADING_SCHEME==4
320: #warning indirect threading scheme 4: autoinc, low latency
321: # define CFA_NEXT
322: # define NEXT_P0 ({cfa=*ip++;})
323: # define IP (ip-1)
324: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
334: #if THREADING_SCHEME==5
335: #warning indirect threading scheme 5: long latency, cisc
336: # define CFA_NEXT
337: # define NEXT_P0 ({cfa=*ip;})
338: # define IP (ip)
339: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
348: #if THREADING_SCHEME==6
349: #warning indirect threading scheme 6: long latency
350: # define CFA_NEXT
351: # define NEXT_P0 ({cfa=*ip;})
352: # define IP (ip)
353: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
362: #if THREADING_SCHEME==7
363: #warning indirect threading scheme 7: low latency
364: # define CFA_NEXT
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
375:
376: #if THREADING_SCHEME==8
377: #warning indirect threading scheme 8: low latency,cisc
378: # define CFA_NEXT
379: # define NEXT_P0
380: # define IP (ip)
381: # define SET_IP(p) ({ip=(p); NEXT_P0;})
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:
390: /* indirect threaded */
391: #endif
392:
393: #endif /* !defined(DOUBLY_INDIRECT) */
394:
395: #define NEXT ({DEF_CA NEXT_P1; NEXT_P2;})
396:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>