Annotation of gforth/engine/engine.c, revision 1.34
1.1 anton 1: /* Gforth virtual machine (aka inner interpreter)
2:
1.22 anton 3: Copyright (C) 1995,1996,1997,1998,2000 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
1.23 anton 19: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1 anton 20: */
21:
22: #include "config.h"
1.31 pazsan 23: #include "forth.h"
1.1 anton 24: #include <ctype.h>
25: #include <stdio.h>
26: #include <string.h>
27: #include <math.h>
1.4 pazsan 28: #include <assert.h>
29: #include <stdlib.h>
30: #include <errno.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.17 pazsan 41: #include <dirent.h>
1.21 anton 42: #include <sys/resource.h>
1.19 anton 43: #ifdef HAVE_FNMATCH_H
1.18 anton 44: #include <fnmatch.h>
1.19 anton 45: #else
46: #include "fnmatch.h"
47: #endif
1.4 pazsan 48: #else
49: #include "systypes.h"
50: #endif
1.1 anton 51:
52: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
53: #include <dlfcn.h>
54: #endif
1.8 pazsan 55: #if defined(_WIN32)
56: #include <windows.h>
57: #endif
1.1 anton 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:
1.4 pazsan 69: struct F83Name {
70: struct F83Name *next; /* the link field for old hands */
71: char countetc;
72: char name[0];
73: };
1.1 anton 74:
75: #define F83NAME_COUNT(np) ((np)->countetc & 0x1f)
1.25 anton 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))
1.1 anton 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:
1.14 pazsan 95: #ifdef MEMCMP_AS_SUBROUTINE
96: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
1.15 anton 97: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
1.14 pazsan 98: #endif
99:
1.9 pazsan 100: #ifdef HAS_FILE
1.1 anton 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: ;
1.13 anton 152: if (i==2 && from[1]=='+') /* deal with "~+", i.e., the wd */
153: return cstr(from+3, size<3?0:size-3,clear);
1.1 anton 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: }
1.9 pazsan 176: #endif
1.21 anton 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: }
1.1 anton 190:
191: #define NEWLINE '\n'
192:
193: #ifndef HAVE_RINT
194: #define rint(x) floor((x)+0.5)
195: #endif
196:
1.9 pazsan 197: #ifdef HAS_FILE
1.16 anton 198: static char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
1.32 pazsan 199: static char* pfileattr[6]={"r","r","r+","r+","w","w"};
1.1 anton 200:
201: #ifndef O_BINARY
202: #define O_BINARY 0
203: #endif
204: #ifndef O_TEXT
205: #define O_TEXT 0
206: #endif
207:
208: static int ufileattr[6]= {
1.16 anton 209: O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
210: O_RDWR |O_BINARY, O_RDWR |O_BINARY,
211: O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
1.9 pazsan 212: #endif
1.1 anton 213:
1.26 anton 214: /* conversion on fetch */
215:
216: #define vm_Cell2f(x) ((Bool)(x))
217: #define vm_Cell2c(x) ((Char)(x))
218: #define vm_Cell2n(x) ((Cell)x)
219: #define vm_Cell2w(x) ((Cell)x)
220: #define vm_Cell2u(x) ((UCell)(x))
221: #define vm_Cell2a_(x) ((Cell *)(x))
222: #define vm_Cell2c_(x) ((Char *)(x))
223: #define vm_Cell2f_(x) ((Float *)(x))
224: #define vm_Cell2df_(x) ((DFloat *)(x))
225: #define vm_Cell2sf_(x) ((SFloat *)(x))
226: #define vm_Cell2xt(x) ((Xt)(x))
227: #define vm_Cell2f83name(x) ((struct F83Name *)(x))
228: #define vm_Cell2longname(x) ((struct Longname *)(x))
229: #define vm_Float2r(x) (x)
230:
231: /* conversion on store */
232:
233: #define vm_f2Cell(x) ((Cell)(x))
234: #define vm_c2Cell(x) ((Cell)(x))
235: #define vm_n2Cell(x) ((Cell)(x))
236: #define vm_w2Cell(x) ((Cell)(x))
237: #define vm_u2Cell(x) ((Cell)(x))
238: #define vm_a_2Cell(x) ((Cell)(x))
239: #define vm_c_2Cell(x) ((Cell)(x))
240: #define vm_f_2Cell(x) ((Cell)(x))
241: #define vm_df_2Cell(x) ((Cell)(x))
242: #define vm_sf_2Cell(x) ((Cell)(x))
243: #define vm_xt2Cell(x) ((Cell)(x))
244: #define vm_f83name2Cell(x) ((Cell)(x))
245: #define vm_longname2Cell(x) ((Cell)(x))
246: #define vm_r2Float(x) (x)
247:
1.29 anton 248: #define vm_Cell2Cell(x) (x)
249:
1.1 anton 250: /* if machine.h has not defined explicit registers, define them as implicit */
251: #ifndef IPREG
252: #define IPREG
253: #endif
254: #ifndef SPREG
255: #define SPREG
256: #endif
257: #ifndef RPREG
258: #define RPREG
259: #endif
260: #ifndef FPREG
261: #define FPREG
262: #endif
263: #ifndef LPREG
264: #define LPREG
265: #endif
266: #ifndef CFAREG
267: #define CFAREG
268: #endif
269: #ifndef UPREG
270: #define UPREG
271: #endif
272: #ifndef TOSREG
273: #define TOSREG
274: #endif
275: #ifndef FTOSREG
276: #define FTOSREG
277: #endif
278:
279: #ifndef CPU_DEP1
280: # define CPU_DEP1 0
281: #endif
282:
283: /* declare and compute cfa for certain threading variants */
284: /* warning: this is nonsyntactical; it will not work in place of a statement */
1.12 anton 285: #ifndef GETCFA
1.1 anton 286: #define DOCFA
287: #else
288: #define DOCFA Xt cfa; GETCFA(cfa)
289: #endif
290:
1.28 anton 291: /* instructions containing these must be the last instruction of a
292: super-instruction (e.g., branches, EXECUTE, and other instructions
293: ending the basic block). Instructions containing SET_IP get this
294: automatically, so you usually don't have to write it. If you have
295: to write it, write it after IP points to the next instruction.
296: Used for profiling. Don't write it in a word containing SET_IP, or
297: the following block will be counted twice. */
298: #ifdef VM_PROFILING
299: #define SUPER_END vm_count_block(IP)
300: #else
301: #define SUPER_END
302: #endif
303:
1.10 anton 304: #ifdef GFORTH_DEBUGGING
305: /* define some VM registers as global variables, so they survive exceptions;
306: global register variables are not up to the task (according to the
307: GNU C manual) */
308: Xt *ip;
309: Cell *rp;
310: #endif
311:
1.30 anton 312: Xt *primtable(Label symbols[], Cell size)
313: {
314: #ifdef DIRECT_THREADED
315: return symbols;
316: #else /* !defined(DIRECT_THREADED) */
317: Xt *xts = (Xt *)malloc(size*sizeof(Xt));
318: Cell i;
319:
320: for (i=0; i<size; i++)
321: xts[i] = &symbols[i];
322: return xts;
323: #endif /* !defined(DIRECT_THREADED) */
324: }
325:
1.34 ! anton 326:
! 327: define(enginerest,
! 328: `(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
1.1 anton 329: /* executes code at ip, if ip!=NULL
330: returns array of machine code labels (for use in a loader), if ip==NULL
331: */
332: {
1.10 anton 333: #ifndef GFORTH_DEBUGGING
334: register Xt *ip IPREG;
335: register Cell *rp RPREG;
336: #endif
1.1 anton 337: register Cell *sp SPREG = sp0;
338: register Float *fp FPREG = fp0;
339: register Address lp LPREG = lp0;
340: #ifdef CFA_NEXT
341: register Xt cfa CFAREG;
342: #endif
1.11 anton 343: #ifdef MORE_VARS
344: MORE_VARS
345: #endif
1.1 anton 346: register Address up UPREG = UP;
1.24 anton 347: IF_spTOS(register Cell spTOS TOSREG;)
348: IF_fpTOS(register Float fpTOS FTOSREG;)
1.1 anton 349: #if defined(DOUBLY_INDIRECT)
350: static Label *symbols;
351: static void *routines[]= {
1.27 anton 352: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
1.1 anton 353: #else /* !defined(DOUBLY_INDIRECT) */
354: static Label symbols[]= {
1.27 anton 355: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
1.1 anton 356: #endif /* !defined(DOUBLY_INDIRECT) */
1.4 pazsan 357: (Label)&&docol,
358: (Label)&&docon,
359: (Label)&&dovar,
360: (Label)&&douser,
361: (Label)&&dodefer,
362: (Label)&&dofield,
363: (Label)&&dodoes,
1.1 anton 364: /* the following entry is normally unused;
1.34 ! anton 365: it is there because its index indicates a does-handler */
1.7 pazsan 366: CPU_DEP1,
1.34 ! anton 367: #define INST_ADDR(name) (Label)&&I_##name
! 368: #include "prim_lab.i"
! 369: #undef INST_ADDR
! 370: (Label)&&after_last,
! 371: (Label)0,
! 372: #ifdef IN_ENGINE2
! 373: #define INST_ADDR(name) (Label)&&J_##name
1.1 anton 374: #include "prim_lab.i"
1.34 ! anton 375: #undef INST_ADDR
! 376: #endif
1.1 anton 377: };
378: #ifdef CPU_DEP2
379: CPU_DEP2
380: #endif
381:
1.10 anton 382: ip = ip0;
383: rp = rp0;
1.1 anton 384: #ifdef DEBUG
385: fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
386: (unsigned)ip,(unsigned)sp,(unsigned)rp,
387: (unsigned)fp,(unsigned)lp,(unsigned)up);
388: #endif
389:
390: if (ip == NULL) {
391: #if defined(DOUBLY_INDIRECT)
1.3 anton 392: #define CODE_OFFSET (22*sizeof(Cell))
1.1 anton 393: int i;
1.3 anton 394: Cell code_offset = offset_image? CODE_OFFSET : 0;
1.7 pazsan 395:
1.3 anton 396: symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
1.1 anton 397: for (i=0; i<DOESJUMP+1; i++)
1.7 pazsan 398: symbols[i] = (Label)routines[i];
1.1 anton 399: for (; routines[i]!=0; i++) {
400: if (i>=MAX_SYMBOLS) {
401: fprintf(stderr,"gforth-ditc: more than %d primitives\n",MAX_SYMBOLS);
402: exit(1);
1.20 anton 403: }
404: symbols[i] = &routines[i];
1.1 anton 405: }
1.20 anton 406: #endif /* defined(DOUBLY_INDIRECT) */
407: return symbols;
1.7 pazsan 408: }
1.1 anton 409:
1.24 anton 410: IF_spTOS(spTOS = sp[0]);
411: IF_fpTOS(fpTOS = fp[0]);
1.7 pazsan 412: /* prep_terminal(); */
1.11 anton 413: SET_IP(ip);
1.28 anton 414: SUPER_END; /* count the first block, too */
1.1 anton 415: NEXT;
416:
1.11 anton 417:
1.1 anton 418: #ifdef CPU_DEP3
419: CPU_DEP3
420: #endif
421:
422: docol:
423: {
424: DOCFA;
425: #ifdef DEBUG
426: fprintf(stderr,"%08lx: col: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
427: #endif
428: #ifdef CISC_NEXT
429: /* this is the simple version */
430: *--rp = (Cell)ip;
1.11 anton 431: SET_IP((Xt *)PFA1(cfa));
1.28 anton 432: SUPER_END;
1.1 anton 433: NEXT;
434: #else
1.11 anton 435: /* this one is important, so we help the compiler optimizing */
1.1 anton 436: {
437: DEF_CA
1.11 anton 438: rp[-1] = (Cell)ip;
439: SET_IP((Xt *)PFA1(cfa));
1.28 anton 440: SUPER_END;
1.11 anton 441: NEXT_P1;
442: rp--;
443: NEXT_P2;
1.1 anton 444: }
445: #endif
446: }
447:
448: docon:
449: {
450: DOCFA;
451: #ifdef DEBUG
452: fprintf(stderr,"%08lx: con: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
453: #endif
454: #ifdef USE_TOS
1.24 anton 455: *sp-- = spTOS;
456: spTOS = *(Cell *)PFA1(cfa);
1.1 anton 457: #else
458: *--sp = *(Cell *)PFA1(cfa);
459: #endif
460: }
461: NEXT_P0;
462: NEXT;
463:
464: dovar:
465: {
466: DOCFA;
467: #ifdef DEBUG
468: fprintf(stderr,"%08lx: var: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
469: #endif
470: #ifdef USE_TOS
1.24 anton 471: *sp-- = spTOS;
472: spTOS = (Cell)PFA1(cfa);
1.1 anton 473: #else
474: *--sp = (Cell)PFA1(cfa);
475: #endif
476: }
477: NEXT_P0;
478: NEXT;
479:
480: douser:
481: {
482: DOCFA;
483: #ifdef DEBUG
484: fprintf(stderr,"%08lx: user: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
485: #endif
486: #ifdef USE_TOS
1.24 anton 487: *sp-- = spTOS;
488: spTOS = (Cell)(up+*(Cell*)PFA1(cfa));
1.1 anton 489: #else
490: *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
491: #endif
492: }
493: NEXT_P0;
494: NEXT;
495:
496: dodefer:
497: {
498: DOCFA;
499: #ifdef DEBUG
500: fprintf(stderr,"%08lx: defer: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
501: #endif
1.28 anton 502: SUPER_END;
1.1 anton 503: EXEC(*(Xt *)PFA1(cfa));
504: }
505:
506: dofield:
507: {
508: DOCFA;
509: #ifdef DEBUG
510: fprintf(stderr,"%08lx: field: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
511: #endif
1.24 anton 512: spTOS += *(Cell*)PFA1(cfa);
1.1 anton 513: }
514: NEXT_P0;
515: NEXT;
516:
517: dodoes:
518: /* this assumes the following structure:
519: defining-word:
520:
521: ...
522: DOES>
523: (possible padding)
524: possibly handler: jmp dodoes
525: (possible branch delay slot(s))
526: Forth code after DOES>
527:
528: defined word:
529:
530: cfa: address of or jump to handler OR
531: address of or jump to dodoes, address of DOES-code
532: pfa:
533:
534: */
535: {
536: DOCFA;
537:
538: /* fprintf(stderr, "Got CFA %08lx at doescode %08lx/%08lx: does: %08lx\n",cfa,(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));*/
539: #ifdef DEBUG
540: fprintf(stderr,"%08lx/%08lx: does: %08lx\n",(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));
541: fflush(stderr);
542: #endif
543: *--rp = (Cell)ip;
544: /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
545: #ifdef USE_TOS
1.24 anton 546: *sp-- = spTOS;
547: spTOS = (Cell)PFA(cfa);
1.1 anton 548: #else
549: *--sp = (Cell)PFA(cfa);
550: #endif
1.11 anton 551: SET_IP(DOES_CODE1(cfa));
1.28 anton 552: SUPER_END;
1.24 anton 553: /* fprintf(stderr,"TOS = %08lx, IP=%08lx\n", spTOS, IP);*/
1.1 anton 554: }
555: NEXT;
556:
1.34 ! anton 557: #ifndef IN_ENGINE2
1.33 anton 558: #define LABEL(name) I_##name
1.34 ! anton 559: #else
! 560: #define LABEL(name) J_##name: asm(".skip 16"); I_##name
! 561: #endif
1.1 anton 562: #include "prim.i"
1.34 ! anton 563: #undef LABEL
! 564: after_last: return (Label *)0;
! 565: /*needed only to get the length of the last primitive */
! 566: }'
! 567: )
! 568:
! 569: Label *engine enginerest
! 570:
! 571: #define IN_ENGINE2
! 572: Label *engine2 enginerest
! 573:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>