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