File:  [gforth] / gforth / engine / forth.h
Revision 1.64: download - view: text, annotated - select for diffs
Wed Jan 19 22:11:53 2005 UTC (19 years, 2 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added more detailed BUGGY_LL_* flags

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

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