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