1: /* common header file
2:
3: Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.
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
19: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20: */
21:
22: #define _GNU_SOURCE
23:
24: #include "config.h"
25: #include <stdio.h>
26:
27: #if defined(DOUBLY_INDIRECT)
28: # undef DIRECT_THREADED
29: # undef INDIRECT_THREADED
30: # define INDIRECT_THREADED
31: #endif
32:
33: #if defined(GFORTH_DEBUGGING)
34: # undef USE_TOS
35: # undef USE_FTOS
36: # define USE_NO_TOS
37: # define USE_NO_FTOS
38: #endif
39:
40: #include <limits.h>
41:
42: #if defined(NeXT)
43: # include <libc.h>
44: #endif /* NeXT */
45:
46: /* symbol indexed constants */
47:
48: #define DOCOL 0
49: #define DOCON 1
50: #define DOVAR 2
51: #define DOUSER 3
52: #define DODEFER 4
53: #define DOFIELD 5
54: #define DODOES 6
55: #define DOESJUMP 7
56:
57: /* the size of the DOESJUMP, which resides between DOES> and the does-code */
58: #define DOES_HANDLER_SIZE (2*sizeof(Cell))
59:
60: #include "machine.h"
61:
62: /* Forth data types */
63: /* Cell and UCell must be the same size as a pointer */
64: #define CELL_BITS (sizeof(Cell) * CHAR_BIT)
65: #define FLAG(b) (-(b))
66: #define FILEIO(error) (FLAG(error) & -37)
67: #define FILEEXIST(error) (FLAG(error) & -38)
68:
69: #define F_TRUE (FLAG(0==0))
70: #define F_FALSE (FLAG(0!=0))
71:
72: #ifdef BUGGY_LONG_LONG
73: typedef struct {
74: Cell hi;
75: UCell lo;
76: } DCell;
77:
78: typedef struct {
79: UCell hi;
80: UCell lo;
81: } UDCell;
82:
83: #define LONG2UD(l) ({UDCell _ud; _ud.hi=0; _ud.lo=(Cell)(l); _ud;})
84: #define UD2LONG(ud) ((long)(ud.lo))
85: #define DZERO ((DCell){0,0})
86:
87: #else /* ! defined(BUGGY_LONG_LONG) */
88:
89: /* DCell and UDCell must be twice as large as Cell */
90: typedef DOUBLE_CELL_TYPE DCell;
91: typedef unsigned DOUBLE_CELL_TYPE UDCell;
92:
93: #define LONG2UD(l) ((UDCell)(l))
94: #define UD2LONG(ud) ((long)(ud))
95: #define DZERO ((DCell)0)
96:
97: #endif /* ! defined(BUGGY_LONG_LONG) */
98:
99: typedef union {
100: struct {
101: #if defined(WORDS_BIGENDIAN)||defined(BUGGY_LONG_LONG)
102: Cell high;
103: UCell low;
104: #else
105: UCell low;
106: Cell high;
107: #endif
108: } cells;
109: DCell d;
110: UDCell ud;
111: } Double_Store;
112:
113: #define FETCH_DCELL_T(d_,lo,hi,t_) ({ \
114: Double_Store _d; \
115: _d.cells.low = (lo); \
116: _d.cells.high = (hi); \
117: (d_) = _d.t_; \
118: })
119:
120: #define STORE_DCELL_T(d_,lo,hi,t_) ({ \
121: Double_Store _d; \
122: _d.t_ = (d_); \
123: (lo) = _d.cells.low; \
124: (hi) = _d.cells.high; \
125: })
126:
127: #define vm_twoCell2d(d_,lo,hi) FETCH_DCELL_T(d_,lo,hi,d)
128: #define vm_twoCell2ud(d_,lo,hi) FETCH_DCELL_T(d_,lo,hi,ud)
129:
130: #define vm_d2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,d)
131: #define vm_ud2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,ud)
132:
133: #ifdef DIRECT_THREADED
134: typedef Label Xt;
135: #else
136: typedef Label *Xt;
137: #endif
138:
139: #if !defined(DIRECT_THREADED)
140: /* i.e. indirect threaded our doubly indirect threaded */
141: /* the direct threaded version is machine dependent and resides in machine.h */
142:
143: /* PFA gives the parameter field address corresponding to a cfa */
144: #define PFA(cfa) (((Cell *)cfa)+2)
145: /* PFA1 is a special version for use just after a NEXT1 */
146: #define PFA1(cfa) PFA(cfa)
147: /* CODE_ADDRESS is the address of the code jumped to through the code field */
148: #define CODE_ADDRESS(cfa) (*(Xt)(cfa))
149:
150: /* DOES_CODE is the Forth code does jumps to */
151: #if !defined(DOUBLY_INDIRECT)
152: # define DOES_CA (symbols[DODOES])
153: #else /* defined(DOUBLY_INDIRECT) */
154: # define DOES_CA ((Label)&xts[DODOES])
155: #endif /* defined(DOUBLY_INDIRECT) */
156:
157:
158:
159: #define DOES_CODE(cfa) ({Xt _cfa=(Xt)(cfa); \
160: (Xt *)(_cfa[0]==DOES_CA ? _cfa[1] : NULL);})
161: #define DOES_CODE1(cfa) ((Xt *)(cfa[1]))
162: /* MAKE_CF creates an appropriate code field at the cfa;
163: ca is the code address */
164: #define MAKE_CF(cfa,ca) ((*(Label *)(cfa)) = ((Label)ca))
165: /* make a code field for a defining-word-defined word */
166: #define MAKE_DOES_CF(cfa,does_code) ({MAKE_CF(cfa,DOES_CA); \
167: ((Cell *)cfa)[1] = (Cell)(does_code);})
168: /* the does handler resides between DOES> and the following Forth code */
169: /* not needed in indirect threaded code */
170: #if defined(DOUBLY_INDIRECT)
171: #define MAKE_DOES_HANDLER(addr) MAKE_CF(addr, ((Label)&symbols[DOESJUMP]))
172: #else /* !defined(DOUBLY_INDIRECT) */
173: #define MAKE_DOES_HANDLER(addr) 0
174: #endif /* !defined(DOUBLY_INDIRECT) */
175: #endif /* !defined(DIRECT_THREADED) */
176:
177: #ifdef DEBUG
178: # define NAME(string) fprintf(stderr,"%08lx: "string"\n",(Cell)ip);
179: #else
180: # define NAME(string)
181: #endif
182:
183: #define CF(const) (-const-2)
184:
185: #define CF_NIL -1
186:
187: #ifndef FLUSH_ICACHE
188: #warning flush-icache probably will not work (see manual)
189: # define FLUSH_ICACHE(addr,size)
190: #endif
191:
192: #if defined(DIRECT_THREADED)
193: #define CACHE_FLUSH(addr,size) FLUSH_ICACHE(addr,size)
194: #else
195: #define CACHE_FLUSH(addr,size)
196: #endif
197:
198: #ifdef USE_TOS
199: #define IF_spTOS(x) x
200: #else
201: #define IF_spTOS(x)
202: #define spTOS (sp[0])
203: #endif
204:
205: #ifdef USE_FTOS
206: #define IF_fpTOS(x) x
207: #else
208: #define IF_fpTOS(x)
209: #define fpTOS (fp[0])
210: #endif
211:
212: #define IF_rpTOS(x)
213: #define rpTOS (rp[0])
214:
215: typedef struct {
216: Address base; /* base address of image (0 if relocatable) */
217: UCell checksum; /* checksum of ca's to protect against some
218: incompatible binary/executable combinations
219: (0 if relocatable) */
220: UCell image_size; /* all sizes in bytes */
221: UCell dict_size;
222: UCell data_stack_size;
223: UCell fp_stack_size;
224: UCell return_stack_size;
225: UCell locals_stack_size;
226: Xt *boot_entry; /* initial ip for booting (in BOOT) */
227: Xt *throw_entry; /* ip after signal (in THROW) */
228: Cell unused1; /* possibly tib stack size */
229: Label *xt_base; /* base of DOUBLE_INDIRECT xts[], for comp-i.fs */
230: Address data_stack_base; /* this and the following fields are initialized by the loader */
231: Address fp_stack_base;
232: Address return_stack_base;
233: Address locals_stack_base;
234: } ImageHeader;
235: /* the image-header is created in main.fs */
236:
237: Label *engine(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
238: Address my_alloc(Cell size);
239: char *tilde_cstr(Char *from, UCell size, int clear);
240:
241: /* dblsub routines */
242: DCell dnegate(DCell d1);
243: UDCell ummul (UCell a, UCell b);
244: DCell mmul (Cell a, Cell b);
245: UDCell umdiv (UDCell u, UCell v);
246: DCell smdiv (DCell num, Cell denom);
247: DCell fmdiv (DCell num, Cell denom);
248:
249: Cell memcasecmp(const Char *s1, const Char *s2, Cell n);
250:
251: /* peephole routines */
252:
253: Xt *primtable(Label symbols[], Cell size);
254: Cell prepare_peephole_table(Xt xts[]);
255: Xt peephole_opt(Xt xt1, Xt xt2, Cell peeptable);
256: void vm_print_profile(FILE *file);
257: void vm_count_block(Xt *ip);
258:
259: /* dynamic superinstruction stuff */
260: Label compile_prim(Label prim);
261:
262: extern int offset_image;
263: extern int die_on_signal;
264: extern UCell pagesize;
265: extern ImageHeader *gforth_header;
266: extern Label *vm_prims;
267: extern Label *xts;
268: extern Cell npriminfos;
269:
270: #ifdef GFORTH_DEBUGGING
271: extern Xt *ip;
272: extern Cell *rp;
273: #endif
274:
275:
276: /* declare all the functions that are missing */
277: #ifndef HAVE_ATANH
278: extern double atanh(double r1);
279: extern double asinh(double r1);
280: extern double acosh(double r1);
281: #endif
282: #ifndef HAVE_ECVT
283: /* extern char* ecvt(double x, int len, int* exp, int* sign);*/
284: #endif
285: #ifndef HAVE_MEMMOVE
286: /* extern char *memmove(char *dest, const char *src, long n); */
287: #endif
288: #ifndef HAVE_POW10
289: extern double pow10(double x);
290: #endif
291: #ifndef HAVE_STRERROR
292: extern char *strerror(int err);
293: #endif
294: #ifndef HAVE_STRSIGNAL
295: extern char *strsignal(int sig);
296: #endif
297: #ifndef HAVE_STRTOUL
298: extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
299: #endif
300:
301:
302: #define GROUP(x)
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>