Annotation of gforth/engine/engine.c, revision 1.60
1.1 anton 1: /* Gforth virtual machine (aka inner interpreter)
2:
1.57 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:
1.53 anton 67: #ifndef HAVE_FSEEKO
68: #define fseeko fseek
69: #endif
70:
71: #ifndef HAVE_FTELLO
72: #define ftello ftell
73: #endif
74:
1.1 anton 75: #define NULLC '\0'
76:
1.14 pazsan 77: #ifdef MEMCMP_AS_SUBROUTINE
78: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
1.15 anton 79: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
1.14 pazsan 80: #endif
81:
1.1 anton 82: #define NEWLINE '\n'
83:
1.26 anton 84: /* conversion on fetch */
85:
1.41 anton 86: #define vm_Cell2f(_cell,_x) ((_x)=(Bool)(_cell))
87: #define vm_Cell2c(_cell,_x) ((_x)=(Char)(_cell))
88: #define vm_Cell2n(_cell,_x) ((_x)=(Cell)(_cell))
89: #define vm_Cell2w(_cell,_x) ((_x)=(Cell)(_cell))
90: #define vm_Cell2u(_cell,_x) ((_x)=(UCell)(_cell))
91: #define vm_Cell2a_(_cell,_x) ((_x)=(Cell *)(_cell))
92: #define vm_Cell2c_(_cell,_x) ((_x)=(Char *)(_cell))
93: #define vm_Cell2f_(_cell,_x) ((_x)=(Float *)(_cell))
94: #define vm_Cell2df_(_cell,_x) ((_x)=(DFloat *)(_cell))
95: #define vm_Cell2sf_(_cell,_x) ((_x)=(SFloat *)(_cell))
96: #define vm_Cell2xt(_cell,_x) ((_x)=(Xt)(_cell))
97: #define vm_Cell2f83name(_cell,_x) ((_x)=(struct F83Name *)(_cell))
98: #define vm_Cell2longname(_cell,_x) ((_x)=(struct Longname *)(_cell))
99: #define vm_Float2r(_float,_x) (_x=_float)
1.26 anton 100:
101: /* conversion on store */
102:
1.41 anton 103: #define vm_f2Cell(_x,_cell) ((_cell)=(Cell)(_x))
104: #define vm_c2Cell(_x,_cell) ((_cell)=(Cell)(_x))
105: #define vm_n2Cell(_x,_cell) ((_cell)=(Cell)(_x))
106: #define vm_w2Cell(_x,_cell) ((_cell)=(Cell)(_x))
107: #define vm_u2Cell(_x,_cell) ((_cell)=(Cell)(_x))
108: #define vm_a_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
109: #define vm_c_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
110: #define vm_f_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
111: #define vm_df_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
112: #define vm_sf_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
113: #define vm_xt2Cell(_x,_cell) ((_cell)=(Cell)(_x))
114: #define vm_f83name2Cell(_x,_cell) ((_cell)=(Cell)(_x))
115: #define vm_longname2Cell(_x,_cell) ((_cell)=(Cell)(_x))
116: #define vm_r2Float(_x,_float) (_float=_x)
1.26 anton 117:
1.41 anton 118: #define vm_Cell2Cell(_x,_y) (_y=_x)
1.29 anton 119:
1.46 anton 120: #ifdef NO_IP
121: #define IMM_ARG(access,value) (VARIANT(value))
122: #else
123: #define IMM_ARG(access,value) (access)
124: #endif
125:
1.1 anton 126: /* if machine.h has not defined explicit registers, define them as implicit */
127: #ifndef IPREG
128: #define IPREG
129: #endif
130: #ifndef SPREG
131: #define SPREG
132: #endif
133: #ifndef RPREG
134: #define RPREG
135: #endif
136: #ifndef FPREG
137: #define FPREG
138: #endif
139: #ifndef LPREG
140: #define LPREG
141: #endif
142: #ifndef CFAREG
143: #define CFAREG
144: #endif
145: #ifndef UPREG
146: #define UPREG
147: #endif
148: #ifndef TOSREG
149: #define TOSREG
150: #endif
151: #ifndef FTOSREG
152: #define FTOSREG
153: #endif
154:
155: #ifndef CPU_DEP1
156: # define CPU_DEP1 0
157: #endif
158:
1.28 anton 159: /* instructions containing these must be the last instruction of a
160: super-instruction (e.g., branches, EXECUTE, and other instructions
161: ending the basic block). Instructions containing SET_IP get this
162: automatically, so you usually don't have to write it. If you have
163: to write it, write it after IP points to the next instruction.
164: Used for profiling. Don't write it in a word containing SET_IP, or
165: the following block will be counted twice. */
166: #ifdef VM_PROFILING
167: #define SUPER_END vm_count_block(IP)
168: #else
169: #define SUPER_END
170: #endif
1.35 anton 171: #define SUPER_CONTINUE
1.46 anton 172:
1.37 pazsan 173: #ifdef DEBUG
174: #define CFA_TO_NAME(__cfa) \
175: Cell len, i; \
176: char * name = __cfa; \
177: for(i=0; i<32; i+=sizeof(Cell)) { \
178: len = ((Cell*)name)[-1]; \
179: if(len < 0) { \
180: len &= 0x1F; \
181: if((len+sizeof(Cell)) > i) break; \
182: } len = 0; \
183: name -= sizeof(Cell); \
184: }
185: #endif
1.30 anton 186:
1.50 anton 187: #if !defined(ENGINE)
188: /* normal engine */
189: #define VARIANT(v) (v)
190: #define JUMP(target) goto I_noop
1.51 anton 191: #define LABEL(name) J_##name: asm(""); I_##name:
1.50 anton 192:
193: #elif ENGINE==2
194: /* variant with padding between VM instructions for finding out
195: cross-inst jumps (for dynamic code) */
196: #define engine engine2
197: #define VARIANT(v) (v)
198: #define JUMP(target) goto I_noop
1.52 anton 199: #define LABEL(name) J_##name: SKIP16; I_##name:
1.50 anton 200: #define IN_ENGINE2
201:
202: #elif ENGINE==3
203: /* variant with different immediate arguments for finding out
204: immediate arguments (for native code) */
205: #define engine engine3
206: #define VARIANT(v) ((v)^0xffffffff)
207: #define JUMP(target) goto K_lit
1.51 anton 208: #define LABEL(name) J_##name: asm(""); I_##name:
1.50 anton 209: #else
210: #error illegal ENGINE value
211: #endif /* ENGINE */
212:
213: #define LABEL2(name) K_##name:
214:
215:
216: Label *engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
1.1 anton 217: /* executes code at ip, if ip!=NULL
218: returns array of machine code labels (for use in a loader), if ip==NULL
219: */
220: {
1.10 anton 221: #ifndef GFORTH_DEBUGGING
222: register Cell *rp RPREG;
223: #endif
1.46 anton 224: #ifndef NO_IP
225: register Xt *ip IPREG = ip0;
226: #endif
1.1 anton 227: register Cell *sp SPREG = sp0;
228: register Float *fp FPREG = fp0;
229: register Address lp LPREG = lp0;
230: register Xt cfa CFAREG;
1.11 anton 231: #ifdef MORE_VARS
232: MORE_VARS
233: #endif
1.1 anton 234: register Address up UPREG = UP;
1.24 anton 235: IF_spTOS(register Cell spTOS TOSREG;)
236: IF_fpTOS(register Float fpTOS FTOSREG;)
1.1 anton 237: #if defined(DOUBLY_INDIRECT)
238: static Label *symbols;
239: static void *routines[]= {
1.27 anton 240: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
1.1 anton 241: #else /* !defined(DOUBLY_INDIRECT) */
242: static Label symbols[]= {
1.27 anton 243: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
1.1 anton 244: #endif /* !defined(DOUBLY_INDIRECT) */
1.4 pazsan 245: (Label)&&docol,
246: (Label)&&docon,
247: (Label)&&dovar,
248: (Label)&&douser,
249: (Label)&&dodefer,
250: (Label)&&dofield,
251: (Label)&&dodoes,
1.1 anton 252: /* the following entry is normally unused;
1.34 anton 253: it is there because its index indicates a does-handler */
1.7 pazsan 254: CPU_DEP1,
1.55 anton 255: #define INST_ADDR(name) ((Label)&&I_##name)
1.34 anton 256: #include "prim_lab.i"
257: #undef INST_ADDR
258: (Label)&&after_last,
259: (Label)0,
1.55 anton 260: #define INST_ADDR(name) ((Label)&&K_##name)
1.46 anton 261: #include "prim_lab.i"
262: #undef INST_ADDR
1.55 anton 263: #define INST_ADDR(name) ((Label)&&J_##name)
1.1 anton 264: #include "prim_lab.i"
1.34 anton 265: #undef INST_ADDR
1.1 anton 266: };
267: #ifdef CPU_DEP2
268: CPU_DEP2
269: #endif
270:
1.10 anton 271: rp = rp0;
1.1 anton 272: #ifdef DEBUG
273: fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
1.46 anton 274: (unsigned)ip0,(unsigned)sp,(unsigned)rp,
1.1 anton 275: (unsigned)fp,(unsigned)lp,(unsigned)up);
276: #endif
277:
1.46 anton 278: if (ip0 == NULL) {
1.1 anton 279: #if defined(DOUBLY_INDIRECT)
1.38 anton 280: #define CODE_OFFSET (26*sizeof(Cell))
281: #define XT_OFFSET (22*sizeof(Cell))
1.1 anton 282: int i;
1.3 anton 283: Cell code_offset = offset_image? CODE_OFFSET : 0;
1.38 anton 284: Cell xt_offset = offset_image? XT_OFFSET : 0;
1.7 pazsan 285:
1.3 anton 286: symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
1.38 anton 287: xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
1.1 anton 288: for (i=0; i<DOESJUMP+1; i++)
1.38 anton 289: xts[i] = symbols[i] = (Label)routines[i];
1.1 anton 290: for (; routines[i]!=0; i++) {
291: if (i>=MAX_SYMBOLS) {
1.60 ! anton 292: fprintf(stderr,"gforth-ditc: more than %ld primitives\n",(long)MAX_SYMBOLS);
1.1 anton 293: exit(1);
1.20 anton 294: }
1.38 anton 295: xts[i] = symbols[i] = &routines[i];
1.1 anton 296: }
1.20 anton 297: #endif /* defined(DOUBLY_INDIRECT) */
298: return symbols;
1.7 pazsan 299: }
1.1 anton 300:
1.24 anton 301: IF_spTOS(spTOS = sp[0]);
302: IF_fpTOS(fpTOS = fp[0]);
1.7 pazsan 303: /* prep_terminal(); */
1.46 anton 304: #ifdef NO_IP
305: goto *(*(Label *)ip0);
306: #else
1.11 anton 307: SET_IP(ip);
1.28 anton 308: SUPER_END; /* count the first block, too */
1.1 anton 309: NEXT;
1.46 anton 310: #endif
1.11 anton 311:
1.1 anton 312: #ifdef CPU_DEP3
313: CPU_DEP3
314: #endif
315:
316: docol:
317: {
1.46 anton 318: #ifdef NO_IP
319: *--rp = next_code;
320: goto **(Label *)PFA1(cfa);
321: #else
1.1 anton 322: #ifdef DEBUG
1.37 pazsan 323: {
324: CFA_TO_NAME(cfa);
325: fprintf(stderr,"%08lx: col: %08lx %.*s\n",(Cell)ip,(Cell)PFA1(cfa),
326: len,name);
327: }
1.1 anton 328: #endif
329: #ifdef CISC_NEXT
330: /* this is the simple version */
331: *--rp = (Cell)ip;
1.11 anton 332: SET_IP((Xt *)PFA1(cfa));
1.28 anton 333: SUPER_END;
1.1 anton 334: NEXT;
335: #else
1.11 anton 336: /* this one is important, so we help the compiler optimizing */
1.1 anton 337: {
338: DEF_CA
1.11 anton 339: rp[-1] = (Cell)ip;
340: SET_IP((Xt *)PFA1(cfa));
1.28 anton 341: SUPER_END;
1.11 anton 342: NEXT_P1;
343: rp--;
344: NEXT_P2;
1.1 anton 345: }
346: #endif
1.46 anton 347: #endif
1.1 anton 348: }
349:
350: docon:
351: {
352: #ifdef DEBUG
353: fprintf(stderr,"%08lx: con: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
354: #endif
355: #ifdef USE_TOS
1.24 anton 356: *sp-- = spTOS;
357: spTOS = *(Cell *)PFA1(cfa);
1.1 anton 358: #else
359: *--sp = *(Cell *)PFA1(cfa);
360: #endif
361: }
1.46 anton 362: #ifdef NO_IP
363: goto *next_code;
364: #else
1.1 anton 365: NEXT_P0;
366: NEXT;
1.46 anton 367: #endif
1.1 anton 368:
369: dovar:
370: {
371: #ifdef DEBUG
372: fprintf(stderr,"%08lx: var: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
373: #endif
374: #ifdef USE_TOS
1.24 anton 375: *sp-- = spTOS;
376: spTOS = (Cell)PFA1(cfa);
1.1 anton 377: #else
378: *--sp = (Cell)PFA1(cfa);
379: #endif
380: }
1.46 anton 381: #ifdef NO_IP
382: goto *next_code;
383: #else
1.1 anton 384: NEXT_P0;
385: NEXT;
1.46 anton 386: #endif
1.1 anton 387:
388: douser:
389: {
390: #ifdef DEBUG
391: fprintf(stderr,"%08lx: user: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
392: #endif
393: #ifdef USE_TOS
1.24 anton 394: *sp-- = spTOS;
395: spTOS = (Cell)(up+*(Cell*)PFA1(cfa));
1.1 anton 396: #else
397: *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
398: #endif
399: }
1.46 anton 400: #ifdef NO_IP
401: goto *next_code;
402: #else
1.1 anton 403: NEXT_P0;
404: NEXT;
1.46 anton 405: #endif
1.1 anton 406:
407: dodefer:
408: {
409: #ifdef DEBUG
410: fprintf(stderr,"%08lx: defer: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
411: #endif
1.28 anton 412: SUPER_END;
1.1 anton 413: EXEC(*(Xt *)PFA1(cfa));
414: }
415:
416: dofield:
417: {
418: #ifdef DEBUG
419: fprintf(stderr,"%08lx: field: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
420: #endif
1.24 anton 421: spTOS += *(Cell*)PFA1(cfa);
1.1 anton 422: }
1.46 anton 423: #ifdef NO_IP
424: goto *next_code;
425: #else
1.1 anton 426: NEXT_P0;
427: NEXT;
1.46 anton 428: #endif
1.1 anton 429:
430: dodoes:
431: /* this assumes the following structure:
432: defining-word:
433:
434: ...
435: DOES>
436: (possible padding)
437: possibly handler: jmp dodoes
438: (possible branch delay slot(s))
439: Forth code after DOES>
440:
441: defined word:
442:
443: cfa: address of or jump to handler OR
444: address of or jump to dodoes, address of DOES-code
445: pfa:
446:
447: */
1.46 anton 448: #ifdef NO_IP
449: *--rp = next_code;
450: IF_spTOS(spTOS = sp[0]);
451: sp--;
452: spTOS = (Cell)PFA(cfa);
453: goto **(Label *)DOES_CODE1(cfa);
454: #else
1.1 anton 455: {
456: /* fprintf(stderr, "Got CFA %08lx at doescode %08lx/%08lx: does: %08lx\n",cfa,(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));*/
457: #ifdef DEBUG
458: fprintf(stderr,"%08lx/%08lx: does: %08lx\n",(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));
459: fflush(stderr);
460: #endif
461: *--rp = (Cell)ip;
462: /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
463: #ifdef USE_TOS
1.24 anton 464: *sp-- = spTOS;
465: spTOS = (Cell)PFA(cfa);
1.1 anton 466: #else
467: *--sp = (Cell)PFA(cfa);
468: #endif
1.11 anton 469: SET_IP(DOES_CODE1(cfa));
1.28 anton 470: SUPER_END;
1.24 anton 471: /* fprintf(stderr,"TOS = %08lx, IP=%08lx\n", spTOS, IP);*/
1.1 anton 472: }
473: NEXT;
1.46 anton 474: #endif
1.1 anton 475:
476: #include "prim.i"
1.34 anton 477: after_last: return (Label *)0;
478: /*needed only to get the length of the last primitive */
1.50 anton 479: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>