Annotation of gforth/engine/engine.c, revision 1.21

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

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