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