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

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: 
1.9       pazsan     83: #ifdef HAS_FILE
1.1       anton      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:       ;
1.13    ! anton     135:     if (i==2 && from[1]=='+') /* deal with "~+", i.e., the wd */
        !           136:       return cstr(from+3, size<3?0:size-3,clear);
1.1       anton     137:     {
                    138:       char user[i];
                    139:       memcpy(user,from+1,i-1);
                    140:       user[i-1]='\0';
                    141:       user_entry=getpwnam(user);
                    142:     }
                    143:     if (user_entry==NULL)
                    144:       return cstr(from, size, clear);
                    145:     s1 = user_entry->pw_dir;
                    146:     s2 = from+i;
                    147:     s2_len = size-i;
                    148:   }
                    149:   s1_len = strlen(s1);
                    150:   if (s1_len>1 && s1[s1_len-1]=='/')
                    151:     s1_len--;
                    152:   {
                    153:     char path[s1_len+s2_len];
                    154:     memcpy(path,s1,s1_len);
                    155:     memcpy(path+s1_len,s2,s2_len);
                    156:     return cstr(path,s1_len+s2_len,clear);
                    157:   }
                    158: }
1.9       pazsan    159: #endif
1.1       anton     160: 
                    161: #define NEWLINE        '\n'
                    162: 
                    163: #ifndef HAVE_RINT
                    164: #define rint(x)        floor((x)+0.5)
                    165: #endif
                    166: 
1.9       pazsan    167: #ifdef HAS_FILE
1.1       anton     168: static char* fileattr[6]={"r","rb","r+","r+b","w","wb"};
                    169: 
                    170: #ifndef O_BINARY
                    171: #define O_BINARY 0
                    172: #endif
                    173: #ifndef O_TEXT
                    174: #define O_TEXT 0
                    175: #endif
                    176: 
                    177: static int ufileattr[6]= {
                    178:   O_RDONLY|O_TEXT, O_RDONLY|O_BINARY,
                    179:   O_RDWR  |O_TEXT, O_RDWR  |O_BINARY,
                    180:   O_WRONLY|O_TEXT, O_WRONLY|O_BINARY };
1.9       pazsan    181: #endif
1.1       anton     182: 
                    183: /* if machine.h has not defined explicit registers, define them as implicit */
                    184: #ifndef IPREG
                    185: #define IPREG
                    186: #endif
                    187: #ifndef SPREG
                    188: #define SPREG
                    189: #endif
                    190: #ifndef RPREG
                    191: #define RPREG
                    192: #endif
                    193: #ifndef FPREG
                    194: #define FPREG
                    195: #endif
                    196: #ifndef LPREG
                    197: #define LPREG
                    198: #endif
                    199: #ifndef CFAREG
                    200: #define CFAREG
                    201: #endif
                    202: #ifndef UPREG
                    203: #define UPREG
                    204: #endif
                    205: #ifndef TOSREG
                    206: #define TOSREG
                    207: #endif
                    208: #ifndef FTOSREG
                    209: #define FTOSREG
                    210: #endif
                    211: 
                    212: #ifndef CPU_DEP1
                    213: # define CPU_DEP1 0
                    214: #endif
                    215: 
                    216: /* declare and compute cfa for certain threading variants */
                    217: /* warning: this is nonsyntactical; it will not work in place of a statement */
1.12      anton     218: #ifndef GETCFA
1.1       anton     219: #define DOCFA
                    220: #else
                    221: #define DOCFA  Xt cfa; GETCFA(cfa)
                    222: #endif
                    223: 
1.10      anton     224: #ifdef GFORTH_DEBUGGING
                    225: /* define some VM registers as global variables, so they survive exceptions;
                    226:    global register variables are not up to the task (according to the 
                    227:    GNU C manual) */
                    228: Xt *ip;
                    229: Cell *rp;
                    230: #endif
                    231: 
1.1       anton     232: Label *engine(Xt *ip0, Cell *sp0, Cell *rp0, Float *fp0, Address lp0)
                    233: /* executes code at ip, if ip!=NULL
                    234:    returns array of machine code labels (for use in a loader), if ip==NULL
                    235: */
                    236: {
1.10      anton     237: #ifndef GFORTH_DEBUGGING
                    238:   register Xt *ip IPREG;
                    239:   register Cell *rp RPREG;
                    240: #endif
1.1       anton     241:   register Cell *sp SPREG = sp0;
                    242:   register Float *fp FPREG = fp0;
                    243:   register Address lp LPREG = lp0;
                    244: #ifdef CFA_NEXT
                    245:   register Xt cfa CFAREG;
                    246: #endif
1.11      anton     247: #ifdef MORE_VARS
                    248:   MORE_VARS
                    249: #endif
1.1       anton     250:   register Address up UPREG = UP;
                    251:   IF_TOS(register Cell TOS TOSREG;)
                    252:   IF_FTOS(register Float FTOS FTOSREG;)
                    253: #if defined(DOUBLY_INDIRECT)
                    254:   static Label *symbols;
                    255:   static void *routines[]= {
                    256: #else /* !defined(DOUBLY_INDIRECT) */
                    257:   static Label symbols[]= {
                    258: #endif /* !defined(DOUBLY_INDIRECT) */
1.4       pazsan    259:     (Label)&&docol,
                    260:     (Label)&&docon,
                    261:     (Label)&&dovar,
                    262:     (Label)&&douser,
                    263:     (Label)&&dodefer,
                    264:     (Label)&&dofield,
                    265:     (Label)&&dodoes,
1.1       anton     266:     /* the following entry is normally unused;
                    267:        it's there because its index indicates a does-handler */
1.7       pazsan    268:     CPU_DEP1,
1.1       anton     269: #include "prim_lab.i"
1.4       pazsan    270:     (Label)0
1.1       anton     271:   };
                    272: #ifdef CPU_DEP2
                    273:   CPU_DEP2
                    274: #endif
                    275: 
1.10      anton     276:   ip = ip0;
                    277:   rp = rp0;
1.1       anton     278: #ifdef DEBUG
                    279:   fprintf(stderr,"ip=%x, sp=%x, rp=%x, fp=%x, lp=%x, up=%x\n",
                    280:           (unsigned)ip,(unsigned)sp,(unsigned)rp,
                    281:          (unsigned)fp,(unsigned)lp,(unsigned)up);
                    282: #endif
                    283: 
                    284:   if (ip == NULL) {
                    285: #if defined(DOUBLY_INDIRECT)
1.3       anton     286: #define MAX_SYMBOLS (sizeof(routines)/sizeof(routines[0]))
                    287: #define CODE_OFFSET (22*sizeof(Cell))
1.1       anton     288:     int i;
1.3       anton     289:     Cell code_offset = offset_image? CODE_OFFSET : 0;
1.7       pazsan    290: 
1.3       anton     291:     symbols = (Label *)(malloc(MAX_SYMBOLS*sizeof(Cell)+CODE_OFFSET)+code_offset);
1.1       anton     292:     for (i=0; i<DOESJUMP+1; i++)
1.7       pazsan    293:       symbols[i] = (Label)routines[i];
1.1       anton     294:     for (; routines[i]!=0; i++) {
                    295:       if (i>=MAX_SYMBOLS) {
                    296:        fprintf(stderr,"gforth-ditc: more than %d primitives\n",MAX_SYMBOLS);
                    297:        exit(1);
                    298:     }
1.7       pazsan    299:     symbols[i] = &routines[i];
                    300:   }
1.5       pazsan    301: #endif /* defined(DOUBLY_INDIRECT) */
1.7       pazsan    302:   return symbols;
                    303: }
1.1       anton     304: 
                    305:   IF_TOS(TOS = sp[0]);
                    306:   IF_FTOS(FTOS = fp[0]);
1.7       pazsan    307: /*  prep_terminal(); */
1.11      anton     308:   SET_IP(ip);
1.1       anton     309:   NEXT;
                    310: 
1.11      anton     311: 
1.1       anton     312: #ifdef CPU_DEP3
                    313:   CPU_DEP3
                    314: #endif
                    315:   
                    316:  docol:
                    317:   {
                    318:     DOCFA;
                    319: #ifdef DEBUG
                    320:     fprintf(stderr,"%08lx: col: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
                    321: #endif
                    322: #ifdef CISC_NEXT
                    323:     /* this is the simple version */
                    324:     *--rp = (Cell)ip;
1.11      anton     325:     SET_IP((Xt *)PFA1(cfa));
1.1       anton     326:     NEXT;
                    327: #else
1.11      anton     328:     /* this one is important, so we help the compiler optimizing */
1.1       anton     329:     {
                    330:       DEF_CA
1.11      anton     331:       rp[-1] = (Cell)ip;
                    332:       SET_IP((Xt *)PFA1(cfa));
                    333:       NEXT_P1;
                    334:       rp--;
                    335:       NEXT_P2;
1.1       anton     336:     }
                    337: #endif
                    338:   }
                    339: 
                    340:  docon:
                    341:   {
                    342:     DOCFA;
                    343: #ifdef DEBUG
                    344:     fprintf(stderr,"%08lx: con: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
                    345: #endif
                    346: #ifdef USE_TOS
                    347:     *sp-- = TOS;
                    348:     TOS = *(Cell *)PFA1(cfa);
                    349: #else
                    350:     *--sp = *(Cell *)PFA1(cfa);
                    351: #endif
                    352:   }
                    353:   NEXT_P0;
                    354:   NEXT;
                    355:   
                    356:  dovar:
                    357:   {
                    358:     DOCFA;
                    359: #ifdef DEBUG
                    360:     fprintf(stderr,"%08lx: var: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
                    361: #endif
                    362: #ifdef USE_TOS
                    363:     *sp-- = TOS;
                    364:     TOS = (Cell)PFA1(cfa);
                    365: #else
                    366:     *--sp = (Cell)PFA1(cfa);
                    367: #endif
                    368:   }
                    369:   NEXT_P0;
                    370:   NEXT;
                    371:   
                    372:  douser:
                    373:   {
                    374:     DOCFA;
                    375: #ifdef DEBUG
                    376:     fprintf(stderr,"%08lx: user: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
                    377: #endif
                    378: #ifdef USE_TOS
                    379:     *sp-- = TOS;
                    380:     TOS = (Cell)(up+*(Cell*)PFA1(cfa));
                    381: #else
                    382:     *--sp = (Cell)(up+*(Cell*)PFA1(cfa));
                    383: #endif
                    384:   }
                    385:   NEXT_P0;
                    386:   NEXT;
                    387:   
                    388:  dodefer:
                    389:   {
                    390:     DOCFA;
                    391: #ifdef DEBUG
                    392:     fprintf(stderr,"%08lx: defer: %08lx\n",(Cell)ip,*(Cell*)PFA1(cfa));
                    393: #endif
                    394:     EXEC(*(Xt *)PFA1(cfa));
                    395:   }
                    396: 
                    397:  dofield:
                    398:   {
                    399:     DOCFA;
                    400: #ifdef DEBUG
                    401:     fprintf(stderr,"%08lx: field: %08lx\n",(Cell)ip,(Cell)PFA1(cfa));
                    402: #endif
1.11      anton     403:     TOS += *(Cell*)PFA1(cfa);
1.1       anton     404:   }
                    405:   NEXT_P0;
                    406:   NEXT;
                    407: 
                    408:  dodoes:
                    409:   /* this assumes the following structure:
                    410:      defining-word:
                    411:      
                    412:      ...
                    413:      DOES>
                    414:      (possible padding)
                    415:      possibly handler: jmp dodoes
                    416:      (possible branch delay slot(s))
                    417:      Forth code after DOES>
                    418:      
                    419:      defined word:
                    420:      
                    421:      cfa: address of or jump to handler OR
                    422:           address of or jump to dodoes, address of DOES-code
                    423:      pfa:
                    424:      
                    425:      */
                    426:   {
                    427:     DOCFA;
                    428: 
                    429:     /*    fprintf(stderr, "Got CFA %08lx at doescode %08lx/%08lx: does: %08lx\n",cfa,(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));*/
                    430: #ifdef DEBUG
                    431:     fprintf(stderr,"%08lx/%08lx: does: %08lx\n",(Cell)ip,(Cell)PFA(cfa),(Cell)DOES_CODE1(cfa));
                    432:     fflush(stderr);
                    433: #endif
                    434:     *--rp = (Cell)ip;
                    435:     /* PFA1 might collide with DOES_CODE1 here, so we use PFA */
                    436: #ifdef USE_TOS
                    437:     *sp-- = TOS;
                    438:     TOS = (Cell)PFA(cfa);
                    439: #else
                    440:     *--sp = (Cell)PFA(cfa);
                    441: #endif
1.11      anton     442:     SET_IP(DOES_CODE1(cfa));
1.1       anton     443:     /*    fprintf(stderr,"TOS = %08lx, IP=%08lx\n", TOS, IP);*/
                    444:   }
                    445:   NEXT;
                    446: 
                    447: #include "prim.i"
                    448: }

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