Annotation of gforth/engine/forth.h, revision 1.91
1.1 anton 1: /* common header file
2:
1.88 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.89 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.89 anton 18: along with this program; if not, see http://www.gnu.org/licenses/.
1.1 anton 19: */
20:
21: #include "config.h"
1.90 anton 22: #include "128bit.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.91 ! anton 346: #ifdef GFORTH_DEBUGGING
! 347: #if defined(GLOBALS_NONRELOC)
! 348: /* if globals cause non-relocatable primitives, keep saved_ip and rp
! 349: in a structure and access it through locals */
! 350: typedef struct saved_regs {
! 351: Xt *sr_saved_ip;
! 352: Cell *sr_rp;
! 353: } saved_regs;
! 354: extern saved_regs saved_regs_v, *saved_regs_p;
! 355: #define saved_ip (saved_regs_p->sr_saved_ip)
! 356: #define rp (saved_regs_p->sr_rp)
! 357: /* for use in gforth_engine header */
! 358: #define sr_proto , struct saved_regs *saved_regs_p
! 359: #define sr_call , saved_regs_p
! 360: #else /* !defined(GLOBALS_NONRELOC) */
! 361: extern Xt *saved_ip;
! 362: extern Cell *rp;
! 363: #define sr_proto
! 364: #define sr_call
! 365: #endif /* !defined(GLOBALS_NONRELOC) */
! 366: #else /* !defined(GFORTH_DEBUGGING) */
! 367: #define sr_proto
! 368: #define sr_call
! 369: #endif /* !defined(GFORTH_DEBUGGING) */
! 370:
! 371: Label *gforth_engine(Xt *ip, Cell *sp, Cell *rp0, Float *fp, Address lp sr_proto);
! 372: Label *gforth_engine2(Xt *ip, Cell *sp, Cell *rp0, Float *fp, Address lp sr_proto);
! 373: Label *gforth_engine3(Xt *ip, Cell *sp, Cell *rp0, Float *fp, Address lp sr_proto);
1.48 anton 374:
375: /* engine/prim support routines */
1.71 pazsan 376: Address gforth_alloc(Cell size);
1.35 anton 377: char *cstr(Char *from, UCell size, int clear);
1.11 anton 378: char *tilde_cstr(Char *from, UCell size, int clear);
1.85 anton 379: Cell opencreate_file(char *s, Cell wfam, int flags, Cell *wiorp);
1.35 anton 380: DCell timeval2us(struct timeval *tvp);
1.48 anton 381: void cmove(Char *c_from, Char *c_to, UCell u);
382: void cmove_up(Char *c_from, Char *c_to, UCell u);
383: Cell compare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
384: struct Longname *listlfind(Char *c_addr, UCell u, struct Longname *longname1);
385: struct Longname *hashlfind(Char *c_addr, UCell u, Cell *a_addr);
386: struct Longname *tablelfind(Char *c_addr, UCell u, Cell *a_addr);
387: UCell hashkey1(Char *c_addr, UCell u, UCell ubits);
388: struct Cellpair parse_white(Char *c_addr1, UCell u1);
389: Cell rename_file(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
390: struct Cellquad read_line(Char *c_addr, UCell u1, Cell wfileid);
391: struct Cellpair file_status(Char *c_addr, UCell u);
1.91 ! anton 392: Cell to_float(Char *c_addr, UCell u, Float *r_p);
1.48 anton 393: Float v_star(Float *f_addr1, Cell nstride1, Float *f_addr2, Cell nstride2, UCell ucount);
394: void faxpy(Float ra, Float *f_x, Cell nstridex, Float *f_y, Cell nstridey, UCell ucount);
1.60 pazsan 395: UCell lshift(UCell u1, UCell n);
396: UCell rshift(UCell u1, UCell n);
1.61 anton 397: int gforth_system(Char *c_addr, UCell u);
1.83 anton 398: void gforth_ms(UCell u);
1.72 pazsan 399: Cell capscompare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
1.1 anton 400:
1.50 anton 401: /* signal handler stuff */
402: void install_signal_handlers(void);
1.75 anton 403: void throw(int code);
1.76 anton 404: /* throw codes */
405: #define BALL_DIVZERO -10
406: #define BALL_RESULTRANGE -11
407:
1.50 anton 408: typedef void Sigfunc(int);
409: Sigfunc *bsd_signal(int signo, Sigfunc *func);
410:
1.1 anton 411: /* dblsub routines */
412: DCell dnegate(DCell d1);
413: UDCell ummul (UCell a, UCell b);
414: DCell mmul (Cell a, Cell b);
415: UDCell umdiv (UDCell u, UCell v);
416: DCell smdiv (DCell num, Cell denom);
417: DCell fmdiv (DCell num, Cell denom);
418:
1.7 pazsan 419: Cell memcasecmp(const Char *s1, const Char *s2, Cell n);
1.1 anton 420:
1.18 anton 421: void vm_print_profile(FILE *file);
422: void vm_count_block(Xt *ip);
1.17 anton 423:
1.22 anton 424: /* dynamic superinstruction stuff */
1.33 anton 425: void compile_prim1(Cell *start);
426: void finish_code(void);
1.34 anton 427: int forget_dyncode(Address code);
428: Label decompile_code(Label prim);
1.17 anton 429:
1.1 anton 430: extern int offset_image;
1.5 anton 431: extern int die_on_signal;
1.74 anton 432: extern int ignore_async_signals;
1.10 anton 433: extern UCell pagesize;
434: extern ImageHeader *gforth_header;
1.19 anton 435: extern Label *vm_prims;
1.24 anton 436: extern Label *xts;
1.25 anton 437: extern Cell npriminfos;
1.2 pazsan 438:
1.31 anton 439: #ifdef HAS_DEBUG
440: extern int debug;
441: #else
442: # define debug 0
443: #endif
444:
1.71 pazsan 445: extern Cell *gforth_SP;
446: extern Float *gforth_FP;
447: extern Address gforth_UP;
1.35 anton 448:
1.53 pazsan 449: #ifdef HAS_FFCALL
1.71 pazsan 450: extern Cell *gforth_RP;
451: extern Address gforth_LP;
452: extern void gforth_callback(Xt* fcall, void * alist);
1.53 pazsan 453: #endif
454:
1.69 pazsan 455: #ifdef HAS_LIBFFI
1.71 pazsan 456: extern Cell *gforth_RP;
457: extern Address gforth_LP;
1.69 pazsan 458: #include <ffi.h>
1.72 pazsan 459: extern void gforth_callback(ffi_cif * cif, void * resp, void ** args, void * ip);
1.69 pazsan 460: #endif
461:
1.35 anton 462: #ifdef NO_IP
463: extern Label next_code;
464: #endif
465:
466: #ifdef HAS_FILE
467: extern char* fileattr[6];
468: extern char* pfileattr[6];
469: extern int ufileattr[6];
1.9 anton 470: #endif
471:
1.27 anton 472: #ifdef PRINT_SUPER_LENGTHS
473: Cell prim_length(Cell prim);
474: void print_super_lengths();
475: #endif
1.9 anton 476:
1.2 pazsan 477: /* declare all the functions that are missing */
478: #ifndef HAVE_ATANH
479: extern double atanh(double r1);
480: extern double asinh(double r1);
481: extern double acosh(double r1);
482: #endif
483: #ifndef HAVE_ECVT
1.4 anton 484: /* extern char* ecvt(double x, int len, int* exp, int* sign);*/
1.2 pazsan 485: #endif
486: #ifndef HAVE_MEMMOVE
1.3 anton 487: /* extern char *memmove(char *dest, const char *src, long n); */
1.2 pazsan 488: #endif
489: #ifndef HAVE_POW10
490: extern double pow10(double x);
491: #endif
492: #ifndef HAVE_STRERROR
493: extern char *strerror(int err);
494: #endif
495: #ifndef HAVE_STRSIGNAL
496: extern char *strsignal(int sig);
497: #endif
498: #ifndef HAVE_STRTOUL
1.3 anton 499: extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
1.2 pazsan 500: #endif
501:
1.37 pazsan 502: #define GROUP(x, n)
1.53 pazsan 503: #define GROUPADD(n)
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>