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