Annotation of gforth/engine/engine.c, revision 1.91
1.1 anton 1: /* Gforth virtual machine (aka inner interpreter)
2:
1.87 anton 3: Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005 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:
1.72 anton 22: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
1.68 anton 23: #define USE_NO_TOS
1.72 anton 24: #else
25: #define USE_TOS
26: #endif
1.68 anton 27: #define USE_NO_FTOS
28:
1.1 anton 29: #include "config.h"
1.31 pazsan 30: #include "forth.h"
1.1 anton 31: #include <ctype.h>
32: #include <stdio.h>
33: #include <string.h>
34: #include <math.h>
1.4 pazsan 35: #include <assert.h>
36: #include <stdlib.h>
37: #include <errno.h>
38: #include "io.h"
39: #include "threaded.h"
40: #ifndef STANDALONE
1.1 anton 41: #include <sys/types.h>
42: #include <sys/stat.h>
43: #include <fcntl.h>
44: #include <time.h>
45: #include <sys/time.h>
46: #include <unistd.h>
47: #include <pwd.h>
1.17 pazsan 48: #include <dirent.h>
1.89 pazsan 49: #include <wchar.h>
1.21 anton 50: #include <sys/resource.h>
1.19 anton 51: #ifdef HAVE_FNMATCH_H
1.18 anton 52: #include <fnmatch.h>
1.19 anton 53: #else
54: #include "fnmatch.h"
55: #endif
1.4 pazsan 56: #else
57: #include "systypes.h"
58: #endif
1.1 anton 59:
60: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
61: #include <dlfcn.h>
62: #endif
1.8 pazsan 63: #if defined(_WIN32)
64: #include <windows.h>
65: #endif
1.1 anton 66: #ifdef hpux
67: #include <dl.h>
68: #endif
69:
1.63 pazsan 70: #ifdef HAS_FFCALL
71: #include <avcall.h>
72: #include <callback.h>
73: #endif
74:
1.84 pazsan 75: #ifdef HAS_LIBFFI
76: #include <ffi.h>
77: #endif
78:
1.1 anton 79: #ifndef SEEK_SET
80: /* should be defined in stdio.h, but some systems don't have it */
81: #define SEEK_SET 0
82: #endif
83:
1.53 anton 84: #ifndef HAVE_FSEEKO
85: #define fseeko fseek
86: #endif
87:
88: #ifndef HAVE_FTELLO
89: #define ftello ftell
90: #endif
91:
1.1 anton 92: #define NULLC '\0'
93:
1.14 pazsan 94: #ifdef MEMCMP_AS_SUBROUTINE
95: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
1.15 anton 96: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
1.14 pazsan 97: #endif
98:
1.1 anton 99: #define NEWLINE '\n'
1.91 ! anton 100: #define CELL_MIN (((Cell)1)<<(sizeof(Cell)*CHAR_BIT-1))
! 101: #define CHECK_DIVISION 1
1.1 anton 102:
1.26 anton 103: /* conversion on fetch */
104:
1.41 anton 105: #define vm_Cell2f(_cell,_x) ((_x)=(Bool)(_cell))
106: #define vm_Cell2c(_cell,_x) ((_x)=(Char)(_cell))
107: #define vm_Cell2n(_cell,_x) ((_x)=(Cell)(_cell))
108: #define vm_Cell2w(_cell,_x) ((_x)=(Cell)(_cell))
109: #define vm_Cell2u(_cell,_x) ((_x)=(UCell)(_cell))
110: #define vm_Cell2a_(_cell,_x) ((_x)=(Cell *)(_cell))
111: #define vm_Cell2c_(_cell,_x) ((_x)=(Char *)(_cell))
112: #define vm_Cell2f_(_cell,_x) ((_x)=(Float *)(_cell))
113: #define vm_Cell2df_(_cell,_x) ((_x)=(DFloat *)(_cell))
114: #define vm_Cell2sf_(_cell,_x) ((_x)=(SFloat *)(_cell))
115: #define vm_Cell2xt(_cell,_x) ((_x)=(Xt)(_cell))
116: #define vm_Cell2f83name(_cell,_x) ((_x)=(struct F83Name *)(_cell))
117: #define vm_Cell2longname(_cell,_x) ((_x)=(struct Longname *)(_cell))
118: #define vm_Float2r(_float,_x) (_x=_float)
1.26 anton 119:
120: /* conversion on store */
121:
1.41 anton 122: #define vm_f2Cell(_x,_cell) ((_cell)=(Cell)(_x))
123: #define vm_c2Cell(_x,_cell) ((_cell)=(Cell)(_x))
124: #define vm_n2Cell(_x,_cell) ((_cell)=(Cell)(_x))
125: #define vm_w2Cell(_x,_cell) ((_cell)=(Cell)(_x))
126: #define vm_u2Cell(_x,_cell) ((_cell)=(Cell)(_x))
127: #define vm_a_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
128: #define vm_c_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
129: #define vm_f_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
130: #define vm_df_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
131: #define vm_sf_2Cell(_x,_cell) ((_cell)=(Cell)(_x))
132: #define vm_xt2Cell(_x,_cell) ((_cell)=(Cell)(_x))
133: #define vm_f83name2Cell(_x,_cell) ((_cell)=(Cell)(_x))
134: #define vm_longname2Cell(_x,_cell) ((_cell)=(Cell)(_x))
135: #define vm_r2Float(_x,_float) (_float=_x)
1.26 anton 136:
1.41 anton 137: #define vm_Cell2Cell(_x,_y) (_y=_x)
1.29 anton 138:
1.46 anton 139: #ifdef NO_IP
140: #define IMM_ARG(access,value) (VARIANT(value))
141: #else
142: #define IMM_ARG(access,value) (access)
143: #endif
144:
1.1 anton 145: /* if machine.h has not defined explicit registers, define them as implicit */
146: #ifndef IPREG
147: #define IPREG
148: #endif
149: #ifndef SPREG
150: #define SPREG
151: #endif
152: #ifndef RPREG
153: #define RPREG
154: #endif
155: #ifndef FPREG
156: #define FPREG
157: #endif
158: #ifndef LPREG
159: #define LPREG
160: #endif
1.79 pazsan 161: #ifndef CAREG
162: #define CAREG
163: #endif
1.1 anton 164: #ifndef CFAREG
165: #define CFAREG
166: #endif
167: #ifndef UPREG
168: #define UPREG
169: #endif
170: #ifndef TOSREG
171: #define TOSREG
172: #endif
1.69 anton 173: #ifndef spbREG
174: #define spbREG
175: #endif
1.74 anton 176: #ifndef spcREG
177: #define spcREG
178: #endif
1.80 anton 179: #ifndef spdREG
180: #define spdREG
181: #endif
182: #ifndef speREG
183: #define speREG
184: #endif
1.81 anton 185: #ifndef spfREG
186: #define spfREG
187: #endif
188: #ifndef spgREG
189: #define spgREG
190: #endif
191: #ifndef sphREG
192: #define sphREG
193: #endif
1.1 anton 194: #ifndef FTOSREG
195: #define FTOSREG
196: #endif
197:
198: #ifndef CPU_DEP1
199: # define CPU_DEP1 0
200: #endif
201:
1.62 anton 202: /* instructions containing SUPER_END must be the last instruction of a
1.28 anton 203: super-instruction (e.g., branches, EXECUTE, and other instructions
204: ending the basic block). Instructions containing SET_IP get this
205: automatically, so you usually don't have to write it. If you have
206: to write it, write it after IP points to the next instruction.
207: Used for profiling. Don't write it in a word containing SET_IP, or
208: the following block will be counted twice. */
209: #ifdef VM_PROFILING
210: #define SUPER_END vm_count_block(IP)
211: #else
212: #define SUPER_END
213: #endif
1.35 anton 214: #define SUPER_CONTINUE
1.46 anton 215:
1.66 anton 216: #ifdef GFORTH_DEBUGGING
1.70 anton 217: #if DEBUG
1.73 pazsan 218: #define NAME(string) { saved_ip=ip; asm("# "string); fprintf(stderr,"%08lx depth=%3ld: "string"\n",(Cell)ip,sp0+3-sp);}
1.70 anton 219: #else /* !DEBUG */
1.66 anton 220: #define NAME(string) { saved_ip=ip; asm(""); }
221: /* the asm here is to avoid reordering of following stuff above the
222: assignment; this is an old-style asm (no operands), and therefore
223: is treated like "asm volatile ..."; i.e., it prevents most
224: reorderings across itself. We want the assignment above first,
225: because the stack loads may already cause a stack underflow. */
1.70 anton 226: #endif /* !DEBUG */
1.66 anton 227: #elif DEBUG
1.72 anton 228: # define NAME(string) {Cell __depth=sp0+3-sp; int i; fprintf(stderr,"%08lx depth=%3ld: "string,(Cell)ip,sp0+3-sp); for (i=__depth-1; i>0; i--) fprintf(stderr, " $%lx",sp[i]); fprintf(stderr, " $%lx\n",spTOS); }
1.66 anton 229: #else
1.73 pazsan 230: # define NAME(string) asm("# "string);
1.66 anton 231: #endif
232:
1.37 pazsan 233: #ifdef DEBUG
234: #define CFA_TO_NAME(__cfa) \
235: Cell len, i; \
236: char * name = __cfa; \
237: for(i=0; i<32; i+=sizeof(Cell)) { \
238: len = ((Cell*)name)[-1]; \
239: if(len < 0) { \
240: len &= 0x1F; \
241: if((len+sizeof(Cell)) > i) break; \
242: } len = 0; \
243: name -= sizeof(Cell); \
244: }
245: #endif
1.30 anton 246:
1.85 pazsan 247: #if defined(HAS_FFCALL) || defined(HAS_LIBFFI)
1.88 pazsan 248: #define SAVE_REGS IF_fpTOS(fp[0]=fpTOS); gforth_SP=sp; gforth_FP=fp; gforth_RP=rp; gforth_LP=lp;
249: #define REST_REGS sp=gforth_SP; fp=gforth_FP; rp=gforth_RP; lp=gforth_LP; IF_fpTOS(fpTOS=fp[0]);
1.63 pazsan 250: #endif
251:
1.50 anton 252: #if !defined(ENGINE)
253: /* normal engine */
254: #define VARIANT(v) (v)
255: #define JUMP(target) goto I_noop
1.82 anton 256: #define LABEL(name) H_##name: asm(""); I_##name:
1.50 anton 257:
258: #elif ENGINE==2
259: /* variant with padding between VM instructions for finding out
260: cross-inst jumps (for dynamic code) */
1.90 pazsan 261: #define gforth_engine gforth_engine2
1.50 anton 262: #define VARIANT(v) (v)
263: #define JUMP(target) goto I_noop
1.78 anton 264: #define LABEL(name) H_##name: SKIP16; I_##name:
1.50 anton 265:
266: #elif ENGINE==3
267: /* variant with different immediate arguments for finding out
268: immediate arguments (for native code) */
1.90 pazsan 269: #define gforth_engine gforth_engine3
1.50 anton 270: #define VARIANT(v) ((v)^0xffffffff)
271: #define JUMP(target) goto K_lit
1.83 anton 272: #define LABEL(name) H_##name: asm(""); I_##name:
1.50 anton 273: #else
274: #error illegal ENGINE value
275: #endif /* ENGINE */
276:
1.67 anton 277: /* the asm(""); is there to get a stop compiled on Itanium */
278: #define LABEL2(name) K_##name: asm("");
1.77 anton 279: #define LABEL3(name) J_##name: asm("");
1.50 anton 280:
1.90 pazsan 281: Label *gforth_engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
1.1 anton 282: /* executes code at ip, if ip!=NULL
283: returns array of machine code labels (for use in a loader), if ip==NULL
284: */
285: {
1.10 anton 286: #ifndef GFORTH_DEBUGGING
287: register Cell *rp RPREG;
288: #endif
1.46 anton 289: #ifndef NO_IP
290: register Xt *ip IPREG = ip0;
291: #endif
1.1 anton 292: register Cell *sp SPREG = sp0;
293: register Float *fp FPREG = fp0;
294: register Address lp LPREG = lp0;
295: register Xt cfa CFAREG;
1.79 pazsan 296: register Label real_ca CAREG;
1.11 anton 297: #ifdef MORE_VARS
298: MORE_VARS
1.63 pazsan 299: #endif
300: #ifdef HAS_FFCALL
301: av_alist alist;
1.90 pazsan 302: extern va_alist gforth_clist;
1.63 pazsan 303: float frv;
304: int irv;
305: double drv;
306: long long llrv;
307: void * prv;
1.84 pazsan 308: #endif
309: #ifdef HAS_LIBFFI
1.90 pazsan 310: extern void * gforth_ritem;
311: extern void ** gforth_clist;
1.84 pazsan 312: extern void ffi_callback(ffi_cif * cif, void * resp, void ** args, Xt * ip);
1.11 anton 313: #endif
1.88 pazsan 314: register Address up UPREG = gforth_UP;
1.80 anton 315: register Cell MAYBE_UNUSED spTOS TOSREG;
1.74 anton 316: register Cell MAYBE_UNUSED spb spbREG;
317: register Cell MAYBE_UNUSED spc spcREG;
1.80 anton 318: register Cell MAYBE_UNUSED spd spdREG;
319: register Cell MAYBE_UNUSED spe speREG;
1.81 anton 320: register Cell MAYBE_UNUSED spf speREG;
321: register Cell MAYBE_UNUSED spg speREG;
322: register Cell MAYBE_UNUSED sph speREG;
1.24 anton 323: IF_fpTOS(register Float fpTOS FTOSREG;)
1.1 anton 324: #if defined(DOUBLY_INDIRECT)
325: static Label *symbols;
326: static void *routines[]= {
1.27 anton 327: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
1.1 anton 328: #else /* !defined(DOUBLY_INDIRECT) */
329: static Label symbols[]= {
1.27 anton 330: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
1.1 anton 331: #endif /* !defined(DOUBLY_INDIRECT) */
1.55 anton 332: #define INST_ADDR(name) ((Label)&&I_##name)
1.71 anton 333: #include PRIM_LAB_I
1.34 anton 334: #undef INST_ADDR
335: (Label)0,
1.55 anton 336: #define INST_ADDR(name) ((Label)&&K_##name)
1.71 anton 337: #include PRIM_LAB_I
1.46 anton 338: #undef INST_ADDR
1.55 anton 339: #define INST_ADDR(name) ((Label)&&J_##name)
1.71 anton 340: #include PRIM_LAB_I
1.34 anton 341: #undef INST_ADDR
1.76 anton 342: (Label)&&after_last,
343: (Label)&&before_goto,
1.78 anton 344: (Label)&&after_goto,
345: /* just mention the H_ labels, so the SKIP16s are not optimized away */
346: #define INST_ADDR(name) ((Label)&&H_##name)
347: #include PRIM_LAB_I
348: #undef INST_ADDR
1.1 anton 349: };
350: #ifdef CPU_DEP2
351: CPU_DEP2
352: #endif
353:
1.10 anton 354: rp = rp0;
1.1 anton 355: #ifdef DEBUG
356: fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
1.46 anton 357: (unsigned)ip0,(unsigned)sp,(unsigned)rp,
1.1 anton 358: (unsigned)fp,(unsigned)lp,(unsigned)up);
359: #endif
360:
1.46 anton 361: if (ip0 == NULL) {
1.1 anton 362: #if defined(DOUBLY_INDIRECT)
1.38 anton 363: #define CODE_OFFSET (26*sizeof(Cell))
364: #define XT_OFFSET (22*sizeof(Cell))
1.1 anton 365: int i;
1.3 anton 366: Cell code_offset = offset_image? CODE_OFFSET : 0;
1.38 anton 367: Cell xt_offset = offset_image? XT_OFFSET : 0;
1.7 pazsan 368:
1.3 anton 369: symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
1.38 anton 370: xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
1.1 anton 371: for (i=0; i<DOESJUMP+1; i++)
1.38 anton 372: xts[i] = symbols[i] = (Label)routines[i];
1.1 anton 373: for (; routines[i]!=0; i++) {
374: if (i>=MAX_SYMBOLS) {
1.60 anton 375: fprintf(stderr,"gforth-ditc: more than %ld primitives\n",(long)MAX_SYMBOLS);
1.1 anton 376: exit(1);
1.20 anton 377: }
1.38 anton 378: xts[i] = symbols[i] = &routines[i];
1.1 anton 379: }
1.20 anton 380: #endif /* defined(DOUBLY_INDIRECT) */
381: return symbols;
1.7 pazsan 382: }
1.1 anton 383:
1.81 anton 384: #if !(defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING))
1.80 anton 385: sp += STACK_CACHE_DEFAULT-1;
1.81 anton 386: /* some of those registers are dead, but its simpler to initialize them all */ spTOS = sp[0];
1.80 anton 387: spb = sp[-1];
388: spc = sp[-2];
389: spd = sp[-3];
390: spe = sp[-4];
1.81 anton 391: spf = sp[-5];
392: spg = sp[-6];
393: sph = sp[-7];
1.80 anton 394: #endif
395:
1.24 anton 396: IF_fpTOS(fpTOS = fp[0]);
1.7 pazsan 397: /* prep_terminal(); */
1.46 anton 398: #ifdef NO_IP
399: goto *(*(Label *)ip0);
1.79 pazsan 400: before_goto:
401: goto *real_ca;
402: after_goto:;
1.46 anton 403: #else
1.11 anton 404: SET_IP(ip);
1.28 anton 405: SUPER_END; /* count the first block, too */
1.79 pazsan 406: FIRST_NEXT;
1.46 anton 407: #endif
1.11 anton 408:
1.1 anton 409: #ifdef CPU_DEP3
410: CPU_DEP3
1.46 anton 411: #endif
1.76 anton 412:
1.78 anton 413: #include PRIM_I
414: after_last: return (Label *)0;
415: /*needed only to get the length of the last primitive */
416:
417: return (Label *)0;
1.50 anton 418: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>