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

1.1       anton       1: /* common header file
                      2: 
1.12      anton       3:   Copyright (C) 1995,1996,1997,1998,2000 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: 
1.14      anton      22: #define _GNU_SOURCE
                     23: 
1.1       anton      24: #include "config.h"
1.18      anton      25: #include <stdio.h>
1.1       anton      26: 
                     27: #if defined(DOUBLY_INDIRECT)
                     28: #  undef DIRECT_THREADED
                     29: #  undef INDIRECT_THREADED
                     30: #  define INDIRECT_THREADED
                     31: #endif
                     32: 
1.23      anton      33: #if defined(GFORTH_DEBUGGING)
                     34: #  undef USE_TOS
                     35: #  undef USE_FTOS
                     36: #  define USE_NO_TOS
                     37: #  define USE_NO_FTOS
                     38: #endif
                     39: 
1.1       anton      40: #include <limits.h>
                     41: 
                     42: #if defined(NeXT)
                     43: #  include <libc.h>
                     44: #endif /* NeXT */
                     45: 
                     46: /* symbol indexed constants */
                     47: 
                     48: #define DOCOL  0
                     49: #define DOCON  1
                     50: #define DOVAR  2
                     51: #define DOUSER 3
                     52: #define DODEFER        4
                     53: #define DOFIELD        5
                     54: #define DODOES 6
                     55: #define DOESJUMP       7
                     56: 
                     57: /* the size of the DOESJUMP, which resides between DOES> and the does-code */
                     58: #define DOES_HANDLER_SIZE      (2*sizeof(Cell))
                     59: 
1.6       jwilke     60: #include "machine.h"
1.1       anton      61: 
                     62: /* Forth data types */
                     63: /* Cell and UCell must be the same size as a pointer */
                     64: #define CELL_BITS      (sizeof(Cell) * CHAR_BIT)
                     65: #define FLAG(b) (-(b))
                     66: #define FILEIO(error)  (FLAG(error) & -37)
                     67: #define FILEEXIST(error)       (FLAG(error) & -38)
                     68: 
                     69: #define F_TRUE (FLAG(0==0))
                     70: #define F_FALSE (FLAG(0!=0))
                     71: 
                     72: #ifdef BUGGY_LONG_LONG
                     73: typedef struct {
                     74:   Cell hi;
                     75:   UCell lo;
                     76: } DCell;
                     77: 
                     78: typedef struct {
                     79:   UCell hi;
                     80:   UCell lo;
                     81: } UDCell;
                     82: 
                     83: #define LONG2UD(l)     ({UDCell _ud; _ud.hi=0; _ud.lo=(Cell)(l); _ud;})
                     84: #define UD2LONG(ud)    ((long)(ud.lo))
                     85: #define DZERO          ((DCell){0,0})
                     86: 
                     87: #else /* ! defined(BUGGY_LONG_LONG) */
                     88: 
                     89: /* DCell and UDCell must be twice as large as Cell */
                     90: typedef DOUBLE_CELL_TYPE DCell;
                     91: typedef unsigned DOUBLE_CELL_TYPE UDCell;
                     92: 
1.16      anton      93: #define LONG2UD(l)     ((UDCell)(l))
                     94: #define UD2LONG(ud)    ((long)(ud))
                     95: #define DZERO          ((DCell)0)
                     96: 
                     97: #endif /* ! defined(BUGGY_LONG_LONG) */
                     98: 
1.1       anton      99: typedef union {
                    100:   struct {
1.16      anton     101: #if defined(WORDS_BIGENDIAN)||defined(BUGGY_LONG_LONG)
1.1       anton     102:     Cell high;
                    103:     UCell low;
                    104: #else
                    105:     UCell low;
                    106:     Cell high;
1.20      crook     107: #endif
1.1       anton     108:   } cells;
1.16      anton     109:   DCell d;
                    110:   UDCell ud;
1.1       anton     111: } Double_Store;
                    112: 
1.16      anton     113: #define FETCH_DCELL_T(d_,lo,hi,t_)     ({ \
1.1       anton     114:                                     Double_Store _d; \
                    115:                                     _d.cells.low = (lo); \
                    116:                                     _d.cells.high = (hi); \
1.16      anton     117:                                     (d_) = _d.t_; \
1.1       anton     118:                                 })
                    119: 
1.16      anton     120: #define STORE_DCELL_T(d_,lo,hi,t_)     ({ \
1.1       anton     121:                                     Double_Store _d; \
1.16      anton     122:                                     _d.t_ = (d_); \
1.1       anton     123:                                     (lo) = _d.cells.low; \
                    124:                                     (hi) = _d.cells.high; \
                    125:                                 })
                    126: 
1.28      anton     127: #define vm_twoCell2d(lo,hi,d_)  FETCH_DCELL_T(d_,lo,hi,d);
                    128: #define vm_twoCell2ud(lo,hi,d_) FETCH_DCELL_T(d_,lo,hi,ud);
1.1       anton     129: 
1.28      anton     130: #define vm_d2twoCell(d_,lo,hi)  STORE_DCELL_T(d_,lo,hi,d);
                    131: #define vm_ud2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,ud);
1.1       anton     132: 
                    133: typedef Label *Xt;
                    134: 
                    135: /* PFA gives the parameter field address corresponding to a cfa */
                    136: #define PFA(cfa)       (((Cell *)cfa)+2)
                    137: /* PFA1 is a special version for use just after a NEXT1 */
                    138: #define PFA1(cfa)      PFA(cfa)
                    139: /* CODE_ADDRESS is the address of the code jumped to through the code field */
                    140: #define CODE_ADDRESS(cfa)      (*(Xt)(cfa))
                    141: 
                    142: /* DOES_CODE is the Forth code does jumps to */
                    143: #if !defined(DOUBLY_INDIRECT)
                    144: #  define DOES_CA (symbols[DODOES])
                    145: #else /* defined(DOUBLY_INDIRECT) */
1.24      anton     146: #  define DOES_CA ((Label)&xts[DODOES])
1.1       anton     147: #endif /* defined(DOUBLY_INDIRECT) */
                    148: 
                    149: 
                    150: 
                    151: #define DOES_CODE(cfa) ({Xt _cfa=(Xt)(cfa); \
                    152:                          (Xt *)(_cfa[0]==DOES_CA ? _cfa[1] : NULL);})
                    153: #define DOES_CODE1(cfa)        ((Xt *)(cfa[1]))
                    154: /* MAKE_CF creates an appropriate code field at the cfa;
                    155:    ca is the code address */
                    156: #define MAKE_CF(cfa,ca) ((*(Label *)(cfa)) = ((Label)ca))
                    157: /* make a code field for a defining-word-defined word */
                    158: #define MAKE_DOES_CF(cfa,does_code)  ({MAKE_CF(cfa,DOES_CA);   \
                    159:                                       ((Cell *)cfa)[1] = (Cell)(does_code);})
                    160: /* the does handler resides between DOES> and the following Forth code */
                    161: /* not needed in indirect threaded code */
                    162: #if defined(DOUBLY_INDIRECT)
                    163: #define MAKE_DOES_HANDLER(addr)        MAKE_CF(addr, ((Label)&symbols[DOESJUMP]))
                    164: #else /* !defined(DOUBLY_INDIRECT) */
                    165: #define MAKE_DOES_HANDLER(addr)        0
                    166: #endif /* !defined(DOUBLY_INDIRECT) */
                    167: 
1.29    ! anton     168: #ifdef GFORTH_DEBUGGING
        !           169: /* #define NAME(string) saved_ip=ip;*/
        !           170: #define NAME(string) *(Cell *)&saved_ip=(Cell)ip;
        !           171: /* the casting is there to get aliasing with loads at the start of the
        !           172:    primitives, so the store is not moved down across them; does not
        !           173:    work for FP-stack loads; ideally the -fno-strict-aliasing flag
        !           174:    should do this for us, but at least in gcc-2.95 it does not */
        !           175: #elif DEBUG
1.1       anton     176: #      define  NAME(string)    fprintf(stderr,"%08lx: "string"\n",(Cell)ip);
                    177: #else
                    178: #      define  NAME(string)
                    179: #endif
                    180: 
                    181: #define CF(const)      (-const-2)
                    182: 
                    183: #define CF_NIL -1
                    184: 
                    185: #ifndef FLUSH_ICACHE
                    186: #warning flush-icache probably will not work (see manual)
                    187: #      define FLUSH_ICACHE(addr,size)
                    188: #endif
                    189: 
                    190: #ifdef USE_TOS
1.14      anton     191: #define IF_spTOS(x) x
1.1       anton     192: #else
1.14      anton     193: #define IF_spTOS(x)
                    194: #define spTOS (sp[0])
1.1       anton     195: #endif
                    196: 
                    197: #ifdef USE_FTOS
1.14      anton     198: #define IF_fpTOS(x) x
1.1       anton     199: #else
1.14      anton     200: #define IF_fpTOS(x)
                    201: #define fpTOS (fp[0])
1.1       anton     202: #endif
                    203: 
1.15      anton     204: #define IF_rpTOS(x)
                    205: #define rpTOS (rp[0])
                    206: 
1.10      anton     207: typedef struct {
                    208:   Address base;                /* base address of image (0 if relocatable) */
                    209:   UCell checksum;      /* checksum of ca's to protect against some
                    210:                           incompatible binary/executable combinations
                    211:                           (0 if relocatable) */
                    212:   UCell image_size;    /* all sizes in bytes */
                    213:   UCell dict_size;
                    214:   UCell data_stack_size;
                    215:   UCell fp_stack_size;
                    216:   UCell return_stack_size;
                    217:   UCell locals_stack_size;
                    218:   Xt *boot_entry;      /* initial ip for booting (in BOOT) */
                    219:   Xt *throw_entry;     /* ip after signal (in THROW) */
                    220:   Cell unused1;                /* possibly tib stack size */
1.24      anton     221:   Label *xt_base;         /* base of DOUBLE_INDIRECT xts[], for comp-i.fs */
1.10      anton     222:   Address data_stack_base; /* this and the following fields are initialized by the loader */
                    223:   Address fp_stack_base;
                    224:   Address return_stack_base;
                    225:   Address locals_stack_base;
                    226: } ImageHeader;
                    227: /* the image-header is created in main.fs */
                    228: 
1.1       anton     229: Label *engine(Xt *ip, Cell *sp, Cell *rp, Float *fp, Address lp);
                    230: Address my_alloc(Cell size);
1.11      anton     231: char *tilde_cstr(Char *from, UCell size, int clear);
1.1       anton     232: 
                    233: /* dblsub routines */
                    234: DCell dnegate(DCell d1);
                    235: UDCell ummul (UCell a, UCell b);
                    236: DCell mmul (Cell a, Cell b);
                    237: UDCell umdiv (UDCell u, UCell v);
                    238: DCell smdiv (DCell num, Cell denom);
                    239: DCell fmdiv (DCell num, Cell denom);
                    240: 
1.7       pazsan    241: Cell memcasecmp(const Char *s1, const Char *s2, Cell n);
1.1       anton     242: 
1.17      anton     243: /* peephole routines */
                    244: 
                    245: Xt *primtable(Label symbols[], Cell size);
                    246: Cell prepare_peephole_table(Xt xts[]);
                    247: Xt peephole_opt(Xt xt1, Xt xt2, Cell peeptable);
1.18      anton     248: void vm_print_profile(FILE *file);
                    249: void vm_count_block(Xt *ip);
1.17      anton     250: 
1.22      anton     251: /* dynamic superinstruction stuff */
                    252: Label compile_prim(Label prim);
1.17      anton     253: 
1.1       anton     254: extern int offset_image;
1.5       anton     255: extern int die_on_signal;
1.10      anton     256: extern UCell pagesize;
                    257: extern ImageHeader *gforth_header;
1.19      anton     258: extern Label *vm_prims;
1.24      anton     259: extern Label *xts;
1.25      anton     260: extern Cell npriminfos;
1.2       pazsan    261: 
1.9       anton     262: #ifdef GFORTH_DEBUGGING
1.29    ! anton     263: extern Xt *saved_ip;
1.9       anton     264: extern Cell *rp;
                    265: #endif
                    266: 
1.27      anton     267: #ifdef PRINT_SUPER_LENGTHS
                    268: Cell prim_length(Cell prim);
                    269: void print_super_lengths();
                    270: #endif
1.9       anton     271: 
1.2       pazsan    272: /* declare all the functions that are missing */
                    273: #ifndef HAVE_ATANH
                    274: extern double atanh(double r1);
                    275: extern double asinh(double r1);
                    276: extern double acosh(double r1);
                    277: #endif
                    278: #ifndef HAVE_ECVT
1.4       anton     279: /* extern char* ecvt(double x, int len, int* exp, int* sign);*/
1.2       pazsan    280: #endif
                    281: #ifndef HAVE_MEMMOVE
1.3       anton     282: /* extern char *memmove(char *dest, const char *src, long n); */
1.2       pazsan    283: #endif
                    284: #ifndef HAVE_POW10
                    285: extern double pow10(double x);
                    286: #endif
                    287: #ifndef HAVE_STRERROR
                    288: extern char *strerror(int err);
                    289: #endif
                    290: #ifndef HAVE_STRSIGNAL
                    291: extern char *strsignal(int sig);
                    292: #endif
                    293: #ifndef HAVE_STRTOUL
1.3       anton     294: extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
1.2       pazsan    295: #endif
                    296: 
                    297: 
1.21      pazsan    298: #define GROUP(x)

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