File:  [gforth] / gforth / engine / forth.h
Revision 1.36: download - view: text, annotated - select for diffs
Tue Dec 24 23:40:30 2002 UTC (21 years, 4 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Gforth now supports large files (>2GB) on small machines (32-bits/cell).
forth.h now asks for all kinds of POSIX, X/Open, and GNU support.
rearranged include files such that forth.h precedes the system 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: */
   21: 
   22: /* turn on all POSIX, SUSv3, and GNU features if available */
   23: #define _GNU_SOURCE
   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
   31: 
   32: #include "config.h"
   33: #include <stdio.h>
   34: #include <sys/time.h>
   35: #include <unistd.h>
   36: 
   37: #if defined(DOUBLY_INDIRECT)||defined(INDIRECT_THREADED)||defined(VM_PROFILING)
   38: #define NO_DYNAMIC
   39: #endif
   40: 
   41: #if defined(DOUBLY_INDIRECT)
   42: #  undef DIRECT_THREADED
   43: #  undef INDIRECT_THREADED
   44: #  define INDIRECT_THREADED
   45: #endif
   46: 
   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: 
   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: 
   74: #include "machine.h"
   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: 
   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;})
   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: 
  107: #define OFF2UD(o)	((UDCell)(o))
  108: #define UD2OFF(ud)	((off_t)(ud))
  109: #define DZERO		((DCell)0)
  110: 
  111: #endif /* ! defined(BUGGY_LONG_LONG) */
  112: 
  113: typedef union {
  114:   struct {
  115: #if defined(WORDS_BIGENDIAN)||defined(BUGGY_LONG_LONG)
  116:     Cell high;
  117:     UCell low;
  118: #else
  119:     UCell low;
  120:     Cell high;
  121: #endif
  122:   } cells;
  123:   DCell d;
  124:   UDCell ud;
  125: } Double_Store;
  126: 
  127: #define FETCH_DCELL_T(d_,lo,hi,t_)	({ \
  128: 				     Double_Store _d; \
  129: 				     _d.cells.low = (lo); \
  130: 				     _d.cells.high = (hi); \
  131: 				     (d_) = _d.t_; \
  132: 				 })
  133: 
  134: #define STORE_DCELL_T(d_,lo,hi,t_)	({ \
  135: 				     Double_Store _d; \
  136: 				     _d.t_ = (d_); \
  137: 				     (lo) = _d.cells.low; \
  138: 				     (hi) = _d.cells.high; \
  139: 				 })
  140: 
  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);
  143: 
  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);
  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) */
  160: #  define DOES_CA ((Label)&xts[DODOES])
  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: 
  182: #ifdef GFORTH_DEBUGGING
  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. */
  189: #elif DEBUG
  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
  205: #define IF_spTOS(x) x
  206: #else
  207: #define IF_spTOS(x)
  208: #define spTOS (sp[0])
  209: #endif
  210: 
  211: #ifdef USE_FTOS
  212: #define IF_fpTOS(x) x
  213: #else
  214: #define IF_fpTOS(x)
  215: #define fpTOS (fp[0])
  216: #endif
  217: 
  218: #define IF_rpTOS(x)
  219: #define rpTOS (rp[0])
  220: 
  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 */
  235:   Label *xt_base;         /* base of DOUBLE_INDIRECT xts[], for comp-i.fs */
  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: 
  243: Label *engine(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
  244: Address my_alloc(Cell size);
  245: char *cstr(Char *from, UCell size, int clear);
  246: char *tilde_cstr(Char *from, UCell size, int clear);
  247: DCell timeval2us(struct timeval *tvp);
  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: 
  257: Cell memcasecmp(const Char *s1, const Char *s2, Cell n);
  258: 
  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);
  264: void vm_print_profile(FILE *file);
  265: void vm_count_block(Xt *ip);
  266: 
  267: /* dynamic superinstruction stuff */
  268: Label compile_prim(Label prim);
  269: void compile_prim1(Cell *start);
  270: void finish_code(void);
  271: int forget_dyncode(Address code);
  272: Label decompile_code(Label prim);
  273: 
  274: extern int offset_image;
  275: extern int die_on_signal;
  276: extern UCell pagesize;
  277: extern ImageHeader *gforth_header;
  278: extern Label *vm_prims;
  279: extern Label *xts;
  280: extern Cell npriminfos;
  281: 
  282: #ifdef HAS_DEBUG
  283: extern int debug;
  284: #else
  285: # define debug 0
  286: #endif
  287: 
  288: extern Cell *SP;
  289: extern Float *FP;
  290: extern Address UP;
  291: 
  292: #ifdef GFORTH_DEBUGGING
  293: extern Xt *saved_ip;
  294: extern Cell *rp;
  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];
  305: #endif
  306: 
  307: #ifdef PRINT_SUPER_LENGTHS
  308: Cell prim_length(Cell prim);
  309: void print_super_lengths();
  310: #endif
  311: 
  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
  319: /* extern char* ecvt(double x, int len, int* exp, int* sign);*/
  320: #endif
  321: #ifndef HAVE_MEMMOVE
  322: /* extern char *memmove(char *dest, const char *src, long n); */
  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
  334: extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
  335: #endif
  336: 
  337: 
  338: #define GROUP(x)

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