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