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