Annotation of gforth/engine/forth.h, revision 1.87
1.1 anton 1: /* common header file
2:
1.79 anton 3: Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006 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.13 anton 19: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1 anton 20: */
21:
22: #include "config.h"
1.18 anton 23: #include <stdio.h>
1.35 anton 24: #include <sys/time.h>
25: #include <unistd.h>
1.87 ! anton 26: #include <ltdl.h>
1.1 anton 27:
1.32 anton 28: #if defined(DOUBLY_INDIRECT)||defined(INDIRECT_THREADED)||defined(VM_PROFILING)
29: #define NO_DYNAMIC
30: #endif
31:
1.1 anton 32: #if defined(DOUBLY_INDIRECT)
33: # undef DIRECT_THREADED
34: # undef INDIRECT_THREADED
35: # define INDIRECT_THREADED
36: #endif
37:
1.57 anton 38: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
1.23 anton 39: # undef USE_TOS
40: # undef USE_FTOS
1.56 anton 41: # undef USE_NO_TOS
42: # undef USE_NO_FTOS
1.23 anton 43: # define USE_NO_TOS
44: # define USE_NO_FTOS
1.57 anton 45:
46: #define PRIM_I "prim.i"
47: #define PRIM_LAB_I "prim_lab.i"
48: #define PRIM_NAMES_I "prim_names.i"
49: #define PRIM_SUPEREND_I "prim_superend.i"
50: #define PRIM_NUM_I "prim_num.i"
51: #define PRIM_GRP_I "prim_grp.i"
52: #define COSTS_I "costs.i"
53: #define SUPER2_I "super2.i"
1.58 anton 54: /* #define PROFILE_I "profile.i" */
1.57 anton 55:
56: #else
57: /* gforth-fast or gforth-native */
1.59 anton 58: # undef USE_TOS
59: # undef USE_FTOS
60: # undef USE_NO_TOS
61: # undef USE_NO_FTOS
62: # define USE_TOS
1.57 anton 63:
64: #define PRIM_I "prim-fast.i"
65: #define PRIM_LAB_I "prim_lab-fast.i"
66: #define PRIM_NAMES_I "prim_names-fast.i"
67: #define PRIM_SUPEREND_I "prim_superend-fast.i"
68: #define PRIM_NUM_I "prim_num-fast.i"
69: #define PRIM_GRP_I "prim_grp-fast.i"
70: #define COSTS_I "costs-fast.i"
71: #define SUPER2_I "super2-fast.i"
1.58 anton 72: /* profile.c uses profile.i but does not define VM_PROFILING */
73: /* #define PROFILE_I "profile-fast.i" */
1.57 anton 74:
1.23 anton 75: #endif
1.57 anton 76:
77:
1.23 anton 78:
1.1 anton 79: #include <limits.h>
80:
81: #if defined(NeXT)
82: # include <libc.h>
83: #endif /* NeXT */
84:
85: /* symbol indexed constants */
86:
87: #define DOCOL 0
88: #define DOCON 1
89: #define DOVAR 2
90: #define DOUSER 3
91: #define DODEFER 4
92: #define DOFIELD 5
1.86 pazsan 93: #define DOVAL 6
94: #define DODOES 7
95: #define DOESJUMP 8
1.1 anton 96:
97: /* the size of the DOESJUMP, which resides between DOES> and the does-code */
98: #define DOES_HANDLER_SIZE (2*sizeof(Cell))
99:
1.6 jwilke 100: #include "machine.h"
1.1 anton 101:
1.68 pazsan 102: /* C interface data types */
103:
104: typedef WYDE_TYPE Wyde;
105: typedef TETRABYTE_TYPE Tetrabyte;
106: typedef unsigned WYDE_TYPE UWyde;
107: typedef unsigned TETRABYTE_TYPE UTetrabyte;
108:
1.1 anton 109: /* Forth data types */
110: /* Cell and UCell must be the same size as a pointer */
111: #define CELL_BITS (sizeof(Cell) * CHAR_BIT)
1.77 anton 112: #define CELL_MIN (((Cell)1)<<(sizeof(Cell)*CHAR_BIT-1))
1.78 anton 113:
114: #define HALFCELL_BITS (CELL_BITS/2)
115: #define HALFCELL_MASK ((~(UCell)0)>>HALFCELL_BITS)
116: #define UH(x) (((UCell)(x))>>HALFCELL_BITS)
117: #define LH(x) ((x)&HALFCELL_MASK)
118: #define L2U(x) (((UCell)(x))<<HALFCELL_BITS)
119: #define HIGHBIT(x) (((UCell)(x))>>(CELL_BITS-1))
120:
1.1 anton 121: #define FLAG(b) (-(b))
122: #define FILEIO(error) (FLAG(error) & -37)
123: #define FILEEXIST(error) (FLAG(error) & -38)
124:
125: #define F_TRUE (FLAG(0==0))
126: #define F_FALSE (FLAG(0!=0))
127:
1.65 pazsan 128: /* define this false if you want native division */
1.66 pazsan 129: #ifdef FORCE_CDIV
130: #define FLOORED_DIV 0
131: #else
1.65 pazsan 132: #define FLOORED_DIV ((1%-3)>0)
1.66 pazsan 133: #endif
1.65 pazsan 134:
1.1 anton 135: #ifdef BUGGY_LONG_LONG
1.64 pazsan 136:
137: #define BUGGY_LL_CMP /* compares not possible */
138: #define BUGGY_LL_MUL /* multiplication not possible */
139: #define BUGGY_LL_DIV /* division not possible */
140: #define BUGGY_LL_ADD /* addition not possible */
141: #define BUGGY_LL_SHIFT /* shift not possible */
142: #define BUGGY_LL_D2F /* to float not possible */
143: #define BUGGY_LL_F2D /* from float not possible */
144: #define BUGGY_LL_SIZE /* long long "too short", so we use something else */
145:
1.1 anton 146: typedef struct {
147: Cell hi;
148: UCell lo;
149: } DCell;
150:
151: typedef struct {
152: UCell hi;
153: UCell lo;
154: } UDCell;
155:
1.64 pazsan 156: #define DHI(x) (x).hi
157: #define DLO(x) (x).lo
158: #define DHI_IS(x,y) (x).hi=(y)
159: #define DLO_IS(x,y) (x).lo=(y)
160:
1.78 anton 161: #define UD2D(ud) ({UDCell _ud=(ud); (DCell){_ud.hi,_ud.lo};})
1.80 anton 162: #define D2UD(d) ({DCell _d1=(d); (UDCell){_d1.hi,_d1.lo};})
163:
164: /* shifts by less than CELL_BITS */
165: #define DLSHIFT(d,u) ({DCell _d=(d); UCell _u=(u); \
166: (DCell){(_d.hi<<_u)|(_d.lo>>(CELL_BITS-_u)),_d.lo<<_u};})
167: #define UDLSHIFT(ud,u) D2UD(DLSHIFT(UD2D(ud),u))
1.78 anton 168:
1.39 anton 169: #if SMALL_OFF_T
170: #define OFF2UD(o) ({UDCell _ud; _ud.hi=0; _ud.lo=(Cell)(o); _ud;})
171: #define UD2OFF(ud) ((ud).lo)
172: #else /* !SMALL_OFF_T */
173: #define OFF2UD(o) ({UDCell _ud; off_t _o=(o); _ud.hi=_o>>CELL_BITS; _ud.lo=(Cell)_o; _ud;})
1.36 anton 174: #define UD2OFF(ud) ({UDCell _ud=(ud); (((off_t)_ud.hi)<<CELL_BITS)+_ud.lo;})
1.39 anton 175: #endif /* !SMALL_OFF_T */
1.1 anton 176: #define DZERO ((DCell){0,0})
177:
178: #else /* ! defined(BUGGY_LONG_LONG) */
179:
180: /* DCell and UDCell must be twice as large as Cell */
181: typedef DOUBLE_CELL_TYPE DCell;
1.63 pazsan 182: typedef DOUBLE_UCELL_TYPE UDCell;
1.1 anton 183:
1.36 anton 184: #define OFF2UD(o) ((UDCell)(o))
185: #define UD2OFF(ud) ((off_t)(ud))
1.16 anton 186: #define DZERO ((DCell)0)
1.80 anton 187: /* shifts by less than CELL_BITS */
188: #define DLSHIFT(d,u) ((d)<<(u))
189: #define UDLSHIFT(d,u) ((d)<<(u))
1.16 anton 190:
191: #endif /* ! defined(BUGGY_LONG_LONG) */
192:
1.1 anton 193: typedef union {
194: struct {
1.16 anton 195: #if defined(WORDS_BIGENDIAN)||defined(BUGGY_LONG_LONG)
1.1 anton 196: Cell high;
197: UCell low;
198: #else
199: UCell low;
200: Cell high;
1.20 crook 201: #endif
1.1 anton 202: } cells;
1.16 anton 203: DCell d;
204: UDCell ud;
1.1 anton 205: } Double_Store;
1.64 pazsan 206:
207: #ifndef BUGGY_LONG_LONG
208: #define DHI(x) ({ Double_Store _d; _d.d=(x); _d.cells.high; })
209: #define DLO(x) ({ Double_Store _d; _d.d=(x); _d.cells.low; })
210:
211: /* beware with the assignment: x is referenced twice! */
1.78 anton 212: #define DHI_IS(x,y) ({ Double_Store _d; _d.d=(x); _d.cells.high=(y); (x)=_d.d; })
213: #define DLO_IS(x,y) ({ Double_Store _d; _d.d=(x); _d.cells.low =(y); (x)=_d.d; })
214:
215: #define UD2D(ud) ((DCell)(ud))
216: #define D2UD(d) ((UDCell)(d))
1.64 pazsan 217: #endif
1.1 anton 218:
1.16 anton 219: #define FETCH_DCELL_T(d_,lo,hi,t_) ({ \
1.1 anton 220: Double_Store _d; \
221: _d.cells.low = (lo); \
222: _d.cells.high = (hi); \
1.16 anton 223: (d_) = _d.t_; \
1.1 anton 224: })
225:
1.16 anton 226: #define STORE_DCELL_T(d_,lo,hi,t_) ({ \
1.1 anton 227: Double_Store _d; \
1.16 anton 228: _d.t_ = (d_); \
1.1 anton 229: (lo) = _d.cells.low; \
230: (hi) = _d.cells.high; \
231: })
232:
1.28 anton 233: #define vm_twoCell2d(lo,hi,d_) FETCH_DCELL_T(d_,lo,hi,d);
234: #define vm_twoCell2ud(lo,hi,d_) FETCH_DCELL_T(d_,lo,hi,ud);
1.1 anton 235:
1.28 anton 236: #define vm_d2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,d);
237: #define vm_ud2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,ud);
1.1 anton 238:
239: typedef Label *Xt;
240:
241: /* PFA gives the parameter field address corresponding to a cfa */
242: #define PFA(cfa) (((Cell *)cfa)+2)
243: /* PFA1 is a special version for use just after a NEXT1 */
244: #define PFA1(cfa) PFA(cfa)
245: /* CODE_ADDRESS is the address of the code jumped to through the code field */
246: #define CODE_ADDRESS(cfa) (*(Xt)(cfa))
247:
248: /* DOES_CODE is the Forth code does jumps to */
249: #if !defined(DOUBLY_INDIRECT)
250: # define DOES_CA (symbols[DODOES])
251: #else /* defined(DOUBLY_INDIRECT) */
1.24 anton 252: # define DOES_CA ((Label)&xts[DODOES])
1.1 anton 253: #endif /* defined(DOUBLY_INDIRECT) */
254:
255:
256:
257: #define DOES_CODE1(cfa) ((Xt *)(cfa[1]))
258: /* MAKE_CF creates an appropriate code field at the cfa;
259: ca is the code address */
260: #define MAKE_CF(cfa,ca) ((*(Label *)(cfa)) = ((Label)ca))
261: /* make a code field for a defining-word-defined word */
262: #define MAKE_DOES_CF(cfa,does_code) ({MAKE_CF(cfa,DOES_CA); \
263: ((Cell *)cfa)[1] = (Cell)(does_code);})
264:
265: #define CF(const) (-const-2)
266:
267: #define CF_NIL -1
268:
269: #ifndef FLUSH_ICACHE
270: #warning flush-icache probably will not work (see manual)
271: # define FLUSH_ICACHE(addr,size)
1.45 anton 272: #warning no FLUSH_ICACHE, turning off dynamic native code by default
273: #undef NO_DYNAMIC_DEFAULT
274: #define NO_DYNAMIC_DEFAULT 1
1.1 anton 275: #endif
276:
1.67 anton 277: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
278: #define STACK_CACHE_DEFAULT 0
1.1 anton 279: #else
1.67 anton 280: #define STACK_CACHE_DEFAULT STACK_CACHE_DEFAULT_FAST
1.1 anton 281: #endif
282:
283: #ifdef USE_FTOS
1.14 anton 284: #define IF_fpTOS(x) x
1.1 anton 285: #else
1.14 anton 286: #define IF_fpTOS(x)
287: #define fpTOS (fp[0])
1.1 anton 288: #endif
289:
1.15 anton 290: #define IF_rpTOS(x)
291: #define rpTOS (rp[0])
292:
1.10 anton 293: typedef struct {
294: Address base; /* base address of image (0 if relocatable) */
295: UCell checksum; /* checksum of ca's to protect against some
296: incompatible binary/executable combinations
297: (0 if relocatable) */
298: UCell image_size; /* all sizes in bytes */
299: UCell dict_size;
300: UCell data_stack_size;
301: UCell fp_stack_size;
302: UCell return_stack_size;
303: UCell locals_stack_size;
304: Xt *boot_entry; /* initial ip for booting (in BOOT) */
305: Xt *throw_entry; /* ip after signal (in THROW) */
306: Cell unused1; /* possibly tib stack size */
1.24 anton 307: Label *xt_base; /* base of DOUBLE_INDIRECT xts[], for comp-i.fs */
1.10 anton 308: Address data_stack_base; /* this and the following fields are initialized by the loader */
309: Address fp_stack_base;
310: Address return_stack_base;
311: Address locals_stack_base;
312: } ImageHeader;
313: /* the image-header is created in main.fs */
314:
1.82 pazsan 315: #ifdef HAS_F83HEADERSTRING
316: struct F83Name {
317: struct F83Name *next; /* the link field for old hands */
318: char countetc;
319: char name[0];
320: };
321:
322: #define F83NAME_COUNT(np) ((np)->countetc & 0x1f)
323: #endif
1.48 anton 324: struct Longname {
325: struct Longname *next; /* the link field for old hands */
326: Cell countetc;
327: char name[0];
328: };
329:
330: #define LONGNAME_COUNT(np) ((np)->countetc & (((~((UCell)0))<<3)>>3))
331:
332: struct Cellpair {
333: Cell n1;
334: Cell n2;
335: };
336:
337: struct Cellquad {
338: Cell n1;
339: Cell n2;
340: Cell n3;
341: Cell n4;
342: };
1.49 anton 343:
344: #define IOR(flag) ((flag)? -512-errno : 0)
1.48 anton 345:
1.73 pazsan 346: Label *gforth_engine(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
347: Label *gforth_engine2(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
348: Label *gforth_engine3(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
1.48 anton 349:
350: /* engine/prim support routines */
1.71 pazsan 351: Address gforth_alloc(Cell size);
1.35 anton 352: char *cstr(Char *from, UCell size, int clear);
1.11 anton 353: char *tilde_cstr(Char *from, UCell size, int clear);
1.85 anton 354: Cell opencreate_file(char *s, Cell wfam, int flags, Cell *wiorp);
1.35 anton 355: DCell timeval2us(struct timeval *tvp);
1.48 anton 356: void cmove(Char *c_from, Char *c_to, UCell u);
357: void cmove_up(Char *c_from, Char *c_to, UCell u);
358: Cell compare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
359: struct Longname *listlfind(Char *c_addr, UCell u, struct Longname *longname1);
360: struct Longname *hashlfind(Char *c_addr, UCell u, Cell *a_addr);
361: struct Longname *tablelfind(Char *c_addr, UCell u, Cell *a_addr);
362: UCell hashkey1(Char *c_addr, UCell u, UCell ubits);
363: struct Cellpair parse_white(Char *c_addr1, UCell u1);
364: Cell rename_file(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
365: struct Cellquad read_line(Char *c_addr, UCell u1, Cell wfileid);
366: struct Cellpair file_status(Char *c_addr, UCell u);
367: Cell to_float(Char *c_addr, UCell u, Float *rp);
368: Float v_star(Float *f_addr1, Cell nstride1, Float *f_addr2, Cell nstride2, UCell ucount);
369: void faxpy(Float ra, Float *f_x, Cell nstridex, Float *f_y, Cell nstridey, UCell ucount);
1.60 pazsan 370: UCell lshift(UCell u1, UCell n);
371: UCell rshift(UCell u1, UCell n);
1.61 anton 372: int gforth_system(Char *c_addr, UCell u);
1.83 anton 373: void gforth_ms(UCell u);
1.72 pazsan 374: Cell capscompare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
1.1 anton 375:
1.50 anton 376: /* signal handler stuff */
377: void install_signal_handlers(void);
1.75 anton 378: void throw(int code);
1.76 anton 379: /* throw codes */
380: #define BALL_DIVZERO -10
381: #define BALL_RESULTRANGE -11
382:
1.50 anton 383: typedef void Sigfunc(int);
384: Sigfunc *bsd_signal(int signo, Sigfunc *func);
385:
1.1 anton 386: /* dblsub routines */
387: DCell dnegate(DCell d1);
388: UDCell ummul (UCell a, UCell b);
389: DCell mmul (Cell a, Cell b);
390: UDCell umdiv (UDCell u, UCell v);
391: DCell smdiv (DCell num, Cell denom);
392: DCell fmdiv (DCell num, Cell denom);
393:
1.7 pazsan 394: Cell memcasecmp(const Char *s1, const Char *s2, Cell n);
1.1 anton 395:
1.18 anton 396: void vm_print_profile(FILE *file);
397: void vm_count_block(Xt *ip);
1.17 anton 398:
1.22 anton 399: /* dynamic superinstruction stuff */
1.33 anton 400: void compile_prim1(Cell *start);
401: void finish_code(void);
1.34 anton 402: int forget_dyncode(Address code);
403: Label decompile_code(Label prim);
1.17 anton 404:
1.1 anton 405: extern int offset_image;
1.5 anton 406: extern int die_on_signal;
1.74 anton 407: extern int ignore_async_signals;
1.10 anton 408: extern UCell pagesize;
409: extern ImageHeader *gforth_header;
1.19 anton 410: extern Label *vm_prims;
1.24 anton 411: extern Label *xts;
1.25 anton 412: extern Cell npriminfos;
1.2 pazsan 413:
1.31 anton 414: #ifdef HAS_DEBUG
415: extern int debug;
416: #else
417: # define debug 0
418: #endif
419:
1.71 pazsan 420: extern Cell *gforth_SP;
421: extern Float *gforth_FP;
422: extern Address gforth_UP;
1.35 anton 423:
1.53 pazsan 424: #ifdef HAS_FFCALL
1.71 pazsan 425: extern Cell *gforth_RP;
426: extern Address gforth_LP;
427: extern void gforth_callback(Xt* fcall, void * alist);
1.53 pazsan 428: #endif
429:
1.69 pazsan 430: #ifdef HAS_LIBFFI
1.71 pazsan 431: extern Cell *gforth_RP;
432: extern Address gforth_LP;
1.69 pazsan 433: #include <ffi.h>
1.72 pazsan 434: extern void gforth_callback(ffi_cif * cif, void * resp, void ** args, void * ip);
1.69 pazsan 435: #endif
436:
1.9 anton 437: #ifdef GFORTH_DEBUGGING
1.29 anton 438: extern Xt *saved_ip;
1.9 anton 439: extern Cell *rp;
1.35 anton 440: #endif
441:
442: #ifdef NO_IP
443: extern Label next_code;
444: #endif
445:
446: #ifdef HAS_FILE
447: extern char* fileattr[6];
448: extern char* pfileattr[6];
449: extern int ufileattr[6];
1.9 anton 450: #endif
451:
1.27 anton 452: #ifdef PRINT_SUPER_LENGTHS
453: Cell prim_length(Cell prim);
454: void print_super_lengths();
455: #endif
1.9 anton 456:
1.2 pazsan 457: /* declare all the functions that are missing */
458: #ifndef HAVE_ATANH
459: extern double atanh(double r1);
460: extern double asinh(double r1);
461: extern double acosh(double r1);
462: #endif
463: #ifndef HAVE_ECVT
1.4 anton 464: /* extern char* ecvt(double x, int len, int* exp, int* sign);*/
1.2 pazsan 465: #endif
466: #ifndef HAVE_MEMMOVE
1.3 anton 467: /* extern char *memmove(char *dest, const char *src, long n); */
1.2 pazsan 468: #endif
469: #ifndef HAVE_POW10
470: extern double pow10(double x);
471: #endif
472: #ifndef HAVE_STRERROR
473: extern char *strerror(int err);
474: #endif
475: #ifndef HAVE_STRSIGNAL
476: extern char *strsignal(int sig);
477: #endif
478: #ifndef HAVE_STRTOUL
1.3 anton 479: extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
1.2 pazsan 480: #endif
481:
1.37 pazsan 482: #define GROUP(x, n)
1.53 pazsan 483: #define GROUPADD(n)
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>