Annotation of gforth/engine/forth.h, revision 1.36
1.1 anton 1: /* common header file
2:
1.12 anton 3: Copyright (C) 1995,1996,1997,1998,2000 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:
1.36 ! anton 22: /* turn on all POSIX, SUSv3, and GNU features if available */
1.14 anton 23: #define _GNU_SOURCE
1.36 ! anton 24: #define _POSIX_SOURCE
! 25: #define _POSIX_C_SOURCE 199506L
! 26: #define _XOPEN_SOURCE 600
! 27:
! 28: /* turn on large file support with 64-bit off_t where available */
! 29: #define _LARGEFILE_SOURCE
! 30: #define _FILE_OFFSET_BITS 64
1.14 anton 31:
1.1 anton 32: #include "config.h"
1.18 anton 33: #include <stdio.h>
1.35 anton 34: #include <sys/time.h>
35: #include <unistd.h>
1.1 anton 36:
1.32 anton 37: #if defined(DOUBLY_INDIRECT)||defined(INDIRECT_THREADED)||defined(VM_PROFILING)
38: #define NO_DYNAMIC
39: #endif
40:
1.1 anton 41: #if defined(DOUBLY_INDIRECT)
42: # undef DIRECT_THREADED
43: # undef INDIRECT_THREADED
44: # define INDIRECT_THREADED
45: #endif
46:
1.23 anton 47: #if defined(GFORTH_DEBUGGING)
48: # undef USE_TOS
49: # undef USE_FTOS
50: # define USE_NO_TOS
51: # define USE_NO_FTOS
52: #endif
53:
1.1 anton 54: #include <limits.h>
55:
56: #if defined(NeXT)
57: # include <libc.h>
58: #endif /* NeXT */
59:
60: /* symbol indexed constants */
61:
62: #define DOCOL 0
63: #define DOCON 1
64: #define DOVAR 2
65: #define DOUSER 3
66: #define DODEFER 4
67: #define DOFIELD 5
68: #define DODOES 6
69: #define DOESJUMP 7
70:
71: /* the size of the DOESJUMP, which resides between DOES> and the does-code */
72: #define DOES_HANDLER_SIZE (2*sizeof(Cell))
73:
1.6 jwilke 74: #include "machine.h"
1.1 anton 75:
76: /* Forth data types */
77: /* Cell and UCell must be the same size as a pointer */
78: #define CELL_BITS (sizeof(Cell) * CHAR_BIT)
79: #define FLAG(b) (-(b))
80: #define FILEIO(error) (FLAG(error) & -37)
81: #define FILEEXIST(error) (FLAG(error) & -38)
82:
83: #define F_TRUE (FLAG(0==0))
84: #define F_FALSE (FLAG(0!=0))
85:
86: #ifdef BUGGY_LONG_LONG
87: typedef struct {
88: Cell hi;
89: UCell lo;
90: } DCell;
91:
92: typedef struct {
93: UCell hi;
94: UCell lo;
95: } UDCell;
96:
1.36 ! anton 97: #define OFF2UD(o) ({UDCell _ud; _ud.hi=(o)>>CELL_BITS; _ud.lo=(Cell)(o); _ud;})
! 98: #define UD2OFF(ud) ({UDCell _ud=(ud); (((off_t)_ud.hi)<<CELL_BITS)+_ud.lo;})
1.1 anton 99: #define DZERO ((DCell){0,0})
100:
101: #else /* ! defined(BUGGY_LONG_LONG) */
102:
103: /* DCell and UDCell must be twice as large as Cell */
104: typedef DOUBLE_CELL_TYPE DCell;
105: typedef unsigned DOUBLE_CELL_TYPE UDCell;
106:
1.36 ! anton 107: #define OFF2UD(o) ((UDCell)(o))
! 108: #define UD2OFF(ud) ((off_t)(ud))
1.16 anton 109: #define DZERO ((DCell)0)
110:
111: #endif /* ! defined(BUGGY_LONG_LONG) */
112:
1.1 anton 113: typedef union {
114: struct {
1.16 anton 115: #if defined(WORDS_BIGENDIAN)||defined(BUGGY_LONG_LONG)
1.1 anton 116: Cell high;
117: UCell low;
118: #else
119: UCell low;
120: Cell high;
1.20 crook 121: #endif
1.1 anton 122: } cells;
1.16 anton 123: DCell d;
124: UDCell ud;
1.1 anton 125: } Double_Store;
126:
1.16 anton 127: #define FETCH_DCELL_T(d_,lo,hi,t_) ({ \
1.1 anton 128: Double_Store _d; \
129: _d.cells.low = (lo); \
130: _d.cells.high = (hi); \
1.16 anton 131: (d_) = _d.t_; \
1.1 anton 132: })
133:
1.16 anton 134: #define STORE_DCELL_T(d_,lo,hi,t_) ({ \
1.1 anton 135: Double_Store _d; \
1.16 anton 136: _d.t_ = (d_); \
1.1 anton 137: (lo) = _d.cells.low; \
138: (hi) = _d.cells.high; \
139: })
140:
1.28 anton 141: #define vm_twoCell2d(lo,hi,d_) FETCH_DCELL_T(d_,lo,hi,d);
142: #define vm_twoCell2ud(lo,hi,d_) FETCH_DCELL_T(d_,lo,hi,ud);
1.1 anton 143:
1.28 anton 144: #define vm_d2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,d);
145: #define vm_ud2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,ud);
1.1 anton 146:
147: typedef Label *Xt;
148:
149: /* PFA gives the parameter field address corresponding to a cfa */
150: #define PFA(cfa) (((Cell *)cfa)+2)
151: /* PFA1 is a special version for use just after a NEXT1 */
152: #define PFA1(cfa) PFA(cfa)
153: /* CODE_ADDRESS is the address of the code jumped to through the code field */
154: #define CODE_ADDRESS(cfa) (*(Xt)(cfa))
155:
156: /* DOES_CODE is the Forth code does jumps to */
157: #if !defined(DOUBLY_INDIRECT)
158: # define DOES_CA (symbols[DODOES])
159: #else /* defined(DOUBLY_INDIRECT) */
1.24 anton 160: # define DOES_CA ((Label)&xts[DODOES])
1.1 anton 161: #endif /* defined(DOUBLY_INDIRECT) */
162:
163:
164:
165: #define DOES_CODE(cfa) ({Xt _cfa=(Xt)(cfa); \
166: (Xt *)(_cfa[0]==DOES_CA ? _cfa[1] : NULL);})
167: #define DOES_CODE1(cfa) ((Xt *)(cfa[1]))
168: /* MAKE_CF creates an appropriate code field at the cfa;
169: ca is the code address */
170: #define MAKE_CF(cfa,ca) ((*(Label *)(cfa)) = ((Label)ca))
171: /* make a code field for a defining-word-defined word */
172: #define MAKE_DOES_CF(cfa,does_code) ({MAKE_CF(cfa,DOES_CA); \
173: ((Cell *)cfa)[1] = (Cell)(does_code);})
174: /* the does handler resides between DOES> and the following Forth code */
175: /* not needed in indirect threaded code */
176: #if defined(DOUBLY_INDIRECT)
177: #define MAKE_DOES_HANDLER(addr) MAKE_CF(addr, ((Label)&symbols[DOESJUMP]))
178: #else /* !defined(DOUBLY_INDIRECT) */
179: #define MAKE_DOES_HANDLER(addr) 0
180: #endif /* !defined(DOUBLY_INDIRECT) */
181:
1.29 anton 182: #ifdef GFORTH_DEBUGGING
1.30 anton 183: #define NAME(string) { saved_ip=ip; asm(""); }
184: /* the asm here is to avoid reordering of following stuff above the
185: assignment; this is an old-style asm (no operands), and therefore
186: is treated like "asm volatile ..."; i.e., it prevents most
187: reorderings across itself. We want the assignment above first,
188: because the stack loads may already cause a stack underflow. */
1.29 anton 189: #elif DEBUG
1.1 anton 190: # define NAME(string) fprintf(stderr,"%08lx: "string"\n",(Cell)ip);
191: #else
192: # define NAME(string)
193: #endif
194:
195: #define CF(const) (-const-2)
196:
197: #define CF_NIL -1
198:
199: #ifndef FLUSH_ICACHE
200: #warning flush-icache probably will not work (see manual)
201: # define FLUSH_ICACHE(addr,size)
202: #endif
203:
204: #ifdef USE_TOS
1.14 anton 205: #define IF_spTOS(x) x
1.1 anton 206: #else
1.14 anton 207: #define IF_spTOS(x)
208: #define spTOS (sp[0])
1.1 anton 209: #endif
210:
211: #ifdef USE_FTOS
1.14 anton 212: #define IF_fpTOS(x) x
1.1 anton 213: #else
1.14 anton 214: #define IF_fpTOS(x)
215: #define fpTOS (fp[0])
1.1 anton 216: #endif
217:
1.15 anton 218: #define IF_rpTOS(x)
219: #define rpTOS (rp[0])
220:
1.10 anton 221: typedef struct {
222: Address base; /* base address of image (0 if relocatable) */
223: UCell checksum; /* checksum of ca's to protect against some
224: incompatible binary/executable combinations
225: (0 if relocatable) */
226: UCell image_size; /* all sizes in bytes */
227: UCell dict_size;
228: UCell data_stack_size;
229: UCell fp_stack_size;
230: UCell return_stack_size;
231: UCell locals_stack_size;
232: Xt *boot_entry; /* initial ip for booting (in BOOT) */
233: Xt *throw_entry; /* ip after signal (in THROW) */
234: Cell unused1; /* possibly tib stack size */
1.24 anton 235: Label *xt_base; /* base of DOUBLE_INDIRECT xts[], for comp-i.fs */
1.10 anton 236: Address data_stack_base; /* this and the following fields are initialized by the loader */
237: Address fp_stack_base;
238: Address return_stack_base;
239: Address locals_stack_base;
240: } ImageHeader;
241: /* the image-header is created in main.fs */
242:
1.1 anton 243: Label *engine(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
244: Address my_alloc(Cell size);
1.35 anton 245: char *cstr(Char *from, UCell size, int clear);
1.11 anton 246: char *tilde_cstr(Char *from, UCell size, int clear);
1.35 anton 247: DCell timeval2us(struct timeval *tvp);
1.1 anton 248:
249: /* dblsub routines */
250: DCell dnegate(DCell d1);
251: UDCell ummul (UCell a, UCell b);
252: DCell mmul (Cell a, Cell b);
253: UDCell umdiv (UDCell u, UCell v);
254: DCell smdiv (DCell num, Cell denom);
255: DCell fmdiv (DCell num, Cell denom);
256:
1.7 pazsan 257: Cell memcasecmp(const Char *s1, const Char *s2, Cell n);
1.1 anton 258:
1.17 anton 259: /* peephole routines */
260:
261: Xt *primtable(Label symbols[], Cell size);
262: Cell prepare_peephole_table(Xt xts[]);
263: Xt peephole_opt(Xt xt1, Xt xt2, Cell peeptable);
1.18 anton 264: void vm_print_profile(FILE *file);
265: void vm_count_block(Xt *ip);
1.17 anton 266:
1.22 anton 267: /* dynamic superinstruction stuff */
268: Label compile_prim(Label prim);
1.33 anton 269: void compile_prim1(Cell *start);
270: void finish_code(void);
1.34 anton 271: int forget_dyncode(Address code);
272: Label decompile_code(Label prim);
1.17 anton 273:
1.1 anton 274: extern int offset_image;
1.5 anton 275: extern int die_on_signal;
1.10 anton 276: extern UCell pagesize;
277: extern ImageHeader *gforth_header;
1.19 anton 278: extern Label *vm_prims;
1.24 anton 279: extern Label *xts;
1.25 anton 280: extern Cell npriminfos;
1.2 pazsan 281:
1.31 anton 282: #ifdef HAS_DEBUG
283: extern int debug;
284: #else
285: # define debug 0
286: #endif
287:
1.35 anton 288: extern Cell *SP;
289: extern Float *FP;
290: extern Address UP;
291:
1.9 anton 292: #ifdef GFORTH_DEBUGGING
1.29 anton 293: extern Xt *saved_ip;
1.9 anton 294: extern Cell *rp;
1.35 anton 295: #endif
296:
297: #ifdef NO_IP
298: extern Label next_code;
299: #endif
300:
301: #ifdef HAS_FILE
302: extern char* fileattr[6];
303: extern char* pfileattr[6];
304: extern int ufileattr[6];
1.9 anton 305: #endif
306:
1.27 anton 307: #ifdef PRINT_SUPER_LENGTHS
308: Cell prim_length(Cell prim);
309: void print_super_lengths();
310: #endif
1.9 anton 311:
1.2 pazsan 312: /* declare all the functions that are missing */
313: #ifndef HAVE_ATANH
314: extern double atanh(double r1);
315: extern double asinh(double r1);
316: extern double acosh(double r1);
317: #endif
318: #ifndef HAVE_ECVT
1.4 anton 319: /* extern char* ecvt(double x, int len, int* exp, int* sign);*/
1.2 pazsan 320: #endif
321: #ifndef HAVE_MEMMOVE
1.3 anton 322: /* extern char *memmove(char *dest, const char *src, long n); */
1.2 pazsan 323: #endif
324: #ifndef HAVE_POW10
325: extern double pow10(double x);
326: #endif
327: #ifndef HAVE_STRERROR
328: extern char *strerror(int err);
329: #endif
330: #ifndef HAVE_STRSIGNAL
331: extern char *strsignal(int sig);
332: #endif
333: #ifndef HAVE_STRTOUL
1.3 anton 334: extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
1.2 pazsan 335: #endif
336:
337:
1.21 pazsan 338: #define GROUP(x)
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>