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