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

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

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