Annotation of gforth/main.c, revision 1.22
1.1 anton 1: /*
1.21 pazsan 2: $Id: main.c,v 1.20 1994/12/12 17:10:42 anton Exp $
1.1 anton 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"
1.5 pazsan 16: #include "io.h"
1.20 anton 17: #include "getopt.h"
1.2 pazsan 18:
1.22 ! pazsan 19: #ifdef MSDOS
! 20: jmp_buf throw_jmp_buf;
! 21: #endif
! 22:
1.10 anton 23: #ifndef DEFAULTPATH
1.16 pazsan 24: # define DEFAULTPATH "/usr/local/lib/gforth:."
1.10 anton 25: #endif
26:
1.1 anton 27: #ifdef DIRECT_THREADED
1.16 pazsan 28: # define CA(n) (symbols[(n)])
1.1 anton 29: #else
1.21 pazsan 30: # define CA(n) ((Cell)(symbols+(n)))
1.1 anton 31: #endif
32:
1.10 anton 33: #define maxaligned(n) ((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
34:
1.21 pazsan 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;
1.10 anton 40: char *progname;
41:
42:
1.1 anton 43: /* image file format:
1.18 pazsan 44: * preamble (is skipped off), size multiple of 8
45: * magig: "gforth00" (means format version 0.0)
1.1 anton 46: * size of image with stacks without tags (in bytes)
47: * size of image without stacks and tags (in bytes)
1.5 pazsan 48: * size of data and FP stack (in bytes)
1.1 anton 49: * pointer to start of code
1.7 anton 50: * pointer into throw (for signal handling)
1.16 pazsan 51: * pointer to dictionary
1.1 anton 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,
1.2 pazsan 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
1.1 anton 63: */
64:
1.10 anton 65: void relocate(Cell *image, char *bitstring, int size, Label symbols[])
1.1 anton 66: {
1.16 pazsan 67: int i=0, j, k, steps=(size/sizeof(Cell))/8;
68: char bits;
1.5 pazsan 69: /* static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};*/
70:
1.16 pazsan 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;
1.21 pazsan 83: case CF(DODOES) : MAKE_DOES_CF(image+i,image[i+1]+((Cell)image));
1.16 pazsan 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;
1.1 anton 90: }
91:
1.10 anton 92: Cell *loader(FILE *imagefile)
93: {
1.16 pazsan 94: Cell header[3];
95: Cell *image;
1.18 pazsan 96: Char magic[8];
1.16 pazsan 97: int wholesize;
98: int imagesize; /* everything needed by the image */
1.18 pazsan 99:
100: do
101: {
102: if(fread(magic,sizeof(Char),8,imagefile) < 8) {
1.21 pazsan 103: fprintf(stderr,"This file doesn't seem to be a gforth image\n");
1.18 pazsan 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);
1.16 pazsan 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: }
1.1 anton 149:
1.16 pazsan 150: CACHE_FLUSH(image,image[1]);
151:
152: return(image);
1.1 anton 153: }
154:
1.10 anton 155: int go_forth(Cell *image, int stack, Cell *entries)
1.1 anton 156: {
1.15 anton 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);
1.21 pazsan 161: Xt *ip=(Xt *)((Cell)image[3]);
1.15 anton 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));
1.1 anton 181: }
182:
1.10 anton 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:
1.1 anton 208: int main(int argc, char **argv, char **env)
209: {
1.16 pazsan 210: char *path, *path1;
211: char *imagename="gforth.fi";
212: FILE *image_file;
213: int c, retvalue;
1.10 anton 214:
1.14 anton 215: #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
1.6 anton 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
1.2 pazsan 220:
1.16 pazsan 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: };
1.10 anton 238:
1.17 pazsan 239: c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:", opts, &option_index);
1.12 pazsan 240:
1.16 pazsan 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: }
1.1 anton 293: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>