File:  [gforth] / gforth / engine / engine.c
Revision 1.48: download - view: text, annotated - select for diffs
Thu Dec 19 20:14:57 2002 UTC (21 years, 4 months ago) by anton
Branches: MAIN
CVS tags: HEAD
moved all functions except engine() out of engine.c into (new file) support.c
minor documentation changes (suggested by John A. Peters <japeters@pacbell.net>)

    1: /* Gforth virtual machine (aka inner interpreter)
    2: 
    3:   Copyright (C) 1995,1996,1997,1998,2000 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: undefine(`symbols')
   23: 
   24: #include "config.h"
   25: #include "forth.h"
   26: #include <ctype.h>
   27: #include <stdio.h>
   28: #include <string.h>
   29: #include <math.h>
   30: #include <assert.h>
   31: #include <stdlib.h>
   32: #include <errno.h>
   33: #include "io.h"
   34: #include "threaded.h"
   35: #ifndef STANDALONE
   36: #include <sys/types.h>
   37: #include <sys/stat.h>
   38: #include <fcntl.h>
   39: #include <time.h>
   40: #include <sys/time.h>
   41: #include <unistd.h>
   42: #include <pwd.h>
   43: #include <dirent.h>
   44: #include <sys/resource.h>
   45: #ifdef HAVE_FNMATCH_H
   46: #include <fnmatch.h>
   47: #else
   48: #include "fnmatch.h"
   49: #endif
   50: #else
   51: #include "systypes.h"
   52: #endif
   53: 
   54: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
   55: #include <dlfcn.h>
   56: #endif
   57: #if defined(_WIN32)
   58: #include <windows.h>
   59: #endif
   60: #ifdef hpux
   61: #include <dl.h>
   62: #endif
   63: 
   64: #ifndef SEEK_SET
   65: /* should be defined in stdio.h, but some systems don't have it */
   66: #define SEEK_SET 0
   67: #endif
   68: 
   69: #define IOR(flag)	((flag)? -512-errno : 0)
   70: 
   71: struct F83Name {
   72:   struct F83Name *next;  /* the link field for old hands */
   73:   char		countetc;
   74:   char		name[0];
   75: };
   76: 
   77: #define F83NAME_COUNT(np)	((np)->countetc & 0x1f)
   78: 
   79: struct Longname {
   80:   struct Longname *next;  /* the link field for old hands */
   81:   Cell		countetc;
   82:   char		name[0];
   83: };
   84: 
   85: #define LONGNAME_COUNT(np)	((np)->countetc & (((~((UCell)0))<<3)>>3))
   86: 
   87: Cell *SP;
   88: Float *FP;
   89: Address UP=NULL;
   90: 
   91: #if 0
   92: /* not used currently */
   93: int emitcounter;
   94: #endif
   95: #define NULLC '\0'
   96: 
   97: #ifdef MEMCMP_AS_SUBROUTINE
   98: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
   99: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
  100: #endif
  101: 
  102: #define NEWLINE	'\n'
  103: 
  104: #ifdef HAS_FILE
  105: static char* fileattr[6]={"rb","rb","r+b","r+b","wb","wb"};
  106: static char* pfileattr[6]={"r","r","r+","r+","w","w"};
  107: 
  108: #ifndef O_BINARY
  109: #define O_BINARY 0
  110: #endif
  111: #ifndef O_TEXT
  112: #define O_TEXT 0
  113: #endif
  114: 
  115: static int ufileattr[6]= {
  116:   O_RDONLY|O_BINARY, O_RDONLY|O_BINARY,
  117:   O_RDWR  |O_BINARY, O_RDWR  |O_BINARY,
  118:   O_WRONLY|O_BINARY, O_WRONLY|O_BINARY };
  119: #endif
  120: 
  121: /* conversion on fetch */
  122: 
  123: #define vm_Cell2f(_cell,_x)		((_x)=(Bool)(_cell))
  124: #define vm_Cell2c(_cell,_x)		((_x)=(Char)(_cell))
  125: #define vm_Cell2n(_cell,_x)		((_x)=(Cell)(_cell))
  126: #define vm_Cell2w(_cell,_x)		((_x)=(Cell)(_cell))
  127: #define vm_Cell2u(_cell,_x)		((_x)=(UCell)(_cell))
  128: #define vm_Cell2a_(_cell,_x)		((_x)=(Cell *)(_cell))
  129: #define vm_Cell2c_(_cell,_x)		((_x)=(Char *)(_cell))
  130: #define vm_Cell2f_(_cell,_x)		((_x)=(Float *)(_cell))
  131: #define vm_Cell2df_(_cell,_x)		((_x)=(DFloat *)(_cell))
  132: #define vm_Cell2sf_(_cell,_x)		((_x)=(SFloat *)(_cell))
  133: #define vm_Cell2xt(_cell,_x)		((_x)=(Xt)(_cell))
  134: #define vm_Cell2f83name(_cell,_x)	((_x)=(struct F83Name *)(_cell))
  135: #define vm_Cell2longname(_cell,_x)	((_x)=(struct Longname *)(_cell))
  136: #define vm_Float2r(_float,_x)		(_x=_float)
  137: 
  138: /* conversion on store */
  139: 
  140: #define vm_f2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  141: #define vm_c2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  142: #define vm_n2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  143: #define vm_w2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  144: #define vm_u2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  145: #define vm_a_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  146: #define vm_c_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  147: #define vm_f_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  148: #define vm_df_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  149: #define vm_sf_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  150: #define vm_xt2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  151: #define vm_f83name2Cell(_x,_cell)	((_cell)=(Cell)(_x))
  152: #define vm_longname2Cell(_x,_cell)	((_cell)=(Cell)(_x))
  153: #define vm_r2Float(_x,_float)		(_float=_x)
  154: 
  155: #define vm_Cell2Cell(_x,_y)		(_y=_x)
  156: 
  157: #ifdef NO_IP
  158: #define IMM_ARG(access,value)		(VARIANT(value))
  159: #else
  160: #define IMM_ARG(access,value)		(access)
  161: #endif
  162: 
  163: /* if machine.h has not defined explicit registers, define them as implicit */
  164: #ifndef IPREG
  165: #define IPREG
  166: #endif
  167: #ifndef SPREG
  168: #define SPREG
  169: #endif
  170: #ifndef RPREG
  171: #define RPREG
  172: #endif
  173: #ifndef FPREG
  174: #define FPREG
  175: #endif
  176: #ifndef LPREG
  177: #define LPREG
  178: #endif
  179: #ifndef CFAREG
  180: #define CFAREG
  181: #endif
  182: #ifndef UPREG
  183: #define UPREG
  184: #endif
  185: #ifndef TOSREG
  186: #define TOSREG
  187: #endif
  188: #ifndef FTOSREG
  189: #define FTOSREG
  190: #endif
  191: 
  192: #ifndef CPU_DEP1
  193: # define CPU_DEP1 0
  194: #endif
  195: 
  196: /* instructions containing these must be the last instruction of a
  197:    super-instruction (e.g., branches, EXECUTE, and other instructions
  198:    ending the basic block). Instructions containing SET_IP get this
  199:    automatically, so you usually don't have to write it.  If you have
  200:    to write it, write it after IP points to the next instruction.
  201:    Used for profiling.  Don't write it in a word containing SET_IP, or
  202:    the following block will be counted twice. */
  203: #ifdef VM_PROFILING
  204: #define SUPER_END  vm_count_block(IP)
  205: #else
  206: #define SUPER_END
  207: #endif
  208: #define SUPER_CONTINUE
  209: 
  210: #ifdef GFORTH_DEBUGGING
  211: /* define some VM registers as global variables, so they survive exceptions;
  212:    global register variables are not up to the task (according to the 
  213:    GNU C manual) */
  214: Xt *saved_ip;
  215: Cell *rp;
  216: #endif
  217: 
  218: #ifdef NO_IP
  219: static Label next_code;
  220: #endif
  221: 
  222: #ifdef DEBUG
  223: #define CFA_TO_NAME(__cfa) \
  224:       Cell len, i; \
  225:       char * name = __cfa; \
  226:       for(i=0; i<32; i+=sizeof(Cell)) { \
  227:         len = ((Cell*)name)[-1]; \
  228:         if(len < 0) { \
  229: 	  len &= 0x1F; \
  230:           if((len+sizeof(Cell)) > i) break; \
  231: 	} len = 0; \
  232: 	name -= sizeof(Cell); \
  233:       }
  234: #endif
  235: 
  236: define(enginerest,
  237: `(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
  238: /* executes code at ip, if ip!=NULL
  239:    returns array of machine code labels (for use in a loader), if ip==NULL
  240: */
  241: {
  242: #ifndef GFORTH_DEBUGGING
  243:   register Cell *rp RPREG;
  244: #endif
  245: #ifndef NO_IP
  246:   register Xt *ip IPREG = ip0;
  247: #endif
  248:   register Cell *sp SPREG = sp0;
  249:   register Float *fp FPREG = fp0;
  250:   register Address lp LPREG = lp0;
  251:   register Xt cfa CFAREG;
  252: #ifdef MORE_VARS
  253:   MORE_VARS
  254: #endif
  255:   register Address up UPREG = UP;
  256:   IF_spTOS(register Cell spTOS TOSREG;)
  257:   IF_fpTOS(register Float fpTOS FTOSREG;)
  258: #if defined(DOUBLY_INDIRECT)
  259:   static Label *symbols;
  260:   static void *routines[]= {
  261: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
  262: #else /* !defined(DOUBLY_INDIRECT) */
  263:   static Label symbols[]= {
  264: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
  265: #endif /* !defined(DOUBLY_INDIRECT) */
  266:     (Label)&&docol,
  267:     (Label)&&docon,
  268:     (Label)&&dovar,
  269:     (Label)&&douser,
  270:     (Label)&&dodefer,
  271:     (Label)&&dofield,
  272:     (Label)&&dodoes,
  273:     /* the following entry is normally unused;
  274:        it is there because its index indicates a does-handler */
  275:     CPU_DEP1,
  276: #define INST_ADDR(name) (Label)&&I_##name
  277: #include "prim_lab.i"
  278: #undef INST_ADDR
  279:     (Label)&&after_last,
  280:     (Label)0,
  281: #define INST_ADDR(name) (Label)&&K_##name
  282: #include "prim_lab.i"
  283: #undef INST_ADDR
  284: #ifdef IN_ENGINE2
  285: #define INST_ADDR(name) (Label)&&J_##name
  286: #include "prim_lab.i"
  287: #undef INST_ADDR
  288: #endif
  289:   };
  290: #ifdef CPU_DEP2
  291:   CPU_DEP2
  292: #endif
  293: 
  294:   rp = rp0;
  295: #ifdef DEBUG
  296:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
  297:           (unsigned)ip0,(unsigned)sp,(unsigned)rp,
  298: 	  (unsigned)fp,(unsigned)lp,(unsigned)up);
  299: #endif
  300: 
  301:   if (ip0 == NULL) {
  302: #if defined(DOUBLY_INDIRECT)
  303: #define CODE_OFFSET (26*sizeof(Cell))
  304: #define XT_OFFSET (22*sizeof(Cell))
  305:     int i;
  306:     Cell code_offset = offset_image? CODE_OFFSET : 0;
  307:     Cell xt_offset = offset_image? XT_OFFSET : 0;
  308: 
  309:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
  310:     xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
  311:     for (i=0; i<DOESJUMP+1; i++)
  312:       xts[i] = symbols[i] = (Label)routines[i];
  313:     for (; routines[i]!=0; i++) {
  314:       if (i>=MAX_SYMBOLS) {
  315: 	fprintf(stderr,"gforth-ditc: more than %d primitives\n",MAX_SYMBOLS);
  316: 	exit(1);
  317:       }
  318:       xts[i] = symbols[i] = &routines[i];
  319:     }
  320: #endif /* defined(DOUBLY_INDIRECT) */
  321:     return symbols;
  322:   }
  323: 
  324:   IF_spTOS(spTOS = sp[0]);
  325:   IF_fpTOS(fpTOS = fp[0]);
  326: /*  prep_terminal(); */
  327: #ifdef NO_IP
  328:   goto *(*(Label *)ip0);
  329: #else
  330:   SET_IP(ip);
  331:   SUPER_END; /* count the first block, too */
  332:   NEXT;
  333: #endif
  334: 
  335: #ifdef CPU_DEP3
  336:   CPU_DEP3
  337: #endif
  338:   
  339:  docol:
  340:   {
  341: #ifdef NO_IP
  342:     *--rp = next_code;
  343:     goto **(Label *)PFA1(cfa);
  344: #else
  345: #ifdef DEBUG
  346:     {
  347:       CFA_TO_NAME(cfa);
  348:       fprintf(stderr,"%08lx: col: %08lx %.*s\n",(Cell)ip,(Cell)PFA1(cfa),
  349: 	      len,name);
  350:     }
  351: #endif
  352: #ifdef CISC_NEXT
  353:     /* this is the simple version */
  354:     *--rp = (Cell)ip;
  355:     SET_IP((Xt *)PFA1(cfa));
  356:     SUPER_END;
  357:     NEXT;
  358: #else
  359:     /* this one is important, so we help the compiler optimizing */
  360:     {
  361:       DEF_CA
  362:       rp[-1] = (Cell)ip;
  363:       SET_IP((Xt *)PFA1(cfa));
  364:       SUPER_END;
  365:       NEXT_P1;
  366:       rp--;
  367:       NEXT_P2;
  368:     }
  369: #endif
  370: #endif
  371:   }
  372: 
  373:  docon:
  374:   {
  375: #ifdef DEBUG
  376:     fprintf(stderr,"%08lx: con: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
  377: #endif
  378: #ifdef USE_TOS
  379:     *sp-- = spTOS;
  380:     spTOS = *(Cell *)PFA1(cfa);
  381: #else
  382:     *--sp = *(Cell *)PFA1(cfa);
  383: #endif
  384:   }
  385: #ifdef NO_IP
  386:   goto *next_code;
  387: #else
  388:   NEXT_P0;
  389:   NEXT;
  390: #endif
  391:   
  392:  dovar:
  393:   {
  394: #ifdef DEBUG
  395:     fprintf(stderr,"%08lx: var: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  396: #endif
  397: #ifdef USE_TOS
  398:     *sp-- = spTOS;
  399:     spTOS = (Cell)PFA1(cfa);
  400: #else
  401:     *--sp = (Cell)PFA1(cfa);
  402: #endif
  403:   }
  404: #ifdef NO_IP
  405:   goto *next_code;
  406: #else
  407:   NEXT_P0;
  408:   NEXT;
  409: #endif
  410:   
  411:  douser:
  412:   {
  413: #ifdef DEBUG
  414:     fprintf(stderr,"%08lx: user: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  415: #endif
  416: #ifdef USE_TOS
  417:     *sp-- = spTOS;
  418:     spTOS = (Cell)(up+*(Cell*)PFA1(cfa));
  419: #else
  420:     *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
  421: #endif
  422:   }
  423: #ifdef NO_IP
  424:   goto *next_code;
  425: #else
  426:   NEXT_P0;
  427:   NEXT;
  428: #endif
  429:   
  430:  dodefer:
  431:   {
  432: #ifdef DEBUG
  433:     fprintf(stderr,"%08lx: defer: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
  434: #endif
  435:     SUPER_END;
  436:     EXEC(*(Xt *)PFA1(cfa));
  437:   }
  438: 
  439:  dofield:
  440:   {
  441: #ifdef DEBUG
  442:     fprintf(stderr,"%08lx: field: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  443: #endif
  444:     spTOS += *(Cell*)PFA1(cfa);
  445:   }
  446: #ifdef NO_IP
  447:   goto *next_code;
  448: #else
  449:   NEXT_P0;
  450:   NEXT;
  451: #endif
  452: 
  453:  dodoes:
  454:   /* this assumes the following structure:
  455:      defining-word:
  456:      
  457:      ...
  458:      DOES>
  459:      (possible padding)
  460:      possibly handler: jmp dodoes
  461:      (possible branch delay slot(s))
  462:      Forth code after DOES>
  463:      
  464:      defined word:
  465:      
  466:      cfa: address of or jump to handler OR
  467:           address of or jump to dodoes, address of DOES-code
  468:      pfa:
  469:      
  470:      */
  471: #ifdef NO_IP
  472:   *--rp = next_code;
  473:   IF_spTOS(spTOS = sp[0]);
  474:   sp--;
  475:   spTOS = (Cell)PFA(cfa);
  476:   goto **(Label *)DOES_CODE1(cfa);
  477: #else
  478:   {
  479:     /*    fprintf(stderr, "Got CFA %08lx at doescode %08lx/%08lx: does: %08lx\n",cfa,(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));*/
  480: #ifdef DEBUG
  481:     fprintf(stderr,"%08lx/%08lx: does: %08lx\n",(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));
  482:     fflush(stderr);
  483: #endif
  484:     *--rp = (Cell)ip;
  485:     /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
  486: #ifdef USE_TOS
  487:     *sp-- = spTOS;
  488:     spTOS = (Cell)PFA(cfa);
  489: #else
  490:     *--sp = (Cell)PFA(cfa);
  491: #endif
  492:     SET_IP(DOES_CODE1(cfa));
  493:     SUPER_END;
  494:     /*    fprintf(stderr,"TOS = %08lx, IP=%08lx\n", spTOS, IP);*/
  495:   }
  496:   NEXT;
  497: #endif
  498: 
  499: #ifndef IN_ENGINE2
  500: #define LABEL(name) I_##name:
  501: #else
  502: #define LABEL(name) J_##name: asm(".skip 16"); I_##name:
  503: #endif
  504: #define LABEL2(name) K_##name:
  505: #include "prim.i"
  506: #undef LABEL
  507:   after_last: return (Label *)0;
  508:   /*needed only to get the length of the last primitive */
  509: }'
  510: )
  511: 
  512: #define VARIANT(v)	(v)
  513: #define JUMP(target)	goto I_noop
  514: 
  515: Label *engine enginerest
  516: 
  517: #ifndef NO_DYNAMIC
  518: 
  519: #ifdef NO_IP
  520: #undef VARIANT
  521: #define VARIANT(v)	((v)^0xffffffff)
  522: #undef JUMP
  523: #define JUMP(target)	goto K_lit
  524: Label *engine3 enginerest
  525: #endif
  526: 
  527: #undef VARIANT
  528: #define VARIANT(v)	(v)
  529: #undef JUMP
  530: #define JUMP(target)	goto I_noop
  531: #define IN_ENGINE2
  532: Label *engine2 enginerest
  533: #endif

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