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

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

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