Annotation of gforth/engine.c, revision 1.36
1.31 anton 1: /* Gforth virtual machine (aka inner interpreter)
2:
3: Copyright (C) 1995 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.
1.1 anton 20: */
21:
1.33 anton 22: #include "config.h"
1.1 anton 23: #include <ctype.h>
24: #include <stdio.h>
25: #include <string.h>
26: #include <math.h>
27: #include <sys/types.h>
28: #include <sys/stat.h>
29: #include <fcntl.h>
30: #include <assert.h>
31: #include <stdlib.h>
1.2 pazsan 32: #include <time.h>
1.6 pazsan 33: #include <sys/time.h>
1.22 anton 34: #include <unistd.h>
1.25 anton 35: #include <errno.h>
1.27 anton 36: #include <pwd.h>
1.1 anton 37: #include "forth.h"
38: #include "io.h"
1.30 pazsan 39: #include "threading.h"
1.1 anton 40:
1.20 anton 41: #ifndef SEEK_SET
42: /* should be defined in stdio.h, but some systems don't have it */
43: #define SEEK_SET 0
44: #endif
45:
1.25 anton 46: #define IOR(flag) ((flag)? -512-errno : 0)
47:
1.1 anton 48: typedef struct F83Name {
49: struct F83Name *next; /* the link field for old hands */
50: char countetc;
51: Char name[0];
52: } F83Name;
53:
54: /* are macros for setting necessary? */
55: #define F83NAME_COUNT(np) ((np)->countetc & 0x1f)
56: #define F83NAME_SMUDGE(np) (((np)->countetc & 0x40) != 0)
57: #define F83NAME_IMMEDIATE(np) (((np)->countetc & 0x20) != 0)
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:
1.25 anton 73: Cell *SP;
74: Float *FP;
1.34 anton 75: #if 0
76: /* not used currently */
1.1 anton 77: int emitcounter;
1.34 anton 78: #endif
1.1 anton 79: #define NULLC '\0'
80:
1.14 anton 81: char *cstr(Char *from, UCell size, int clear)
1.33 anton 82: /* return a C-string corresponding to the Forth string ( FROM SIZE ).
83: the C-string lives until the next call of cstr with CLEAR being true */
1.14 anton 84: {
1.33 anton 85: static struct cstr_buffer {
86: char *buffer;
87: size_t size;
88: } *buffers=NULL;
89: static int nbuffers=0;
90: static int used=0;
91: struct cstr_buffer *b;
1.14 anton 92:
1.33 anton 93: if (buffers==NULL)
94: buffers=malloc(0);
1.14 anton 95: if (clear)
1.33 anton 96: used=0;
97: if (used>=nbuffers) {
98: buffers=realloc(buffers,sizeof(struct cstr_buffer)*(used+1));
99: buffers[used]=(struct cstr_buffer){malloc(0),0};
100: nbuffers=used+1;
1.14 anton 101: }
1.33 anton 102: b=&buffers[used];
103: if (size+1 > b->size) {
104: b->buffer = realloc(b->buffer,size+1);
105: b->size = size+1;
1.14 anton 106: }
1.33 anton 107: memcpy(b->buffer,from,size);
108: b->buffer[size]='\0';
109: used++;
110: return b->buffer;
1.14 anton 111: }
1.27 anton 112:
113: char *tilde_cstr(Char *from, UCell size, int clear)
114: /* like cstr(), but perform tilde expansion on the string */
115: {
116: char *s1,*s2;
117: int s1_len, s2_len;
118: struct passwd *getpwnam (), *user_entry;
119:
120: if (size<1 || from[0]!='~')
121: return cstr(from, size, clear);
122: if (size<2 || from[1]=='/') {
123: s1 = (char *)getenv ("HOME");
124: s2 = from+1;
125: s2_len = size-1;
126: } else {
127: int i;
128: for (i=1; i<size && from[i]!='/'; i++)
129: ;
130: {
131: char user[i];
132: memcpy(user,from+1,i-1);
133: user[i-1]='\0';
134: user_entry=getpwnam(user);
135: }
136: if (user_entry==NULL)
137: return cstr(from, size, clear);
138: s1 = user_entry->pw_dir;
139: s2 = from+i;
140: s2_len = size-i;
141: }
142: s1_len = strlen(s1);
143: if (s1_len>1 && s1[s1_len-1]=='/')
144: s1_len--;
145: {
146: char path[s1_len+s2_len];
147: memcpy(path,s1,s1_len);
148: memcpy(path+s1_len,s2,s2_len);
149: return cstr(path,s1_len+s2_len,clear);
150: }
151: }
152:
1.13 pazsan 153:
1.1 anton 154: #define NEWLINE '\n'
155:
1.21 pazsan 156: #ifndef HAVE_RINT
157: #define rint(x) floor((x)+0.5)
158: #endif
1.13 pazsan 159:
1.26 anton 160: static char* fileattr[6]={"r","rb","r+","r+b","w","wb"};
1.6 pazsan 161:
1.11 pazsan 162: static Address up0=NULL;
163:
1.15 anton 164: /* if machine.h has not defined explicit registers, define them as implicit */
165: #ifndef IPREG
166: #define IPREG
167: #endif
168: #ifndef SPREG
169: #define SPREG
170: #endif
171: #ifndef RPREG
172: #define RPREG
173: #endif
174: #ifndef FPREG
175: #define FPREG
176: #endif
177: #ifndef LPREG
178: #define LPREG
179: #endif
180: #ifndef CFAREG
181: #define CFAREG
182: #endif
183: #ifndef UPREG
184: #define UPREG
185: #endif
186: #ifndef TOSREG
187: #define TOSREG
188: #endif
189: #ifndef FTOSREG
190: #define FTOSREG
191: #endif
1.13 pazsan 192:
1.32 anton 193: /* declare and compute cfa for certain threading variants */
194: /* warning: this is nonsyntactical; it will not work in place of a statement */
195: #ifdef CFA_NEXT
196: #define DOCFA
197: #else
198: #define DOCFA Xt cfa; GETCFA(cfa)
199: #endif
200:
1.15 anton 201: Label *engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
1.1 anton 202: /* executes code at ip, if ip!=NULL
203: returns array of machine code labels (for use in a loader), if ip==NULL
204: */
1.15 anton 205: {
206: register Xt *ip IPREG = ip0;
207: register Cell *sp SPREG = sp0;
208: register Cell *rp RPREG = rp0;
209: register Float *fp FPREG = fp0;
210: register Address lp LPREG = lp0;
1.30 pazsan 211: #ifdef CFA_NEXT
1.15 anton 212: register Xt cfa CFAREG;
1.30 pazsan 213: #endif
1.15 anton 214: register Address up UPREG = up0;
215: IF_TOS(register Cell TOS TOSREG;)
216: IF_FTOS(register Float FTOS FTOSREG;)
1.1 anton 217: static Label symbols[]= {
218: &&docol,
219: &&docon,
220: &&dovar,
1.4 pazsan 221: &&douser,
1.12 anton 222: &&dodefer,
1.29 anton 223: &&dofield,
1.1 anton 224: &&dodoes,
1.32 anton 225: /* the following entry is normally unused;
226: it's there because its index indicates a does-handler */
227: #ifdef CPU_DEP1
228: CPU_DEP1,
229: #else
230: (Label)0,
231: #endif
1.1 anton 232: #include "prim_labels.i"
233: };
1.32 anton 234: #ifdef CPU_DEP2
235: CPU_DEP2
1.1 anton 236: #endif
237:
1.16 pazsan 238: #ifdef DEBUG
239: fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
1.19 anton 240: (unsigned)ip,(unsigned)sp,(unsigned)rp,
241: (unsigned)fp,(unsigned)lp,(unsigned)up);
1.16 pazsan 242: #endif
243:
1.1 anton 244: if (ip == NULL)
245: return symbols;
1.10 anton 246:
1.1 anton 247: IF_TOS(TOS = sp[0]);
248: IF_FTOS(FTOS = fp[0]);
1.28 pazsan 249: /* prep_terminal(); */
1.23 anton 250: NEXT_P0;
1.1 anton 251: NEXT;
1.32 anton 252:
253: #ifdef CPU_DEP3
254: CPU_DEP3
255: #endif
1.1 anton 256:
257: docol:
1.30 pazsan 258: {
1.32 anton 259: DOCFA;
1.1 anton 260: #ifdef DEBUG
1.32 anton 261: fprintf(stderr,"%08lx: col: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
1.1 anton 262: #endif
1.15 anton 263: #ifdef CISC_NEXT
1.32 anton 264: /* this is the simple version */
265: *--rp = (Cell)ip;
266: ip = (Xt *)PFA1(cfa);
267: NEXT_P0;
268: NEXT;
1.15 anton 269: #else
1.32 anton 270: /* this one is important, so we help the compiler optimizing
271: The following version may be better (for scheduling), but probably has
272: problems with code fields employing calls and delay slots
273: */
274: {
275: DEF_CA
276: Xt *current_ip = (Xt *)PFA1(cfa);
277: cfa = *current_ip;
278: NEXT1_P1;
279: *--rp = (Cell)ip;
280: ip = current_ip+1;
281: NEXT1_P2;
282: }
1.15 anton 283: #endif
1.30 pazsan 284: }
1.23 anton 285:
1.1 anton 286: docon:
1.30 pazsan 287: {
1.32 anton 288: DOCFA;
1.1 anton 289: #ifdef DEBUG
1.32 anton 290: fprintf(stderr,"%08lx: con: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
1.1 anton 291: #endif
292: #ifdef USE_TOS
1.32 anton 293: *sp-- = TOS;
294: TOS = *(Cell *)PFA1(cfa);
1.1 anton 295: #else
1.32 anton 296: *--sp = *(Cell *)PFA1(cfa);
1.1 anton 297: #endif
1.30 pazsan 298: }
1.23 anton 299: NEXT_P0;
1.1 anton 300: NEXT;
301:
302: dovar:
1.30 pazsan 303: {
1.32 anton 304: DOCFA;
1.1 anton 305: #ifdef DEBUG
1.32 anton 306: fprintf(stderr,"%08lx: var: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
1.1 anton 307: #endif
308: #ifdef USE_TOS
1.32 anton 309: *sp-- = TOS;
310: TOS = (Cell)PFA1(cfa);
1.1 anton 311: #else
1.32 anton 312: *--sp = (Cell)PFA1(cfa);
1.1 anton 313: #endif
1.30 pazsan 314: }
1.23 anton 315: NEXT_P0;
1.1 anton 316: NEXT;
317:
1.4 pazsan 318: douser:
1.30 pazsan 319: {
1.32 anton 320: DOCFA;
1.4 pazsan 321: #ifdef DEBUG
1.32 anton 322: fprintf(stderr,"%08lx: user: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
1.4 pazsan 323: #endif
324: #ifdef USE_TOS
1.32 anton 325: *sp-- = TOS;
326: TOS = (Cell)(up+*(Cell*)PFA1(cfa));
1.4 pazsan 327: #else
1.32 anton 328: *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
1.4 pazsan 329: #endif
1.30 pazsan 330: }
1.23 anton 331: NEXT_P0;
1.4 pazsan 332: NEXT;
333:
1.12 anton 334: dodefer:
1.30 pazsan 335: {
1.32 anton 336: DOCFA;
1.12 anton 337: #ifdef DEBUG
1.32 anton 338: fprintf(stderr,"%08lx: defer: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
1.30 pazsan 339: #endif
1.32 anton 340: EXEC(*(Xt *)PFA1(cfa));
1.30 pazsan 341: }
1.24 pazsan 342:
1.29 anton 343: dofield:
1.30 pazsan 344: {
1.32 anton 345: DOCFA;
1.24 pazsan 346: #ifdef DEBUG
1.32 anton 347: fprintf(stderr,"%08lx: field: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
1.24 pazsan 348: #endif
1.32 anton 349: TOS += *(Cell*)PFA1(cfa);
1.30 pazsan 350: }
1.24 pazsan 351: NEXT_P0;
352: NEXT;
1.12 anton 353:
1.1 anton 354: dodoes:
355: /* this assumes the following structure:
356: defining-word:
357:
358: ...
359: DOES>
360: (possible padding)
361: possibly handler: jmp dodoes
362: (possible branch delay slot(s))
363: Forth code after DOES>
364:
365: defined word:
366:
367: cfa: address of or jump to handler OR
368: address of or jump to dodoes, address of DOES-code
369: pfa:
370:
371: */
1.30 pazsan 372: {
1.32 anton 373: DOCFA;
1.30 pazsan 374:
1.32 anton 375: /* fprintf(stderr, "Got CFA %08lx at doescode %08lx/%08lx: does: %08lx\n",cfa,(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));*/
1.1 anton 376: #ifdef DEBUG
1.32 anton 377: fprintf(stderr,"%08lx/%08lx: does: %08lx\n",(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));
378: fflush(stderr);
1.1 anton 379: #endif
1.32 anton 380: *--rp = (Cell)ip;
381: /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
382: ip = DOES_CODE1(cfa);
1.1 anton 383: #ifdef USE_TOS
1.32 anton 384: *sp-- = TOS;
385: TOS = (Cell)PFA(cfa);
1.1 anton 386: #else
1.32 anton 387: *--sp = (Cell)PFA(cfa);
1.30 pazsan 388: #endif
1.32 anton 389: /* fprintf(stderr,"TOS = %08lx, IP=%08lx\n", TOS, IP);*/
1.30 pazsan 390: }
1.23 anton 391: NEXT_P0;
1.1 anton 392: NEXT;
1.16 pazsan 393:
1.1 anton 394: #include "primitives.i"
395: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>