File:  [gforth] / gforth / engine / engine.c
Revision 1.92: download - view: text, annotated - select for diffs
Sun Oct 22 16:54:01 2006 UTC (17 years, 5 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added checking for division by zero and division overflow to
  mixed-size division words (*/ */mod fm/mod sm/rem um/mod).
these exceptions are now tested in our test suite
fixed bug in testing

    1: /* Gforth virtual machine (aka inner interpreter)
    2: 
    3:   Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005 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 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
   19:   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: */
   21: 
   22: #if defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING)
   23: #define USE_NO_TOS
   24: #else
   25: #define USE_TOS
   26: #endif
   27: #define USE_NO_FTOS
   28: 
   29: #include "config.h"
   30: #include "forth.h"
   31: #include <ctype.h>
   32: #include <stdio.h>
   33: #include <string.h>
   34: #include <math.h>
   35: #include <assert.h>
   36: #include <stdlib.h>
   37: #include <errno.h>
   38: #include "io.h"
   39: #include "threaded.h"
   40: #ifndef STANDALONE
   41: #include <sys/types.h>
   42: #include <sys/stat.h>
   43: #include <fcntl.h>
   44: #include <time.h>
   45: #include <sys/time.h>
   46: #include <unistd.h>
   47: #include <pwd.h>
   48: #include <dirent.h>
   49: #include <wchar.h>
   50: #include <sys/resource.h>
   51: #ifdef HAVE_FNMATCH_H
   52: #include <fnmatch.h>
   53: #else
   54: #include "fnmatch.h"
   55: #endif
   56: #else
   57: #include "systypes.h"
   58: #endif
   59: 
   60: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
   61: #include <dlfcn.h>
   62: #endif
   63: #if defined(_WIN32)
   64: #include <windows.h>
   65: #endif
   66: #ifdef hpux
   67: #include <dl.h>
   68: #endif
   69: 
   70: #ifdef HAS_FFCALL
   71: #include <avcall.h>
   72: #include <callback.h>
   73: #endif
   74: 
   75: #ifdef HAS_LIBFFI
   76: #include <ffi.h>
   77: #endif
   78: 
   79: #ifndef SEEK_SET
   80: /* should be defined in stdio.h, but some systems don't have it */
   81: #define SEEK_SET 0
   82: #endif
   83: 
   84: #ifndef HAVE_FSEEKO
   85: #define fseeko fseek
   86: #endif
   87: 
   88: #ifndef HAVE_FTELLO
   89: #define ftello ftell
   90: #endif
   91: 
   92: #define NULLC '\0'
   93: 
   94: #ifdef MEMCMP_AS_SUBROUTINE
   95: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
   96: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
   97: #endif
   98: 
   99: #define NEWLINE	'\n'
  100: #define CELL_MIN (((Cell)1)<<(sizeof(Cell)*CHAR_BIT-1))
  101: /* set CHECK_DIVISION to 0 if the hardware checks division by zero and
  102:    division overflow.  Note that not all checks are controlled by
  103:    CHECK_DIVISION, in particular not overflow checks in mixed-size
  104:    words implemented by C's double-by-double division, because that
  105:    division does not signal such overflows.  !! make an autoconf test
  106:    that sets CHECK_DIVISION */
  107: #define CHECK_DIVISION 1
  108: 
  109: /* conversion on fetch */
  110: 
  111: #define vm_Cell2f(_cell,_x)		((_x)=(Bool)(_cell))
  112: #define vm_Cell2c(_cell,_x)		((_x)=(Char)(_cell))
  113: #define vm_Cell2n(_cell,_x)		((_x)=(Cell)(_cell))
  114: #define vm_Cell2w(_cell,_x)		((_x)=(Cell)(_cell))
  115: #define vm_Cell2u(_cell,_x)		((_x)=(UCell)(_cell))
  116: #define vm_Cell2a_(_cell,_x)		((_x)=(Cell *)(_cell))
  117: #define vm_Cell2c_(_cell,_x)		((_x)=(Char *)(_cell))
  118: #define vm_Cell2f_(_cell,_x)		((_x)=(Float *)(_cell))
  119: #define vm_Cell2df_(_cell,_x)		((_x)=(DFloat *)(_cell))
  120: #define vm_Cell2sf_(_cell,_x)		((_x)=(SFloat *)(_cell))
  121: #define vm_Cell2xt(_cell,_x)		((_x)=(Xt)(_cell))
  122: #define vm_Cell2f83name(_cell,_x)	((_x)=(struct F83Name *)(_cell))
  123: #define vm_Cell2longname(_cell,_x)	((_x)=(struct Longname *)(_cell))
  124: #define vm_Float2r(_float,_x)		(_x=_float)
  125: 
  126: /* conversion on store */
  127: 
  128: #define vm_f2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  129: #define vm_c2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  130: #define vm_n2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  131: #define vm_w2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  132: #define vm_u2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  133: #define vm_a_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  134: #define vm_c_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  135: #define vm_f_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  136: #define vm_df_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  137: #define vm_sf_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  138: #define vm_xt2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  139: #define vm_f83name2Cell(_x,_cell)	((_cell)=(Cell)(_x))
  140: #define vm_longname2Cell(_x,_cell)	((_cell)=(Cell)(_x))
  141: #define vm_r2Float(_x,_float)		(_float=_x)
  142: 
  143: #define vm_Cell2Cell(_x,_y)		(_y=_x)
  144: 
  145: #ifdef NO_IP
  146: #define IMM_ARG(access,value)		(VARIANT(value))
  147: #else
  148: #define IMM_ARG(access,value)		(access)
  149: #endif
  150: 
  151: /* if machine.h has not defined explicit registers, define them as implicit */
  152: #ifndef IPREG
  153: #define IPREG
  154: #endif
  155: #ifndef SPREG
  156: #define SPREG
  157: #endif
  158: #ifndef RPREG
  159: #define RPREG
  160: #endif
  161: #ifndef FPREG
  162: #define FPREG
  163: #endif
  164: #ifndef LPREG
  165: #define LPREG
  166: #endif
  167: #ifndef CAREG
  168: #define CAREG
  169: #endif
  170: #ifndef CFAREG
  171: #define CFAREG
  172: #endif
  173: #ifndef UPREG
  174: #define UPREG
  175: #endif
  176: #ifndef TOSREG
  177: #define TOSREG
  178: #endif
  179: #ifndef spbREG
  180: #define spbREG
  181: #endif
  182: #ifndef spcREG
  183: #define spcREG
  184: #endif
  185: #ifndef spdREG
  186: #define spdREG
  187: #endif
  188: #ifndef speREG
  189: #define speREG
  190: #endif
  191: #ifndef spfREG
  192: #define spfREG
  193: #endif
  194: #ifndef spgREG
  195: #define spgREG
  196: #endif
  197: #ifndef sphREG
  198: #define sphREG
  199: #endif
  200: #ifndef FTOSREG
  201: #define FTOSREG
  202: #endif
  203: 
  204: #ifndef CPU_DEP1
  205: # define CPU_DEP1 0
  206: #endif
  207: 
  208: /* instructions containing SUPER_END must be the last instruction of a
  209:    super-instruction (e.g., branches, EXECUTE, and other instructions
  210:    ending the basic block). Instructions containing SET_IP get this
  211:    automatically, so you usually don't have to write it.  If you have
  212:    to write it, write it after IP points to the next instruction.
  213:    Used for profiling.  Don't write it in a word containing SET_IP, or
  214:    the following block will be counted twice. */
  215: #ifdef VM_PROFILING
  216: #define SUPER_END  vm_count_block(IP)
  217: #else
  218: #define SUPER_END
  219: #endif
  220: #define SUPER_CONTINUE
  221: 
  222: #ifdef GFORTH_DEBUGGING
  223: #if DEBUG
  224: #define NAME(string) { saved_ip=ip; asm("# "string); fprintf(stderr,"%08lx depth=%3ld: "string"\n",(Cell)ip,sp0+3-sp);}
  225: #else /* !DEBUG */
  226: #define NAME(string) { saved_ip=ip; asm(""); }
  227: /* the asm here is to avoid reordering of following stuff above the
  228:    assignment; this is an old-style asm (no operands), and therefore
  229:    is treated like "asm volatile ..."; i.e., it prevents most
  230:    reorderings across itself.  We want the assignment above first,
  231:    because the stack loads may already cause a stack underflow. */
  232: #endif /* !DEBUG */
  233: #elif DEBUG
  234: #       define  NAME(string)    {Cell __depth=sp0+3-sp; int i; fprintf(stderr,"%08lx depth=%3ld: "string,(Cell)ip,sp0+3-sp); for (i=__depth-1; i>0; i--) fprintf(stderr, " $%lx",sp[i]); fprintf(stderr, " $%lx\n",spTOS); }
  235: #else
  236: #	define	NAME(string) asm("# "string);
  237: #endif
  238: 
  239: #ifdef DEBUG
  240: #define CFA_TO_NAME(__cfa) \
  241:       Cell len, i; \
  242:       char * name = __cfa; \
  243:       for(i=0; i<32; i+=sizeof(Cell)) { \
  244:         len = ((Cell*)name)[-1]; \
  245:         if(len < 0) { \
  246: 	  len &= 0x1F; \
  247:           if((len+sizeof(Cell)) > i) break; \
  248: 	} len = 0; \
  249: 	name -= sizeof(Cell); \
  250:       }
  251: #endif
  252: 
  253: #if defined(HAS_FFCALL) || defined(HAS_LIBFFI)
  254: #define SAVE_REGS IF_fpTOS(fp[0]=fpTOS); gforth_SP=sp; gforth_FP=fp; gforth_RP=rp; gforth_LP=lp;
  255: #define REST_REGS sp=gforth_SP; fp=gforth_FP; rp=gforth_RP; lp=gforth_LP; IF_fpTOS(fpTOS=fp[0]);
  256: #endif
  257: 
  258: #if !defined(ENGINE)
  259: /* normal engine */
  260: #define VARIANT(v)	(v)
  261: #define JUMP(target)	goto I_noop
  262: #define LABEL(name) H_##name: asm(""); I_##name:
  263: 
  264: #elif ENGINE==2
  265: /* variant with padding between VM instructions for finding out
  266:    cross-inst jumps (for dynamic code) */
  267: #define gforth_engine gforth_engine2
  268: #define VARIANT(v)	(v)
  269: #define JUMP(target)	goto I_noop
  270: #define LABEL(name) H_##name: SKIP16; I_##name:
  271: 
  272: #elif ENGINE==3
  273: /* variant with different immediate arguments for finding out
  274:    immediate arguments (for native code) */
  275: #define gforth_engine gforth_engine3
  276: #define VARIANT(v)	((v)^0xffffffff)
  277: #define JUMP(target)	goto K_lit
  278: #define LABEL(name) H_##name: asm(""); I_##name:
  279: #else
  280: #error illegal ENGINE value
  281: #endif /* ENGINE */
  282: 
  283: /* the asm(""); is there to get a stop compiled on Itanium */
  284: #define LABEL2(name) K_##name: asm("");
  285: #define LABEL3(name) J_##name: asm("");
  286: 
  287: Label *gforth_engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
  288: /* executes code at ip, if ip!=NULL
  289:    returns array of machine code labels (for use in a loader), if ip==NULL
  290: */
  291: {
  292: #ifndef GFORTH_DEBUGGING
  293:   register Cell *rp RPREG;
  294: #endif
  295: #ifndef NO_IP
  296:   register Xt *ip IPREG = ip0;
  297: #endif
  298:   register Cell *sp SPREG = sp0;
  299:   register Float *fp FPREG = fp0;
  300:   register Address lp LPREG = lp0;
  301:   register Xt cfa CFAREG;
  302:   register Label real_ca CAREG;
  303: #ifdef MORE_VARS
  304:   MORE_VARS
  305: #endif
  306: #ifdef HAS_FFCALL
  307:   av_alist alist;
  308:   extern va_alist gforth_clist;
  309:   float frv;
  310:   int irv;
  311:   double drv;
  312:   long long llrv;
  313:   void * prv;
  314: #endif
  315: #ifdef HAS_LIBFFI
  316:   extern void * gforth_ritem;
  317:   extern void ** gforth_clist;
  318:   extern void ffi_callback(ffi_cif * cif, void * resp, void ** args, Xt * ip);
  319: #endif
  320:   register Address up UPREG = gforth_UP;
  321:   register Cell MAYBE_UNUSED spTOS TOSREG;
  322:   register Cell MAYBE_UNUSED spb spbREG;
  323:   register Cell MAYBE_UNUSED spc spcREG;
  324:   register Cell MAYBE_UNUSED spd spdREG;
  325:   register Cell MAYBE_UNUSED spe speREG;
  326:   register Cell MAYBE_UNUSED spf speREG;
  327:   register Cell MAYBE_UNUSED spg speREG;
  328:   register Cell MAYBE_UNUSED sph speREG;
  329:   IF_fpTOS(register Float fpTOS FTOSREG;)
  330: #if defined(DOUBLY_INDIRECT)
  331:   static Label *symbols;
  332:   static void *routines[]= {
  333: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
  334: #else /* !defined(DOUBLY_INDIRECT) */
  335:   static Label symbols[]= {
  336: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
  337: #endif /* !defined(DOUBLY_INDIRECT) */
  338: #define INST_ADDR(name) ((Label)&&I_##name)
  339: #include PRIM_LAB_I
  340: #undef INST_ADDR
  341:     (Label)0,
  342: #define INST_ADDR(name) ((Label)&&K_##name)
  343: #include PRIM_LAB_I
  344: #undef INST_ADDR
  345: #define INST_ADDR(name) ((Label)&&J_##name)
  346: #include PRIM_LAB_I
  347: #undef INST_ADDR
  348:     (Label)&&after_last,
  349:     (Label)&&before_goto,
  350:     (Label)&&after_goto,
  351: /* just mention the H_ labels, so the SKIP16s are not optimized away */
  352: #define INST_ADDR(name) ((Label)&&H_##name)
  353: #include PRIM_LAB_I
  354: #undef INST_ADDR
  355:   };
  356: #ifdef CPU_DEP2
  357:   CPU_DEP2
  358: #endif
  359: 
  360:   rp = rp0;
  361: #ifdef DEBUG
  362:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
  363:           (unsigned)ip0,(unsigned)sp,(unsigned)rp,
  364: 	  (unsigned)fp,(unsigned)lp,(unsigned)up);
  365: #endif
  366: 
  367:   if (ip0 == NULL) {
  368: #if defined(DOUBLY_INDIRECT)
  369: #define CODE_OFFSET (26*sizeof(Cell))
  370: #define XT_OFFSET (22*sizeof(Cell))
  371:     int i;
  372:     Cell code_offset = offset_image? CODE_OFFSET : 0;
  373:     Cell xt_offset = offset_image? XT_OFFSET : 0;
  374: 
  375:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
  376:     xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
  377:     for (i=0; i<DOESJUMP+1; i++)
  378:       xts[i] = symbols[i] = (Label)routines[i];
  379:     for (; routines[i]!=0; i++) {
  380:       if (i>=MAX_SYMBOLS) {
  381: 	fprintf(stderr,"gforth-ditc: more than %ld primitives\n",(long)MAX_SYMBOLS);
  382: 	exit(1);
  383:       }
  384:       xts[i] = symbols[i] = &routines[i];
  385:     }
  386: #endif /* defined(DOUBLY_INDIRECT) */
  387:     return symbols;
  388:   }
  389: 
  390: #if !(defined(GFORTH_DEBUGGING) || defined(INDIRECT_THREADED) || defined(DOUBLY_INDIRECT) || defined(VM_PROFILING))
  391:   sp += STACK_CACHE_DEFAULT-1;
  392:   /* some of those registers are dead, but its simpler to initialize them all */  spTOS = sp[0];
  393:   spb = sp[-1];
  394:   spc = sp[-2];
  395:   spd = sp[-3];
  396:   spe = sp[-4];
  397:   spf = sp[-5];
  398:   spg = sp[-6];
  399:   sph = sp[-7];
  400: #endif
  401: 
  402:   IF_fpTOS(fpTOS = fp[0]);
  403: /*  prep_terminal(); */
  404: #ifdef NO_IP
  405:   goto *(*(Label *)ip0);
  406:   before_goto:
  407:   goto *real_ca;
  408:   after_goto:;
  409: #else
  410:   SET_IP(ip);
  411:   SUPER_END; /* count the first block, too */
  412:   FIRST_NEXT;
  413: #endif
  414: 
  415: #ifdef CPU_DEP3
  416:   CPU_DEP3
  417: #endif
  418: 
  419: #include PRIM_I
  420:   after_last: return (Label *)0;
  421:   /*needed only to get the length of the last primitive */
  422: 
  423:   return (Label *)0;
  424: }

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