File:  [gforth] / gforth / Attic / main.c
Revision 1.38: download - view: text, annotated - select for diffs
Fri Jul 26 15:28:29 1996 UTC (27 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
adapted DOES_CODE in alpha.h m68k.h mips.h power.h and sparc.h
worked around a bug in SunOS4 in prims2x.fs
fixed typo in main.c

    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 DEFAULTPATH
   43: #  define DEFAULTPATH "/usr/local/lib/gforth:."
   44: #endif
   45: 
   46: #ifdef DIRECT_THREADED
   47: #  define CA(n)	(symbols[(n)])
   48: #else
   49: #  define CA(n)	((Cell)(symbols+(n)))
   50: #endif
   51: 
   52: #define maxaligned(n)	(typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
   53: 
   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;
   59: char *progname;
   60: 
   61: /* image file format:
   62:  *  "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.2.0 -i\n")
   63:  *   padding to a multiple of 8
   64:  *   magic: "Gforth1x" means format 0.2,
   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)
   71:  *
   72:  * tag==1 means that the corresponding word is an address;
   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.
   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
   81:  */
   82: 
   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: 
   99: void relocate(Cell *image, char *bitstring, int size, Label symbols[])
  100: {
  101:   int i=0, j, k, steps=(size/sizeof(Cell))/8;
  102:   char bits;
  103: /*   static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};*/
  104: 
  105: /*  printf("relocating %x[%x]\n", image, size); */
  106:    
  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)  : 
  118: 	    case CF(DODEFER) : 
  119: 	    case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(image[i])]); break;
  120: 	    case CF(DODOES)  : MAKE_DOES_CF(image+i,image[i+1]+((Cell)image));
  121: 	      break;
  122: 	    case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
  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]));
  127: 	    }
  128: 	else
  129: 	  image[i]+=(Cell)image;
  130: }
  131: 
  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: 
  156: Address loader(FILE *imagefile)
  157: /* returns the address of the image proper (after the preamble) */
  158: {
  159:   ImageHeader header;
  160:   Address image;
  161:   Address imp; /* image+preamble */
  162:   Char magic[8];
  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);
  168: 
  169:   static char* endianstring[]= { "big","little" };
  170: 
  171:   do
  172:     {
  173:       if(fread(magic,sizeof(Char),8,imagefile) < 8) {
  174: 	fprintf(stderr,"%s: image doesn't seem to be a Gforth (>=0.2) image.\n",progname);
  175: 	exit(1);
  176:       }
  177:       preamblesize+=8;
  178: #ifdef DEBUG
  179:       fprintf(stderr,"Magic found: %-8s\n",magic);
  180: #endif
  181:     }
  182:   while(memcmp(magic,"Gforth1",7));
  183:   
  184:   if(magic[7] != sizeof(Cell) +
  185: #ifdef WORDS_BIGENDIAN
  186:        '0'
  187: #else
  188:        '1'
  189: #endif
  190:        )
  191:     { fprintf(stderr,"This image is %d bit %s-endian, whereas the machine is %d bit %s-endian.\n", 
  192: 	      ((magic[7]-'0')&~1)*8, endianstring[magic[7]&1],
  193: 	      sizeof(Cell)*8, endianstring[
  194: #ifdef WORDS_BIGENDIAN
  195: 		      0
  196: #else
  197: 		      1
  198: #endif
  199: 		      ]);
  200:       exit(-2);
  201:     };
  202: 
  203:   fread((void *)&header,sizeof(ImageHeader),1,imagefile);
  204:   if (dictsize==0)
  205:     dictsize = header.dict_size;
  206:   if (dsize==0)
  207:     dsize=header.data_stack_size;
  208:   if (rsize==0)
  209:     rsize=header.return_stack_size;
  210:   if (fsize==0)
  211:     fsize=header.fp_stack_size;
  212:   if (lsize==0)
  213:     lsize=header.locals_stack_size;
  214:   dictsize=maxaligned(dictsize);
  215:   dsize=maxaligned(dsize);
  216:   rsize=maxaligned(rsize);
  217:   lsize=maxaligned(lsize);
  218:   fsize=maxaligned(fsize);
  219:   
  220:   wholesize = preamblesize+dictsize+dsize+rsize+fsize+lsize;
  221:   imagesize = preamblesize+header.image_size+((header.image_size-1)/sizeof(Cell))/8+1;
  222:   image=malloc((wholesize>imagesize?wholesize:imagesize)/*+sizeof(Float)*/);
  223:   /*image = maxaligned(image);*/
  224:   memset(image,0,wholesize); /* why? - anton */
  225:   rewind(imagefile);  /* fseek(imagefile,0L,SEEK_SET); */
  226:   fread(image,1,imagesize,imagefile);
  227:   fclose(imagefile);
  228:   imp=image+preamblesize;
  229:   
  230:   if(header.base==0) {
  231:     relocate((Cell *)imp,imp+header.image_size,header.image_size,symbols);
  232:     ((ImageHeader *)imp)->checksum=check_sum;
  233:   }
  234:   else if(header.base!=imp) {
  235:     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\nThe Gforth installer should look into the INSTALL file\n",
  236: 	    progname, (unsigned long)header.base, (unsigned long)imp);
  237:     exit(1);
  238:   } else if (header.checksum != check_sum) {
  239:     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\nThe Gforth installer should look into the INSTALL file\n",
  240: 	    progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
  241:     exit(1);
  242:   }
  243: 
  244:   ((ImageHeader *)imp)->dict_size=dictsize;
  245:   ((ImageHeader *)imp)->data_stack_size=dsize;
  246:   ((ImageHeader *)imp)->return_stack_size=rsize;
  247:   ((ImageHeader *)imp)->fp_stack_size=fsize;
  248:   ((ImageHeader *)imp)->locals_stack_size=lsize;
  249: 
  250:   CACHE_FLUSH(imp, header.image_size);
  251: 
  252:   return imp;
  253: }
  254: 
  255: int go_forth(Address image, int stack, Cell *entries)
  256: {
  257:   Cell *sp=(Cell*)(image+dictsize+dsize);
  258:   Address lp=(Address)((void *)sp+lsize);
  259:   Float *fp=(Float *)((void *)lp+fsize);
  260:   Cell *rp=(Cell*)((void *)fp+rsize);
  261:   Xt *ip=(Xt *)(((ImageHeader *)image)->boot_entry);
  262:   int throw_code;
  263:   
  264:   for(;stack>0;stack--)
  265:     *--sp=entries[stack-1];
  266:   
  267:   get_winsize();
  268:   install_signal_handlers(); /* right place? */
  269:   
  270:   if ((throw_code=setjmp(throw_jmp_buf))) {
  271:     static Cell signal_data_stack[8];
  272:     static Cell signal_return_stack[8];
  273:     static Float signal_fp_stack[1];
  274:     
  275:     signal_data_stack[7]=throw_code;
  276:     
  277:     return((int)engine(((ImageHeader *)image)->throw_entry,signal_data_stack+7,
  278: 		       signal_return_stack+8,signal_fp_stack,0));
  279:   }
  280: 
  281:   return((int)engine(ip,sp,rp,fp,lp));
  282: }
  283: 
  284: int convsize(char *s, int elemsize)
  285: /* converts s of the format #+u (e.g. 25k) into the number of bytes.
  286:    the unit u can be one of bekM, where e stands for the element
  287:    size. default is e */
  288: {
  289:   char *endp;
  290:   int n,m;
  291: 
  292:   m = elemsize;
  293:   n = strtoul(s,&endp,0);
  294:   if (endp!=NULL) {
  295:     if (strcmp(endp,"b")==0)
  296:       m=1;
  297:     else if (strcmp(endp,"k")==0)
  298:       m=1024;
  299:     else if (strcmp(endp,"M")==0)
  300:       m=1024*1024;
  301:     else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
  302:       fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
  303:       exit(1);
  304:     }
  305:   }
  306:   return n*m;
  307: }
  308: 
  309: int main(int argc, char **argv, char **env)
  310: {
  311:   char *path, *path1;
  312:   char *imagename="gforth.fi";
  313:   FILE *image_file;
  314:   int c, retvalue;
  315: 	  
  316: #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
  317:   /* turn on alignment checks on the 486.
  318:    * on the 386 this should have no effect. */
  319:   __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
  320:   /* this is unusable with Linux' libc.4.6.27, because this library is
  321:      not alignment-clean; we would have to replace some library
  322:      functions (e.g., memcpy) to make it work */
  323: #endif
  324: 
  325:   progname = argv[0];
  326:   if ((path=getenv("GFORTHPATH"))==NULL)
  327:     path = strcpy(malloc(strlen(DEFAULTPATH)+1),DEFAULTPATH);
  328:   opterr=0;
  329:   while (1) {
  330:     int option_index=0;
  331:     static struct option opts[] = {
  332:       {"image-file", required_argument, NULL, 'i'},
  333:       {"dictionary-size", required_argument, NULL, 'm'},
  334:       {"data-stack-size", required_argument, NULL, 'd'},
  335:       {"return-stack-size", required_argument, NULL, 'r'},
  336:       {"fp-stack-size", required_argument, NULL, 'f'},
  337:       {"locals-stack-size", required_argument, NULL, 'l'},
  338:       {"path", required_argument, NULL, 'p'},
  339:       {0,0,0,0}
  340:       /* no-init-file, no-rc? */
  341:     };
  342: 
  343:     c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:", opts, &option_index);
  344: 
  345:     if (c==EOF)
  346:       break;
  347:     if (c=='?') {
  348:       optind--;
  349:       break;
  350:     }
  351:     switch (c) {
  352:     case 'i': imagename = optarg; break;
  353:     case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
  354:     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
  355:     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
  356:     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
  357:     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
  358:     case 'p': path = optarg; break;
  359:     }
  360:   }
  361:   path1=path;
  362: 
  363:   if(strchr(imagename, '/')==NULL)
  364:     {
  365:       do {
  366: 	char *pend=strchr(path, ':');
  367: 	if (pend==NULL)
  368: 	  pend=path+strlen(path);
  369: 	if (strlen(path)==0) {
  370: 	  fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
  371: 		  progname, imagename, path1);
  372: 	  exit(1);
  373: 	}
  374: 	{
  375: 	  int dirlen=pend-path;
  376: 	  char fullfilename[dirlen+strlen(imagename)+2];
  377: 	  memcpy(fullfilename, path, dirlen);
  378: 	  if (fullfilename[dirlen-1]!='/')
  379: 	    fullfilename[dirlen++]='/';
  380: 	  strcpy(fullfilename+dirlen,imagename);
  381: 	  image_file=fopen(fullfilename,"rb");
  382: 	}
  383: 	path=pend+(*pend==':');
  384:       } while (image_file==NULL);
  385:     }
  386:   else
  387:     {
  388:       image_file=fopen(imagename,"rb");
  389:       if(image_file==NULL) {
  390: 	fprintf(stderr,"%s: %s: %s\n", progname, imagename, strerror(errno));
  391: 	exit(1);
  392:       }
  393:     }
  394: 
  395:   {
  396:     Cell environ[]= {
  397:       (Cell)argc-(optind-1),
  398:       (Cell)(argv+(optind-1)),
  399:       (Cell)path1};
  400:     argv[optind-1] = progname;
  401:     /*
  402:        for (i=0; i<environ[0]; i++)
  403:        printf("%s\n", ((char **)(environ[1]))[i]);
  404:        */
  405:     retvalue=go_forth(loader(image_file),3,environ);
  406:     deprep_terminal();
  407:     exit(retvalue);
  408:   }
  409: }

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