1: /*
2: $Id: main.c,v 1.24 1995/02/23 20:17:22 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 -7, it's a DOES> CFA
61: * If the word is -8, it's a DOES JUMP
62: * If the word is <-9, 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) :
83: case CF(DOSTRUC) : MAKE_CF(image+i,symbols[CF(image[i])]); break;
84: case CF(DODOES) : MAKE_DOES_CF(image+i,image[i+1]+((Cell)image));
85: break;
86: case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
87: default : image[i]=(Cell)CA(CF(image[i]));
88: }
89: else
90: image[i]+=(Cell)image;
91: }
92:
93: Cell *loader(FILE *imagefile)
94: {
95: Cell header[3];
96: Cell *image;
97: Char magic[8];
98: int wholesize;
99: int imagesize; /* everything needed by the image */
100:
101: do
102: {
103: if(fread(magic,sizeof(Char),8,imagefile) < 8) {
104: fprintf(stderr,"This file doesn't seem to be a gforth image\n");
105: exit(1);
106: }
107: #ifdef DEBUG
108: printf("Magic found: %s\n",magic);
109: #endif
110: }
111: while(memcmp(magic,"gforth00",8));
112:
113: fread(header,sizeof(Cell),3,imagefile);
114: if (dictsize==0)
115: dictsize = header[0];
116: if (dsize==0)
117: dsize=header[2];
118: if (rsize==0)
119: rsize=header[2];
120: if (fsize==0)
121: fsize=header[2];
122: if (lsize==0)
123: lsize=header[2];
124: dictsize=maxaligned(dictsize);
125: dsize=maxaligned(dsize);
126: rsize=maxaligned(rsize);
127: lsize=maxaligned(lsize);
128: fsize=maxaligned(fsize);
129:
130: wholesize = dictsize+dsize+rsize+fsize+lsize;
131: imagesize = header[1]+((header[1]-1)/sizeof(Cell))/8+1;
132: image=malloc((wholesize>imagesize?wholesize:imagesize)+sizeof(Float));
133: image = maxaligned(image);
134: memset(image,0,wholesize); /* why? - anton */
135: image[0]=header[0];
136: image[1]=header[1];
137: image[2]=header[2];
138:
139: fread(image+3,1,header[1]-3*sizeof(Cell),imagefile);
140: fread(((void *)image)+header[1],1,((header[1]-1)/sizeof(Cell))/8+1,
141: imagefile);
142: fclose(imagefile);
143:
144: if(image[5]==0) {
145: relocate(image,(char *)image+header[1],header[1],engine(0,0,0,0,0));
146: }
147: else if(image[5]!=(Cell)image) {
148: fprintf(stderr,"Corrupted image address, please recompile image\n");
149: exit(1);
150: }
151:
152: CACHE_FLUSH(image,image[1]);
153:
154: return(image);
155: }
156:
157: int go_forth(Cell *image, int stack, Cell *entries)
158: {
159: Cell *sp=(Cell*)((void *)image+dictsize+dsize);
160: Address lp=(Address)((void *)sp+lsize);
161: Float *fp=(Float *)((void *)lp+fsize);
162: Cell *rp=(Cell*)((void *)fp+rsize);
163: Xt *ip=(Xt *)((Cell)image[3]);
164: int throw_code;
165:
166: for(;stack>0;stack--)
167: *--sp=entries[stack-1];
168:
169: install_signal_handlers(); /* right place? */
170:
171: if ((throw_code=setjmp(throw_jmp_buf))) {
172: static Cell signal_data_stack[8];
173: static Cell signal_return_stack[8];
174: static Float signal_fp_stack[1];
175:
176: signal_data_stack[7]=throw_code;
177:
178: return((int)engine((Xt *)image[4],signal_data_stack+7,
179: signal_return_stack+8,signal_fp_stack,0));
180: }
181:
182: return((int)engine(ip,sp,rp,fp,lp));
183: }
184:
185: int convsize(char *s, int elemsize)
186: /* converts s of the format #+u (e.g. 25k) into the number of bytes.
187: the unit u can be one of bekM, where e stands for the element
188: size. default is e */
189: {
190: char *endp;
191: int n,m;
192:
193: m = elemsize;
194: n = strtoul(s,&endp,0);
195: if (endp!=NULL) {
196: if (strcmp(endp,"b")==0)
197: m=1;
198: else if (strcmp(endp,"k")==0)
199: m=1024;
200: else if (strcmp(endp,"M")==0)
201: m=1024*1024;
202: else if (strcmp(endp,"e")!=0) {
203: fprintf(stderr,"%s: cannot grok size specification %s\n", progname, s);
204: exit(1);
205: }
206: }
207: return n*m;
208: }
209:
210: int main(int argc, char **argv, char **env)
211: {
212: char *path, *path1;
213: char *imagename="gforth.fi";
214: FILE *image_file;
215: int c, retvalue;
216:
217: #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
218: /* turn on alignment checks on the 486.
219: * on the 386 this should have no effect. */
220: __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
221: #endif
222:
223: progname = argv[0];
224: if ((path=getenv("GFORTHPATH"))==NULL)
225: path = strcpy(malloc(strlen(DEFAULTPATH)+1),DEFAULTPATH);
226: opterr=0;
227: while (1) {
228: int option_index=0;
229: static struct option opts[] = {
230: {"image-file", required_argument, NULL, 'i'},
231: {"dictionary-size", required_argument, NULL, 'm'},
232: {"data-stack-size", required_argument, NULL, 'd'},
233: {"return-stack-size", required_argument, NULL, 'r'},
234: {"fp-stack-size", required_argument, NULL, 'f'},
235: {"locals-stack-size", required_argument, NULL, 'l'},
236: {"path", required_argument, NULL, 'p'},
237: {0,0,0,0}
238: /* no-init-file, no-rc? */
239: };
240:
241: c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:", opts, &option_index);
242:
243: if (c==EOF)
244: break;
245: if (c=='?') {
246: optind--;
247: break;
248: }
249: switch (c) {
250: case 'i': imagename = optarg; break;
251: case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
252: case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
253: case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
254: case 'f': fsize = convsize(optarg,sizeof(Float)); break;
255: case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
256: case 'p': path = optarg; break;
257: }
258: }
259: path1=path;
260: do {
261: char *pend=strchr(path, ':');
262: if (pend==NULL)
263: pend=path+strlen(path);
264: if (strlen(path)==0) {
265: fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
266: progname, imagename, path1);
267: exit(1);
268: }
269: {
270: int dirlen=pend-path;
271: char fullfilename[dirlen+strlen(imagename)+2];
272: memcpy(fullfilename, path, dirlen);
273: if (fullfilename[dirlen-1]!='/')
274: fullfilename[dirlen++]='/';
275: strcpy(fullfilename+dirlen,imagename);
276: image_file=fopen(fullfilename,"rb");
277: }
278: path=pend+(*pend==':');
279: } while (image_file==NULL);
280:
281: {
282: Cell environ[]= {
283: (Cell)argc-(optind-1),
284: (Cell)(argv+(optind-1)),
285: (Cell)path1};
286: argv[optind-1] = progname;
287: /*
288: for (i=0; i<environ[0]; i++)
289: printf("%s\n", ((char **)(environ[1]))[i]);
290: */
291: retvalue=go_forth(loader(image_file),3,environ);
292: deprep_terminal();
293: exit(retvalue);
294: }
295: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>