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: undefine(`symbols')
23:
24: #include "config.h"
25: #include "forth.h"
26: #include <ctype.h>
27: #include <stdio.h>
28: #include <string.h>
29: #include <math.h>
30: #include <assert.h>
31: #include <stdlib.h>
32: #include <errno.h>
33: #include "io.h"
34: #include "threaded.h"
35: #ifndef STANDALONE
36: #include <sys/types.h>
37: #include <sys/stat.h>
38: #include <fcntl.h>
39: #include <time.h>
40: #include <sys/time.h>
41: #include <unistd.h>
42: #include <pwd.h>
43: #include <dirent.h>
44: #include <sys/resource.h>
45: #ifdef HAVE_FNMATCH_H
46: #include <fnmatch.h>
47: #else
48: #include "fnmatch.h"
49: #endif
50: #else
51: #include "systypes.h"
52: #endif
53:
54: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
55: #include <dlfcn.h>
56: #endif
57: #if defined(_WIN32)
58: #include <windows.h>
59: #endif
60: #ifdef hpux
61: #include <dl.h>
62: #endif
63:
64: #ifndef SEEK_SET
65: /* should be defined in stdio.h, but some systems don't have it */
66: #define SEEK_SET 0
67: #endif
68:
69: #define IOR(flag) ((flag)? -512-errno : 0)
70:
71: struct F83Name {
72: struct F83Name *next; /* the link field for old hands */
73: char countetc;
74: char name[0];
75: };
76:
77: #define F83NAME_COUNT(np) ((np)->countetc & 0x1f)
78:
79: struct Longname {
80: struct Longname *next; /* the link field for old hands */
81: Cell countetc;
82: char name[0];
83: };
84:
85: #define LONGNAME_COUNT(np) ((np)->countetc & (((~((UCell)0))<<3)>>3))
86:
87: Cell *SP;
88: Float *FP;
89: Address UP=NULL;
90:
91: #if 0
92: /* not used currently */
93: int emitcounter;
94: #endif
95: #define NULLC '\0'
96:
97: #ifdef MEMCMP_AS_SUBROUTINE
98: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
99: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
100: #endif
101:
102: #ifdef HAS_FILE
103: char *cstr(Char *from, UCell size, int clear)
104: /* return a C-string corresponding to the Forth string ( FROM SIZE ).
105: the C-string lives until the next call of cstr with CLEAR being true */
106: {
107: static struct cstr_buffer {
108: char *buffer;
109: size_t size;
110: } *buffers=NULL;
111: static int nbuffers=0;
112: static int used=0;
113: struct cstr_buffer *b;
114:
115: if (buffers==NULL)
116: buffers=malloc(0);
117: if (clear)
118: used=0;
119: if (used>=nbuffers) {
120: buffers=realloc(buffers,sizeof(struct cstr_buffer)*(used+1));
121: buffers[used]=(struct cstr_buffer){malloc(0),0};
122: nbuffers=used+1;
123: }
124: b=&buffers[used];
125: if (size+1 > b->size) {
126: b->buffer = realloc(b->buffer,size+1);
127: b->size = size+1;
128: }
129: memcpy(b->buffer,from,size);
130: b->buffer[size]='\0';
131: used++;
132: return b->buffer;
133: }
134:
135: char *tilde_cstr(Char *from, UCell size, int clear)
136: /* like cstr(), but perform tilde expansion on the string */
137: {
138: char *s1,*s2;
139: int s1_len, s2_len;
140: struct passwd *getpwnam (), *user_entry;
141:
142: if (size<1 || from[0]!='~')
143: return cstr(from, size, clear);
144: if (size<2 || from[1]=='/') {
145: s1 = (char *)getenv ("HOME");
146: if(s1 == NULL)
147: s1 = "";
148: s2 = from+1;
149: s2_len = size-1;
150: } else {
151: UCell i;
152: for (i=1; i<size && from[i]!='/'; i++)
153: ;
154: if (i==2 && from[1]=='+') /* deal with "~+", i.e., the wd */
155: return cstr(from+3, size<3?0:size-3,clear);
156: {
157: char user[i];
158: memcpy(user,from+1,i-1);
159: user[i-1]='\0';
160: user_entry=getpwnam(user);
161: }
162: if (user_entry==NULL)
163: return cstr(from, size, clear);
164: s1 = user_entry->pw_dir;
165: s2 = from+i;
166: s2_len = size-i;
167: }
168: s1_len = strlen(s1);
169: if (s1_len>1 && s1[s1_len-1]=='/')
170: s1_len--;
171: {
172: char path[s1_len+s2_len];
173: memcpy(path,s1,s1_len);
174: memcpy(path+s1_len,s2,s2_len);
175: return cstr(path,s1_len+s2_len,clear);
176: }
177: }
178: #endif
179:
180: DCell timeval2us(struct timeval *tvp)
181: {
182: #ifndef BUGGY_LONG_LONG
183: return (tvp->tv_sec*(DCell)1000000)+tvp->tv_usec;
184: #else
185: DCell d2;
186: DCell d1=mmul(tvp->tv_sec,1000000);
187: d2.lo = d1.lo+tvp->tv_usec;
188: d2.hi = d1.hi + (d2.lo<d1.lo);
189: return d2;
190: #endif
191: }
192:
193: #define NEWLINE '\n'
194:
195: #ifdef HAS_FILE
196: static char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
197: static char* pfileattr[6]={"r","r","r+","r+","w","w"};
198:
199: #ifndef O_BINARY
200: #define O_BINARY 0
201: #endif
202: #ifndef O_TEXT
203: #define O_TEXT 0
204: #endif
205:
206: static int ufileattr[6]= {
207: O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
208: O_RDWR |O_BINARY, O_RDWR |O_BINARY,
209: O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
210: #endif
211:
212: /* conversion on fetch */
213:
214: #define vm_Cell2f(_cell,_x) ((_x)=(Bool)(_cell))
215: #define vm_Cell2c(_cell,_x) ((_x)=(Char)(_cell))
216: #define vm_Cell2n(_cell,_x) ((_x)=(Cell)(_cell))
217: #define vm_Cell2w(_cell,_x) ((_x)=(Cell)(_cell))
218: #define vm_Cell2u(_cell,_x) ((_x)=(UCell)(_cell))
219: #define vm_Cell2a_(_cell,_x) ((_x)=(Cell *)(_cell))
220: #define vm_Cell2c_(_cell,_x) ((_x)=(Char *)(_cell))
221: #define vm_Cell2f_(_cell,_x) ((_x)=(Float *)(_cell))
222: #define vm_Cell2df_(_cell,_x) ((_x)=(DFloat *)(_cell))
223: #define vm_Cell2sf_(_cell,_x) ((_x)=(SFloat *)(_cell))
224: #define vm_Cell2xt(_cell,_x) ((_x)=(Xt)(_cell))
225: #define vm_Cell2f83name(_cell,_x) ((_x)=(struct F83Name *)(_cell))
226: #define vm_Cell2longname(_cell,_x) ((_x)=(struct Longname *)(_cell))
227: #define vm_Float2r(_float,_x) (_x=_float)
228:
229: /* conversion on store */
230:
231: #define vm_f2Cell(_x,_cell) ((_cell)=(Cell)(_x))
232: #define vm_c2Cell(_x,_cell) ((_cell)=(Cell)(_x))
233: #define vm_n2Cell(_x,_cell) ((_cell)=(Cell)(_x))
234: #define vm_w2Cell(_x,_cell) ((_cell)=(Cell)(_x))
235: #define vm_u2Cell(_x,_cell) ((_cell)=(Cell)(_x))
236: #define vm_a_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
237: #define vm_c_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
238: #define vm_f_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
239: #define vm_df_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
240: #define vm_sf_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
241: #define vm_xt2Cell(_x,_cell) ((_cell)=(Cell)(_x))
242: #define vm_f83name2Cell(_x,_cell) ((_cell)=(Cell)(_x))
243: #define vm_longname2Cell(_x,_cell) ((_cell)=(Cell)(_x))
244: #define vm_r2Float(_x,_float) (_float=_x)
245:
246: #define vm_Cell2Cell(_x,_y) (_y=_x)
247:
248: #ifdef NO_IP
249: #define IMM_ARG(access,value) (VARIANT(value))
250: #else
251: #define IMM_ARG(access,value) (access)
252: #endif
253:
254: /* if machine.h has not defined explicit registers, define them as implicit */
255: #ifndef IPREG
256: #define IPREG
257: #endif
258: #ifndef SPREG
259: #define SPREG
260: #endif
261: #ifndef RPREG
262: #define RPREG
263: #endif
264: #ifndef FPREG
265: #define FPREG
266: #endif
267: #ifndef LPREG
268: #define LPREG
269: #endif
270: #ifndef CFAREG
271: #define CFAREG
272: #endif
273: #ifndef UPREG
274: #define UPREG
275: #endif
276: #ifndef TOSREG
277: #define TOSREG
278: #endif
279: #ifndef FTOSREG
280: #define FTOSREG
281: #endif
282:
283: #ifndef CPU_DEP1
284: # define CPU_DEP1 0
285: #endif
286:
287: /* instructions containing these must be the last instruction of a
288: super-instruction (e.g., branches, EXECUTE, and other instructions
289: ending the basic block). Instructions containing SET_IP get this
290: automatically, so you usually don't have to write it. If you have
291: to write it, write it after IP points to the next instruction.
292: Used for profiling. Don't write it in a word containing SET_IP, or
293: the following block will be counted twice. */
294: #ifdef VM_PROFILING
295: #define SUPER_END vm_count_block(IP)
296: #else
297: #define SUPER_END
298: #endif
299: #define SUPER_CONTINUE
300:
301: #ifdef GFORTH_DEBUGGING
302: /* define some VM registers as global variables, so they survive exceptions;
303: global register variables are not up to the task (according to the
304: GNU C manual) */
305: Xt *saved_ip;
306: Cell *rp;
307: #endif
308:
309: #ifdef NO_IP
310: static Label next_code;
311: #endif
312:
313: #ifdef DEBUG
314: #define CFA_TO_NAME(__cfa) \
315: Cell len, i; \
316: char * name = __cfa; \
317: for(i=0; i<32; i+=sizeof(Cell)) { \
318: len = ((Cell*)name)[-1]; \
319: if(len < 0) { \
320: len &= 0x1F; \
321: if((len+sizeof(Cell)) > i) break; \
322: } len = 0; \
323: name -= sizeof(Cell); \
324: }
325: #endif
326:
327: Xt *primtable(Label symbols[], Cell size)
328: /* used in primitive primtable for peephole optimization */
329: {
330: Xt *xts = (Xt *)malloc(size*sizeof(Xt));
331: Cell i;
332:
333: for (i=0; i<size; i++)
334: xts[i] = &symbols[i];
335: return xts;
336: }
337:
338: define(enginerest,
339: `(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
340: /* executes code at ip, if ip!=NULL
341: returns array of machine code labels (for use in a loader), if ip==NULL
342: */
343: {
344: #ifndef GFORTH_DEBUGGING
345: register Cell *rp RPREG;
346: #endif
347: #ifndef NO_IP
348: register Xt *ip IPREG = ip0;
349: #endif
350: register Cell *sp SPREG = sp0;
351: register Float *fp FPREG = fp0;
352: register Address lp LPREG = lp0;
353: register Xt cfa CFAREG;
354: #ifdef MORE_VARS
355: MORE_VARS
356: #endif
357: register Address up UPREG = UP;
358: IF_spTOS(register Cell spTOS TOSREG;)
359: IF_fpTOS(register Float fpTOS FTOSREG;)
360: #if defined(DOUBLY_INDIRECT)
361: static Label *symbols;
362: static void *routines[]= {
363: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
364: #else /* !defined(DOUBLY_INDIRECT) */
365: static Label symbols[]= {
366: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
367: #endif /* !defined(DOUBLY_INDIRECT) */
368: (Label)&&docol,
369: (Label)&&docon,
370: (Label)&&dovar,
371: (Label)&&douser,
372: (Label)&&dodefer,
373: (Label)&&dofield,
374: (Label)&&dodoes,
375: /* the following entry is normally unused;
376: it is there because its index indicates a does-handler */
377: CPU_DEP1,
378: #define INST_ADDR(name) (Label)&&I_##name
379: #include "prim_lab.i"
380: #undef INST_ADDR
381: (Label)&&after_last,
382: (Label)0,
383: #define INST_ADDR(name) (Label)&&K_##name
384: #include "prim_lab.i"
385: #undef INST_ADDR
386: #ifdef IN_ENGINE2
387: #define INST_ADDR(name) (Label)&&J_##name
388: #include "prim_lab.i"
389: #undef INST_ADDR
390: #endif
391: };
392: #ifdef CPU_DEP2
393: CPU_DEP2
394: #endif
395:
396: rp = rp0;
397: #ifdef DEBUG
398: fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
399: (unsigned)ip0,(unsigned)sp,(unsigned)rp,
400: (unsigned)fp,(unsigned)lp,(unsigned)up);
401: #endif
402:
403: if (ip0 == NULL) {
404: #if defined(DOUBLY_INDIRECT)
405: #define CODE_OFFSET (26*sizeof(Cell))
406: #define XT_OFFSET (22*sizeof(Cell))
407: int i;
408: Cell code_offset = offset_image? CODE_OFFSET : 0;
409: Cell xt_offset = offset_image? XT_OFFSET : 0;
410:
411: symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
412: xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
413: for (i=0; i<DOESJUMP+1; i++)
414: xts[i] = symbols[i] = (Label)routines[i];
415: for (; routines[i]!=0; i++) {
416: if (i>=MAX_SYMBOLS) {
417: fprintf(stderr,"gforth-ditc: more than %d primitives\n",MAX_SYMBOLS);
418: exit(1);
419: }
420: xts[i] = symbols[i] = &routines[i];
421: }
422: #endif /* defined(DOUBLY_INDIRECT) */
423: return symbols;
424: }
425:
426: IF_spTOS(spTOS = sp[0]);
427: IF_fpTOS(fpTOS = fp[0]);
428: /* prep_terminal(); */
429: #ifdef NO_IP
430: goto *(*(Label *)ip0);
431: #else
432: SET_IP(ip);
433: SUPER_END; /* count the first block, too */
434: NEXT;
435: #endif
436:
437: #ifdef CPU_DEP3
438: CPU_DEP3
439: #endif
440:
441: docol:
442: {
443: #ifdef NO_IP
444: *--rp = next_code;
445: goto **(Label *)PFA1(cfa);
446: #else
447: #ifdef DEBUG
448: {
449: CFA_TO_NAME(cfa);
450: fprintf(stderr,"%08lx: col: %08lx %.*s\n",(Cell)ip,(Cell)PFA1(cfa),
451: len,name);
452: }
453: #endif
454: #ifdef CISC_NEXT
455: /* this is the simple version */
456: *--rp = (Cell)ip;
457: SET_IP((Xt *)PFA1(cfa));
458: SUPER_END;
459: NEXT;
460: #else
461: /* this one is important, so we help the compiler optimizing */
462: {
463: DEF_CA
464: rp[-1] = (Cell)ip;
465: SET_IP((Xt *)PFA1(cfa));
466: SUPER_END;
467: NEXT_P1;
468: rp--;
469: NEXT_P2;
470: }
471: #endif
472: #endif
473: }
474:
475: docon:
476: {
477: #ifdef DEBUG
478: fprintf(stderr,"%08lx: con: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
479: #endif
480: #ifdef USE_TOS
481: *sp-- = spTOS;
482: spTOS = *(Cell *)PFA1(cfa);
483: #else
484: *--sp = *(Cell *)PFA1(cfa);
485: #endif
486: }
487: #ifdef NO_IP
488: goto *next_code;
489: #else
490: NEXT_P0;
491: NEXT;
492: #endif
493:
494: dovar:
495: {
496: #ifdef DEBUG
497: fprintf(stderr,"%08lx: var: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
498: #endif
499: #ifdef USE_TOS
500: *sp-- = spTOS;
501: spTOS = (Cell)PFA1(cfa);
502: #else
503: *--sp = (Cell)PFA1(cfa);
504: #endif
505: }
506: #ifdef NO_IP
507: goto *next_code;
508: #else
509: NEXT_P0;
510: NEXT;
511: #endif
512:
513: douser:
514: {
515: #ifdef DEBUG
516: fprintf(stderr,"%08lx: user: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
517: #endif
518: #ifdef USE_TOS
519: *sp-- = spTOS;
520: spTOS = (Cell)(up+*(Cell*)PFA1(cfa));
521: #else
522: *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
523: #endif
524: }
525: #ifdef NO_IP
526: goto *next_code;
527: #else
528: NEXT_P0;
529: NEXT;
530: #endif
531:
532: dodefer:
533: {
534: #ifdef DEBUG
535: fprintf(stderr,"%08lx: defer: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
536: #endif
537: SUPER_END;
538: EXEC(*(Xt *)PFA1(cfa));
539: }
540:
541: dofield:
542: {
543: #ifdef DEBUG
544: fprintf(stderr,"%08lx: field: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
545: #endif
546: spTOS += *(Cell*)PFA1(cfa);
547: }
548: #ifdef NO_IP
549: goto *next_code;
550: #else
551: NEXT_P0;
552: NEXT;
553: #endif
554:
555: dodoes:
556: /* this assumes the following structure:
557: defining-word:
558:
559: ...
560: DOES>
561: (possible padding)
562: possibly handler: jmp dodoes
563: (possible branch delay slot(s))
564: Forth code after DOES>
565:
566: defined word:
567:
568: cfa: address of or jump to handler OR
569: address of or jump to dodoes, address of DOES-code
570: pfa:
571:
572: */
573: #ifdef NO_IP
574: *--rp = next_code;
575: IF_spTOS(spTOS = sp[0]);
576: sp--;
577: spTOS = (Cell)PFA(cfa);
578: goto **(Label *)DOES_CODE1(cfa);
579: #else
580: {
581: /* fprintf(stderr, "Got CFA %08lx at doescode %08lx/%08lx: does: %08lx\n",cfa,(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));*/
582: #ifdef DEBUG
583: fprintf(stderr,"%08lx/%08lx: does: %08lx\n",(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));
584: fflush(stderr);
585: #endif
586: *--rp = (Cell)ip;
587: /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
588: #ifdef USE_TOS
589: *sp-- = spTOS;
590: spTOS = (Cell)PFA(cfa);
591: #else
592: *--sp = (Cell)PFA(cfa);
593: #endif
594: SET_IP(DOES_CODE1(cfa));
595: SUPER_END;
596: /* fprintf(stderr,"TOS = %08lx, IP=%08lx\n", spTOS, IP);*/
597: }
598: NEXT;
599: #endif
600:
601: #ifndef IN_ENGINE2
602: #define LABEL(name) I_##name:
603: #else
604: #define LABEL(name) J_##name: asm(".skip 16"); I_##name:
605: #endif
606: #define LABEL2(name) K_##name:
607: #include "prim.i"
608: #undef LABEL
609: after_last: return (Label *)0;
610: /*needed only to get the length of the last primitive */
611: }'
612: )
613:
614: #define VARIANT(v) (v)
615: #define JUMP(target) goto I_noop
616:
617: Label *engine enginerest
618:
619: #ifndef NO_DYNAMIC
620:
621: #ifdef NO_IP
622: #undef VARIANT
623: #define VARIANT(v) ((v)^0xffffffff)
624: #undef JUMP
625: #define JUMP(target) goto K_lit
626: Label *engine3 enginerest
627: #endif
628:
629: #undef VARIANT
630: #define VARIANT(v) (v)
631: #undef JUMP
632: #define JUMP(target) goto I_noop
633: #define IN_ENGINE2
634: Label *engine2 enginerest
635: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>