File:  [gforth] / gforth / Attic / main.c
Revision 1.22: download - view: text, annotated - select for diffs
Thu Feb 2 18:13:06 1995 UTC (29 years, 2 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
added very preliminary support for Alpha/osf1
Moved flush-tos before stores to improve scheduling
merged io-dos.h and io.h
Created new Makefile for DOS
removed ToDo topcics that are already done

    1: /*
    2:   $Id: main.c,v 1.22 1995/02/02 18:13:06 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)	((((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);
  132:   memset(image,0,wholesize); /* why? - anton */
  133:   image[0]=header[0];
  134:   image[1]=header[1];
  135:   image[2]=header[2];
  136:   
  137:   fread(image+3,1,header[1]-3*sizeof(Cell),imagefile);
  138:   fread(((void *)image)+header[1],1,((header[1]-1)/sizeof(Cell))/8+1,
  139: 	imagefile);
  140:   fclose(imagefile);
  141:   
  142:   if(image[5]==0) {
  143:     relocate(image,(char *)image+header[1],header[1],engine(0,0,0,0,0));
  144:   }
  145:   else if(image[5]!=(Cell)image) {
  146:     fprintf(stderr,"Corrupted image address, please recompile image\n");
  147:     exit(1);
  148:   }
  149: 
  150:   CACHE_FLUSH(image,image[1]);
  151:   
  152:   return(image);
  153: }
  154: 
  155: int go_forth(Cell *image, int stack, Cell *entries)
  156: {
  157:   Cell *sp=(Cell*)((void *)image+dictsize+dsize);
  158:   Address lp=(Address)((void *)sp+lsize);
  159:   Float *fp=(Float *)((void *)lp+fsize);
  160:   Cell *rp=(Cell*)((void *)fp+rsize);
  161:   Xt *ip=(Xt *)((Cell)image[3]);
  162:   int throw_code;
  163:   
  164:   for(;stack>0;stack--)
  165:     *--sp=entries[stack-1];
  166:   
  167:   install_signal_handlers(); /* right place? */
  168:   
  169:   if ((throw_code=setjmp(throw_jmp_buf))) {
  170:     static Cell signal_data_stack[8];
  171:     static Cell signal_return_stack[8];
  172:     static Float signal_fp_stack[1];
  173:     
  174:     signal_data_stack[7]=throw_code;
  175:     
  176:     return((int)engine((Xt *)image[4],signal_data_stack+7,
  177: 		       signal_return_stack+8,signal_fp_stack,0));
  178:   }
  179:   
  180:   return((int)engine(ip,sp,rp,fp,lp));
  181: }
  182: 
  183: int convsize(char *s, int elemsize)
  184: /* converts s of the format #+u (e.g. 25k) into the number of bytes.
  185:    the unit u can be one of bekM, where e stands for the element
  186:    size. default is e */
  187: {
  188:   char *endp;
  189:   int n,m;
  190: 
  191:   m = elemsize;
  192:   n = strtoul(s,&endp,0);
  193:   if (endp!=NULL) {
  194:     if (strcmp(endp,"b")==0)
  195:       m=1;
  196:     else if (strcmp(endp,"k")==0)
  197:       m=1024;
  198:     else if (strcmp(endp,"M")==0)
  199:       m=1024*1024;
  200:     else if (strcmp(endp,"e")!=0) {
  201:       fprintf(stderr,"%s: cannot grok size specification %s\n", progname, s);
  202:       exit(1);
  203:     }
  204:   }
  205:   return n*m;
  206: }
  207: 
  208: int main(int argc, char **argv, char **env)
  209: {
  210:   char *path, *path1;
  211:   char *imagename="gforth.fi";
  212:   FILE *image_file;
  213:   int c, retvalue;
  214: 	  
  215: #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
  216: 	/* turn on alignment checks on the 486.
  217: 	 * on the 386 this should have no effect. */
  218: 	__asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
  219: #endif
  220: 
  221:   progname = argv[0];
  222:   if ((path=getenv("GFORTHPATH"))==NULL)
  223:     path = strcpy(malloc(strlen(DEFAULTPATH)+1),DEFAULTPATH);
  224:   opterr=0;
  225:   while (1) {
  226:     int option_index=0;
  227:     static struct option opts[] = {
  228:       {"image-file", required_argument, NULL, 'i'},
  229:       {"dictionary-size", required_argument, NULL, 'm'},
  230:       {"data-stack-size", required_argument, NULL, 'd'},
  231:       {"return-stack-size", required_argument, NULL, 'r'},
  232:       {"fp-stack-size", required_argument, NULL, 'f'},
  233:       {"locals-stack-size", required_argument, NULL, 'l'},
  234:       {"path", required_argument, NULL, 'p'},
  235:       {0,0,0,0}
  236:       /* no-init-file, no-rc? */
  237:     };
  238: 
  239:     c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:", opts, &option_index);
  240: 
  241:     if (c==EOF)
  242:       break;
  243:     if (c=='?') {
  244:       optind--;
  245:       break;
  246:     }
  247:     switch (c) {
  248:     case 'i': imagename = optarg; break;
  249:     case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
  250:     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
  251:     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
  252:     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
  253:     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
  254:     case 'p': path = optarg; break;
  255:     }
  256:   }
  257:   path1=path;
  258:   do {
  259:     char *pend=strchr(path, ':');
  260:     if (pend==NULL)
  261:       pend=path+strlen(path);
  262:     if (strlen(path)==0) {
  263:       fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
  264: 	      progname, imagename, path1);
  265:       exit(1);
  266:     }
  267:     {
  268:       int dirlen=pend-path;
  269:       char fullfilename[dirlen+strlen(imagename)+2];
  270:       memcpy(fullfilename, path, dirlen);
  271:       if (fullfilename[dirlen-1]!='/')
  272: 	fullfilename[dirlen++]='/';
  273:       strcpy(fullfilename+dirlen,imagename);
  274:       image_file=fopen(fullfilename,"rb");
  275:     }
  276:     path=pend+(*pend==':');
  277:   } while (image_file==NULL);
  278:   
  279:   {
  280:     Cell environ[]= {
  281:       (Cell)argc-(optind-1),
  282:       (Cell)(argv+(optind-1)),
  283:       (Cell)path1};
  284:     argv[optind-1] = progname;
  285:     /*
  286:        for (i=0; i<environ[0]; i++)
  287:        printf("%s\n", ((char **)(environ[1]))[i]);
  288:        */
  289:     retvalue=go_forth(loader(image_file),3,environ);
  290:     deprep_terminal();
  291:     exit(retvalue);
  292:   }
  293: }

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