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