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