File:  [gforth] / gforth / engine / engine.c
Revision 1.49: download - view: text, annotated - select for diffs
Thu Dec 19 20:43:26 2002 UTC (17 years, 7 months ago) by anton
Branches: MAIN
CVS tags: HEAD
moved global/static vars from engine.c into main.c (to avoid duplicating them)

    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: #define NULLC '\0'
   88: 
   89: #ifdef MEMCMP_AS_SUBROUTINE
   90: extern int gforth_memcmp(const char * s1, const char * s2, size_t n);
   91: #define memcmp(s1,s2,n) gforth_memcmp(s1,s2,n)
   92: #endif
   93: 
   94: #define NEWLINE	'\n'
   95: 
   96: /* conversion on fetch */
   97: 
   98: #define vm_Cell2f(_cell,_x)		((_x)=(Bool)(_cell))
   99: #define vm_Cell2c(_cell,_x)		((_x)=(Char)(_cell))
  100: #define vm_Cell2n(_cell,_x)		((_x)=(Cell)(_cell))
  101: #define vm_Cell2w(_cell,_x)		((_x)=(Cell)(_cell))
  102: #define vm_Cell2u(_cell,_x)		((_x)=(UCell)(_cell))
  103: #define vm_Cell2a_(_cell,_x)		((_x)=(Cell *)(_cell))
  104: #define vm_Cell2c_(_cell,_x)		((_x)=(Char *)(_cell))
  105: #define vm_Cell2f_(_cell,_x)		((_x)=(Float *)(_cell))
  106: #define vm_Cell2df_(_cell,_x)		((_x)=(DFloat *)(_cell))
  107: #define vm_Cell2sf_(_cell,_x)		((_x)=(SFloat *)(_cell))
  108: #define vm_Cell2xt(_cell,_x)		((_x)=(Xt)(_cell))
  109: #define vm_Cell2f83name(_cell,_x)	((_x)=(struct F83Name *)(_cell))
  110: #define vm_Cell2longname(_cell,_x)	((_x)=(struct Longname *)(_cell))
  111: #define vm_Float2r(_float,_x)		(_x=_float)
  112: 
  113: /* conversion on store */
  114: 
  115: #define vm_f2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  116: #define vm_c2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  117: #define vm_n2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  118: #define vm_w2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  119: #define vm_u2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  120: #define vm_a_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  121: #define vm_c_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  122: #define vm_f_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  123: #define vm_df_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  124: #define vm_sf_2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  125: #define vm_xt2Cell(_x,_cell)		((_cell)=(Cell)(_x))
  126: #define vm_f83name2Cell(_x,_cell)	((_cell)=(Cell)(_x))
  127: #define vm_longname2Cell(_x,_cell)	((_cell)=(Cell)(_x))
  128: #define vm_r2Float(_x,_float)		(_float=_x)
  129: 
  130: #define vm_Cell2Cell(_x,_y)		(_y=_x)
  131: 
  132: #ifdef NO_IP
  133: #define IMM_ARG(access,value)		(VARIANT(value))
  134: #else
  135: #define IMM_ARG(access,value)		(access)
  136: #endif
  137: 
  138: /* if machine.h has not defined explicit registers, define them as implicit */
  139: #ifndef IPREG
  140: #define IPREG
  141: #endif
  142: #ifndef SPREG
  143: #define SPREG
  144: #endif
  145: #ifndef RPREG
  146: #define RPREG
  147: #endif
  148: #ifndef FPREG
  149: #define FPREG
  150: #endif
  151: #ifndef LPREG
  152: #define LPREG
  153: #endif
  154: #ifndef CFAREG
  155: #define CFAREG
  156: #endif
  157: #ifndef UPREG
  158: #define UPREG
  159: #endif
  160: #ifndef TOSREG
  161: #define TOSREG
  162: #endif
  163: #ifndef FTOSREG
  164: #define FTOSREG
  165: #endif
  166: 
  167: #ifndef CPU_DEP1
  168: # define CPU_DEP1 0
  169: #endif
  170: 
  171: /* instructions containing these must be the last instruction of a
  172:    super-instruction (e.g., branches, EXECUTE, and other instructions
  173:    ending the basic block). Instructions containing SET_IP get this
  174:    automatically, so you usually don't have to write it.  If you have
  175:    to write it, write it after IP points to the next instruction.
  176:    Used for profiling.  Don't write it in a word containing SET_IP, or
  177:    the following block will be counted twice. */
  178: #ifdef VM_PROFILING
  179: #define SUPER_END  vm_count_block(IP)
  180: #else
  181: #define SUPER_END
  182: #endif
  183: #define SUPER_CONTINUE
  184: 
  185: #ifdef DEBUG
  186: #define CFA_TO_NAME(__cfa) \
  187:       Cell len, i; \
  188:       char * name = __cfa; \
  189:       for(i=0; i<32; i+=sizeof(Cell)) { \
  190:         len = ((Cell*)name)[-1]; \
  191:         if(len < 0) { \
  192: 	  len &= 0x1F; \
  193:           if((len+sizeof(Cell)) > i) break; \
  194: 	} len = 0; \
  195: 	name -= sizeof(Cell); \
  196:       }
  197: #endif
  198: 
  199: define(enginerest,
  200: `(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
  201: /* executes code at ip, if ip!=NULL
  202:    returns array of machine code labels (for use in a loader), if ip==NULL
  203: */
  204: {
  205: #ifndef GFORTH_DEBUGGING
  206:   register Cell *rp RPREG;
  207: #endif
  208: #ifndef NO_IP
  209:   register Xt *ip IPREG = ip0;
  210: #endif
  211:   register Cell *sp SPREG = sp0;
  212:   register Float *fp FPREG = fp0;
  213:   register Address lp LPREG = lp0;
  214:   register Xt cfa CFAREG;
  215: #ifdef MORE_VARS
  216:   MORE_VARS
  217: #endif
  218:   register Address up UPREG = UP;
  219:   IF_spTOS(register Cell spTOS TOSREG;)
  220:   IF_fpTOS(register Float fpTOS FTOSREG;)
  221: #if defined(DOUBLY_INDIRECT)
  222:   static Label *symbols;
  223:   static void *routines[]= {
  224: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
  225: #else /* !defined(DOUBLY_INDIRECT) */
  226:   static Label symbols[]= {
  227: #define MAX_SYMBOLS (sizeof(symbols)/sizeof(symbols[0]))
  228: #endif /* !defined(DOUBLY_INDIRECT) */
  229:     (Label)&&docol,
  230:     (Label)&&docon,
  231:     (Label)&&dovar,
  232:     (Label)&&douser,
  233:     (Label)&&dodefer,
  234:     (Label)&&dofield,
  235:     (Label)&&dodoes,
  236:     /* the following entry is normally unused;
  237:        it is there because its index indicates a does-handler */
  238:     CPU_DEP1,
  239: #define INST_ADDR(name) (Label)&&I_##name
  240: #include "prim_lab.i"
  241: #undef INST_ADDR
  242:     (Label)&&after_last,
  243:     (Label)0,
  244: #define INST_ADDR(name) (Label)&&K_##name
  245: #include "prim_lab.i"
  246: #undef INST_ADDR
  247: #ifdef IN_ENGINE2
  248: #define INST_ADDR(name) (Label)&&J_##name
  249: #include "prim_lab.i"
  250: #undef INST_ADDR
  251: #endif
  252:   };
  253: #ifdef CPU_DEP2
  254:   CPU_DEP2
  255: #endif
  256: 
  257:   rp = rp0;
  258: #ifdef DEBUG
  259:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
  260:           (unsigned)ip0,(unsigned)sp,(unsigned)rp,
  261: 	  (unsigned)fp,(unsigned)lp,(unsigned)up);
  262: #endif
  263: 
  264:   if (ip0 == NULL) {
  265: #if defined(DOUBLY_INDIRECT)
  266: #define CODE_OFFSET (26*sizeof(Cell))
  267: #define XT_OFFSET (22*sizeof(Cell))
  268:     int i;
  269:     Cell code_offset = offset_image? CODE_OFFSET : 0;
  270:     Cell xt_offset = offset_image? XT_OFFSET : 0;
  271: 
  272:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
  273:     xts = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+XT_OFFSET)+xt_offset);
  274:     for (i=0; i<DOESJUMP+1; i++)
  275:       xts[i] = symbols[i] = (Label)routines[i];
  276:     for (; routines[i]!=0; i++) {
  277:       if (i>=MAX_SYMBOLS) {
  278: 	fprintf(stderr,"gforth-ditc: more than %d primitives\n",MAX_SYMBOLS);
  279: 	exit(1);
  280:       }
  281:       xts[i] = symbols[i] = &routines[i];
  282:     }
  283: #endif /* defined(DOUBLY_INDIRECT) */
  284:     return symbols;
  285:   }
  286: 
  287:   IF_spTOS(spTOS = sp[0]);
  288:   IF_fpTOS(fpTOS = fp[0]);
  289: /*  prep_terminal(); */
  290: #ifdef NO_IP
  291:   goto *(*(Label *)ip0);
  292: #else
  293:   SET_IP(ip);
  294:   SUPER_END; /* count the first block, too */
  295:   NEXT;
  296: #endif
  297: 
  298: #ifdef CPU_DEP3
  299:   CPU_DEP3
  300: #endif
  301:   
  302:  docol:
  303:   {
  304: #ifdef NO_IP
  305:     *--rp = next_code;
  306:     goto **(Label *)PFA1(cfa);
  307: #else
  308: #ifdef DEBUG
  309:     {
  310:       CFA_TO_NAME(cfa);
  311:       fprintf(stderr,"%08lx: col: %08lx %.*s\n",(Cell)ip,(Cell)PFA1(cfa),
  312: 	      len,name);
  313:     }
  314: #endif
  315: #ifdef CISC_NEXT
  316:     /* this is the simple version */
  317:     *--rp = (Cell)ip;
  318:     SET_IP((Xt *)PFA1(cfa));
  319:     SUPER_END;
  320:     NEXT;
  321: #else
  322:     /* this one is important, so we help the compiler optimizing */
  323:     {
  324:       DEF_CA
  325:       rp[-1] = (Cell)ip;
  326:       SET_IP((Xt *)PFA1(cfa));
  327:       SUPER_END;
  328:       NEXT_P1;
  329:       rp--;
  330:       NEXT_P2;
  331:     }
  332: #endif
  333: #endif
  334:   }
  335: 
  336:  docon:
  337:   {
  338: #ifdef DEBUG
  339:     fprintf(stderr,"%08lx: con: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
  340: #endif
  341: #ifdef USE_TOS
  342:     *sp-- = spTOS;
  343:     spTOS = *(Cell *)PFA1(cfa);
  344: #else
  345:     *--sp = *(Cell *)PFA1(cfa);
  346: #endif
  347:   }
  348: #ifdef NO_IP
  349:   goto *next_code;
  350: #else
  351:   NEXT_P0;
  352:   NEXT;
  353: #endif
  354:   
  355:  dovar:
  356:   {
  357: #ifdef DEBUG
  358:     fprintf(stderr,"%08lx: var: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  359: #endif
  360: #ifdef USE_TOS
  361:     *sp-- = spTOS;
  362:     spTOS = (Cell)PFA1(cfa);
  363: #else
  364:     *--sp = (Cell)PFA1(cfa);
  365: #endif
  366:   }
  367: #ifdef NO_IP
  368:   goto *next_code;
  369: #else
  370:   NEXT_P0;
  371:   NEXT;
  372: #endif
  373:   
  374:  douser:
  375:   {
  376: #ifdef DEBUG
  377:     fprintf(stderr,"%08lx: user: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  378: #endif
  379: #ifdef USE_TOS
  380:     *sp-- = spTOS;
  381:     spTOS = (Cell)(up+*(Cell*)PFA1(cfa));
  382: #else
  383:     *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
  384: #endif
  385:   }
  386: #ifdef NO_IP
  387:   goto *next_code;
  388: #else
  389:   NEXT_P0;
  390:   NEXT;
  391: #endif
  392:   
  393:  dodefer:
  394:   {
  395: #ifdef DEBUG
  396:     fprintf(stderr,"%08lx: defer: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
  397: #endif
  398:     SUPER_END;
  399:     EXEC(*(Xt *)PFA1(cfa));
  400:   }
  401: 
  402:  dofield:
  403:   {
  404: #ifdef DEBUG
  405:     fprintf(stderr,"%08lx: field: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  406: #endif
  407:     spTOS += *(Cell*)PFA1(cfa);
  408:   }
  409: #ifdef NO_IP
  410:   goto *next_code;
  411: #else
  412:   NEXT_P0;
  413:   NEXT;
  414: #endif
  415: 
  416:  dodoes:
  417:   /* this assumes the following structure:
  418:      defining-word:
  419:      
  420:      ...
  421:      DOES>
  422:      (possible padding)
  423:      possibly handler: jmp dodoes
  424:      (possible branch delay slot(s))
  425:      Forth code after DOES>
  426:      
  427:      defined word:
  428:      
  429:      cfa: address of or jump to handler OR
  430:           address of or jump to dodoes, address of DOES-code
  431:      pfa:
  432:      
  433:      */
  434: #ifdef NO_IP
  435:   *--rp = next_code;
  436:   IF_spTOS(spTOS = sp[0]);
  437:   sp--;
  438:   spTOS = (Cell)PFA(cfa);
  439:   goto **(Label *)DOES_CODE1(cfa);
  440: #else
  441:   {
  442:     /*    fprintf(stderr, "Got CFA %08lx at doescode %08lx/%08lx: does: %08lx\n",cfa,(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));*/
  443: #ifdef DEBUG
  444:     fprintf(stderr,"%08lx/%08lx: does: %08lx\n",(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));
  445:     fflush(stderr);
  446: #endif
  447:     *--rp = (Cell)ip;
  448:     /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
  449: #ifdef USE_TOS
  450:     *sp-- = spTOS;
  451:     spTOS = (Cell)PFA(cfa);
  452: #else
  453:     *--sp = (Cell)PFA(cfa);
  454: #endif
  455:     SET_IP(DOES_CODE1(cfa));
  456:     SUPER_END;
  457:     /*    fprintf(stderr,"TOS = %08lx, IP=%08lx\n", spTOS, IP);*/
  458:   }
  459:   NEXT;
  460: #endif
  461: 
  462: #ifndef IN_ENGINE2
  463: #define LABEL(name) I_##name:
  464: #else
  465: #define LABEL(name) J_##name: asm(".skip 16"); I_##name:
  466: #endif
  467: #define LABEL2(name) K_##name:
  468: #include "prim.i"
  469: #undef LABEL
  470:   after_last: return (Label *)0;
  471:   /*needed only to get the length of the last primitive */
  472: }'
  473: )
  474: 
  475: #define VARIANT(v)	(v)
  476: #define JUMP(target)	goto I_noop
  477: 
  478: Label *engine enginerest
  479: 
  480: #ifndef NO_DYNAMIC
  481: 
  482: #ifdef NO_IP
  483: #undef VARIANT
  484: #define VARIANT(v)	((v)^0xffffffff)
  485: #undef JUMP
  486: #define JUMP(target)	goto K_lit
  487: Label *engine3 enginerest
  488: #endif
  489: 
  490: #undef VARIANT
  491: #define VARIANT(v)	(v)
  492: #undef JUMP
  493: #define JUMP(target)	goto I_noop
  494: #define IN_ENGINE2
  495: Label *engine2 enginerest
  496: #endif

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