File:  [gforth] / gforth / engine / forth.h
Revision 1.12: download - view: text, annotated - select for diffs
Sat Sep 23 15:06:08 2000 UTC (21 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright dates in many files (not in ec-related files)

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>