Annotation of gforth/engine.c, revision 1.38

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

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