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

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

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