File:  [gforth] / gforth / engine / forth.h
Revision 1.57: download - view: text, annotated - select for diffs
Sun Nov 2 18:18:35 2003 UTC (20 years, 5 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Now the _fast.i files are included for gforth-fast and gforth-native

    1: /* common header file
    2: 
    3:   Copyright (C) 1995,1996,1997,1998,2000,2003 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 PROFILE_I "profile.i"
   50: #define PRIM_NUM_I "prim_num.i"
   51: #define PRIM_GRP_I "prim_grp.i"
   52: #define COSTS_I "costs.i"
   53: #define SUPER2_I "super2.i"
   54: 
   55: #else
   56: /* gforth-fast or gforth-native */
   57: 
   58: #define PRIM_I "prim-fast.i"
   59: #define PRIM_LAB_I "prim_lab-fast.i"
   60: #define PRIM_NAMES_I "prim_names-fast.i"
   61: #define PRIM_SUPEREND_I "prim_superend-fast.i"
   62: #define PROFILE_I "profile-fast.i"
   63: #define PRIM_NUM_I "prim_num-fast.i"
   64: #define PRIM_GRP_I "prim_grp-fast.i"
   65: #define COSTS_I "costs-fast.i"
   66: #define SUPER2_I "super2-fast.i"
   67: 
   68: #endif
   69: 
   70: 
   71: 
   72: #include <limits.h>
   73: 
   74: #if defined(NeXT)
   75: #  include <libc.h>
   76: #endif /* NeXT */
   77: 
   78: /* symbol indexed constants */
   79: 
   80: #define DOCOL	0
   81: #define DOCON	1
   82: #define DOVAR	2
   83: #define DOUSER	3
   84: #define DODEFER	4
   85: #define DOFIELD	5
   86: #define DODOES	6
   87: #define DOESJUMP	7
   88: 
   89: /* the size of the DOESJUMP, which resides between DOES> and the does-code */
   90: #define DOES_HANDLER_SIZE	(2*sizeof(Cell))
   91: 
   92: #include "machine.h"
   93: 
   94: /* Forth data types */
   95: /* Cell and UCell must be the same size as a pointer */
   96: #define CELL_BITS	(sizeof(Cell) * CHAR_BIT)
   97: #define FLAG(b) (-(b))
   98: #define FILEIO(error)	(FLAG(error) & -37)
   99: #define FILEEXIST(error)	(FLAG(error) & -38)
  100: 
  101: #define F_TRUE (FLAG(0==0))
  102: #define F_FALSE (FLAG(0!=0))
  103: 
  104: #ifdef BUGGY_LONG_LONG
  105: typedef struct {
  106:   Cell hi;
  107:   UCell lo;
  108: } DCell;
  109: 
  110: typedef struct {
  111:   UCell hi;
  112:   UCell lo;
  113: } UDCell;
  114: 
  115: #if SMALL_OFF_T
  116: #define OFF2UD(o) ({UDCell _ud; _ud.hi=0; _ud.lo=(Cell)(o); _ud;})
  117: #define UD2OFF(ud) ((ud).lo)
  118: #else /* !SMALL_OFF_T */
  119: #define OFF2UD(o) ({UDCell _ud; off_t _o=(o); _ud.hi=_o>>CELL_BITS; _ud.lo=(Cell)_o; _ud;})
  120: #define UD2OFF(ud) ({UDCell _ud=(ud); (((off_t)_ud.hi)<<CELL_BITS)+_ud.lo;})
  121: #endif /* !SMALL_OFF_T */
  122: #define DZERO		((DCell){0,0})
  123: 
  124: #else /* ! defined(BUGGY_LONG_LONG) */
  125: 
  126: /* DCell and UDCell must be twice as large as Cell */
  127: typedef DOUBLE_CELL_TYPE DCell;
  128: typedef unsigned DOUBLE_CELL_TYPE UDCell;
  129: 
  130: #define OFF2UD(o)	((UDCell)(o))
  131: #define UD2OFF(ud)	((off_t)(ud))
  132: #define DZERO		((DCell)0)
  133: 
  134: #endif /* ! defined(BUGGY_LONG_LONG) */
  135: 
  136: typedef union {
  137:   struct {
  138: #if defined(WORDS_BIGENDIAN)||defined(BUGGY_LONG_LONG)
  139:     Cell high;
  140:     UCell low;
  141: #else
  142:     UCell low;
  143:     Cell high;
  144: #endif
  145:   } cells;
  146:   DCell d;
  147:   UDCell ud;
  148: } Double_Store;
  149: 
  150: #define FETCH_DCELL_T(d_,lo,hi,t_)	({ \
  151: 				     Double_Store _d; \
  152: 				     _d.cells.low = (lo); \
  153: 				     _d.cells.high = (hi); \
  154: 				     (d_) = _d.t_; \
  155: 				 })
  156: 
  157: #define STORE_DCELL_T(d_,lo,hi,t_)	({ \
  158: 				     Double_Store _d; \
  159: 				     _d.t_ = (d_); \
  160: 				     (lo) = _d.cells.low; \
  161: 				     (hi) = _d.cells.high; \
  162: 				 })
  163: 
  164: #define vm_twoCell2d(lo,hi,d_)  FETCH_DCELL_T(d_,lo,hi,d);
  165: #define vm_twoCell2ud(lo,hi,d_) FETCH_DCELL_T(d_,lo,hi,ud);
  166: 
  167: #define vm_d2twoCell(d_,lo,hi)  STORE_DCELL_T(d_,lo,hi,d);
  168: #define vm_ud2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,ud);
  169: 
  170: typedef Label *Xt;
  171: 
  172: /* PFA gives the parameter field address corresponding to a cfa */
  173: #define PFA(cfa)	(((Cell *)cfa)+2)
  174: /* PFA1 is a special version for use just after a NEXT1 */
  175: #define PFA1(cfa)	PFA(cfa)
  176: /* CODE_ADDRESS is the address of the code jumped to through the code field */
  177: #define CODE_ADDRESS(cfa)	(*(Xt)(cfa))
  178: 
  179: /* DOES_CODE is the Forth code does jumps to */
  180: #if !defined(DOUBLY_INDIRECT)
  181: #  define DOES_CA (symbols[DODOES])
  182: #else /* defined(DOUBLY_INDIRECT) */
  183: #  define DOES_CA ((Label)&xts[DODOES])
  184: #endif /* defined(DOUBLY_INDIRECT) */
  185: 
  186: 
  187: 
  188: #define DOES_CODE1(cfa)	((Xt *)(cfa[1]))
  189: /* MAKE_CF creates an appropriate code field at the cfa;
  190:    ca is the code address */
  191: #define MAKE_CF(cfa,ca) ((*(Label *)(cfa)) = ((Label)ca))
  192: /* make a code field for a defining-word-defined word */
  193: #define MAKE_DOES_CF(cfa,does_code)  ({MAKE_CF(cfa,DOES_CA);	\
  194: 				       ((Cell *)cfa)[1] = (Cell)(does_code);})
  195: 
  196: #define CF(const)	(-const-2)
  197: 
  198: #define CF_NIL	-1
  199: 
  200: #ifndef FLUSH_ICACHE
  201: #warning flush-icache probably will not work (see manual)
  202: #	define FLUSH_ICACHE(addr,size)
  203: #warning no FLUSH_ICACHE, turning off dynamic native code by default
  204: #undef NO_DYNAMIC_DEFAULT
  205: #define NO_DYNAMIC_DEFAULT 1
  206: #endif
  207: 
  208: #ifdef USE_TOS
  209: #define IF_spTOS(x) x
  210: #else
  211: #define IF_spTOS(x)
  212: #define spTOS (sp[0])
  213: #endif
  214: 
  215: #ifdef USE_FTOS
  216: #define IF_fpTOS(x) x
  217: #else
  218: #define IF_fpTOS(x)
  219: #define fpTOS (fp[0])
  220: #endif
  221: 
  222: #define IF_rpTOS(x)
  223: #define rpTOS (rp[0])
  224: 
  225: typedef struct {
  226:   Address base;		/* base address of image (0 if relocatable) */
  227:   UCell checksum;	/* checksum of ca's to protect against some
  228: 			   incompatible	binary/executable combinations
  229: 			   (0 if relocatable) */
  230:   UCell image_size;	/* all sizes in bytes */
  231:   UCell dict_size;
  232:   UCell data_stack_size;
  233:   UCell fp_stack_size;
  234:   UCell return_stack_size;
  235:   UCell locals_stack_size;
  236:   Xt *boot_entry;	/* initial ip for booting (in BOOT) */
  237:   Xt *throw_entry;	/* ip after signal (in THROW) */
  238:   Cell unused1;		/* possibly tib stack size */
  239:   Label *xt_base;         /* base of DOUBLE_INDIRECT xts[], for comp-i.fs */
  240:   Address data_stack_base; /* this and the following fields are initialized by the loader */
  241:   Address fp_stack_base;
  242:   Address return_stack_base;
  243:   Address locals_stack_base;
  244: } ImageHeader;
  245: /* the image-header is created in main.fs */
  246: 
  247: struct Longname {
  248:   struct Longname *next;  /* the link field for old hands */
  249:   Cell		countetc;
  250:   char		name[0];
  251: };
  252: 
  253: #define LONGNAME_COUNT(np)	((np)->countetc & (((~((UCell)0))<<3)>>3))
  254: 
  255: struct Cellpair {
  256:   Cell n1;
  257:   Cell n2;
  258: };
  259: 
  260: struct Cellquad {
  261:   Cell n1;
  262:   Cell n2;
  263:   Cell n3;
  264:   Cell n4;
  265: };
  266: 
  267: #define IOR(flag)	((flag)? -512-errno : 0)
  268: 
  269: Label *engine(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
  270: Label *engine2(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
  271: Label *engine3(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
  272: 
  273: /* engine/prim support routines */
  274: Address my_alloc(Cell size);
  275: char *cstr(Char *from, UCell size, int clear);
  276: char *tilde_cstr(Char *from, UCell size, int clear);
  277: DCell timeval2us(struct timeval *tvp);
  278: void cmove(Char *c_from, Char *c_to, UCell u);
  279: void cmove_up(Char *c_from, Char *c_to, UCell u);
  280: Cell compare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
  281: struct Longname *listlfind(Char *c_addr, UCell u, struct Longname *longname1);
  282: struct Longname *hashlfind(Char *c_addr, UCell u, Cell *a_addr);
  283: struct Longname *tablelfind(Char *c_addr, UCell u, Cell *a_addr);
  284: UCell hashkey1(Char *c_addr, UCell u, UCell ubits);
  285: struct Cellpair parse_white(Char *c_addr1, UCell u1);
  286: Cell rename_file(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
  287: struct Cellquad read_line(Char *c_addr, UCell u1, Cell wfileid);
  288: struct Cellpair file_status(Char *c_addr, UCell u);
  289: Cell to_float(Char *c_addr, UCell u, Float *rp);
  290: Float v_star(Float *f_addr1, Cell nstride1, Float *f_addr2, Cell nstride2, UCell ucount);
  291: void faxpy(Float ra, Float *f_x, Cell nstridex, Float *f_y, Cell nstridey, UCell ucount);
  292: 
  293: /* signal handler stuff */
  294: void install_signal_handlers(void);
  295: typedef void Sigfunc(int);
  296: Sigfunc *bsd_signal(int signo, Sigfunc *func);
  297: 
  298: /* dblsub routines */
  299: DCell dnegate(DCell d1);
  300: UDCell ummul (UCell a, UCell b);
  301: DCell mmul (Cell a, Cell b);
  302: UDCell umdiv (UDCell u, UCell v);
  303: DCell smdiv (DCell num, Cell denom);
  304: DCell fmdiv (DCell num, Cell denom);
  305: 
  306: Cell memcasecmp(const Char *s1, const Char *s2, Cell n);
  307: 
  308: void vm_print_profile(FILE *file);
  309: void vm_count_block(Xt *ip);
  310: 
  311: /* dynamic superinstruction stuff */
  312: void compile_prim1(Cell *start);
  313: void finish_code(void);
  314: int forget_dyncode(Address code);
  315: Label decompile_code(Label prim);
  316: 
  317: extern int offset_image;
  318: extern int die_on_signal;
  319: extern UCell pagesize;
  320: extern ImageHeader *gforth_header;
  321: extern Label *vm_prims;
  322: extern Label *xts;
  323: extern Cell npriminfos;
  324: 
  325: #ifdef HAS_DEBUG
  326: extern int debug;
  327: #else
  328: # define debug 0
  329: #endif
  330: 
  331: extern Cell *SP;
  332: extern Float *FP;
  333: extern Address UP;
  334: 
  335: #ifdef HAS_FFCALL
  336: extern Cell *RP;
  337: extern Address LP;
  338: extern void engine_callback(Xt* fcall, void * alist);
  339: #endif
  340: 
  341: #ifdef GFORTH_DEBUGGING
  342: extern Xt *saved_ip;
  343: extern Cell *rp;
  344: #endif
  345: 
  346: #ifdef NO_IP
  347: extern Label next_code;
  348: #endif
  349: 
  350: #ifdef HAS_FILE
  351: extern char* fileattr[6];
  352: extern char* pfileattr[6];
  353: extern int ufileattr[6];
  354: #endif
  355: 
  356: #ifdef PRINT_SUPER_LENGTHS
  357: Cell prim_length(Cell prim);
  358: void print_super_lengths();
  359: #endif
  360: 
  361: /* declare all the functions that are missing */
  362: #ifndef HAVE_ATANH
  363: extern double atanh(double r1);
  364: extern double asinh(double r1);
  365: extern double acosh(double r1);
  366: #endif
  367: #ifndef HAVE_ECVT
  368: /* extern char* ecvt(double x, int len, int* exp, int* sign);*/
  369: #endif
  370: #ifndef HAVE_MEMMOVE
  371: /* extern char *memmove(char *dest, const char *src, long n); */
  372: #endif
  373: #ifndef HAVE_POW10
  374: extern double pow10(double x);
  375: #endif
  376: #ifndef HAVE_STRERROR
  377: extern char *strerror(int err);
  378: #endif
  379: #ifndef HAVE_STRSIGNAL
  380: extern char *strsignal(int sig);
  381: #endif
  382: #ifndef HAVE_STRTOUL
  383: extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
  384: #endif
  385: 
  386: 
  387: #define GROUP(x, n)
  388: #define GROUPADD(n)

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