File:  [gforth] / gforth / engine / forth.h
Revision 1.84: download - view: text, annotated - select for diffs
Sat Jul 7 21:46:05 2007 UTC (16 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
enabled fpTOS caching

    1: /* common header file
    2: 
    3:   Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006 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: #include "config.h"
   23: #include <stdio.h>
   24: #include <sys/time.h>
   25: #include <unistd.h>
   26: 
   27: #if defined(DOUBLY_INDIRECT)||defined(INDIRECT_THREADED)||defined(VM_PROFILING)
   28: #define NO_DYNAMIC
   29: #endif
   30: 
   31: #if defined(DOUBLY_INDIRECT)
   32: #  undef DIRECT_THREADED
   33: #  undef INDIRECT_THREADED
   34: #  define INDIRECT_THREADED
   35: #endif
   36: 
   37: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
   38: #  undef USE_TOS
   39: #  undef USE_FTOS
   40: #  undef USE_NO_TOS
   41: #  undef USE_NO_FTOS
   42: #  define USE_NO_TOS
   43: #  define USE_NO_FTOS
   44: 
   45: #define PRIM_I "prim.i"
   46: #define PRIM_LAB_I "prim_lab.i"
   47: #define PRIM_NAMES_I "prim_names.i"
   48: #define PRIM_SUPEREND_I "prim_superend.i"
   49: #define PRIM_NUM_I "prim_num.i"
   50: #define PRIM_GRP_I "prim_grp.i"
   51: #define COSTS_I "costs.i"
   52: #define SUPER2_I "super2.i"
   53: /* #define PROFILE_I "profile.i" */
   54: 
   55: #else
   56: /* gforth-fast or gforth-native */
   57: #  undef USE_TOS
   58: #  undef USE_FTOS
   59: #  undef USE_NO_TOS
   60: #  undef USE_NO_FTOS
   61: #  define USE_TOS
   62: 
   63: #define PRIM_I "prim-fast.i"
   64: #define PRIM_LAB_I "prim_lab-fast.i"
   65: #define PRIM_NAMES_I "prim_names-fast.i"
   66: #define PRIM_SUPEREND_I "prim_superend-fast.i"
   67: #define PRIM_NUM_I "prim_num-fast.i"
   68: #define PRIM_GRP_I "prim_grp-fast.i"
   69: #define COSTS_I "costs-fast.i"
   70: #define SUPER2_I "super2-fast.i"
   71: /* profile.c uses profile.i but does not define VM_PROFILING */
   72: /* #define PROFILE_I "profile-fast.i" */
   73: 
   74: #endif
   75: 
   76: 
   77: 
   78: #include <limits.h>
   79: 
   80: #if defined(NeXT)
   81: #  include <libc.h>
   82: #endif /* NeXT */
   83: 
   84: /* symbol indexed constants */
   85: 
   86: #define DOCOL	0
   87: #define DOCON	1
   88: #define DOVAR	2
   89: #define DOUSER	3
   90: #define DODEFER	4
   91: #define DOFIELD	5
   92: #define DODOES	6
   93: #define DOESJUMP	7
   94: 
   95: /* the size of the DOESJUMP, which resides between DOES> and the does-code */
   96: #define DOES_HANDLER_SIZE	(2*sizeof(Cell))
   97: 
   98: #include "machine.h"
   99: 
  100: /* C interface data types */
  101: 
  102: typedef WYDE_TYPE Wyde;
  103: typedef TETRABYTE_TYPE Tetrabyte;
  104: typedef unsigned WYDE_TYPE UWyde;
  105: typedef unsigned TETRABYTE_TYPE UTetrabyte;
  106: 
  107: /* Forth data types */
  108: /* Cell and UCell must be the same size as a pointer */
  109: #define CELL_BITS	(sizeof(Cell) * CHAR_BIT)
  110: #define CELL_MIN (((Cell)1)<<(sizeof(Cell)*CHAR_BIT-1))
  111: 
  112: #define HALFCELL_BITS	(CELL_BITS/2)
  113: #define HALFCELL_MASK   ((~(UCell)0)>>HALFCELL_BITS)
  114: #define UH(x)		(((UCell)(x))>>HALFCELL_BITS)
  115: #define LH(x)		((x)&HALFCELL_MASK)
  116: #define L2U(x)		(((UCell)(x))<<HALFCELL_BITS)
  117: #define HIGHBIT(x)	(((UCell)(x))>>(CELL_BITS-1))
  118: 
  119: #define FLAG(b) (-(b))
  120: #define FILEIO(error)	(FLAG(error) & -37)
  121: #define FILEEXIST(error)	(FLAG(error) & -38)
  122: 
  123: #define F_TRUE (FLAG(0==0))
  124: #define F_FALSE (FLAG(0!=0))
  125: 
  126: /* define this false if you want native division */
  127: #ifdef FORCE_CDIV
  128: #define FLOORED_DIV 0
  129: #else
  130: #define FLOORED_DIV ((1%-3)>0)
  131: #endif
  132: 
  133: #ifdef BUGGY_LONG_LONG
  134: 
  135: #define BUGGY_LL_CMP    /* compares not possible */
  136: #define BUGGY_LL_MUL    /* multiplication not possible */
  137: #define BUGGY_LL_DIV    /* division not possible */
  138: #define BUGGY_LL_ADD    /* addition not possible */
  139: #define BUGGY_LL_SHIFT  /* shift not possible */
  140: #define BUGGY_LL_D2F    /* to float not possible */
  141: #define BUGGY_LL_F2D    /* from float not possible */
  142: #define BUGGY_LL_SIZE   /* long long "too short", so we use something else */
  143: 
  144: typedef struct {
  145:   Cell hi;
  146:   UCell lo;
  147: } DCell;
  148: 
  149: typedef struct {
  150:   UCell hi;
  151:   UCell lo;
  152: } UDCell;
  153: 
  154: #define DHI(x) (x).hi
  155: #define DLO(x) (x).lo
  156: #define DHI_IS(x,y) (x).hi=(y)
  157: #define DLO_IS(x,y) (x).lo=(y)
  158: 
  159: #define UD2D(ud)	({UDCell _ud=(ud); (DCell){_ud.hi,_ud.lo};})
  160: #define D2UD(d)		({DCell _d1=(d); (UDCell){_d1.hi,_d1.lo};})
  161: 
  162: /* shifts by less than CELL_BITS */
  163: #define DLSHIFT(d,u)  ({DCell _d=(d); UCell _u=(u); \
  164:                        (DCell){(_d.hi<<_u)|(_d.lo>>(CELL_BITS-_u)),_d.lo<<_u};})
  165: #define UDLSHIFT(ud,u) D2UD(DLSHIFT(UD2D(ud),u))
  166: 
  167: #if SMALL_OFF_T
  168: #define OFF2UD(o) ({UDCell _ud; _ud.hi=0; _ud.lo=(Cell)(o); _ud;})
  169: #define UD2OFF(ud) ((ud).lo)
  170: #else /* !SMALL_OFF_T */
  171: #define OFF2UD(o) ({UDCell _ud; off_t _o=(o); _ud.hi=_o>>CELL_BITS; _ud.lo=(Cell)_o; _ud;})
  172: #define UD2OFF(ud) ({UDCell _ud=(ud); (((off_t)_ud.hi)<<CELL_BITS)+_ud.lo;})
  173: #endif /* !SMALL_OFF_T */
  174: #define DZERO		((DCell){0,0})
  175: 
  176: #else /* ! defined(BUGGY_LONG_LONG) */
  177: 
  178: /* DCell and UDCell must be twice as large as Cell */
  179: typedef DOUBLE_CELL_TYPE DCell;
  180: typedef DOUBLE_UCELL_TYPE UDCell;
  181: 
  182: #define OFF2UD(o)	((UDCell)(o))
  183: #define UD2OFF(ud)	((off_t)(ud))
  184: #define DZERO		((DCell)0)
  185: /* shifts by less than CELL_BITS */
  186: #define DLSHIFT(d,u)  ((d)<<(u))
  187: #define UDLSHIFT(d,u)  ((d)<<(u))
  188: 
  189: #endif /* ! defined(BUGGY_LONG_LONG) */
  190: 
  191: typedef union {
  192:   struct {
  193: #if defined(WORDS_BIGENDIAN)||defined(BUGGY_LONG_LONG)
  194:     Cell high;
  195:     UCell low;
  196: #else
  197:     UCell low;
  198:     Cell high;
  199: #endif
  200:   } cells;
  201:   DCell d;
  202:   UDCell ud;
  203: } Double_Store;
  204: 
  205: #ifndef BUGGY_LONG_LONG
  206: #define DHI(x) ({ Double_Store _d; _d.d=(x); _d.cells.high; })
  207: #define DLO(x) ({ Double_Store _d; _d.d=(x); _d.cells.low;  })
  208: 
  209: /* beware with the assignment: x is referenced twice! */
  210: #define DHI_IS(x,y) ({ Double_Store _d; _d.d=(x); _d.cells.high=(y); (x)=_d.d; })
  211: #define DLO_IS(x,y) ({ Double_Store _d; _d.d=(x); _d.cells.low =(y); (x)=_d.d; })
  212: 
  213: #define UD2D(ud)	((DCell)(ud))
  214: #define D2UD(d)		((UDCell)(d))
  215: #endif
  216: 
  217: #define FETCH_DCELL_T(d_,lo,hi,t_)	({ \
  218: 				     Double_Store _d; \
  219: 				     _d.cells.low = (lo); \
  220: 				     _d.cells.high = (hi); \
  221: 				     (d_) = _d.t_; \
  222: 				 })
  223: 
  224: #define STORE_DCELL_T(d_,lo,hi,t_)	({ \
  225: 				     Double_Store _d; \
  226: 				     _d.t_ = (d_); \
  227: 				     (lo) = _d.cells.low; \
  228: 				     (hi) = _d.cells.high; \
  229: 				 })
  230: 
  231: #define vm_twoCell2d(lo,hi,d_)  FETCH_DCELL_T(d_,lo,hi,d);
  232: #define vm_twoCell2ud(lo,hi,d_) FETCH_DCELL_T(d_,lo,hi,ud);
  233: 
  234: #define vm_d2twoCell(d_,lo,hi)  STORE_DCELL_T(d_,lo,hi,d);
  235: #define vm_ud2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,ud);
  236: 
  237: typedef Label *Xt;
  238: 
  239: /* PFA gives the parameter field address corresponding to a cfa */
  240: #define PFA(cfa)	(((Cell *)cfa)+2)
  241: /* PFA1 is a special version for use just after a NEXT1 */
  242: #define PFA1(cfa)	PFA(cfa)
  243: /* CODE_ADDRESS is the address of the code jumped to through the code field */
  244: #define CODE_ADDRESS(cfa)	(*(Xt)(cfa))
  245: 
  246: /* DOES_CODE is the Forth code does jumps to */
  247: #if !defined(DOUBLY_INDIRECT)
  248: #  define DOES_CA (symbols[DODOES])
  249: #else /* defined(DOUBLY_INDIRECT) */
  250: #  define DOES_CA ((Label)&xts[DODOES])
  251: #endif /* defined(DOUBLY_INDIRECT) */
  252: 
  253: 
  254: 
  255: #define DOES_CODE1(cfa)	((Xt *)(cfa[1]))
  256: /* MAKE_CF creates an appropriate code field at the cfa;
  257:    ca is the code address */
  258: #define MAKE_CF(cfa,ca) ((*(Label *)(cfa)) = ((Label)ca))
  259: /* make a code field for a defining-word-defined word */
  260: #define MAKE_DOES_CF(cfa,does_code)  ({MAKE_CF(cfa,DOES_CA);	\
  261: 				       ((Cell *)cfa)[1] = (Cell)(does_code);})
  262: 
  263: #define CF(const)	(-const-2)
  264: 
  265: #define CF_NIL	-1
  266: 
  267: #ifndef FLUSH_ICACHE
  268: #warning flush-icache probably will not work (see manual)
  269: #	define FLUSH_ICACHE(addr,size)
  270: #warning no FLUSH_ICACHE, turning off dynamic native code by default
  271: #undef NO_DYNAMIC_DEFAULT
  272: #define NO_DYNAMIC_DEFAULT 1
  273: #endif
  274: 
  275: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
  276: #define STACK_CACHE_DEFAULT 0
  277: #else
  278: #define STACK_CACHE_DEFAULT STACK_CACHE_DEFAULT_FAST
  279: #endif
  280: 
  281: #ifdef USE_FTOS
  282: #define IF_fpTOS(x) x
  283: #else
  284: #define IF_fpTOS(x)
  285: #define fpTOS (fp[0])
  286: #endif
  287: 
  288: #define IF_rpTOS(x)
  289: #define rpTOS (rp[0])
  290: 
  291: typedef struct {
  292:   Address base;		/* base address of image (0 if relocatable) */
  293:   UCell checksum;	/* checksum of ca's to protect against some
  294: 			   incompatible	binary/executable combinations
  295: 			   (0 if relocatable) */
  296:   UCell image_size;	/* all sizes in bytes */
  297:   UCell dict_size;
  298:   UCell data_stack_size;
  299:   UCell fp_stack_size;
  300:   UCell return_stack_size;
  301:   UCell locals_stack_size;
  302:   Xt *boot_entry;	/* initial ip for booting (in BOOT) */
  303:   Xt *throw_entry;	/* ip after signal (in THROW) */
  304:   Cell unused1;		/* possibly tib stack size */
  305:   Label *xt_base;         /* base of DOUBLE_INDIRECT xts[], for comp-i.fs */
  306:   Address data_stack_base; /* this and the following fields are initialized by the loader */
  307:   Address fp_stack_base;
  308:   Address return_stack_base;
  309:   Address locals_stack_base;
  310: } ImageHeader;
  311: /* the image-header is created in main.fs */
  312: 
  313: #ifdef HAS_F83HEADERSTRING
  314: struct F83Name {
  315:   struct F83Name *next;  /* the link field for old hands */
  316:   char		countetc;
  317:   char		name[0];
  318: };
  319: 
  320: #define F83NAME_COUNT(np)	((np)->countetc & 0x1f)
  321: #endif
  322: struct Longname {
  323:   struct Longname *next;  /* the link field for old hands */
  324:   Cell		countetc;
  325:   char		name[0];
  326: };
  327: 
  328: #define LONGNAME_COUNT(np)	((np)->countetc & (((~((UCell)0))<<3)>>3))
  329: 
  330: struct Cellpair {
  331:   Cell n1;
  332:   Cell n2;
  333: };
  334: 
  335: struct Cellquad {
  336:   Cell n1;
  337:   Cell n2;
  338:   Cell n3;
  339:   Cell n4;
  340: };
  341: 
  342: #define IOR(flag)	((flag)? -512-errno : 0)
  343: 
  344: Label *gforth_engine(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
  345: Label *gforth_engine2(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
  346: Label *gforth_engine3(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
  347: 
  348: /* engine/prim support routines */
  349: Address gforth_alloc(Cell size);
  350: char *cstr(Char *from, UCell size, int clear);
  351: char *tilde_cstr(Char *from, UCell size, int clear);
  352: DCell timeval2us(struct timeval *tvp);
  353: void cmove(Char *c_from, Char *c_to, UCell u);
  354: void cmove_up(Char *c_from, Char *c_to, UCell u);
  355: Cell compare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
  356: struct Longname *listlfind(Char *c_addr, UCell u, struct Longname *longname1);
  357: struct Longname *hashlfind(Char *c_addr, UCell u, Cell *a_addr);
  358: struct Longname *tablelfind(Char *c_addr, UCell u, Cell *a_addr);
  359: UCell hashkey1(Char *c_addr, UCell u, UCell ubits);
  360: struct Cellpair parse_white(Char *c_addr1, UCell u1);
  361: Cell rename_file(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
  362: struct Cellquad read_line(Char *c_addr, UCell u1, Cell wfileid);
  363: struct Cellpair file_status(Char *c_addr, UCell u);
  364: Cell to_float(Char *c_addr, UCell u, Float *rp);
  365: Float v_star(Float *f_addr1, Cell nstride1, Float *f_addr2, Cell nstride2, UCell ucount);
  366: void faxpy(Float ra, Float *f_x, Cell nstridex, Float *f_y, Cell nstridey, UCell ucount);
  367: UCell lshift(UCell u1, UCell n);
  368: UCell rshift(UCell u1, UCell n);
  369: int gforth_system(Char *c_addr, UCell u);
  370: void gforth_ms(UCell u);
  371: Cell capscompare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
  372: 
  373: /* signal handler stuff */
  374: void install_signal_handlers(void);
  375: void throw(int code);
  376: /* throw codes */
  377: #define BALL_DIVZERO     -10
  378: #define BALL_RESULTRANGE -11
  379: 
  380: typedef void Sigfunc(int);
  381: Sigfunc *bsd_signal(int signo, Sigfunc *func);
  382: 
  383: /* dblsub routines */
  384: DCell dnegate(DCell d1);
  385: UDCell ummul (UCell a, UCell b);
  386: DCell mmul (Cell a, Cell b);
  387: UDCell umdiv (UDCell u, UCell v);
  388: DCell smdiv (DCell num, Cell denom);
  389: DCell fmdiv (DCell num, Cell denom);
  390: 
  391: Cell memcasecmp(const Char *s1, const Char *s2, Cell n);
  392: 
  393: void vm_print_profile(FILE *file);
  394: void vm_count_block(Xt *ip);
  395: 
  396: /* dynamic superinstruction stuff */
  397: void compile_prim1(Cell *start);
  398: void finish_code(void);
  399: int forget_dyncode(Address code);
  400: Label decompile_code(Label prim);
  401: 
  402: extern int offset_image;
  403: extern int die_on_signal;
  404: extern int ignore_async_signals;
  405: extern UCell pagesize;
  406: extern ImageHeader *gforth_header;
  407: extern Label *vm_prims;
  408: extern Label *xts;
  409: extern Cell npriminfos;
  410: 
  411: #ifdef HAS_DEBUG
  412: extern int debug;
  413: #else
  414: # define debug 0
  415: #endif
  416: 
  417: extern Cell *gforth_SP;
  418: extern Float *gforth_FP;
  419: extern Address gforth_UP;
  420: 
  421: #ifdef HAS_FFCALL
  422: extern Cell *gforth_RP;
  423: extern Address gforth_LP;
  424: extern void gforth_callback(Xt* fcall, void * alist);
  425: #endif
  426: 
  427: #ifdef HAS_LIBFFI
  428: extern Cell *gforth_RP;
  429: extern Address gforth_LP;
  430: #include <ffi.h>
  431: extern void gforth_callback(ffi_cif * cif, void * resp, void ** args, void * ip);
  432: #endif
  433: 
  434: #ifdef GFORTH_DEBUGGING
  435: extern Xt *saved_ip;
  436: extern Cell *rp;
  437: #endif
  438: 
  439: #ifdef NO_IP
  440: extern Label next_code;
  441: #endif
  442: 
  443: #ifdef HAS_FILE
  444: extern char* fileattr[6];
  445: extern char* pfileattr[6];
  446: extern int ufileattr[6];
  447: #endif
  448: 
  449: #ifdef PRINT_SUPER_LENGTHS
  450: Cell prim_length(Cell prim);
  451: void print_super_lengths();
  452: #endif
  453: 
  454: /* declare all the functions that are missing */
  455: #ifndef HAVE_ATANH
  456: extern double atanh(double r1);
  457: extern double asinh(double r1);
  458: extern double acosh(double r1);
  459: #endif
  460: #ifndef HAVE_ECVT
  461: /* extern char* ecvt(double x, int len, int* exp, int* sign);*/
  462: #endif
  463: #ifndef HAVE_MEMMOVE
  464: /* extern char *memmove(char *dest, const char *src, long n); */
  465: #endif
  466: #ifndef HAVE_POW10
  467: extern double pow10(double x);
  468: #endif
  469: #ifndef HAVE_STRERROR
  470: extern char *strerror(int err);
  471: #endif
  472: #ifndef HAVE_STRSIGNAL
  473: extern char *strsignal(int sig);
  474: #endif
  475: #ifndef HAVE_STRTOUL
  476: extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
  477: #endif
  478: 
  479: #define GROUP(x, n)
  480: #define GROUPADD(n)

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