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