File:  [gforth] / gforth / Attic / engine.c
Revision 1.40: download - view: text, annotated - select for diffs
Sun Feb 16 20:51:07 1997 UTC (27 years, 2 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
go up to 0.3.0

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

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