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