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

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

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