File:
[gforth] /
gforth /
engine /
engine.c
Revision
1.68:
download - view:
text,
annotated -
select for diffs
Thu Oct 9 14:15:20 2003 UTC (17 years, 3 months ago) by
anton
Branches:
MAIN
CVS tags:
HEAD
put stack caching stuff in prims2x.fs (cvs log prims2y.fs for
fine-grained changes)
set up prim etc. to work with the changes prims2x.fs (disabled USE_TOS for now)
1: /* Gforth virtual machine (aka inner interpreter)
2:
3: Copyright (C) 1995,1996,1997,1998,2000,2003 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: #define USE_NO_TOS
23: #define USE_NO_FTOS
24:
25: #include "config.h"
26: #include "forth.h"
27: #include <ctype.h>
28: #include <stdio.h>
29: #include <string.h>
30: #include <math.h>
31: #include <assert.h>
32: #include <stdlib.h>
33: #include <errno.h>
34: #include "io.h"
35: #include "threaded.h"
36: #ifndef STANDALONE
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>
44: #include <dirent.h>
45: #include <sys/resource.h>
46: #ifdef HAVE_FNMATCH_H
47: #include <fnmatch.h>
48: #else
49: #include "fnmatch.h"
50: #endif
51: #else
52: #include "systypes.h"
53: #endif
54:
55: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
56: #include <dlfcn.h>
57: #endif
58: #if defined(_WIN32)
59: #include <windows.h>
60: #endif
61: #ifdef hpux
62: #include <dl.h>
63: #endif
64:
65: #ifdef HAS_FFCALL
66: #include <avcall.h>
67: #include <callback.h>
68: #endif
69:
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:
75: #ifndef HAVE_FSEEKO
76: #define fseeko fseek
77: #endif
78:
79: #ifndef HAVE_FTELLO
80: #define ftello ftell
81: #endif
82:
83: #define NULLC '\0'
84:
85: #ifdef MEMCMP_AS_SUBROUTINE
86: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
87: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
88: #endif
89:
90: #define NEWLINE '\n'
91:
92: /* conversion on fetch */
93:
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)
108:
109: /* conversion on store */
110:
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)
125:
126: #define vm_Cell2Cell(_x,_y) (_y=_x)
127:
128: #ifdef NO_IP
129: #define IMM_ARG(access,value) (VARIANT(value))
130: #else
131: #define IMM_ARG(access,value) (access)
132: #endif
133:
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
159: #ifndef FTOSREG
160: #define FTOSREG
161: #endif
162:
163: #ifndef CPU_DEP1
164: # define CPU_DEP1 0
165: #endif
166:
167: /* instructions containing SUPER_END must be the last instruction of a
168: super-instruction (e.g., branches, EXECUTE, and other instructions
169: ending the basic block). Instructions containing SET_IP get this
170: automatically, so you usually don't have to write it. If you have
171: to write it, write it after IP points to the next instruction.
172: Used for profiling. Don't write it in a word containing SET_IP, or
173: the following block will be counted twice. */
174: #ifdef VM_PROFILING
175: #define SUPER_END vm_count_block(IP)
176: #else
177: #define SUPER_END
178: #endif
179: #define SUPER_CONTINUE
180:
181: #ifdef GFORTH_DEBUGGING
182: #define NAME(string) { saved_ip=ip; asm(""); }
183: /* the asm here is to avoid reordering of following stuff above the
184: assignment; this is an old-style asm (no operands), and therefore
185: is treated like "asm volatile ..."; i.e., it prevents most
186: reorderings across itself. We want the assignment above first,
187: because the stack loads may already cause a stack underflow. */
188: #elif DEBUG
189: # define NAME(string) fprintf(stderr,"%08lx depth=%3ld: "string"\n",(Cell)ip,sp0+3-sp);
190: #else
191: # define NAME(string)
192: #endif
193:
194: #ifdef DEBUG
195: #define CFA_TO_NAME(__cfa) \
196: Cell len, i; \
197: char * name = __cfa; \
198: for(i=0; i<32; i+=sizeof(Cell)) { \
199: len = ((Cell*)name)[-1]; \
200: if(len < 0) { \
201: len &= 0x1F; \
202: if((len+sizeof(Cell)) > i) break; \
203: } len = 0; \
204: name -= sizeof(Cell); \
205: }
206: #endif
207:
208: #ifdef HAS_FFCALL
209: #define SAVE_REGS IF_spTOS(sp[0]=spTOS); IF_fpTOS(fp[0]=fpTOS); SP=sp; FP=fp; RP=rp; LP=lp;
210: #define REST_REGS sp=SP; fp=FP; rp=RP; lp=LP; IF_spTOS(spTOS=sp[0]); IF_fpTOS(fpTOS=fp[0]);
211: #endif
212:
213: #if !defined(ENGINE)
214: /* normal engine */
215: #define VARIANT(v) (v)
216: #define JUMP(target) goto I_noop
217: #define LABEL(name) J_##name: asm(""); I_##name:
218:
219: #elif ENGINE==2
220: /* variant with padding between VM instructions for finding out
221: cross-inst jumps (for dynamic code) */
222: #define engine engine2
223: #define VARIANT(v) (v)
224: #define JUMP(target) goto I_noop
225: #define LABEL(name) J_##name: SKIP16; I_##name:
226: #define IN_ENGINE2
227:
228: #elif ENGINE==3
229: /* variant with different immediate arguments for finding out
230: immediate arguments (for native code) */
231: #define engine engine3
232: #define VARIANT(v) ((v)^0xffffffff)
233: #define JUMP(target) goto K_lit
234: #define LABEL(name) J_##name: asm(""); I_##name:
235: #else
236: #error illegal ENGINE value
237: #endif /* ENGINE */
238:
239: /* the asm(""); is there to get a stop compiled on Itanium */
240: #define LABEL2(name) K_##name: asm("");
241:
242: Label *engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
243: /* executes code at ip, if ip!=NULL
244: returns array of machine code labels (for use in a loader), if ip==NULL
245: */
246: {
247: #ifndef GFORTH_DEBUGGING
248: register Cell *rp RPREG;
249: #endif
250: #ifndef NO_IP
251: register Xt *ip IPREG = ip0;
252: #endif
253: register Cell *sp SPREG = sp0;
254: register Float *fp FPREG = fp0;
255: register Address lp LPREG = lp0;
256: register Xt cfa CFAREG;
257: #ifdef MORE_VARS
258: MORE_VARS
259: #endif
260: #ifdef HAS_FFCALL
261: av_alist alist;
262: extern va_alist clist;
263: float frv;
264: int irv;
265: double drv;
266: long long llrv;
267: void * prv;
268: #endif
269: register Address up UPREG = UP;
270: IF_spTOS(register Cell spTOS TOSREG;)
271: IF_fpTOS(register Float fpTOS FTOSREG;)
272: #if defined(DOUBLY_INDIRECT)
273: static Label *symbols;
274: static void *routines[]= {
275: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
276: #else /* !defined(DOUBLY_INDIRECT) */
277: static Label symbols[]= {
278: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
279: #endif /* !defined(DOUBLY_INDIRECT) */
280: #define INST_ADDR(name) ((Label)&&I_##name)
281: #include "prim_lab.i"
282: #undef INST_ADDR
283: (Label)0,
284: #define INST_ADDR(name) ((Label)&&K_##name)
285: #include "prim_lab.i"
286: #undef INST_ADDR
287: #define INST_ADDR(name) ((Label)&&J_##name)
288: #include "prim_lab.i"
289: #undef INST_ADDR
290: (Label)&&after_last
291: };
292: #ifdef CPU_DEP2
293: CPU_DEP2
294: #endif
295:
296: rp = rp0;
297: #ifdef DEBUG
298: fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
299: (unsigned)ip0,(unsigned)sp,(unsigned)rp,
300: (unsigned)fp,(unsigned)lp,(unsigned)up);
301: #endif
302:
303: if (ip0 == NULL) {
304: #if defined(DOUBLY_INDIRECT)
305: #define CODE_OFFSET (26*sizeof(Cell))
306: #define XT_OFFSET (22*sizeof(Cell))
307: int i;
308: Cell code_offset = offset_image? CODE_OFFSET : 0;
309: Cell xt_offset = offset_image? XT_OFFSET : 0;
310:
311: symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
312: xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
313: for (i=0; i<DOESJUMP+1; i++)
314: xts[i] = symbols[i] = (Label)routines[i];
315: for (; routines[i]!=0; i++) {
316: if (i>=MAX_SYMBOLS) {
317: fprintf(stderr,"gforth-ditc: more than %ld primitives\n",(long)MAX_SYMBOLS);
318: exit(1);
319: }
320: xts[i] = symbols[i] = &routines[i];
321: }
322: #endif /* defined(DOUBLY_INDIRECT) */
323: return symbols;
324: }
325:
326: IF_spTOS(spTOS = sp[0]);
327: IF_fpTOS(fpTOS = fp[0]);
328: /* prep_terminal(); */
329: #ifdef NO_IP
330: goto *(*(Label *)ip0);
331: #else
332: SET_IP(ip);
333: SUPER_END; /* count the first block, too */
334: NEXT;
335: #endif
336:
337: #ifdef CPU_DEP3
338: CPU_DEP3
339: #endif
340:
341: #include "prim.i"
342: after_last: return (Label *)0;
343: /*needed only to get the length of the last primitive */
344: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>