Annotation of gforth/main.c, revision 1.39

1.30      anton       1: /* command line interpretation, image loading etc. for Gforth
                      2: 
                      3: 
                      4:   Copyright (C) 1995 Free Software Foundation, Inc.
                      5: 
                      6:   This file is part of Gforth.
                      7: 
                      8:   Gforth is free software; you can redistribute it and/or
                      9:   modify it under the terms of the GNU General Public License
                     10:   as published by the Free Software Foundation; either version 2
                     11:   of the License, or (at your option) any later version.
                     12: 
                     13:   This program is distributed in the hope that it will be useful,
                     14:   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16:   GNU General Public License for more details.
                     17: 
                     18:   You should have received a copy of the GNU General Public License
                     19:   along with this program; if not, write to the Free Software
                     20:   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1.1       anton      21: */
                     22: 
1.32      anton      23: #include "config.h"
1.35      anton      24: #include <errno.h>
1.1       anton      25: #include <ctype.h>
                     26: #include <stdio.h>
                     27: #include <string.h>
                     28: #include <math.h>
                     29: #include <sys/types.h>
                     30: #include <sys/stat.h>
                     31: #include <fcntl.h>
                     32: #include <assert.h>
                     33: #include <stdlib.h>
                     34: #include "forth.h"
1.5       pazsan     35: #include "io.h"
1.20      anton      36: #include "getopt.h"
1.2       pazsan     37: 
1.22      pazsan     38: #ifdef MSDOS
                     39: jmp_buf throw_jmp_buf;
                     40: #endif
                     41: 
1.10      anton      42: #ifndef DEFAULTPATH
1.16      pazsan     43: #  define DEFAULTPATH "/usr/local/lib/gforth:."
1.10      anton      44: #endif
                     45: 
1.1       anton      46: #ifdef DIRECT_THREADED
1.16      pazsan     47: #  define CA(n)        (symbols[(n)])
1.1       anton      48: #else
1.21      pazsan     49: #  define CA(n)        ((Cell)(symbols+(n)))
1.1       anton      50: #endif
                     51: 
1.23      pazsan     52: #define maxaligned(n)  (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
1.10      anton      53: 
1.21      pazsan     54: static Cell dictsize=0;
                     55: static Cell dsize=0;
                     56: static Cell rsize=0;
                     57: static Cell fsize=0;
                     58: static Cell lsize=0;
1.10      anton      59: char *progname;
                     60: 
1.1       anton      61: /* image file format:
1.35      anton      62:  *  "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.2.0 -i\n")
                     63:  *   padding to a multiple of 8
1.36      anton      64:  *   magic: "Gforth1x" means format 0.2,
1.35      anton      65:  *              where x is even for big endian and odd for little endian
                     66:  *              and x & ~1 is the size of the cell in bytes.
                     67:  *  padding to max alignment (no padding necessary on current machines)
                     68:  *  ImageHeader structure (see below)
                     69:  *  data (size in ImageHeader.image_size)
                     70:  *  tags ((if relocatable, 1 bit/data cell)
1.1       anton      71:  *
1.35      anton      72:  * tag==1 means that the corresponding word is an address;
1.1       anton      73:  * If the word is >=0, the address is within the image;
                     74:  * addresses within the image are given relative to the start of the image.
1.35      anton      75:  * If the word =-1 (CF_NIL), the address is NIL,
                     76:  * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
                     77:  * If the word =CF(DODOES), it's a DOES> CFA
                     78:  * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
                     79:  *                                     possibly containing a jump to dodoes)
                     80:  * If the word is <CF(DOESJUMP), it's a primitive
1.1       anton      81:  */
                     82: 
1.35      anton      83: typedef struct {
                     84:   Address base;                /* base address of image (0 if relocatable) */
                     85:   UCell checksum;      /* checksum of ca's to protect against some
                     86:                           incompatible binary/executable combinations
                     87:                           (0 if relocatable) */
                     88:   UCell image_size;    /* all sizes in bytes */
                     89:   UCell dict_size;
                     90:   UCell data_stack_size;
                     91:   UCell fp_stack_size;
                     92:   UCell return_stack_size;
                     93:   UCell locals_stack_size;
                     94:   Xt *boot_entry;      /* initial ip for booting (in BOOT) */
                     95:   Xt *throw_entry;     /* ip after signal (in THROW) */
                     96: } ImageHeader;
                     97: /* the image-header is created in main.fs */
                     98: 
1.10      anton      99: void relocate(Cell *image, char *bitstring, int size, Label symbols[])
1.1       anton     100: {
1.16      pazsan    101:   int i=0, j, k, steps=(size/sizeof(Cell))/8;
                    102:   char bits;
1.5       pazsan    103: /*   static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};*/
1.37      pazsan    104: 
                    105: /*  printf("relocating %x[%x]\n", image, size); */
1.5       pazsan    106:    
1.16      pazsan    107:   for(k=0; k<=steps; k++)
                    108:     for(j=0, bits=bitstring[k]; j<8; j++, i++, bits<<=1)
                    109:       if(bits & 0x80)
                    110:        if(image[i]<0)
                    111:          switch(image[i])
                    112:            {
                    113:            case CF_NIL      : image[i]=0; break;
                    114:            case CF(DOCOL)   :
                    115:            case CF(DOVAR)   :
                    116:            case CF(DOCON)   :
                    117:            case CF(DOUSER)  : 
1.24      pazsan    118:            case CF(DODEFER) : 
1.28      anton     119:            case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(image[i])]); break;
1.21      pazsan    120:            case CF(DODOES)  : MAKE_DOES_CF(image+i,image[i+1]+((Cell)image));
1.16      pazsan    121:              break;
                    122:            case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
1.37      pazsan    123:            default          :
                    124: /*           printf("Code field generation image[%x]:=CA(%x)\n",
                    125:                     i, CF(image[i]));
                    126: */           image[i]=(Cell)CA(CF(image[i]));
1.16      pazsan    127:            }
                    128:        else
                    129:          image[i]+=(Cell)image;
1.1       anton     130: }
                    131: 
1.35      anton     132: UCell checksum(Label symbols[])
                    133: {
                    134:   UCell r=0;
                    135:   Cell i;
                    136: 
                    137:   for (i=DOCOL; i<=DOESJUMP; i++) {
                    138:     r ^= (UCell)(symbols[i]);
                    139:     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
                    140:   }
                    141: #ifdef DIRECT_THREADED
                    142:   /* we have to consider all the primitives */
                    143:   for (; symbols[i]!=(Label)0; i++) {
                    144:     r ^= (UCell)(symbols[i]);
                    145:     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
                    146:   }
                    147: #else
                    148:   /* in indirect threaded code all primitives are accessed through the
                    149:      symbols table, so we just have to put the base address of symbols
                    150:      in the checksum */
                    151:   r ^= (UCell)symbols;
                    152: #endif
                    153:   return r;
                    154: }
                    155: 
1.39    ! pazsan    156: Address loader(FILE *imagefile, char* filename)
1.35      anton     157: /* returns the address of the image proper (after the preamble) */
1.10      anton     158: {
1.35      anton     159:   ImageHeader header;
                    160:   Address image;
                    161:   Address imp; /* image+preamble */
1.18      pazsan    162:   Char magic[8];
1.35      anton     163:   Cell wholesize;
                    164:   Cell imagesize; /* everything needed by the image */
                    165:   Cell preamblesize=0;
                    166:   Label *symbols=engine(0,0,0,0,0);
                    167:   UCell check_sum=checksum(symbols);
1.18      pazsan    168: 
1.35      anton     169:   static char* endianstring[]= { "big","little" };
1.25      pazsan    170: 
1.18      pazsan    171:   do
                    172:     {
                    173:       if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1.39    ! pazsan    174:        fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.2) image.\n",
        !           175:                progname, filename);
1.18      pazsan    176:        exit(1);
                    177:       }
1.35      anton     178:       preamblesize+=8;
1.18      pazsan    179: #ifdef DEBUG
1.35      anton     180:       fprintf(stderr,"Magic found: %-8s\n",magic);
1.18      pazsan    181: #endif
                    182:     }
1.35      anton     183:   while(memcmp(magic,"Gforth1",7));
1.25      pazsan    184:   
1.35      anton     185:   if(magic[7] != sizeof(Cell) +
1.25      pazsan    186: #ifdef WORDS_BIGENDIAN
                    187:        '0'
                    188: #else
                    189:        '1'
                    190: #endif
1.35      anton     191:        )
                    192:     { fprintf(stderr,"This image is %d bit %s-endian, whereas the machine is %d bit %s-endian.\n", 
                    193:              ((magic[7]-'0')&~1)*8, endianstring[magic[7]&1],
                    194:              sizeof(Cell)*8, endianstring[
1.25      pazsan    195: #ifdef WORDS_BIGENDIAN
                    196:                      0
                    197: #else
                    198:                      1
                    199: #endif
                    200:                      ]);
                    201:       exit(-2);
                    202:     };
1.18      pazsan    203: 
1.35      anton     204:   fread((void *)&header,sizeof(ImageHeader),1,imagefile);
1.16      pazsan    205:   if (dictsize==0)
1.35      anton     206:     dictsize = header.dict_size;
1.16      pazsan    207:   if (dsize==0)
1.35      anton     208:     dsize=header.data_stack_size;
1.16      pazsan    209:   if (rsize==0)
1.35      anton     210:     rsize=header.return_stack_size;
1.16      pazsan    211:   if (fsize==0)
1.35      anton     212:     fsize=header.fp_stack_size;
1.16      pazsan    213:   if (lsize==0)
1.35      anton     214:     lsize=header.locals_stack_size;
1.16      pazsan    215:   dictsize=maxaligned(dictsize);
                    216:   dsize=maxaligned(dsize);
                    217:   rsize=maxaligned(rsize);
                    218:   lsize=maxaligned(lsize);
                    219:   fsize=maxaligned(fsize);
                    220:   
1.35      anton     221:   wholesize = preamblesize+dictsize+dsize+rsize+fsize+lsize;
                    222:   imagesize = preamblesize+header.image_size+((header.image_size-1)/sizeof(Cell))/8+1;
                    223:   image=malloc((wholesize>imagesize?wholesize:imagesize)/*+sizeof(Float)*/);
                    224:   /*image = maxaligned(image);*/
1.16      pazsan    225:   memset(image,0,wholesize); /* why? - anton */
1.35      anton     226:   rewind(imagefile);  /* fseek(imagefile,0L,SEEK_SET); */
                    227:   fread(image,1,imagesize,imagefile);
1.16      pazsan    228:   fclose(imagefile);
1.35      anton     229:   imp=image+preamblesize;
1.16      pazsan    230:   
1.35      anton     231:   if(header.base==0) {
                    232:     relocate((Cell *)imp,imp+header.image_size,header.image_size,symbols);
                    233:     ((ImageHeader *)imp)->checksum=check_sum;
1.16      pazsan    234:   }
1.35      anton     235:   else if(header.base!=imp) {
1.36      anton     236:     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\nThe Gforth installer should look into the INSTALL file\n",
1.35      anton     237:            progname, (unsigned long)header.base, (unsigned long)imp);
                    238:     exit(1);
                    239:   } else if (header.checksum != check_sum) {
1.36      anton     240:     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\nThe Gforth installer should look into the INSTALL file\n",
1.35      anton     241:            progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
1.16      pazsan    242:     exit(1);
                    243:   }
1.1       anton     244: 
1.35      anton     245:   ((ImageHeader *)imp)->dict_size=dictsize;
                    246:   ((ImageHeader *)imp)->data_stack_size=dsize;
                    247:   ((ImageHeader *)imp)->return_stack_size=rsize;
                    248:   ((ImageHeader *)imp)->fp_stack_size=fsize;
                    249:   ((ImageHeader *)imp)->locals_stack_size=lsize;
                    250: 
1.38      anton     251:   CACHE_FLUSH(imp, header.image_size);
1.35      anton     252: 
                    253:   return imp;
1.1       anton     254: }
                    255: 
1.35      anton     256: int go_forth(Address image, int stack, Cell *entries)
1.1       anton     257: {
1.35      anton     258:   Cell *sp=(Cell*)(image+dictsize+dsize);
1.15      anton     259:   Address lp=(Address)((void *)sp+lsize);
                    260:   Float *fp=(Float *)((void *)lp+fsize);
                    261:   Cell *rp=(Cell*)((void *)fp+rsize);
1.35      anton     262:   Xt *ip=(Xt *)(((ImageHeader *)image)->boot_entry);
1.15      anton     263:   int throw_code;
                    264:   
                    265:   for(;stack>0;stack--)
                    266:     *--sp=entries[stack-1];
1.39    ! pazsan    267: 
        !           268: #ifndef MSDOS
1.33      anton     269:   get_winsize();
1.39    ! pazsan    270: #endif
        !           271:    
1.15      anton     272:   install_signal_handlers(); /* right place? */
                    273:   
                    274:   if ((throw_code=setjmp(throw_jmp_buf))) {
                    275:     static Cell signal_data_stack[8];
                    276:     static Cell signal_return_stack[8];
                    277:     static Float signal_fp_stack[1];
                    278:     
                    279:     signal_data_stack[7]=throw_code;
                    280:     
1.35      anton     281:     return((int)engine(((ImageHeader *)image)->throw_entry,signal_data_stack+7,
1.15      anton     282:                       signal_return_stack+8,signal_fp_stack,0));
                    283:   }
1.35      anton     284: 
1.15      anton     285:   return((int)engine(ip,sp,rp,fp,lp));
1.1       anton     286: }
                    287: 
1.10      anton     288: int convsize(char *s, int elemsize)
                    289: /* converts s of the format #+u (e.g. 25k) into the number of bytes.
                    290:    the unit u can be one of bekM, where e stands for the element
                    291:    size. default is e */
                    292: {
                    293:   char *endp;
                    294:   int n,m;
                    295: 
                    296:   m = elemsize;
                    297:   n = strtoul(s,&endp,0);
                    298:   if (endp!=NULL) {
                    299:     if (strcmp(endp,"b")==0)
                    300:       m=1;
                    301:     else if (strcmp(endp,"k")==0)
                    302:       m=1024;
                    303:     else if (strcmp(endp,"M")==0)
                    304:       m=1024*1024;
1.31      anton     305:     else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
                    306:       fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
1.10      anton     307:       exit(1);
                    308:     }
                    309:   }
                    310:   return n*m;
                    311: }
                    312: 
1.1       anton     313: int main(int argc, char **argv, char **env)
                    314: {
1.16      pazsan    315:   char *path, *path1;
                    316:   char *imagename="gforth.fi";
                    317:   FILE *image_file;
                    318:   int c, retvalue;
1.10      anton     319:          
1.14      anton     320: #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
1.30      anton     321:   /* turn on alignment checks on the 486.
                    322:    * on the 386 this should have no effect. */
                    323:   __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
                    324:   /* this is unusable with Linux' libc.4.6.27, because this library is
                    325:      not alignment-clean; we would have to replace some library
                    326:      functions (e.g., memcpy) to make it work */
1.6       anton     327: #endif
1.2       pazsan    328: 
1.16      pazsan    329:   progname = argv[0];
                    330:   if ((path=getenv("GFORTHPATH"))==NULL)
                    331:     path = strcpy(malloc(strlen(DEFAULTPATH)+1),DEFAULTPATH);
                    332:   opterr=0;
                    333:   while (1) {
                    334:     int option_index=0;
                    335:     static struct option opts[] = {
                    336:       {"image-file", required_argument, NULL, 'i'},
                    337:       {"dictionary-size", required_argument, NULL, 'm'},
                    338:       {"data-stack-size", required_argument, NULL, 'd'},
                    339:       {"return-stack-size", required_argument, NULL, 'r'},
                    340:       {"fp-stack-size", required_argument, NULL, 'f'},
                    341:       {"locals-stack-size", required_argument, NULL, 'l'},
                    342:       {"path", required_argument, NULL, 'p'},
                    343:       {0,0,0,0}
                    344:       /* no-init-file, no-rc? */
                    345:     };
1.10      anton     346: 
1.17      pazsan    347:     c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:", opts, &option_index);
1.12      pazsan    348: 
1.16      pazsan    349:     if (c==EOF)
                    350:       break;
                    351:     if (c=='?') {
                    352:       optind--;
                    353:       break;
                    354:     }
                    355:     switch (c) {
                    356:     case 'i': imagename = optarg; break;
                    357:     case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
                    358:     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
                    359:     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
                    360:     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
                    361:     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
                    362:     case 'p': path = optarg; break;
                    363:     }
                    364:   }
                    365:   path1=path;
1.29      pazsan    366: 
                    367:   if(strchr(imagename, '/')==NULL)
                    368:     {
                    369:       do {
                    370:        char *pend=strchr(path, ':');
                    371:        if (pend==NULL)
                    372:          pend=path+strlen(path);
                    373:        if (strlen(path)==0) {
                    374:          fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
                    375:                  progname, imagename, path1);
                    376:          exit(1);
                    377:        }
                    378:        {
                    379:          int dirlen=pend-path;
                    380:          char fullfilename[dirlen+strlen(imagename)+2];
                    381:          memcpy(fullfilename, path, dirlen);
                    382:          if (fullfilename[dirlen-1]!='/')
                    383:            fullfilename[dirlen++]='/';
                    384:          strcpy(fullfilename+dirlen,imagename);
                    385:          image_file=fopen(fullfilename,"rb");
                    386:        }
                    387:        path=pend+(*pend==':');
                    388:       } while (image_file==NULL);
1.16      pazsan    389:     }
1.29      pazsan    390:   else
1.16      pazsan    391:     {
1.29      pazsan    392:       image_file=fopen(imagename,"rb");
1.35      anton     393:       if(image_file==NULL) {
                    394:        fprintf(stderr,"%s: %s: %s\n", progname, imagename, strerror(errno));
                    395:        exit(1);
                    396:       }
1.16      pazsan    397:     }
1.29      pazsan    398: 
1.16      pazsan    399:   {
                    400:     Cell environ[]= {
                    401:       (Cell)argc-(optind-1),
                    402:       (Cell)(argv+(optind-1)),
                    403:       (Cell)path1};
                    404:     argv[optind-1] = progname;
                    405:     /*
                    406:        for (i=0; i<environ[0]; i++)
                    407:        printf("%s\n", ((char **)(environ[1]))[i]);
                    408:        */
1.39    ! pazsan    409:     retvalue=go_forth(loader(image_file, imagename),3,environ);
1.16      pazsan    410:     deprep_terminal();
                    411:     exit(retvalue);
                    412:   }
1.1       anton     413: }

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