Annotation of gforth/main.c, revision 1.16

1.1       anton       1: /*
1.16    ! pazsan      2:   $Id: main.c,v 1.15 1994/10/24 19:16:02 anton Exp $
1.1       anton       3:   Copyright 1993 by the ANSI figForth Development Group
                      4: */
                      5: 
                      6: #include <ctype.h>
                      7: #include <stdio.h>
                      8: #include <string.h>
                      9: #include <math.h>
                     10: #include <sys/types.h>
                     11: #include <sys/stat.h>
                     12: #include <fcntl.h>
                     13: #include <assert.h>
                     14: #include <stdlib.h>
                     15: #include "forth.h"
1.5       pazsan     16: #include "io.h"
1.12      pazsan     17: #ifdef USE_GETOPT
                     18: #  include "getopt.h"
                     19: #else
1.16    ! pazsan     20:    extern int getopt (int , char * const [], const char *);
1.12      pazsan     21: 
1.16    ! pazsan     22:    extern char *optarg;
        !            23:    extern int optind, opterr;
1.12      pazsan     24: #endif
1.2       pazsan     25: 
1.10      anton      26: #ifndef DEFAULTPATH
1.16    ! pazsan     27: #  define DEFAULTPATH "/usr/local/lib/gforth:."
1.10      anton      28: #endif
                     29: 
1.1       anton      30: #ifdef DIRECT_THREADED
1.16    ! pazsan     31: #  define CA(n)        (symbols[(n)])
1.1       anton      32: #else
1.16    ! pazsan     33: #  define CA(n)        ((int)(symbols+(n)))
1.1       anton      34: #endif
                     35: 
1.10      anton      36: #define maxaligned(n)  ((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
                     37: 
                     38: static int dictsize=0;
                     39: static int dsize=0;
                     40: static int rsize=0;
                     41: static int fsize=0;
                     42: static int lsize=0;
                     43: char *progname;
                     44: 
                     45: 
1.1       anton      46: /* image file format:
                     47:  *   size of image with stacks without tags (in bytes)
                     48:  *   size of image without stacks and tags (in bytes)
1.5       pazsan     49:  *   size of data and FP stack (in bytes)
1.1       anton      50:  *   pointer to start of code
1.7       anton      51:  *   pointer into throw (for signal handling)
1.16    ! pazsan     52:  *   pointer to dictionary
1.1       anton      53:  *   data (size in image[1])
                     54:  *   tags (1 bit/data cell)
                     55:  *
                     56:  * tag==1 mean that the corresponding word is an address;
                     57:  * If the word is >=0, the address is within the image;
                     58:  * addresses within the image are given relative to the start of the image.
                     59:  * If the word is =-1, the address is NIL,
1.2       pazsan     60:  * If the word is between -2 and -5, it's a CFA (:, Create, Constant, User)
                     61:  * If the word is -6, it's a DOES> CFA
                     62:  * If the word is -7, it's a DOES JUMP
                     63:  * If the word is <-7, it's a primitive
1.1       anton      64:  */
                     65: 
1.10      anton      66: void relocate(Cell *image, char *bitstring, int size, Label symbols[])
1.1       anton      67: {
1.16    ! pazsan     68:   int i=0, j, k, steps=(size/sizeof(Cell))/8;
        !            69:   char bits;
1.5       pazsan     70: /*   static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};*/
                     71:    
1.16    ! pazsan     72:   for(k=0; k<=steps; k++)
        !            73:     for(j=0, bits=bitstring[k]; j<8; j++, i++, bits<<=1)
        !            74:       if(bits & 0x80)
        !            75:        if(image[i]<0)
        !            76:          switch(image[i])
        !            77:            {
        !            78:            case CF_NIL      : image[i]=0; break;
        !            79:            case CF(DOCOL)   :
        !            80:            case CF(DOVAR)   :
        !            81:            case CF(DOCON)   :
        !            82:            case CF(DOUSER)  : 
        !            83:            case CF(DODEFER) : MAKE_CF(image+i,symbols[CF(image[i])]); break;
        !            84:            case CF(DODOES)  : MAKE_DOES_CF(image+i,image[i+1]+((int)image));
        !            85:              break;
        !            86:            case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
        !            87:            default          : image[i]=(Cell)CA(CF(image[i]));
        !            88:            }
        !            89:        else
        !            90:          image[i]+=(Cell)image;
1.1       anton      91: }
                     92: 
1.10      anton      93: Cell *loader(FILE *imagefile)
                     94: {
1.16    ! pazsan     95:   Cell header[3];
        !            96:   Cell *image;
        !            97:   int wholesize;
        !            98:   int imagesize; /* everything needed by the image */
        !            99:   
        !           100:   fread(header,1,3*sizeof(Cell),imagefile);
        !           101:   if (dictsize==0)
        !           102:     dictsize = header[0];
        !           103:   if (dsize==0)
        !           104:     dsize=header[2];
        !           105:   if (rsize==0)
        !           106:     rsize=header[2];
        !           107:   if (fsize==0)
        !           108:     fsize=header[2];
        !           109:   if (lsize==0)
        !           110:     lsize=header[2];
        !           111:   dictsize=maxaligned(dictsize);
        !           112:   dsize=maxaligned(dsize);
        !           113:   rsize=maxaligned(rsize);
        !           114:   lsize=maxaligned(lsize);
        !           115:   fsize=maxaligned(fsize);
        !           116:   
        !           117:   wholesize = dictsize+dsize+rsize+fsize+lsize;
        !           118:   imagesize = header[1]+((header[1]-1)/sizeof(Cell))/8+1;
        !           119:   image=malloc(wholesize>imagesize?wholesize:imagesize);
        !           120:   memset(image,0,wholesize); /* why? - anton */
        !           121:   image[0]=header[0];
        !           122:   image[1]=header[1];
        !           123:   image[2]=header[2];
        !           124:   
        !           125:   fread(image+3,1,header[1]-3*sizeof(Cell),imagefile);
        !           126:   fread(((void *)image)+header[1],1,((header[1]-1)/sizeof(Cell))/8+1,
        !           127:        imagefile);
        !           128:   fclose(imagefile);
        !           129:   
        !           130:   if(image[5]==0) {
        !           131:     relocate(image,(char *)image+header[1],header[1],engine(0,0,0,0,0));
        !           132:   }
        !           133:   else if(image[5]!=(Cell)image) {
        !           134:     fprintf(stderr,"Corrupted image address, please recompile image\n");
        !           135:     exit(1);
        !           136:   }
1.1       anton     137: 
1.16    ! pazsan    138:   CACHE_FLUSH(image,image[1]);
        !           139:   
        !           140:   return(image);
1.1       anton     141: }
                    142: 
1.10      anton     143: int go_forth(Cell *image, int stack, Cell *entries)
1.1       anton     144: {
1.15      anton     145:   Cell *sp=(Cell*)((void *)image+dictsize+dsize);
                    146:   Address lp=(Address)((void *)sp+lsize);
                    147:   Float *fp=(Float *)((void *)lp+fsize);
                    148:   Cell *rp=(Cell*)((void *)fp+rsize);
                    149:   Xt *ip=(Xt *)(image[3]);
                    150:   int throw_code;
                    151:   
                    152:   for(;stack>0;stack--)
                    153:     *--sp=entries[stack-1];
                    154:   
                    155:   install_signal_handlers(); /* right place? */
                    156:   
                    157:   if ((throw_code=setjmp(throw_jmp_buf))) {
                    158:     static Cell signal_data_stack[8];
                    159:     static Cell signal_return_stack[8];
                    160:     static Float signal_fp_stack[1];
                    161:     
                    162:     signal_data_stack[7]=throw_code;
                    163:     
                    164:     return((int)engine((Xt *)image[4],signal_data_stack+7,
                    165:                       signal_return_stack+8,signal_fp_stack,0));
                    166:   }
                    167:   
                    168:   return((int)engine(ip,sp,rp,fp,lp));
1.1       anton     169: }
                    170: 
1.10      anton     171: int convsize(char *s, int elemsize)
                    172: /* converts s of the format #+u (e.g. 25k) into the number of bytes.
                    173:    the unit u can be one of bekM, where e stands for the element
                    174:    size. default is e */
                    175: {
                    176:   char *endp;
                    177:   int n,m;
                    178: 
                    179:   m = elemsize;
                    180:   n = strtoul(s,&endp,0);
                    181:   if (endp!=NULL) {
                    182:     if (strcmp(endp,"b")==0)
                    183:       m=1;
                    184:     else if (strcmp(endp,"k")==0)
                    185:       m=1024;
                    186:     else if (strcmp(endp,"M")==0)
                    187:       m=1024*1024;
                    188:     else if (strcmp(endp,"e")!=0) {
                    189:       fprintf(stderr,"%s: cannot grok size specification %s\n", progname, s);
                    190:       exit(1);
                    191:     }
                    192:   }
                    193:   return n*m;
                    194: }
                    195: 
1.1       anton     196: int main(int argc, char **argv, char **env)
                    197: {
1.16    ! pazsan    198:   char *path, *path1;
        !           199:   char *imagename="gforth.fi";
        !           200:   FILE *image_file;
        !           201:   int c, retvalue;
1.10      anton     202:          
1.14      anton     203: #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
1.6       anton     204:        /* turn on alignment checks on the 486.
                    205:         * on the 386 this should have no effect. */
                    206:        __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
                    207: #endif
1.2       pazsan    208: 
1.16    ! pazsan    209:   progname = argv[0];
        !           210:   if ((path=getenv("GFORTHPATH"))==NULL)
        !           211:     path = strcpy(malloc(strlen(DEFAULTPATH)+1),DEFAULTPATH);
        !           212:   opterr=0;
        !           213:   while (1) {
        !           214:     int option_index=0;
1.12      pazsan    215: #ifdef USE_GETOPT
1.16    ! pazsan    216:     static struct option opts[] = {
        !           217:       {"image-file", required_argument, NULL, 'i'},
        !           218:       {"dictionary-size", required_argument, NULL, 'm'},
        !           219:       {"data-stack-size", required_argument, NULL, 'd'},
        !           220:       {"return-stack-size", required_argument, NULL, 'r'},
        !           221:       {"fp-stack-size", required_argument, NULL, 'f'},
        !           222:       {"locals-stack-size", required_argument, NULL, 'l'},
        !           223:       {"path", required_argument, NULL, 'p'},
        !           224:       {0,0,0,0}
        !           225:       /* no-init-file, no-rc? */
        !           226:     };
1.10      anton     227: 
1.16    ! pazsan    228:     c = getopt_long(argc, argv, "i:m:d:r:f:l:p:", opts, &option_index);
1.12      pazsan    229: #else
1.16    ! pazsan    230:     c = getopt(argc, argv, "i:m:d:r:f:l:p:");
1.12      pazsan    231: #endif
                    232: 
1.16    ! pazsan    233:     if (c==EOF)
        !           234:       break;
        !           235:     if (c=='?') {
        !           236:       optind--;
        !           237:       break;
        !           238:     }
        !           239:     switch (c) {
        !           240:     case 'i': imagename = optarg; break;
        !           241:     case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
        !           242:     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
        !           243:     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
        !           244:     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
        !           245:     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
        !           246:     case 'p': path = optarg; break;
        !           247:     }
        !           248:   }
        !           249:   path1=path;
        !           250:   do {
        !           251:     char *pend=strchr(path, ':');
        !           252:     if (pend==NULL)
        !           253:       pend=path+strlen(path);
        !           254:     if (strlen(path)==0) {
        !           255:       fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
        !           256:              progname, imagename, path1);
        !           257:       exit(1);
        !           258:     }
        !           259:     {
        !           260:       int dirlen=pend-path;
        !           261:       char fullfilename[dirlen+strlen(imagename)+2];
        !           262:       memcpy(fullfilename, path, dirlen);
        !           263:       if (fullfilename[dirlen-1]!='/')
        !           264:        fullfilename[dirlen++]='/';
        !           265:       strcpy(fullfilename+dirlen,imagename);
        !           266:       image_file=fopen(fullfilename,"rb");
        !           267:     }
        !           268:     path=pend+(*pend==':');
        !           269:   } while (image_file==NULL);
        !           270:   
        !           271:   {
        !           272:     Cell environ[]= {
        !           273:       (Cell)argc-(optind-1),
        !           274:       (Cell)(argv+(optind-1)),
        !           275:       (Cell)path1};
        !           276:     argv[optind-1] = progname;
        !           277:     /*
        !           278:        for (i=0; i<environ[0]; i++)
        !           279:        printf("%s\n", ((char **)(environ[1]))[i]);
        !           280:        */
        !           281:     retvalue=go_forth(loader(image_file),3,environ);
        !           282:     deprep_terminal();
        !           283:     exit(retvalue);
        !           284:   }
1.1       anton     285: }

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