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