[gforth] / gforth / Attic / main.c  

gforth: gforth/Attic/main.c


1 : anton 1.30 /* command line interpretation, image loading etc. for Gforth
2 :    
3 :    
4 :     Copyright (C) 1995 Free Software Foundation, Inc.
5 :    
6 :     This file is part of Gforth.
7 :    
8 :     Gforth is free software; you can redistribute it and/or
9 :     modify it under the terms of the GNU General Public License
10 :     as published by the Free Software Foundation; either version 2
11 :     of the License, or (at your option) any later version.
12 :    
13 :     This program is distributed in the hope that it will be useful,
14 :     but WITHOUT ANY WARRANTY; without even the implied warranty of
15 :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 :     GNU General Public License for more details.
17 :    
18 :     You should have received a copy of the GNU General Public License
19 :     along with this program; if not, write to the Free Software
20 :     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 : anton 1.1 */
22 :    
23 : anton 1.32 #include "config.h"
24 : anton 1.35 #include <errno.h>
25 : anton 1.1 #include <ctype.h>
26 :     #include <stdio.h>
27 :     #include <string.h>
28 :     #include <math.h>
29 :     #include <sys/types.h>
30 :     #include <sys/stat.h>
31 :     #include <fcntl.h>
32 :     #include <assert.h>
33 :     #include <stdlib.h>
34 :     #include "forth.h"
35 : pazsan 1.5 #include "io.h"
36 : anton 1.20 #include "getopt.h"
37 : anton 1.45 #include "version.h"
38 : pazsan 1.2
39 : pazsan 1.22 #ifdef MSDOS
40 :     jmp_buf throw_jmp_buf;
41 :     #endif
42 :    
43 : anton 1.10 #ifndef DEFAULTPATH
44 : pazsan 1.16 # define DEFAULTPATH "/usr/local/lib/gforth:."
45 : anton 1.10 #endif
46 :    
47 : anton 1.1 #ifdef DIRECT_THREADED
48 : pazsan 1.16 # define CA(n) (symbols[(n)])
49 : anton 1.1 #else
50 : pazsan 1.21 # define CA(n) ((Cell)(symbols+(n)))
51 : anton 1.1 #endif
52 :    
53 : pazsan 1.23 #define maxaligned(n) (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
54 : anton 1.10
55 : pazsan 1.21 static Cell dictsize=0;
56 :     static Cell dsize=0;
57 :     static Cell rsize=0;
58 :     static Cell fsize=0;
59 :     static Cell lsize=0;
60 : anton 1.10 char *progname;
61 :    
62 : anton 1.1 /* image file format:
63 : anton 1.35 * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.2.0 -i\n")
64 :     * padding to a multiple of 8
65 : anton 1.36 * magic: "Gforth1x" means format 0.2,
66 : anton 1.35 * where x is even for big endian and odd for little endian
67 :     * and x & ~1 is the size of the cell in bytes.
68 :     * padding to max alignment (no padding necessary on current machines)
69 :     * ImageHeader structure (see below)
70 :     * data (size in ImageHeader.image_size)
71 :     * tags ((if relocatable, 1 bit/data cell)
72 : anton 1.1 *
73 : anton 1.35 * tag==1 means that the corresponding word is an address;
74 : anton 1.1 * If the word is >=0, the address is within the image;
75 :     * addresses within the image are given relative to the start of the image.
76 : anton 1.35 * If the word =-1 (CF_NIL), the address is NIL,
77 :     * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
78 :     * If the word =CF(DODOES), it's a DOES> CFA
79 :     * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
80 :     * possibly containing a jump to dodoes)
81 :     * If the word is <CF(DOESJUMP), it's a primitive
82 : anton 1.1 */
83 :    
84 : anton 1.35 typedef struct {
85 :     Address base; /* base address of image (0 if relocatable) */
86 :     UCell checksum; /* checksum of ca's to protect against some
87 :     incompatible binary/executable combinations
88 :     (0 if relocatable) */
89 :     UCell image_size; /* all sizes in bytes */
90 :     UCell dict_size;
91 :     UCell data_stack_size;
92 :     UCell fp_stack_size;
93 :     UCell return_stack_size;
94 :     UCell locals_stack_size;
95 :     Xt *boot_entry; /* initial ip for booting (in BOOT) */
96 :     Xt *throw_entry; /* ip after signal (in THROW) */
97 :     } ImageHeader;
98 :     /* the image-header is created in main.fs */
99 :    
100 : anton 1.10 void relocate(Cell *image, char *bitstring, int size, Label symbols[])
101 : anton 1.1 {
102 : pazsan 1.16 int i=0, j, k, steps=(size/sizeof(Cell))/8;
103 :     char bits;
104 : pazsan 1.5 /* static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};*/
105 : pazsan 1.37
106 :     /* printf("relocating %x[%x]\n", image, size); */
107 : pazsan 1.5
108 : pazsan 1.16 for(k=0; k<=steps; k++)
109 :     for(j=0, bits=bitstring[k]; j<8; j++, i++, bits<<=1)
110 :     if(bits & 0x80)
111 :     if(image[i]<0)
112 :     switch(image[i])
113 :     {
114 :     case CF_NIL : image[i]=0; break;
115 :     case CF(DOCOL) :
116 :     case CF(DOVAR) :
117 :     case CF(DOCON) :
118 :     case CF(DOUSER) :
119 : pazsan 1.24 case CF(DODEFER) :
120 : anton 1.28 case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(image[i])]); break;
121 : pazsan 1.21 case CF(DODOES) : MAKE_DOES_CF(image+i,image[i+1]+((Cell)image));
122 : pazsan 1.16 break;
123 :     case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
124 : pazsan 1.37 default :
125 :     /* printf("Code field generation image[%x]:=CA(%x)\n",
126 :     i, CF(image[i]));
127 :     */ image[i]=(Cell)CA(CF(image[i]));
128 : pazsan 1.16 }
129 :     else
130 :     image[i]+=(Cell)image;
131 : anton 1.1 }
132 :    
133 : anton 1.35 UCell checksum(Label symbols[])
134 :     {
135 :     UCell r=0;
136 :     Cell i;
137 :    
138 :     for (i=DOCOL; i<=DOESJUMP; i++) {
139 :     r ^= (UCell)(symbols[i]);
140 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
141 :     }
142 :     #ifdef DIRECT_THREADED
143 :     /* we have to consider all the primitives */
144 :     for (; symbols[i]!=(Label)0; i++) {
145 :     r ^= (UCell)(symbols[i]);
146 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
147 :     }
148 :     #else
149 :     /* in indirect threaded code all primitives are accessed through the
150 :     symbols table, so we just have to put the base address of symbols
151 :     in the checksum */
152 :     r ^= (UCell)symbols;
153 :     #endif
154 :     return r;
155 :     }
156 :    
157 : pazsan 1.39 Address loader(FILE *imagefile, char* filename)
158 : anton 1.35 /* returns the address of the image proper (after the preamble) */
159 : anton 1.10 {
160 : anton 1.35 ImageHeader header;
161 :     Address image;
162 :     Address imp; /* image+preamble */
163 : pazsan 1.18 Char magic[8];
164 : anton 1.35 Cell wholesize;
165 :     Cell imagesize; /* everything needed by the image */
166 :     Cell preamblesize=0;
167 :     Label *symbols=engine(0,0,0,0,0);
168 :     UCell check_sum=checksum(symbols);
169 : pazsan 1.18
170 : anton 1.35 static char* endianstring[]= { "big","little" };
171 : pazsan 1.25
172 : pazsan 1.18 do
173 :     {
174 :     if(fread(magic,sizeof(Char),8,imagefile) < 8) {
175 : pazsan 1.39 fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.2) image.\n",
176 :     progname, filename);
177 : pazsan 1.18 exit(1);
178 :     }
179 : anton 1.35 preamblesize+=8;
180 : pazsan 1.18 #ifdef DEBUG
181 : anton 1.35 fprintf(stderr,"Magic found: %-8s\n",magic);
182 : pazsan 1.18 #endif
183 :     }
184 : anton 1.35 while(memcmp(magic,"Gforth1",7));
185 : pazsan 1.25
186 : anton 1.35 if(magic[7] != sizeof(Cell) +
187 : pazsan 1.25 #ifdef WORDS_BIGENDIAN
188 :     '0'
189 :     #else
190 :     '1'
191 :     #endif
192 : anton 1.35 )
193 :     { fprintf(stderr,"This image is %d bit %s-endian, whereas the machine is %d bit %s-endian.\n",
194 :     ((magic[7]-'0')&~1)*8, endianstring[magic[7]&1],
195 :     sizeof(Cell)*8, endianstring[
196 : pazsan 1.25 #ifdef WORDS_BIGENDIAN
197 :     0
198 :     #else
199 :     1
200 :     #endif
201 :     ]);
202 :     exit(-2);
203 :     };
204 : pazsan 1.18
205 : anton 1.35 fread((void *)&header,sizeof(ImageHeader),1,imagefile);
206 : pazsan 1.16 if (dictsize==0)
207 : anton 1.35 dictsize = header.dict_size;
208 : pazsan 1.16 if (dsize==0)
209 : anton 1.35 dsize=header.data_stack_size;
210 : pazsan 1.16 if (rsize==0)
211 : anton 1.35 rsize=header.return_stack_size;
212 : pazsan 1.16 if (fsize==0)
213 : anton 1.35 fsize=header.fp_stack_size;
214 : pazsan 1.16 if (lsize==0)
215 : anton 1.35 lsize=header.locals_stack_size;
216 : pazsan 1.16 dictsize=maxaligned(dictsize);
217 :     dsize=maxaligned(dsize);
218 :     rsize=maxaligned(rsize);
219 :     lsize=maxaligned(lsize);
220 :     fsize=maxaligned(fsize);
221 :    
222 : anton 1.35 wholesize = preamblesize+dictsize+dsize+rsize+fsize+lsize;
223 :     imagesize = preamblesize+header.image_size+((header.image_size-1)/sizeof(Cell))/8+1;
224 : anton 1.42 image=malloc((wholesize>imagesize?wholesize:imagesize)
225 : pazsan 1.46 #ifdef FUZZ
226 : anton 1.42 +FUZZ
227 :     #endif
228 :     );
229 : anton 1.35 /*image = maxaligned(image);*/
230 : anton 1.42 /* memset(image,0,wholesize); */
231 :    
232 : pazsan 1.46 #ifdef FUZZ
233 : anton 1.42 if(header.base==0) image += FUZZ/2;
234 :     else if((UCell)(header.base - (Cell)image + preamblesize) < FUZZ)
235 :     image = header.base - preamblesize;
236 :     #endif
237 : anton 1.35 rewind(imagefile); /* fseek(imagefile,0L,SEEK_SET); */
238 :     fread(image,1,imagesize,imagefile);
239 : pazsan 1.16 fclose(imagefile);
240 : anton 1.35 imp=image+preamblesize;
241 : pazsan 1.16
242 : anton 1.35 if(header.base==0) {
243 :     relocate((Cell *)imp,imp+header.image_size,header.image_size,symbols);
244 :     ((ImageHeader *)imp)->checksum=check_sum;
245 : pazsan 1.16 }
246 : anton 1.35 else if(header.base!=imp) {
247 : anton 1.36 fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\nThe Gforth installer should look into the INSTALL file\n",
248 : anton 1.35 progname, (unsigned long)header.base, (unsigned long)imp);
249 :     exit(1);
250 :     } else if (header.checksum != check_sum) {
251 : anton 1.36 fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\nThe Gforth installer should look into the INSTALL file\n",
252 : anton 1.35 progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
253 : pazsan 1.16 exit(1);
254 :     }
255 : anton 1.1
256 : anton 1.35 ((ImageHeader *)imp)->dict_size=dictsize;
257 :     ((ImageHeader *)imp)->data_stack_size=dsize;
258 :     ((ImageHeader *)imp)->return_stack_size=rsize;
259 :     ((ImageHeader *)imp)->fp_stack_size=fsize;
260 :     ((ImageHeader *)imp)->locals_stack_size=lsize;
261 :    
262 : anton 1.38 CACHE_FLUSH(imp, header.image_size);
263 : anton 1.35
264 :     return imp;
265 : anton 1.1 }
266 :    
267 : anton 1.35 int go_forth(Address image, int stack, Cell *entries)
268 : anton 1.1 {
269 : anton 1.35 Cell *sp=(Cell*)(image+dictsize+dsize);
270 : anton 1.15 Address lp=(Address)((void *)sp+lsize);
271 :     Float *fp=(Float *)((void *)lp+fsize);
272 :     Cell *rp=(Cell*)((void *)fp+rsize);
273 : anton 1.35 Xt *ip=(Xt *)(((ImageHeader *)image)->boot_entry);
274 : anton 1.15 int throw_code;
275 :    
276 :     for(;stack>0;stack--)
277 :     *--sp=entries[stack-1];
278 : pazsan 1.39
279 : pazsan 1.44 #if !defined(MSDOS) && !defined(_WIN32) && !defined(__EMX__)
280 : anton 1.33 get_winsize();
281 : pazsan 1.39 #endif
282 :    
283 : anton 1.15 install_signal_handlers(); /* right place? */
284 :    
285 :     if ((throw_code=setjmp(throw_jmp_buf))) {
286 :     static Cell signal_data_stack[8];
287 :     static Cell signal_return_stack[8];
288 :     static Float signal_fp_stack[1];
289 :    
290 :     signal_data_stack[7]=throw_code;
291 :    
292 : anton 1.35 return((int)engine(((ImageHeader *)image)->throw_entry,signal_data_stack+7,
293 : anton 1.15 signal_return_stack+8,signal_fp_stack,0));
294 :     }
295 : anton 1.35
296 : anton 1.15 return((int)engine(ip,sp,rp,fp,lp));
297 : anton 1.1 }
298 :    
299 : anton 1.10 int convsize(char *s, int elemsize)
300 :     /* converts s of the format #+u (e.g. 25k) into the number of bytes.
301 :     the unit u can be one of bekM, where e stands for the element
302 :     size. default is e */
303 :     {
304 :     char *endp;
305 :     int n,m;
306 :    
307 :     m = elemsize;
308 :     n = strtoul(s,&endp,0);
309 :     if (endp!=NULL) {
310 :     if (strcmp(endp,"b")==0)
311 :     m=1;
312 :     else if (strcmp(endp,"k")==0)
313 :     m=1024;
314 :     else if (strcmp(endp,"M")==0)
315 :     m=1024*1024;
316 : anton 1.31 else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
317 :     fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
318 : anton 1.10 exit(1);
319 :     }
320 :     }
321 :     return n*m;
322 :     }
323 :    
324 : anton 1.1 int main(int argc, char **argv, char **env)
325 :     {
326 : anton 1.43 char *path, *path1;
327 : pazsan 1.16 char *imagename="gforth.fi";
328 :     FILE *image_file;
329 :     int c, retvalue;
330 : anton 1.10
331 : anton 1.14 #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
332 : anton 1.30 /* turn on alignment checks on the 486.
333 :     * on the 386 this should have no effect. */
334 :     __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
335 :     /* this is unusable with Linux' libc.4.6.27, because this library is
336 :     not alignment-clean; we would have to replace some library
337 :     functions (e.g., memcpy) to make it work */
338 : anton 1.6 #endif
339 : pazsan 1.2
340 : pazsan 1.16 progname = argv[0];
341 : pazsan 1.44 if ((path1=getenv("GFORTHPATH"))==NULL)
342 :     path1 = DEFAULTPATH;
343 :    
344 : pazsan 1.16 opterr=0;
345 :     while (1) {
346 :     int option_index=0;
347 :     static struct option opts[] = {
348 :     {"image-file", required_argument, NULL, 'i'},
349 :     {"dictionary-size", required_argument, NULL, 'm'},
350 :     {"data-stack-size", required_argument, NULL, 'd'},
351 :     {"return-stack-size", required_argument, NULL, 'r'},
352 :     {"fp-stack-size", required_argument, NULL, 'f'},
353 :     {"locals-stack-size", required_argument, NULL, 'l'},
354 :     {"path", required_argument, NULL, 'p'},
355 : anton 1.45 {"version", no_argument, NULL, 'v'},
356 :     {"help", no_argument, NULL, 'h'},
357 : pazsan 1.16 {0,0,0,0}
358 :     /* no-init-file, no-rc? */
359 :     };
360 : pazsan 1.44
361 : anton 1.45 c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vh", opts, &option_index);
362 : pazsan 1.44
363 : pazsan 1.16 if (c==EOF)
364 :     break;
365 :     if (c=='?') {
366 :     optind--;
367 :     break;
368 :     }
369 :     switch (c) {
370 :     case 'i': imagename = optarg; break;
371 :     case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
372 :     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
373 :     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
374 :     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
375 :     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
376 : pazsan 1.44 case 'p': path1 = optarg; break;
377 : anton 1.45 case 'v': fprintf(stderr, "gforth %s\n", gforth_version); exit(0);
378 :     case 'h':
379 :     fprintf(stderr, "Usage: %s [engine options] [image arguments]\n\
380 :     Engine Options:\n\
381 :     -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
382 :     -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
383 :     -h, --help Print this message and exit\n\
384 :     -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
385 :     -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
386 :     -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
387 :     -p PATH, --path=PATH Search path for finding image and sources\n\
388 :     -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
389 :     -v, --version Print version and exit\n\
390 :     SIZE arguments consists of an integer followed by a unit. The unit can be\n\
391 :     `b' (bytes), `e' (elements), `k' (kilobytes), or `M' (Megabytes).\n\
392 :     \n\
393 :     Arguments of default image `gforth.fi':\n\
394 :     FILE load FILE (with `require')\n\
395 :     -e STRING, --evaluate STRING interpret STRING (with `EVALUATE')\n",
396 :     argv[0]); exit(0);
397 : pazsan 1.16 }
398 :     }
399 : pazsan 1.44 path=path1;
400 :    
401 : pazsan 1.29 if(strchr(imagename, '/')==NULL)
402 :     {
403 :     do {
404 : anton 1.43 char *pend=strchr(path, PATHSEP);
405 : pazsan 1.29 if (pend==NULL)
406 :     pend=path+strlen(path);
407 :     if (strlen(path)==0) {
408 :     fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
409 : pazsan 1.44 progname, imagename, path);
410 : pazsan 1.29 exit(1);
411 :     }
412 :     {
413 :     int dirlen=pend-path;
414 :     char fullfilename[dirlen+strlen(imagename)+2];
415 :     memcpy(fullfilename, path, dirlen);
416 :     if (fullfilename[dirlen-1]!='/')
417 :     fullfilename[dirlen++]='/';
418 :     strcpy(fullfilename+dirlen,imagename);
419 :     image_file=fopen(fullfilename,"rb");
420 :     }
421 : anton 1.43 path=pend+(*pend==PATHSEP);
422 : pazsan 1.29 } while (image_file==NULL);
423 : pazsan 1.16 }
424 : pazsan 1.29 else
425 : pazsan 1.16 {
426 : pazsan 1.29 image_file=fopen(imagename,"rb");
427 : pazsan 1.16 }
428 : pazsan 1.29
429 : pazsan 1.16 {
430 : anton 1.45 char path2[strlen(path1)+1];
431 : anton 1.43 char *p1, *p2;
432 : pazsan 1.16 Cell environ[]= {
433 :     (Cell)argc-(optind-1),
434 :     (Cell)(argv+(optind-1)),
435 : anton 1.42 (Cell)strlen(path1),
436 : anton 1.43 (Cell)path2};
437 : pazsan 1.16 argv[optind-1] = progname;
438 :     /*
439 :     for (i=0; i<environ[0]; i++)
440 :     printf("%s\n", ((char **)(environ[1]))[i]);
441 :     */
442 : anton 1.42 /* make path OS-independent by replacing path separators with NUL */
443 : anton 1.43 for (p1=path1, p2=path2; *p1!='\0'; p1++, p2++)
444 :     if (*p1==PATHSEP)
445 :     *p2 = '\0';
446 :     else
447 :     *p2 = *p1;
448 :     *p2='\0';
449 : anton 1.42 retvalue=go_forth(loader(image_file, imagename),4,environ);
450 : pazsan 1.16 deprep_terminal();
451 :     exit(retvalue);
452 :     }
453 : anton 1.1 }

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help