File:  [gforth] / gforth / engine / engine.c
Revision 1.58: download - view: text, annotated - select for diffs
Sun Jan 26 20:56:38 2003 UTC (21 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
use AC_SYS_LARGEFILE instead of declaring this stuff unconditionally
moved memcasecmp() into support.c
eliminated -TEXT (all uses replaced, except within prim)
converted CAPSCOMP and -TRAILING into high-level words
folded C code for more complex primitives into support.c
Fliterals are now decompiled ok
f.rdp etc. documentation changes
added test cases for SEARCH

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

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