File:  [gforth] / gforth / Attic / main.c
Revision 1.23: download - view: text, annotated - select for diffs
Tue Feb 14 18:18:36 1995 UTC (29 years, 2 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
image must be maxaligned (malloc from DJGPP doesn't return maxaligned
addresses :-((( )

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

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