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