File:  [gforth] / gforth / engine / engine.c
Revision 1.50: download - view: text, annotated - select for diffs
Thu Dec 19 23:12:07 2002 UTC (17 years, 5 months ago) by anton
Branches: MAIN
CVS tags: HEAD
each engine is now compiled separately; unfortunately, the problem with
  lots of differences between engine and engine2 in gforth-fast on i386 without
  force-reg persists

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

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