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