Annotation of gforth/engine/forth.h, revision 1.65

1.1       anton       1: /* common header file
                      2: 
1.62      anton       3:   Copyright (C) 1995,1996,1997,1998,2000,2003,2004 Free Software Foundation, Inc.
1.1       anton       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
1.13      anton      19:   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1       anton      20: */
                     21: 
                     22: #include "config.h"
1.18      anton      23: #include <stdio.h>
1.35      anton      24: #include <sys/time.h>
                     25: #include <unistd.h>
1.1       anton      26: 
1.32      anton      27: #if defined(DOUBLY_INDIRECT)||defined(INDIRECT_THREADED)||defined(VM_PROFILING)
                     28: #define NO_DYNAMIC
                     29: #endif
                     30: 
1.1       anton      31: #if defined(DOUBLY_INDIRECT)
                     32: #  undef DIRECT_THREADED
                     33: #  undef INDIRECT_THREADED
                     34: #  define INDIRECT_THREADED
                     35: #endif
                     36: 
1.57      anton      37: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
1.23      anton      38: #  undef USE_TOS
                     39: #  undef USE_FTOS
1.56      anton      40: #  undef USE_NO_TOS
                     41: #  undef USE_NO_FTOS
1.23      anton      42: #  define USE_NO_TOS
                     43: #  define USE_NO_FTOS
1.57      anton      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"
1.58      anton      53: /* #define PROFILE_I "profile.i" */
1.57      anton      54: 
                     55: #else
                     56: /* gforth-fast or gforth-native */
1.59      anton      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
1.57      anton      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"
1.58      anton      72: /* profile.c uses profile.i but does not define VM_PROFILING */
                     73: /* #define PROFILE_I "profile-fast.i" */
1.57      anton      74: 
1.23      anton      75: #endif
1.57      anton      76: 
                     77: 
1.23      anton      78: 
1.1       anton      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: 
1.6       jwilke     99: #include "machine.h"
1.1       anton     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: 
1.65    ! pazsan    111: /* define this false if you want native division */
        !           112: #define FLOORED_DIV ((1%-3)>0)
        !           113: 
1.1       anton     114: #ifdef BUGGY_LONG_LONG
1.64      pazsan    115: 
                    116: #define BUGGY_LL_CMP    /* compares not possible */
                    117: #define BUGGY_LL_MUL    /* multiplication not possible */
                    118: #define BUGGY_LL_DIV    /* division not possible */
                    119: #define BUGGY_LL_ADD    /* addition not possible */
                    120: #define BUGGY_LL_SHIFT  /* shift not possible */
                    121: #define BUGGY_LL_D2F    /* to float not possible */
                    122: #define BUGGY_LL_F2D    /* from float not possible */
                    123: #define BUGGY_LL_SIZE   /* long long "too short", so we use something else */
                    124: 
1.1       anton     125: typedef struct {
                    126:   Cell hi;
                    127:   UCell lo;
                    128: } DCell;
                    129: 
                    130: typedef struct {
                    131:   UCell hi;
                    132:   UCell lo;
                    133: } UDCell;
                    134: 
1.64      pazsan    135: #define DHI(x) (x).hi
                    136: #define DLO(x) (x).lo
                    137: #define DHI_IS(x,y) (x).hi=(y)
                    138: #define DLO_IS(x,y) (x).lo=(y)
                    139: 
1.39      anton     140: #if SMALL_OFF_T
                    141: #define OFF2UD(o) ({UDCell _ud; _ud.hi=0; _ud.lo=(Cell)(o); _ud;})
                    142: #define UD2OFF(ud) ((ud).lo)
                    143: #else /* !SMALL_OFF_T */
                    144: #define OFF2UD(o) ({UDCell _ud; off_t _o=(o); _ud.hi=_o>>CELL_BITS; _ud.lo=(Cell)_o; _ud;})
1.36      anton     145: #define UD2OFF(ud) ({UDCell _ud=(ud); (((off_t)_ud.hi)<<CELL_BITS)+_ud.lo;})
1.39      anton     146: #endif /* !SMALL_OFF_T */
1.1       anton     147: #define DZERO          ((DCell){0,0})
                    148: 
                    149: #else /* ! defined(BUGGY_LONG_LONG) */
                    150: 
                    151: /* DCell and UDCell must be twice as large as Cell */
                    152: typedef DOUBLE_CELL_TYPE DCell;
1.63      pazsan    153: typedef DOUBLE_UCELL_TYPE UDCell;
1.1       anton     154: 
1.36      anton     155: #define OFF2UD(o)      ((UDCell)(o))
                    156: #define UD2OFF(ud)     ((off_t)(ud))
1.16      anton     157: #define DZERO          ((DCell)0)
                    158: 
                    159: #endif /* ! defined(BUGGY_LONG_LONG) */
                    160: 
1.1       anton     161: typedef union {
                    162:   struct {
1.16      anton     163: #if defined(WORDS_BIGENDIAN)||defined(BUGGY_LONG_LONG)
1.1       anton     164:     Cell high;
                    165:     UCell low;
                    166: #else
                    167:     UCell low;
                    168:     Cell high;
1.20      crook     169: #endif
1.1       anton     170:   } cells;
1.16      anton     171:   DCell d;
                    172:   UDCell ud;
1.1       anton     173: } Double_Store;
1.64      pazsan    174: 
                    175: #ifndef BUGGY_LONG_LONG
                    176: #define DHI(x) ({ Double_Store _d; _d.d=(x); _d.cells.high; })
                    177: #define DLO(x) ({ Double_Store _d; _d.d=(x); _d.cells.low;  })
                    178: 
                    179: /* beware with the assignment: x is referenced twice! */
                    180: #define DHI_IS(x,y) ({ Double_Store _d; _d.d=(x); _d.cells.high=(y); (x)=_d; })
                    181: #define DLO_IS(x,y) ({ Double_Store _d; _d.d=(x); _d.cells.low =(y); (x)=_d; })
                    182: #endif
1.1       anton     183: 
1.16      anton     184: #define FETCH_DCELL_T(d_,lo,hi,t_)     ({ \
1.1       anton     185:                                     Double_Store _d; \
                    186:                                     _d.cells.low = (lo); \
                    187:                                     _d.cells.high = (hi); \
1.16      anton     188:                                     (d_) = _d.t_; \
1.1       anton     189:                                 })
                    190: 
1.16      anton     191: #define STORE_DCELL_T(d_,lo,hi,t_)     ({ \
1.1       anton     192:                                     Double_Store _d; \
1.16      anton     193:                                     _d.t_ = (d_); \
1.1       anton     194:                                     (lo) = _d.cells.low; \
                    195:                                     (hi) = _d.cells.high; \
                    196:                                 })
                    197: 
1.28      anton     198: #define vm_twoCell2d(lo,hi,d_)  FETCH_DCELL_T(d_,lo,hi,d);
                    199: #define vm_twoCell2ud(lo,hi,d_) FETCH_DCELL_T(d_,lo,hi,ud);
1.1       anton     200: 
1.28      anton     201: #define vm_d2twoCell(d_,lo,hi)  STORE_DCELL_T(d_,lo,hi,d);
                    202: #define vm_ud2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,ud);
1.1       anton     203: 
                    204: typedef Label *Xt;
                    205: 
                    206: /* PFA gives the parameter field address corresponding to a cfa */
                    207: #define PFA(cfa)       (((Cell *)cfa)+2)
                    208: /* PFA1 is a special version for use just after a NEXT1 */
                    209: #define PFA1(cfa)      PFA(cfa)
                    210: /* CODE_ADDRESS is the address of the code jumped to through the code field */
                    211: #define CODE_ADDRESS(cfa)      (*(Xt)(cfa))
                    212: 
                    213: /* DOES_CODE is the Forth code does jumps to */
                    214: #if !defined(DOUBLY_INDIRECT)
                    215: #  define DOES_CA (symbols[DODOES])
                    216: #else /* defined(DOUBLY_INDIRECT) */
1.24      anton     217: #  define DOES_CA ((Label)&xts[DODOES])
1.1       anton     218: #endif /* defined(DOUBLY_INDIRECT) */
                    219: 
                    220: 
                    221: 
                    222: #define DOES_CODE1(cfa)        ((Xt *)(cfa[1]))
                    223: /* MAKE_CF creates an appropriate code field at the cfa;
                    224:    ca is the code address */
                    225: #define MAKE_CF(cfa,ca) ((*(Label *)(cfa)) = ((Label)ca))
                    226: /* make a code field for a defining-word-defined word */
                    227: #define MAKE_DOES_CF(cfa,does_code)  ({MAKE_CF(cfa,DOES_CA);   \
                    228:                                       ((Cell *)cfa)[1] = (Cell)(does_code);})
                    229: 
                    230: #define CF(const)      (-const-2)
                    231: 
                    232: #define CF_NIL -1
                    233: 
                    234: #ifndef FLUSH_ICACHE
                    235: #warning flush-icache probably will not work (see manual)
                    236: #      define FLUSH_ICACHE(addr,size)
1.45      anton     237: #warning no FLUSH_ICACHE, turning off dynamic native code by default
                    238: #undef NO_DYNAMIC_DEFAULT
                    239: #define NO_DYNAMIC_DEFAULT 1
1.1       anton     240: #endif
                    241: 
                    242: #ifdef USE_TOS
1.14      anton     243: #define IF_spTOS(x) x
1.1       anton     244: #else
1.14      anton     245: #define IF_spTOS(x)
                    246: #define spTOS (sp[0])
1.1       anton     247: #endif
                    248: 
                    249: #ifdef USE_FTOS
1.14      anton     250: #define IF_fpTOS(x) x
1.1       anton     251: #else
1.14      anton     252: #define IF_fpTOS(x)
                    253: #define fpTOS (fp[0])
1.1       anton     254: #endif
                    255: 
1.15      anton     256: #define IF_rpTOS(x)
                    257: #define rpTOS (rp[0])
                    258: 
1.10      anton     259: typedef struct {
                    260:   Address base;                /* base address of image (0 if relocatable) */
                    261:   UCell checksum;      /* checksum of ca's to protect against some
                    262:                           incompatible binary/executable combinations
                    263:                           (0 if relocatable) */
                    264:   UCell image_size;    /* all sizes in bytes */
                    265:   UCell dict_size;
                    266:   UCell data_stack_size;
                    267:   UCell fp_stack_size;
                    268:   UCell return_stack_size;
                    269:   UCell locals_stack_size;
                    270:   Xt *boot_entry;      /* initial ip for booting (in BOOT) */
                    271:   Xt *throw_entry;     /* ip after signal (in THROW) */
                    272:   Cell unused1;                /* possibly tib stack size */
1.24      anton     273:   Label *xt_base;         /* base of DOUBLE_INDIRECT xts[], for comp-i.fs */
1.10      anton     274:   Address data_stack_base; /* this and the following fields are initialized by the loader */
                    275:   Address fp_stack_base;
                    276:   Address return_stack_base;
                    277:   Address locals_stack_base;
                    278: } ImageHeader;
                    279: /* the image-header is created in main.fs */
                    280: 
1.48      anton     281: struct Longname {
                    282:   struct Longname *next;  /* the link field for old hands */
                    283:   Cell         countetc;
                    284:   char         name[0];
                    285: };
                    286: 
                    287: #define LONGNAME_COUNT(np)     ((np)->countetc & (((~((UCell)0))<<3)>>3))
                    288: 
                    289: struct Cellpair {
                    290:   Cell n1;
                    291:   Cell n2;
                    292: };
                    293: 
                    294: struct Cellquad {
                    295:   Cell n1;
                    296:   Cell n2;
                    297:   Cell n3;
                    298:   Cell n4;
                    299: };
1.49      anton     300: 
                    301: #define IOR(flag)      ((flag)? -512-errno : 0)
1.48      anton     302: 
1.1       anton     303: Label *engine(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
1.42      anton     304: Label *engine2(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
                    305: Label *engine3(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
1.48      anton     306: 
                    307: /* engine/prim support routines */
1.1       anton     308: Address my_alloc(Cell size);
1.35      anton     309: char *cstr(Char *from, UCell size, int clear);
1.11      anton     310: char *tilde_cstr(Char *from, UCell size, int clear);
1.35      anton     311: DCell timeval2us(struct timeval *tvp);
1.48      anton     312: void cmove(Char *c_from, Char *c_to, UCell u);
                    313: void cmove_up(Char *c_from, Char *c_to, UCell u);
                    314: Cell compare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
                    315: struct Longname *listlfind(Char *c_addr, UCell u, struct Longname *longname1);
                    316: struct Longname *hashlfind(Char *c_addr, UCell u, Cell *a_addr);
                    317: struct Longname *tablelfind(Char *c_addr, UCell u, Cell *a_addr);
                    318: UCell hashkey1(Char *c_addr, UCell u, UCell ubits);
                    319: struct Cellpair parse_white(Char *c_addr1, UCell u1);
                    320: Cell rename_file(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
                    321: struct Cellquad read_line(Char *c_addr, UCell u1, Cell wfileid);
                    322: struct Cellpair file_status(Char *c_addr, UCell u);
                    323: Cell to_float(Char *c_addr, UCell u, Float *rp);
                    324: Float v_star(Float *f_addr1, Cell nstride1, Float *f_addr2, Cell nstride2, UCell ucount);
                    325: void faxpy(Float ra, Float *f_x, Cell nstridex, Float *f_y, Cell nstridey, UCell ucount);
1.60      pazsan    326: UCell lshift(UCell u1, UCell n);
                    327: UCell rshift(UCell u1, UCell n);
1.61      anton     328: int gforth_system(Char *c_addr, UCell u);
1.60      pazsan    329: 
1.1       anton     330: 
1.50      anton     331: /* signal handler stuff */
                    332: void install_signal_handlers(void);
                    333: typedef void Sigfunc(int);
                    334: Sigfunc *bsd_signal(int signo, Sigfunc *func);
                    335: 
1.1       anton     336: /* dblsub routines */
                    337: DCell dnegate(DCell d1);
                    338: UDCell ummul (UCell a, UCell b);
                    339: DCell mmul (Cell a, Cell b);
                    340: UDCell umdiv (UDCell u, UCell v);
                    341: DCell smdiv (DCell num, Cell denom);
                    342: DCell fmdiv (DCell num, Cell denom);
                    343: 
1.7       pazsan    344: Cell memcasecmp(const Char *s1, const Char *s2, Cell n);
1.1       anton     345: 
1.18      anton     346: void vm_print_profile(FILE *file);
                    347: void vm_count_block(Xt *ip);
1.17      anton     348: 
1.22      anton     349: /* dynamic superinstruction stuff */
1.33      anton     350: void compile_prim1(Cell *start);
                    351: void finish_code(void);
1.34      anton     352: int forget_dyncode(Address code);
                    353: Label decompile_code(Label prim);
1.17      anton     354: 
1.1       anton     355: extern int offset_image;
1.5       anton     356: extern int die_on_signal;
1.10      anton     357: extern UCell pagesize;
                    358: extern ImageHeader *gforth_header;
1.19      anton     359: extern Label *vm_prims;
1.24      anton     360: extern Label *xts;
1.25      anton     361: extern Cell npriminfos;
1.2       pazsan    362: 
1.31      anton     363: #ifdef HAS_DEBUG
                    364: extern int debug;
                    365: #else
                    366: # define debug 0
                    367: #endif
                    368: 
1.35      anton     369: extern Cell *SP;
                    370: extern Float *FP;
                    371: extern Address UP;
                    372: 
1.53      pazsan    373: #ifdef HAS_FFCALL
                    374: extern Cell *RP;
                    375: extern Address LP;
1.54      pazsan    376: extern void engine_callback(Xt* fcall, void * alist);
1.53      pazsan    377: #endif
                    378: 
1.9       anton     379: #ifdef GFORTH_DEBUGGING
1.29      anton     380: extern Xt *saved_ip;
1.9       anton     381: extern Cell *rp;
1.35      anton     382: #endif
                    383: 
                    384: #ifdef NO_IP
                    385: extern Label next_code;
                    386: #endif
                    387: 
                    388: #ifdef HAS_FILE
                    389: extern char* fileattr[6];
                    390: extern char* pfileattr[6];
                    391: extern int ufileattr[6];
1.9       anton     392: #endif
                    393: 
1.27      anton     394: #ifdef PRINT_SUPER_LENGTHS
                    395: Cell prim_length(Cell prim);
                    396: void print_super_lengths();
                    397: #endif
1.9       anton     398: 
1.2       pazsan    399: /* declare all the functions that are missing */
                    400: #ifndef HAVE_ATANH
                    401: extern double atanh(double r1);
                    402: extern double asinh(double r1);
                    403: extern double acosh(double r1);
                    404: #endif
                    405: #ifndef HAVE_ECVT
1.4       anton     406: /* extern char* ecvt(double x, int len, int* exp, int* sign);*/
1.2       pazsan    407: #endif
                    408: #ifndef HAVE_MEMMOVE
1.3       anton     409: /* extern char *memmove(char *dest, const char *src, long n); */
1.2       pazsan    410: #endif
                    411: #ifndef HAVE_POW10
                    412: extern double pow10(double x);
                    413: #endif
                    414: #ifndef HAVE_STRERROR
                    415: extern char *strerror(int err);
                    416: #endif
                    417: #ifndef HAVE_STRSIGNAL
                    418: extern char *strsignal(int sig);
                    419: #endif
                    420: #ifndef HAVE_STRTOUL
1.3       anton     421: extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
1.2       pazsan    422: #endif
                    423: 
                    424: 
1.37      pazsan    425: #define GROUP(x, n)
1.53      pazsan    426: #define GROUPADD(n)

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