1: /*
2: $Id: engine.c,v 1.9 1994/07/07 14:59:21 pazsan Exp $
3: Copyright 1992 by the ANSI figForth Development Group
4: */
5:
6: #include <ctype.h>
7: #include <stdio.h>
8: #include <string.h>
9: #include <math.h>
10: #include <sys/types.h>
11: #include <sys/stat.h>
12: #include <fcntl.h>
13: #include <assert.h>
14: #include <stdlib.h>
15: #include <time.h>
16: #include <sys/time.h>
17: #include "forth.h"
18: #include "io.h"
19:
20: typedef union {
21: struct {
22: #ifdef BIG_ENDIAN
23: Cell high;
24: Cell low;
25: #else
26: Cell low;
27: Cell high;
28: #endif;
29: } cells;
30: DCell dcell;
31: } Double_Store;
32:
33: typedef struct F83Name {
34: struct F83Name *next; /* the link field for old hands */
35: char countetc;
36: Char name[0];
37: } F83Name;
38:
39: /* are macros for setting necessary? */
40: #define F83NAME_COUNT(np) ((np)->countetc & 0x1f)
41: #define F83NAME_SMUDGE(np) (((np)->countetc & 0x40) != 0)
42: #define F83NAME_IMMEDIATE(np) (((np)->countetc & 0x20) != 0)
43:
44: /* NEXT and NEXT1 are split into several parts to help scheduling */
45: #ifdef DIRECT_THREADED
46: # define NEXT1_P1
47: # define NEXT1_P2 ({goto *cfa;})
48: # define DEF_CA
49: #else
50: # define NEXT1_P1 ({ca = *cfa;})
51: # define NEXT1_P2 ({goto *ca;})
52: # define DEF_CA Label ca;
53: #endif
54: #define NEXT_P1 ({cfa = *ip++; NEXT1_P1;})
55:
56: #define NEXT1 ({DEF_CA NEXT1_P1; NEXT1_P2;})
57: #define NEXT ({DEF_CA NEXT_P1; NEXT1_P2;})
58:
59: #ifdef USE_TOS
60: #define IF_TOS(x) x
61: #else
62: #define IF_TOS(x)
63: #define TOS (sp[0])
64: #endif
65:
66: #ifdef USE_FTOS
67: #define IF_FTOS(x) x
68: #else
69: #define IF_FTOS(x)
70: #define FTOS (fp[0])
71: #endif
72:
73: int emitcounter;
74: #define NULLC '\0'
75:
76: #define cstr(to,from,size)\
77: { memcpy(to,from,size);\
78: to[size]=NULLC;}
79: #define NEWLINE '\n'
80:
81: static char* fileattr[6]={"r","rb","r+","r+b","w+","w+b"};
82:
83: Label *engine(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp)
84: /* executes code at ip, if ip!=NULL
85: returns array of machine code labels (for use in a loader), if ip==NULL
86: */
87: {
88: Xt cfa;
89: Address up=NULL;
90: static Label symbols[]= {
91: &&docol,
92: &&docon,
93: &&dovar,
94: &&douser,
95: &&dodoes,
96: &&dodoes, /* dummy for does handler address */
97: #include "prim_labels.i"
98: };
99: IF_TOS(register Cell TOS;)
100: IF_FTOS(Float FTOS;)
101: #ifdef CPU_DEP
102: CPU_DEP;
103: #endif
104:
105: if (ip == NULL)
106: return symbols;
107:
108: IF_TOS(TOS = sp[0]);
109: IF_FTOS(FTOS = fp[0]);
110: prep_terminal();
111: NEXT;
112:
113: docol:
114: #ifdef DEBUG
115: printf("%08x: col: %08x\n",(Cell)ip,(Cell)PFA1(cfa));
116: #endif
117: #ifdef undefined
118: /* this is the simple version */
119: *--rp = (Cell)ip;
120: ip = (Xt *)PFA1(cfa);
121: NEXT;
122: #endif
123: /* this one is important, so we help the compiler optimizing
124: The following version may be better (for scheduling), but probably has
125: problems with code fields employing calls and delay slots
126: */
127: {
128: DEF_CA
129: Xt *current_ip = (Xt *)PFA1(cfa);
130: cfa = *current_ip;
131: NEXT1_P1;
132: *--rp = (Cell)ip;
133: ip = current_ip+1;
134: NEXT1_P2;
135: }
136:
137: docon:
138: #ifdef DEBUG
139: printf("%08x: con: %08x\n",(Cell)ip,*(Cell*)PFA1(cfa));
140: #endif
141: #ifdef USE_TOS
142: *sp-- = TOS;
143: TOS = *(Cell *)PFA1(cfa);
144: #else
145: *--sp = *(Cell *)PFA1(cfa);
146: #endif
147: NEXT;
148:
149: dovar:
150: #ifdef DEBUG
151: printf("%08x: var: %08x\n",(Cell)ip,(Cell)PFA1(cfa));
152: #endif
153: #ifdef USE_TOS
154: *sp-- = TOS;
155: TOS = (Cell)PFA1(cfa);
156: #else
157: *--sp = (Cell)PFA1(cfa);
158: #endif
159: NEXT;
160:
161: /* !! user? */
162:
163: douser:
164: #ifdef DEBUG
165: printf("%08x: user: %08x\n",(Cell)ip,(Cell)PFA1(cfa));
166: #endif
167: #ifdef USE_TOS
168: *sp-- = TOS;
169: TOS = (Cell)(up+*(Cell*)PFA1(cfa));
170: #else
171: *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
172: #endif
173: NEXT;
174:
175: dodoes:
176: /* this assumes the following structure:
177: defining-word:
178:
179: ...
180: DOES>
181: (possible padding)
182: possibly handler: jmp dodoes
183: (possible branch delay slot(s))
184: Forth code after DOES>
185:
186: defined word:
187:
188: cfa: address of or jump to handler OR
189: address of or jump to dodoes, address of DOES-code
190: pfa:
191:
192: */
193: #ifdef DEBUG
194: printf("%08x/%08x: does: %08x\n",(Cell)ip,(Cell)cfa,*(Cell)PFA(cfa));
195: fflush(stdout);
196: #endif
197: *--rp = (Cell)ip;
198: /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
199: #ifdef USE_TOS
200: *sp-- = TOS;
201: TOS = (Cell)PFA(cfa);
202: #else
203: *--sp = (Cell)PFA(cfa);
204: #endif
205: ip = DOES_CODE1(cfa);
206: NEXT;
207:
208: #include "primitives.i"
209: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>