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