File:  [gforth] / gforth / engine / engine.c
Revision 1.9: download - view: text, annotated - select for diffs
Sun Dec 20 23:17:56 1998 UTC (21 years, 10 months ago) by pazsan
Branches: MAIN
CVS tags: v0-4-0, HEAD
Changes to make gforth run on DOS and Win32 (I hope it's not broken by the
latest Makefile.in changes)
Some minor tweaks

    1: /* Gforth virtual machine (aka inner interpreter)
    2: 
    3:   Copyright (C) 1995,1996,1997,1998 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., 675 Mass Ave, Cambridge, MA 02139, USA.
   20: */
   21: 
   22: #include "config.h"
   23: #include <ctype.h>
   24: #include <stdio.h>
   25: #include <string.h>
   26: #include <math.h>
   27: #include <assert.h>
   28: #include <stdlib.h>
   29: #include <errno.h>
   30: #include "forth.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: #else
   42: #include "systypes.h"
   43: #endif
   44: 
   45: #if defined(HAVE_LIBDL) || defined(HAVE_DLOPEN) /* what else? */
   46: #include <dlfcn.h>
   47: #endif
   48: #if defined(_WIN32)
   49: #include <windows.h>
   50: #endif
   51: #ifdef hpux
   52: #include <dl.h>
   53: #endif
   54: 
   55: #ifndef SEEK_SET
   56: /* should be defined in stdio.h, but some systems don't have it */
   57: #define SEEK_SET 0
   58: #endif
   59: 
   60: #define IOR(flag)	((flag)? -512-errno : 0)
   61: 
   62: struct F83Name {
   63:   struct F83Name *next;  /* the link field for old hands */
   64:   char		countetc;
   65:   char		name[0];
   66: };
   67: 
   68: /* are macros for setting necessary? */
   69: #define F83NAME_COUNT(np)	((np)->countetc & 0x1f)
   70: #define F83NAME_SMUDGE(np)	(((np)->countetc & 0x40) != 0)
   71: #define F83NAME_IMMEDIATE(np)	(((np)->countetc & 0x20) != 0)
   72: 
   73: Cell *SP;
   74: Float *FP;
   75: Address UP=NULL;
   76: 
   77: #if 0
   78: /* not used currently */
   79: int emitcounter;
   80: #endif
   81: #define NULLC '\0'
   82: 
   83: #ifdef HAS_FILE
   84: char *cstr(Char *from, UCell size, int clear)
   85: /* return a C-string corresponding to the Forth string ( FROM SIZE ).
   86:    the C-string lives until the next call of cstr with CLEAR being true */
   87: {
   88:   static struct cstr_buffer {
   89:     char *buffer;
   90:     size_t size;
   91:   } *buffers=NULL;
   92:   static int nbuffers=0;
   93:   static int used=0;
   94:   struct cstr_buffer *b;
   95: 
   96:   if (buffers==NULL)
   97:     buffers=malloc(0);
   98:   if (clear)
   99:     used=0;
  100:   if (used>=nbuffers) {
  101:     buffers=realloc(buffers,sizeof(struct cstr_buffer)*(used+1));
  102:     buffers[used]=(struct cstr_buffer){malloc(0),0};
  103:     nbuffers=used+1;
  104:   }
  105:   b=&buffers[used];
  106:   if (size+1 > b->size) {
  107:     b->buffer = realloc(b->buffer,size+1);
  108:     b->size = size+1;
  109:   }
  110:   memcpy(b->buffer,from,size);
  111:   b->buffer[size]='\0';
  112:   used++;
  113:   return b->buffer;
  114: }
  115: 
  116: char *tilde_cstr(Char *from, UCell size, int clear)
  117: /* like cstr(), but perform tilde expansion on the string */
  118: {
  119:   char *s1,*s2;
  120:   int s1_len, s2_len;
  121:   struct passwd *getpwnam (), *user_entry;
  122: 
  123:   if (size<1 || from[0]!='~')
  124:     return cstr(from, size, clear);
  125:   if (size<2 || from[1]=='/') {
  126:     s1 = (char *)getenv ("HOME");
  127:     if(s1 == NULL)
  128:       s1 = "";
  129:     s2 = from+1;
  130:     s2_len = size-1;
  131:   } else {
  132:     UCell i;
  133:     for (i=1; i<size && from[i]!='/'; i++)
  134:       ;
  135:     {
  136:       char user[i];
  137:       memcpy(user,from+1,i-1);
  138:       user[i-1]='\0';
  139:       user_entry=getpwnam(user);
  140:     }
  141:     if (user_entry==NULL)
  142:       return cstr(from, size, clear);
  143:     s1 = user_entry->pw_dir;
  144:     s2 = from+i;
  145:     s2_len = size-i;
  146:   }
  147:   s1_len = strlen(s1);
  148:   if (s1_len>1 && s1[s1_len-1]=='/')
  149:     s1_len--;
  150:   {
  151:     char path[s1_len+s2_len];
  152:     memcpy(path,s1,s1_len);
  153:     memcpy(path+s1_len,s2,s2_len);
  154:     return cstr(path,s1_len+s2_len,clear);
  155:   }
  156: }
  157: #endif
  158: 
  159: #define NEWLINE	'\n'
  160: 
  161: #ifndef HAVE_RINT
  162: #define rint(x)	floor((x)+0.5)
  163: #endif
  164: 
  165: #ifdef HAS_FILE
  166: static char* fileattr[6]={"r","rb","r+","r+b","w","wb"};
  167: 
  168: #ifndef O_BINARY
  169: #define O_BINARY 0
  170: #endif
  171: #ifndef O_TEXT
  172: #define O_TEXT 0
  173: #endif
  174: 
  175: static int ufileattr[6]= {
  176:   O_RDONLY|O_TEXT, O_RDONLY|O_BINARY,
  177:   O_RDWR  |O_TEXT, O_RDWR  |O_BINARY,
  178:   O_WRONLY|O_TEXT, O_WRONLY|O_BINARY };
  179: #endif
  180: 
  181: /* if machine.h has not defined explicit registers, define them as implicit */
  182: #ifndef IPREG
  183: #define IPREG
  184: #endif
  185: #ifndef SPREG
  186: #define SPREG
  187: #endif
  188: #ifndef RPREG
  189: #define RPREG
  190: #endif
  191: #ifndef FPREG
  192: #define FPREG
  193: #endif
  194: #ifndef LPREG
  195: #define LPREG
  196: #endif
  197: #ifndef CFAREG
  198: #define CFAREG
  199: #endif
  200: #ifndef UPREG
  201: #define UPREG
  202: #endif
  203: #ifndef TOSREG
  204: #define TOSREG
  205: #endif
  206: #ifndef FTOSREG
  207: #define FTOSREG
  208: #endif
  209: 
  210: #ifndef CPU_DEP1
  211: # define CPU_DEP1 0
  212: #endif
  213: 
  214: /* declare and compute cfa for certain threading variants */
  215: /* warning: this is nonsyntactical; it will not work in place of a statement */
  216: #ifdef CFA_NEXT
  217: #define DOCFA
  218: #else
  219: #define DOCFA	Xt cfa; GETCFA(cfa)
  220: #endif
  221: 
  222: Label *engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
  223: /* executes code at ip, if ip!=NULL
  224:    returns array of machine code labels (for use in a loader), if ip==NULL
  225: */
  226: {
  227:   register Xt *ip IPREG = ip0;
  228:   register Cell *sp SPREG = sp0;
  229:   register Cell *rp RPREG = rp0;
  230:   register Float *fp FPREG = fp0;
  231:   register Address lp LPREG = lp0;
  232: #ifdef CFA_NEXT
  233:   register Xt cfa CFAREG;
  234: #endif
  235:   register Address up UPREG = UP;
  236:   IF_TOS(register Cell TOS TOSREG;)
  237:   IF_FTOS(register Float FTOS FTOSREG;)
  238: #if defined(DOUBLY_INDIRECT)
  239:   static Label *symbols;
  240:   static void *routines[]= {
  241: #else /* !defined(DOUBLY_INDIRECT) */
  242:   static Label symbols[]= {
  243: #endif /* !defined(DOUBLY_INDIRECT) */
  244:     (Label)&&docol,
  245:     (Label)&&docon,
  246:     (Label)&&dovar,
  247:     (Label)&&douser,
  248:     (Label)&&dodefer,
  249:     (Label)&&dofield,
  250:     (Label)&&dodoes,
  251:     /* the following entry is normally unused;
  252:        it's there because its index indicates a does-handler */
  253:     CPU_DEP1,
  254: #include "prim_lab.i"
  255:     (Label)0
  256:   };
  257: #ifdef CPU_DEP2
  258:   CPU_DEP2
  259: #endif
  260: 
  261: #ifdef DEBUG
  262:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
  263:           (unsigned)ip,(unsigned)sp,(unsigned)rp,
  264: 	  (unsigned)fp,(unsigned)lp,(unsigned)up);
  265: #endif
  266: 
  267:   if (ip == NULL) {
  268: #if defined(DOUBLY_INDIRECT)
  269: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
  270: #define CODE_OFFSET (22*sizeof(Cell))
  271:     int i;
  272:     Cell code_offset = offset_image? CODE_OFFSET : 0;
  273: 
  274:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
  275:     for (i=0; i<DOESJUMP+1; i++)
  276:       symbols[i] = (Label)routines[i];
  277:     for (; routines[i]!=0; i++) {
  278:       if (i>=MAX_SYMBOLS) {
  279: 	fprintf(stderr,"gforth-ditc: more than %d primitives\n",MAX_SYMBOLS);
  280: 	exit(1);
  281:     }
  282:     symbols[i] = &routines[i];
  283:   }
  284: #endif /* defined(DOUBLY_INDIRECT) */
  285:   return symbols;
  286: }
  287: 
  288:   IF_TOS(TOS = sp[0]);
  289:   IF_FTOS(FTOS = fp[0]);
  290: /*  prep_terminal(); */
  291:   NEXT_P0;
  292:   NEXT;
  293: 
  294: #ifdef CPU_DEP3
  295:   CPU_DEP3
  296: #endif
  297:   
  298:  docol:
  299:   {
  300:     DOCFA;
  301: #ifdef DEBUG
  302:     fprintf(stderr,"%08lx: col: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  303: #endif
  304: #ifdef CISC_NEXT
  305:     /* this is the simple version */
  306:     *--rp = (Cell)ip;
  307:     ip = (Xt *)PFA1(cfa);
  308:     NEXT_P0;
  309:     NEXT;
  310: #else
  311:     /* this one is important, so we help the compiler optimizing
  312:        The following version may be better (for scheduling), but probably has
  313:        problems with code fields employing calls and delay slots
  314:        */
  315:     {
  316:       DEF_CA
  317:       Xt *current_ip = (Xt *)PFA1(cfa);
  318:       cfa = *current_ip;
  319:       NEXT1_P1;
  320:       *--rp = (Cell)ip;
  321:       ip = current_ip+1;
  322:       NEXT1_P2;
  323:     }
  324: #endif
  325:   }
  326: 
  327:  docon:
  328:   {
  329:     DOCFA;
  330: #ifdef DEBUG
  331:     fprintf(stderr,"%08lx: con: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
  332: #endif
  333: #ifdef USE_TOS
  334:     *sp-- = TOS;
  335:     TOS = *(Cell *)PFA1(cfa);
  336: #else
  337:     *--sp = *(Cell *)PFA1(cfa);
  338: #endif
  339:   }
  340:   NEXT_P0;
  341:   NEXT;
  342:   
  343:  dovar:
  344:   {
  345:     DOCFA;
  346: #ifdef DEBUG
  347:     fprintf(stderr,"%08lx: var: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  348: #endif
  349: #ifdef USE_TOS
  350:     *sp-- = TOS;
  351:     TOS = (Cell)PFA1(cfa);
  352: #else
  353:     *--sp = (Cell)PFA1(cfa);
  354: #endif
  355:   }
  356:   NEXT_P0;
  357:   NEXT;
  358:   
  359:  douser:
  360:   {
  361:     DOCFA;
  362: #ifdef DEBUG
  363:     fprintf(stderr,"%08lx: user: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  364: #endif
  365: #ifdef USE_TOS
  366:     *sp-- = TOS;
  367:     TOS = (Cell)(up+*(Cell*)PFA1(cfa));
  368: #else
  369:     *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
  370: #endif
  371:   }
  372:   NEXT_P0;
  373:   NEXT;
  374:   
  375:  dodefer:
  376:   {
  377:     DOCFA;
  378: #ifdef DEBUG
  379:     fprintf(stderr,"%08lx: defer: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
  380: #endif
  381:     EXEC(*(Xt *)PFA1(cfa));
  382:   }
  383: 
  384:  dofield:
  385:   {
  386:     DOCFA;
  387: #ifdef DEBUG
  388:     fprintf(stderr,"%08lx: field: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
  389: #endif
  390:     TOS += *(Cell*)PFA1(cfa); 
  391:   }
  392:   NEXT_P0;
  393:   NEXT;
  394: 
  395:  dodoes:
  396:   /* this assumes the following structure:
  397:      defining-word:
  398:      
  399:      ...
  400:      DOES>
  401:      (possible padding)
  402:      possibly handler: jmp dodoes
  403:      (possible branch delay slot(s))
  404:      Forth code after DOES>
  405:      
  406:      defined word:
  407:      
  408:      cfa: address of or jump to handler OR
  409:           address of or jump to dodoes, address of DOES-code
  410:      pfa:
  411:      
  412:      */
  413:   {
  414:     DOCFA;
  415: 
  416:     /*    fprintf(stderr, "Got CFA %08lx at doescode %08lx/%08lx: does: %08lx\n",cfa,(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));*/
  417: #ifdef DEBUG
  418:     fprintf(stderr,"%08lx/%08lx: does: %08lx\n",(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));
  419:     fflush(stderr);
  420: #endif
  421:     *--rp = (Cell)ip;
  422:     /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
  423:     ip = DOES_CODE1(cfa);
  424: #ifdef USE_TOS
  425:     *sp-- = TOS;
  426:     TOS = (Cell)PFA(cfa);
  427: #else
  428:     *--sp = (Cell)PFA(cfa);
  429: #endif
  430:     /*    fprintf(stderr,"TOS = %08lx, IP=%08lx\n", TOS, IP);*/
  431:   }
  432:   NEXT_P0;
  433:   NEXT;
  434: 
  435: #include "prim.i"
  436: }

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