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