File:  [gforth] / gforth / Attic / main.c
Revision 1.40: download - view: text, annotated - select for diffs
Tue Sep 24 19:15:04 1996 UTC (27 years, 7 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Some bug fixing:
\G in cross compilation works now
marker <-> local conflict resolved
hack around problems with non-relocating images.

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

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