File:  [gforth] / gforth / engine / forth.h
Revision 1.92: download - view: text, annotated - select for diffs
Sun Jan 13 22:13:08 2008 UTC (16 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
tried to use explicit register allocation for local saved_regs_p, but
  it does not seem to work.

    1: /* common header file
    2: 
    3:   Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007 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 3
   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, see http://www.gnu.org/licenses/.
   19: */
   20: 
   21: #include "config.h"
   22: #include "128bit.h"
   23: #include <stdio.h>
   24: #include <sys/time.h>
   25: #include <unistd.h>
   26: #include <ltdl.h>
   27: 
   28: #if defined(DOUBLY_INDIRECT)||defined(INDIRECT_THREADED)||defined(VM_PROFILING)
   29: #define NO_DYNAMIC
   30: #endif
   31: 
   32: #if defined(DOUBLY_INDIRECT)
   33: #  undef DIRECT_THREADED
   34: #  undef INDIRECT_THREADED
   35: #  define INDIRECT_THREADED
   36: #endif
   37: 
   38: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
   39: #  undef USE_TOS
   40: #  undef USE_FTOS
   41: #  undef USE_NO_TOS
   42: #  undef USE_NO_FTOS
   43: #  define USE_NO_TOS
   44: #  define USE_NO_FTOS
   45: 
   46: #define PRIM_I "prim.i"
   47: #define PRIM_LAB_I "prim_lab.i"
   48: #define PRIM_NAMES_I "prim_names.i"
   49: #define PRIM_SUPEREND_I "prim_superend.i"
   50: #define PRIM_NUM_I "prim_num.i"
   51: #define PRIM_GRP_I "prim_grp.i"
   52: #define COSTS_I "costs.i"
   53: #define SUPER2_I "super2.i"
   54: /* #define PROFILE_I "profile.i" */
   55: 
   56: #else
   57: /* gforth-fast or gforth-native */
   58: #  undef USE_TOS
   59: #  undef USE_FTOS
   60: #  undef USE_NO_TOS
   61: #  undef USE_NO_FTOS
   62: #  define USE_TOS
   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 DOVAL	6
   94: #define DODOES	7
   95: #define DOESJUMP	8
   96: 
   97: /* the size of the DOESJUMP, which resides between DOES> and the does-code */
   98: #define DOES_HANDLER_SIZE	(2*sizeof(Cell))
   99: 
  100: #include "machine.h"
  101: 
  102: /* C interface data types */
  103: 
  104: typedef WYDE_TYPE Wyde;
  105: typedef TETRABYTE_TYPE Tetrabyte;
  106: typedef unsigned WYDE_TYPE UWyde;
  107: typedef unsigned TETRABYTE_TYPE UTetrabyte;
  108: 
  109: /* Forth data types */
  110: /* Cell and UCell must be the same size as a pointer */
  111: #define CELL_BITS	(sizeof(Cell) * CHAR_BIT)
  112: #define CELL_MIN (((Cell)1)<<(sizeof(Cell)*CHAR_BIT-1))
  113: 
  114: #define HALFCELL_BITS	(CELL_BITS/2)
  115: #define HALFCELL_MASK   ((~(UCell)0)>>HALFCELL_BITS)
  116: #define UH(x)		(((UCell)(x))>>HALFCELL_BITS)
  117: #define LH(x)		((x)&HALFCELL_MASK)
  118: #define L2U(x)		(((UCell)(x))<<HALFCELL_BITS)
  119: #define HIGHBIT(x)	(((UCell)(x))>>(CELL_BITS-1))
  120: 
  121: #define FLAG(b) (-(b))
  122: #define FILEIO(error)	(FLAG(error) & -37)
  123: #define FILEEXIST(error)	(FLAG(error) & -38)
  124: 
  125: #define F_TRUE (FLAG(0==0))
  126: #define F_FALSE (FLAG(0!=0))
  127: 
  128: /* define this false if you want native division */
  129: #ifdef FORCE_CDIV
  130: #define FLOORED_DIV 0
  131: #else
  132: #define FLOORED_DIV ((1%-3)>0)
  133: #endif
  134: 
  135: #ifdef BUGGY_LONG_LONG
  136: 
  137: #define BUGGY_LL_CMP    /* compares not possible */
  138: #define BUGGY_LL_MUL    /* multiplication not possible */
  139: #define BUGGY_LL_DIV    /* division not possible */
  140: #define BUGGY_LL_ADD    /* addition not possible */
  141: #define BUGGY_LL_SHIFT  /* shift not possible */
  142: #define BUGGY_LL_D2F    /* to float not possible */
  143: #define BUGGY_LL_F2D    /* from float not possible */
  144: #define BUGGY_LL_SIZE   /* long long "too short", so we use something else */
  145: 
  146: typedef struct {
  147:   Cell hi;
  148:   UCell lo;
  149: } DCell;
  150: 
  151: typedef struct {
  152:   UCell hi;
  153:   UCell lo;
  154: } UDCell;
  155: 
  156: #define DHI(x) (x).hi
  157: #define DLO(x) (x).lo
  158: #define DHI_IS(x,y) (x).hi=(y)
  159: #define DLO_IS(x,y) (x).lo=(y)
  160: 
  161: #define UD2D(ud)	({UDCell _ud=(ud); (DCell){_ud.hi,_ud.lo};})
  162: #define D2UD(d)		({DCell _d1=(d); (UDCell){_d1.hi,_d1.lo};})
  163: 
  164: /* shifts by less than CELL_BITS */
  165: #define DLSHIFT(d,u)  ({DCell _d=(d); UCell _u=(u); \
  166:                        (DCell){(_d.hi<<_u)|(_d.lo>>(CELL_BITS-_u)),_d.lo<<_u};})
  167: #define UDLSHIFT(ud,u) D2UD(DLSHIFT(UD2D(ud),u))
  168: 
  169: #if SMALL_OFF_T
  170: #define OFF2UD(o) ({UDCell _ud; _ud.hi=0; _ud.lo=(Cell)(o); _ud;})
  171: #define UD2OFF(ud) ((ud).lo)
  172: #else /* !SMALL_OFF_T */
  173: #define OFF2UD(o) ({UDCell _ud; off_t _o=(o); _ud.hi=_o>>CELL_BITS; _ud.lo=(Cell)_o; _ud;})
  174: #define UD2OFF(ud) ({UDCell _ud=(ud); (((off_t)_ud.hi)<<CELL_BITS)+_ud.lo;})
  175: #endif /* !SMALL_OFF_T */
  176: #define DZERO		((DCell){0,0})
  177: 
  178: #else /* ! defined(BUGGY_LONG_LONG) */
  179: 
  180: /* DCell and UDCell must be twice as large as Cell */
  181: typedef DOUBLE_CELL_TYPE DCell;
  182: typedef DOUBLE_UCELL_TYPE UDCell;
  183: 
  184: #define OFF2UD(o)	((UDCell)(o))
  185: #define UD2OFF(ud)	((off_t)(ud))
  186: #define DZERO		((DCell)0)
  187: /* shifts by less than CELL_BITS */
  188: #define DLSHIFT(d,u)  ((d)<<(u))
  189: #define UDLSHIFT(d,u)  ((d)<<(u))
  190: 
  191: #endif /* ! defined(BUGGY_LONG_LONG) */
  192: 
  193: typedef union {
  194:   struct {
  195: #if defined(WORDS_BIGENDIAN)||defined(BUGGY_LONG_LONG)
  196:     Cell high;
  197:     UCell low;
  198: #else
  199:     UCell low;
  200:     Cell high;
  201: #endif
  202:   } cells;
  203:   DCell d;
  204:   UDCell ud;
  205: } Double_Store;
  206: 
  207: #ifndef BUGGY_LONG_LONG
  208: #define DHI(x) ({ Double_Store _d; _d.d=(x); _d.cells.high; })
  209: #define DLO(x) ({ Double_Store _d; _d.d=(x); _d.cells.low;  })
  210: 
  211: /* beware with the assignment: x is referenced twice! */
  212: #define DHI_IS(x,y) ({ Double_Store _d; _d.d=(x); _d.cells.high=(y); (x)=_d.d; })
  213: #define DLO_IS(x,y) ({ Double_Store _d; _d.d=(x); _d.cells.low =(y); (x)=_d.d; })
  214: 
  215: #define UD2D(ud)	((DCell)(ud))
  216: #define D2UD(d)		((UDCell)(d))
  217: #endif
  218: 
  219: #define FETCH_DCELL_T(d_,lo,hi,t_)	({ \
  220: 				     Double_Store _d; \
  221: 				     _d.cells.low = (lo); \
  222: 				     _d.cells.high = (hi); \
  223: 				     (d_) = _d.t_; \
  224: 				 })
  225: 
  226: #define STORE_DCELL_T(d_,lo,hi,t_)	({ \
  227: 				     Double_Store _d; \
  228: 				     _d.t_ = (d_); \
  229: 				     (lo) = _d.cells.low; \
  230: 				     (hi) = _d.cells.high; \
  231: 				 })
  232: 
  233: #define vm_twoCell2d(lo,hi,d_)  FETCH_DCELL_T(d_,lo,hi,d);
  234: #define vm_twoCell2ud(lo,hi,d_) FETCH_DCELL_T(d_,lo,hi,ud);
  235: 
  236: #define vm_d2twoCell(d_,lo,hi)  STORE_DCELL_T(d_,lo,hi,d);
  237: #define vm_ud2twoCell(d_,lo,hi) STORE_DCELL_T(d_,lo,hi,ud);
  238: 
  239: typedef Label *Xt;
  240: 
  241: /* PFA gives the parameter field address corresponding to a cfa */
  242: #define PFA(cfa)	(((Cell *)cfa)+2)
  243: /* PFA1 is a special version for use just after a NEXT1 */
  244: #define PFA1(cfa)	PFA(cfa)
  245: /* CODE_ADDRESS is the address of the code jumped to through the code field */
  246: #define CODE_ADDRESS(cfa)	(*(Xt)(cfa))
  247: 
  248: /* DOES_CODE is the Forth code does jumps to */
  249: #if !defined(DOUBLY_INDIRECT)
  250: #  define DOES_CA (symbols[DODOES])
  251: #else /* defined(DOUBLY_INDIRECT) */
  252: #  define DOES_CA ((Label)&xts[DODOES])
  253: #endif /* defined(DOUBLY_INDIRECT) */
  254: 
  255: 
  256: 
  257: #define DOES_CODE1(cfa)	((Xt *)(cfa[1]))
  258: /* MAKE_CF creates an appropriate code field at the cfa;
  259:    ca is the code address */
  260: #define MAKE_CF(cfa,ca) ((*(Label *)(cfa)) = ((Label)ca))
  261: /* make a code field for a defining-word-defined word */
  262: #define MAKE_DOES_CF(cfa,does_code)  ({MAKE_CF(cfa,DOES_CA);	\
  263: 				       ((Cell *)cfa)[1] = (Cell)(does_code);})
  264: 
  265: #define CF(const)	(-const-2)
  266: 
  267: #define CF_NIL	-1
  268: 
  269: #ifndef FLUSH_ICACHE
  270: #warning flush-icache probably will not work (see manual)
  271: #	define FLUSH_ICACHE(addr,size)
  272: #warning no FLUSH_ICACHE, turning off dynamic native code by default
  273: #undef NO_DYNAMIC_DEFAULT
  274: #define NO_DYNAMIC_DEFAULT 1
  275: #endif
  276: 
  277: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
  278: #define STACK_CACHE_DEFAULT 0
  279: #else
  280: #define STACK_CACHE_DEFAULT STACK_CACHE_DEFAULT_FAST
  281: #endif
  282: 
  283: #ifdef USE_FTOS
  284: #define IF_fpTOS(x) x
  285: #else
  286: #define IF_fpTOS(x)
  287: #define fpTOS (fp[0])
  288: #endif
  289: 
  290: #define IF_rpTOS(x)
  291: #define rpTOS (rp[0])
  292: 
  293: typedef struct {
  294:   Address base;		/* base address of image (0 if relocatable) */
  295:   UCell checksum;	/* checksum of ca's to protect against some
  296: 			   incompatible	binary/executable combinations
  297: 			   (0 if relocatable) */
  298:   UCell image_size;	/* all sizes in bytes */
  299:   UCell dict_size;
  300:   UCell data_stack_size;
  301:   UCell fp_stack_size;
  302:   UCell return_stack_size;
  303:   UCell locals_stack_size;
  304:   Xt *boot_entry;	/* initial ip for booting (in BOOT) */
  305:   Xt *throw_entry;	/* ip after signal (in THROW) */
  306:   Cell unused1;		/* possibly tib stack size */
  307:   Label *xt_base;         /* base of DOUBLE_INDIRECT xts[], for comp-i.fs */
  308:   Address data_stack_base; /* this and the following fields are initialized by the loader */
  309:   Address fp_stack_base;
  310:   Address return_stack_base;
  311:   Address locals_stack_base;
  312: } ImageHeader;
  313: /* the image-header is created in main.fs */
  314: 
  315: #ifdef HAS_F83HEADERSTRING
  316: struct F83Name {
  317:   struct F83Name *next;  /* the link field for old hands */
  318:   char		countetc;
  319:   char		name[0];
  320: };
  321: 
  322: #define F83NAME_COUNT(np)	((np)->countetc & 0x1f)
  323: #endif
  324: struct Longname {
  325:   struct Longname *next;  /* the link field for old hands */
  326:   Cell		countetc;
  327:   char		name[0];
  328: };
  329: 
  330: #define LONGNAME_COUNT(np)	((np)->countetc & (((~((UCell)0))<<3)>>3))
  331: 
  332: struct Cellpair {
  333:   Cell n1;
  334:   Cell n2;
  335: };
  336: 
  337: struct Cellquad {
  338:   Cell n1;
  339:   Cell n2;
  340:   Cell n3;
  341:   Cell n4;
  342: };
  343: 
  344: #define IOR(flag)	((flag)? -512-errno : 0)
  345: 
  346: #ifdef GFORTH_DEBUGGING
  347: #if defined(GLOBALS_NONRELOC)
  348: /* if globals cause non-relocatable primitives, keep saved_ip and rp
  349:    in a structure and access it through locals */
  350: typedef struct saved_regs {
  351:   Xt *sr_saved_ip;
  352:   Cell *sr_rp;
  353: } saved_regs;
  354: extern saved_regs saved_regs_v, *saved_regs_p;
  355: #define saved_ip (saved_regs_p->sr_saved_ip)
  356: #define rp       (saved_regs_p->sr_rp)
  357: /* for use in gforth_engine header */
  358: #define sr_proto , struct saved_regs *saved_regs_p0
  359: #define sr_call  , saved_regs_p
  360: #else /* !defined(GLOBALS_NONRELOC) */
  361: extern Xt *saved_ip;
  362: extern Cell *rp;
  363: #define sr_proto
  364: #define sr_call
  365: #endif /* !defined(GLOBALS_NONRELOC) */
  366: #else /* !defined(GFORTH_DEBUGGING) */
  367: #define sr_proto
  368: #define sr_call
  369: #endif /* !defined(GFORTH_DEBUGGING) */
  370: 
  371: Label *gforth_engine(Xt *ip, Cell *sp, Cell *rp0, Float *fp, Address lp sr_proto);
  372: Label *gforth_engine2(Xt *ip, Cell *sp, Cell *rp0, Float *fp, Address lp sr_proto);
  373: Label *gforth_engine3(Xt *ip, Cell *sp, Cell *rp0, Float *fp, Address lp sr_proto);
  374: 
  375: /* engine/prim support routines */
  376: Address gforth_alloc(Cell size);
  377: char *cstr(Char *from, UCell size, int clear);
  378: char *tilde_cstr(Char *from, UCell size, int clear);
  379: Cell opencreate_file(char *s, Cell wfam, int flags, Cell *wiorp);
  380: DCell timeval2us(struct timeval *tvp);
  381: void cmove(Char *c_from, Char *c_to, UCell u);
  382: void cmove_up(Char *c_from, Char *c_to, UCell u);
  383: Cell compare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
  384: struct Longname *listlfind(Char *c_addr, UCell u, struct Longname *longname1);
  385: struct Longname *hashlfind(Char *c_addr, UCell u, Cell *a_addr);
  386: struct Longname *tablelfind(Char *c_addr, UCell u, Cell *a_addr);
  387: UCell hashkey1(Char *c_addr, UCell u, UCell ubits);
  388: struct Cellpair parse_white(Char *c_addr1, UCell u1);
  389: Cell rename_file(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
  390: struct Cellquad read_line(Char *c_addr, UCell u1, Cell wfileid);
  391: struct Cellpair file_status(Char *c_addr, UCell u);
  392: Cell to_float(Char *c_addr, UCell u, Float *r_p);
  393: Float v_star(Float *f_addr1, Cell nstride1, Float *f_addr2, Cell nstride2, UCell ucount);
  394: void faxpy(Float ra, Float *f_x, Cell nstridex, Float *f_y, Cell nstridey, UCell ucount);
  395: UCell lshift(UCell u1, UCell n);
  396: UCell rshift(UCell u1, UCell n);
  397: int gforth_system(Char *c_addr, UCell u);
  398: void gforth_ms(UCell u);
  399: Cell capscompare(Char *c_addr1, UCell u1, Char *c_addr2, UCell u2);
  400: 
  401: /* signal handler stuff */
  402: void install_signal_handlers(void);
  403: void throw(int code);
  404: /* throw codes */
  405: #define BALL_DIVZERO     -10
  406: #define BALL_RESULTRANGE -11
  407: 
  408: typedef void Sigfunc(int);
  409: Sigfunc *bsd_signal(int signo, Sigfunc *func);
  410: 
  411: /* dblsub routines */
  412: DCell dnegate(DCell d1);
  413: UDCell ummul (UCell a, UCell b);
  414: DCell mmul (Cell a, Cell b);
  415: UDCell umdiv (UDCell u, UCell v);
  416: DCell smdiv (DCell num, Cell denom);
  417: DCell fmdiv (DCell num, Cell denom);
  418: 
  419: Cell memcasecmp(const Char *s1, const Char *s2, Cell n);
  420: 
  421: void vm_print_profile(FILE *file);
  422: void vm_count_block(Xt *ip);
  423: 
  424: /* dynamic superinstruction stuff */
  425: void compile_prim1(Cell *start);
  426: void finish_code(void);
  427: int forget_dyncode(Address code);
  428: Label decompile_code(Label prim);
  429: 
  430: extern int offset_image;
  431: extern int die_on_signal;
  432: extern int ignore_async_signals;
  433: extern UCell pagesize;
  434: extern ImageHeader *gforth_header;
  435: extern Label *vm_prims;
  436: extern Label *xts;
  437: extern Cell npriminfos;
  438: 
  439: #ifdef HAS_DEBUG
  440: extern int debug;
  441: #else
  442: # define debug 0
  443: #endif
  444: 
  445: extern Cell *gforth_SP;
  446: extern Float *gforth_FP;
  447: extern Address gforth_UP;
  448: 
  449: #ifdef HAS_FFCALL
  450: extern Cell *gforth_RP;
  451: extern Address gforth_LP;
  452: extern void gforth_callback(Xt* fcall, void * alist);
  453: #endif
  454: 
  455: #ifdef HAS_LIBFFI
  456: extern Cell *gforth_RP;
  457: extern Address gforth_LP;
  458: #include <ffi.h>
  459: extern void gforth_callback(ffi_cif * cif, void * resp, void ** args, void * ip);
  460: #endif
  461: 
  462: #ifdef NO_IP
  463: extern Label next_code;
  464: #endif
  465: 
  466: #ifdef HAS_FILE
  467: extern char* fileattr[6];
  468: extern char* pfileattr[6];
  469: extern int ufileattr[6];
  470: #endif
  471: 
  472: #ifdef PRINT_SUPER_LENGTHS
  473: Cell prim_length(Cell prim);
  474: void print_super_lengths();
  475: #endif
  476: 
  477: /* declare all the functions that are missing */
  478: #ifndef HAVE_ATANH
  479: extern double atanh(double r1);
  480: extern double asinh(double r1);
  481: extern double acosh(double r1);
  482: #endif
  483: #ifndef HAVE_ECVT
  484: /* extern char* ecvt(double x, int len, int* exp, int* sign);*/
  485: #endif
  486: #ifndef HAVE_MEMMOVE
  487: /* extern char *memmove(char *dest, const char *src, long n); */
  488: #endif
  489: #ifndef HAVE_POW10
  490: extern double pow10(double x);
  491: #endif
  492: #ifndef HAVE_STRERROR
  493: extern char *strerror(int err);
  494: #endif
  495: #ifndef HAVE_STRSIGNAL
  496: extern char *strsignal(int sig);
  497: #endif
  498: #ifndef HAVE_STRTOUL
  499: extern unsigned long int strtoul(const char *nptr, char **endptr, int base);
  500: #endif
  501: 
  502: #define GROUP(x, n)
  503: #define GROUPADD(n)

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