[gforth] / gforth / engine / main.c  

gforth: gforth/engine/main.c


1 : anton 1.1 /* command line interpretation, image loading etc. for Gforth
2 :    
3 :    
4 : anton 1.12 Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
5 : anton 1.1
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 :     */
22 :    
23 :     #include "config.h"
24 :     #include <errno.h>
25 :     #include <ctype.h>
26 :     #include <stdio.h>
27 : pazsan 1.2 #include <unistd.h>
28 : anton 1.1 #include <string.h>
29 :     #include <math.h>
30 :     #include <sys/types.h>
31 :     #include <sys/stat.h>
32 :     #include <fcntl.h>
33 :     #include <assert.h>
34 :     #include <stdlib.h>
35 : pazsan 1.11 #ifndef STANDALONE
36 : anton 1.1 #if HAVE_SYS_MMAN_H
37 :     #include <sys/mman.h>
38 :     #endif
39 : pazsan 1.11 #endif
40 : anton 1.1 #include "forth.h"
41 :     #include "io.h"
42 :     #include "getopt.h"
43 : pazsan 1.11 #ifdef STANDALONE
44 :     #include <systypes.h>
45 :     #endif
46 : anton 1.1
47 :     #define PRIM_VERSION 1
48 :     /* increment this whenever the primitives change in an incompatible way */
49 :    
50 : pazsan 1.14 #ifndef DEFAULTPATH
51 : pazsan 1.16 # define DEFAULTPATH "~+"
52 : pazsan 1.14 #endif
53 :    
54 : anton 1.1 #ifdef MSDOS
55 :     jmp_buf throw_jmp_buf;
56 :     #endif
57 :    
58 :     #if defined(DIRECT_THREADED)
59 :     # define CA(n) (symbols[(n)])
60 :     #else
61 :     # define CA(n) ((Cell)(symbols+(n)))
62 :     #endif
63 :    
64 :     #define maxaligned(n) (typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
65 :    
66 :     static UCell dictsize=0;
67 :     static UCell dsize=0;
68 :     static UCell rsize=0;
69 :     static UCell fsize=0;
70 :     static UCell lsize=0;
71 :     int offset_image=0;
72 : anton 1.4 int die_on_signal=0;
73 : pazsan 1.13 #ifndef INCLUDE_IMAGE
74 : anton 1.1 static int clear_dictionary=0;
75 : anton 1.24 UCell pagesize=1;
76 : pazsan 1.22 char *progname;
77 :     #else
78 :     char *progname = "gforth";
79 :     int optind = 1;
80 : pazsan 1.13 #endif
81 : anton 1.1 static int debug=0;
82 : anton 1.24 ImageHeader *gforth_header;
83 : anton 1.1
84 :     /* image file format:
85 : pazsan 1.15 * "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.4.0 -i\n")
86 : anton 1.1 * padding to a multiple of 8
87 : pazsan 1.15 * magic: "Gforth2x" means format 0.4,
88 :     * where x is a byte with
89 :     * bit 7: reserved = 0
90 :     * bit 6:5: address unit size 2^n octets
91 :     * bit 4:3: character size 2^n octets
92 :     * bit 2:1: cell size 2^n octets
93 :     * bit 0: endian, big=0, little=1.
94 :     * The magic are always 8 octets, no matter what the native AU/character size is
95 : anton 1.1 * padding to max alignment (no padding necessary on current machines)
96 : anton 1.24 * ImageHeader structure (see forth.h)
97 : anton 1.1 * data (size in ImageHeader.image_size)
98 :     * tags ((if relocatable, 1 bit/data cell)
99 :     *
100 :     * tag==1 means that the corresponding word is an address;
101 :     * If the word is >=0, the address is within the image;
102 :     * addresses within the image are given relative to the start of the image.
103 :     * If the word =-1 (CF_NIL), the address is NIL,
104 :     * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
105 :     * If the word =CF(DODOES), it's a DOES> CFA
106 :     * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
107 :     * possibly containing a jump to dodoes)
108 :     * If the word is <CF(DOESJUMP), it's a primitive
109 :     */
110 :    
111 : pazsan 1.10 void relocate(Cell *image, const char *bitstring, int size, Label symbols[])
112 : anton 1.1 {
113 : pazsan 1.16 int i=0, j, k, steps=(size/sizeof(Cell))/RELINFOBITS;
114 : pazsan 1.11 Cell token;
115 : anton 1.1 char bits;
116 :     /* static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};*/
117 :    
118 :     /* printf("relocating %x[%x]\n", image, size); */
119 :    
120 :     for(k=0; k<=steps; k++)
121 : pazsan 1.13 for(j=0, bits=bitstring[k]; j<RELINFOBITS; j++, i++, bits<<=1) {
122 : anton 1.1 /* fprintf(stderr,"relocate: image[%d]\n", i);*/
123 : pazsan 1.13 if(bits & (1U << (RELINFOBITS-1))) {
124 : anton 1.1 /* fprintf(stderr,"relocate: image[%d]=%d\n", i, image[i]);*/
125 : pazsan 1.11 if((token=image[i])<0)
126 :     switch(token)
127 : anton 1.1 {
128 :     case CF_NIL : image[i]=0; break;
129 :     #if !defined(DOUBLY_INDIRECT)
130 :     case CF(DOCOL) :
131 :     case CF(DOVAR) :
132 :     case CF(DOCON) :
133 :     case CF(DOUSER) :
134 :     case CF(DODEFER) :
135 : pazsan 1.11 case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(token)]); break;
136 : anton 1.1 case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
137 :     #endif /* !defined(DOUBLY_INDIRECT) */
138 :     case CF(DODOES) :
139 :     MAKE_DOES_CF(image+i,image[i+1]+((Cell)image));
140 :     break;
141 :     default :
142 :     /* printf("Code field generation image[%x]:=CA(%x)\n",
143 :     i, CF(image[i])); */
144 : pazsan 1.11 image[i]=(Cell)CA(CF(token));
145 : anton 1.1 }
146 :     else
147 :     image[i]+=(Cell)image;
148 :     }
149 :     }
150 : pazsan 1.22 ((ImageHeader*)(image))->base = image;
151 : anton 1.1 }
152 :    
153 :     UCell checksum(Label symbols[])
154 :     {
155 :     UCell r=PRIM_VERSION;
156 :     Cell i;
157 :    
158 :     for (i=DOCOL; i<=DOESJUMP; i++) {
159 :     r ^= (UCell)(symbols[i]);
160 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
161 :     }
162 :     #ifdef DIRECT_THREADED
163 :     /* we have to consider all the primitives */
164 :     for (; symbols[i]!=(Label)0; i++) {
165 :     r ^= (UCell)(symbols[i]);
166 :     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
167 :     }
168 :     #else
169 :     /* in indirect threaded code all primitives are accessed through the
170 :     symbols table, so we just have to put the base address of symbols
171 :     in the checksum */
172 :     r ^= (UCell)symbols;
173 :     #endif
174 :     return r;
175 :     }
176 :    
177 : anton 1.3 Address verbose_malloc(Cell size)
178 :     {
179 :     Address r;
180 :     /* leave a little room (64B) for stack underflows */
181 :     if ((r = malloc(size+64))==NULL) {
182 :     perror(progname);
183 :     exit(1);
184 :     }
185 :     r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
186 :     if (debug)
187 :     fprintf(stderr, "malloc succeeds, address=$%lx\n", (long)r);
188 :     return r;
189 :     }
190 :    
191 : anton 1.1 Address my_alloc(Cell size)
192 :     {
193 : jwilke 1.5 #if HAVE_MMAP
194 : anton 1.1 static Address next_address=0;
195 :     Address r;
196 :    
197 :     #if defined(MAP_ANON)
198 :     if (debug)
199 :     fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
200 :     r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
201 :     #else /* !defined(MAP_ANON) */
202 : anton 1.17 /* Ultrix (at least) does not define MAP_FILE and MAP_PRIVATE (both are
203 :     apparently defaults) */
204 : anton 1.1 #ifndef MAP_FILE
205 :     # define MAP_FILE 0
206 :     #endif
207 :     #ifndef MAP_PRIVATE
208 :     # define MAP_PRIVATE 0
209 :     #endif
210 :     static int dev_zero=-1;
211 :    
212 :     if (dev_zero == -1)
213 :     dev_zero = open("/dev/zero", O_RDONLY);
214 :     if (dev_zero == -1) {
215 :     r = (Address)-1;
216 :     if (debug)
217 :     fprintf(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ",
218 :     strerror(errno));
219 :     } else {
220 :     if (debug)
221 :     fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
222 :     r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, dev_zero, 0);
223 :     }
224 :     #endif /* !defined(MAP_ANON) */
225 :    
226 :     if (r != (Address)-1) {
227 :     if (debug)
228 :     fprintf(stderr, "success, address=$%lx\n", (long) r);
229 : anton 1.24 if (pagesize != 1)
230 : anton 1.1 next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
231 :     return r;
232 :     }
233 :     if (debug)
234 :     fprintf(stderr, "failed: %s\n", strerror(errno));
235 :     #endif /* HAVE_MMAP */
236 : anton 1.3 /* use malloc as fallback */
237 :     return verbose_malloc(size);
238 : anton 1.1 }
239 :    
240 : anton 1.3 #if (defined(mips) && !defined(INDIRECT_THREADED))
241 :     /* the 256MB jump restriction on the MIPS architecture makes the
242 :     combination of direct threading and mmap unsafe. */
243 :     #define dict_alloc(size) verbose_malloc(size)
244 :     #else
245 :     #define dict_alloc(size) my_alloc(size)
246 :     #endif
247 :    
248 : pazsan 1.10 void set_stack_sizes(ImageHeader * header)
249 :     {
250 :     if (dictsize==0)
251 :     dictsize = header->dict_size;
252 :     if (dsize==0)
253 :     dsize = header->data_stack_size;
254 :     if (rsize==0)
255 :     rsize = header->return_stack_size;
256 :     if (fsize==0)
257 :     fsize = header->fp_stack_size;
258 :     if (lsize==0)
259 :     lsize = header->locals_stack_size;
260 :     dictsize=maxaligned(dictsize);
261 :     dsize=maxaligned(dsize);
262 :     rsize=maxaligned(rsize);
263 :     lsize=maxaligned(lsize);
264 :     fsize=maxaligned(fsize);
265 :     }
266 :    
267 :     void alloc_stacks(ImageHeader * header)
268 :     {
269 :     header->dict_size=dictsize;
270 :     header->data_stack_size=dsize;
271 :     header->fp_stack_size=fsize;
272 :     header->return_stack_size=rsize;
273 :     header->locals_stack_size=lsize;
274 :    
275 :     header->data_stack_base=my_alloc(dsize);
276 :     header->fp_stack_base=my_alloc(fsize);
277 :     header->return_stack_base=my_alloc(rsize);
278 :     header->locals_stack_base=my_alloc(lsize);
279 :     }
280 :    
281 : pazsan 1.11 int go_forth(Address image, int stack, Cell *entries)
282 :     {
283 : anton 1.18 ImageHeader *image_header = (ImageHeader *)image;
284 :     Cell *sp0=(Cell*)(image_header->data_stack_base + dsize);
285 :     Float *fp0=(Float *)(image_header->fp_stack_base + fsize);
286 :     Cell *rp0=(Cell *)(image_header->return_stack_base + rsize);
287 :     Address lp0=image_header->locals_stack_base + lsize;
288 :     Xt *ip0=(Xt *)(image_header->boot_entry);
289 : pazsan 1.13 #ifdef SYSSIGNALS
290 : pazsan 1.11 int throw_code;
291 : pazsan 1.13 #endif
292 : pazsan 1.11
293 :     /* ensure that the cached elements (if any) are accessible */
294 : anton 1.18 IF_TOS(sp0--);
295 :     IF_FTOS(fp0--);
296 : pazsan 1.11
297 :     for(;stack>0;stack--)
298 : anton 1.18 *--sp0=entries[stack-1];
299 : pazsan 1.11
300 :     get_winsize();
301 :    
302 : pazsan 1.13 #ifdef SYSSIGNALS
303 : pazsan 1.11 install_signal_handlers(); /* right place? */
304 :    
305 :     if ((throw_code=setjmp(throw_jmp_buf))) {
306 :     static Cell signal_data_stack[8];
307 :     static Cell signal_return_stack[8];
308 :     static Float signal_fp_stack[1];
309 : pazsan 1.13
310 : pazsan 1.11 signal_data_stack[7]=throw_code;
311 : anton 1.18
312 :     #ifdef GFORTH_DEBUGGING
313 :     if (rp <= rp0 && rp > (Cell *)(image_header->return_stack_base+5)) {
314 :     /* no rstack overflow or underflow */
315 :     rp0 = rp;
316 :     *--rp0 = ip;
317 :     }
318 :     else /* I love non-syntactic ifdefs :-) */
319 :     #endif
320 :     rp0 = signal_return_stack+8;
321 : pazsan 1.11
322 : anton 1.18 return((int)engine(image_header->throw_entry, signal_data_stack+7,
323 :     rp0, signal_fp_stack, 0));
324 : pazsan 1.11 }
325 : pazsan 1.13 #endif
326 : pazsan 1.11
327 : anton 1.18 return((int)engine(ip0,sp0,rp0,fp0,lp0));
328 : pazsan 1.11 }
329 :    
330 : anton 1.21
331 :     void print_sizes(Cell sizebyte)
332 :     /* print size information */
333 :     {
334 :     static char* endianstring[]= { " big","little" };
335 :    
336 :     fprintf(stderr,"%s endian, cell=%d bytes, char=%d bytes, au=%d bytes\n",
337 :     endianstring[sizebyte & 1],
338 :     1 << ((sizebyte >> 1) & 3),
339 :     1 << ((sizebyte >> 3) & 3),
340 :     1 << ((sizebyte >> 5) & 3));
341 :     }
342 :    
343 : pazsan 1.11 #ifndef INCLUDE_IMAGE
344 : anton 1.1 Address loader(FILE *imagefile, char* filename)
345 :     /* returns the address of the image proper (after the preamble) */
346 :     {
347 :     ImageHeader header;
348 :     Address image;
349 :     Address imp; /* image+preamble */
350 : anton 1.17 Char magic[8];
351 :     char magic7; /* size byte of magic number */
352 : anton 1.1 Cell preamblesize=0;
353 :     Label *symbols = engine(0,0,0,0,0);
354 : pazsan 1.6 Cell data_offset = offset_image ? 56*sizeof(Cell) : 0;
355 : anton 1.1 UCell check_sum;
356 : pazsan 1.15 Cell ausize = ((RELINFOBITS == 8) ? 0 :
357 :     (RELINFOBITS == 16) ? 1 :
358 :     (RELINFOBITS == 32) ? 2 : 3);
359 :     Cell charsize = ((sizeof(Char) == 1) ? 0 :
360 :     (sizeof(Char) == 2) ? 1 :
361 :     (sizeof(Char) == 4) ? 2 : 3) + ausize;
362 :     Cell cellsize = ((sizeof(Cell) == 1) ? 0 :
363 :     (sizeof(Cell) == 2) ? 1 :
364 :     (sizeof(Cell) == 4) ? 2 : 3) + ausize;
365 : anton 1.21 Cell sizebyte = (ausize << 5) + (charsize << 3) + (cellsize << 1) +
366 :     #ifdef WORDS_BIGENDIAN
367 :     0
368 :     #else
369 :     1
370 :     #endif
371 :     ;
372 : anton 1.1
373 :     #ifndef DOUBLY_INDIRECT
374 :     check_sum = checksum(symbols);
375 :     #else /* defined(DOUBLY_INDIRECT) */
376 :     check_sum = (UCell)symbols;
377 :     #endif /* defined(DOUBLY_INDIRECT) */
378 : pazsan 1.10
379 :     do {
380 :     if(fread(magic,sizeof(Char),8,imagefile) < 8) {
381 : pazsan 1.15 fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.4) image.\n",
382 : pazsan 1.10 progname, filename);
383 :     exit(1);
384 : anton 1.1 }
385 : pazsan 1.10 preamblesize+=8;
386 : pazsan 1.15 } while(memcmp(magic,"Gforth2",7));
387 : anton 1.17 magic7 = magic[7];
388 : anton 1.1 if (debug) {
389 : anton 1.17 magic[7]='\0';
390 : anton 1.21 fprintf(stderr,"Magic found: %s ", magic);
391 :     print_sizes(magic7);
392 : anton 1.1 }
393 :    
394 : anton 1.21 if (magic7 != sizebyte)
395 :     {
396 :     fprintf(stderr,"This image is: ");
397 :     print_sizes(magic7);
398 :     fprintf(stderr,"whereas the machine is ");
399 :     print_sizes(sizebyte);
400 : anton 1.1 exit(-2);
401 :     };
402 :    
403 :     fread((void *)&header,sizeof(ImageHeader),1,imagefile);
404 : pazsan 1.10
405 :     set_stack_sizes(&header);
406 : anton 1.1
407 :     #if HAVE_GETPAGESIZE
408 :     pagesize=getpagesize(); /* Linux/GNU libc offers this */
409 :     #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
410 :     pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
411 :     #elif PAGESIZE
412 :     pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
413 :     #endif
414 :     if (debug)
415 : jwilke 1.5 fprintf(stderr,"pagesize=%ld\n",(unsigned long) pagesize);
416 : anton 1.1
417 : anton 1.3 image = dict_alloc(preamblesize+dictsize+data_offset)+data_offset;
418 : anton 1.1 rewind(imagefile); /* fseek(imagefile,0L,SEEK_SET); */
419 :     if (clear_dictionary)
420 : pazsan 1.10 memset(image, 0, dictsize);
421 :     fread(image, 1, preamblesize+header.image_size, imagefile);
422 : anton 1.1 imp=image+preamblesize;
423 :     if(header.base==0) {
424 :     Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
425 :     char reloc_bits[reloc_size];
426 : pazsan 1.10 fread(reloc_bits, 1, reloc_size, imagefile);
427 :     relocate((Cell *)imp, reloc_bits, header.image_size, symbols);
428 : anton 1.1 #if 0
429 :     { /* let's see what the relocator did */
430 :     FILE *snapshot=fopen("snapshot.fi","wb");
431 :     fwrite(image,1,imagesize,snapshot);
432 :     fclose(snapshot);
433 :     }
434 :     #endif
435 :     }
436 :     else if(header.base!=imp) {
437 :     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
438 :     progname, (unsigned long)header.base, (unsigned long)imp);
439 :     exit(1);
440 :     }
441 :     if (header.checksum==0)
442 :     ((ImageHeader *)imp)->checksum=check_sum;
443 :     else if (header.checksum != check_sum) {
444 :     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
445 :     progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
446 :     exit(1);
447 :     }
448 :     fclose(imagefile);
449 :    
450 : pazsan 1.10 alloc_stacks((ImageHeader *)imp);
451 : anton 1.1
452 :     CACHE_FLUSH(imp, header.image_size);
453 :    
454 :     return imp;
455 :     }
456 :    
457 :     int onlypath(char *file)
458 : pazsan 1.10 {
459 :     int i;
460 : anton 1.1 i=strlen(file);
461 : pazsan 1.10 while (i) {
462 :     if (file[i]=='\\' || file[i]=='/') break;
463 :     i--;
464 :     }
465 :     return i;
466 : anton 1.1 }
467 :    
468 :     FILE *openimage(char *fullfilename)
469 : pazsan 1.10 {
470 :     FILE *image_file;
471 :    
472 : anton 1.1 image_file=fopen(fullfilename,"rb");
473 :     if (image_file!=NULL && debug)
474 : pazsan 1.10 fprintf(stderr, "Opened image file: %s\n", fullfilename);
475 :     return image_file;
476 : anton 1.1 }
477 :    
478 :     FILE *checkimage(char *path, int len, char *imagename)
479 : pazsan 1.10 {
480 :     int dirlen=len;
481 : anton 1.1 char fullfilename[dirlen+strlen(imagename)+2];
482 : pazsan 1.10
483 : anton 1.1 memcpy(fullfilename, path, dirlen);
484 :     if (fullfilename[dirlen-1]!='/')
485 :     fullfilename[dirlen++]='/';
486 :     strcpy(fullfilename+dirlen,imagename);
487 : pazsan 1.10 return openimage(fullfilename);
488 : anton 1.1 }
489 :    
490 : pazsan 1.10 FILE * open_image_file(char * imagename, char * path)
491 : anton 1.1 {
492 : pazsan 1.10 FILE * image_file=NULL;
493 :    
494 :     if(strchr(imagename, '/')==NULL) {
495 :     /* first check the directory where the exe file is in !! 01may97jaw */
496 :     if (onlypath(progname))
497 :     image_file=checkimage(progname, onlypath(progname), imagename);
498 :     if (!image_file)
499 :     do {
500 :     char *pend=strchr(path, PATHSEP);
501 :     if (pend==NULL)
502 :     pend=path+strlen(path);
503 :     if (strlen(path)==0) break;
504 :     image_file=checkimage(path, pend-path, imagename);
505 :     path=pend+(*pend==PATHSEP);
506 :     } while (image_file==NULL);
507 :     } else {
508 :     image_file=openimage(imagename);
509 :     }
510 : anton 1.1
511 : pazsan 1.10 if (!image_file) {
512 :     fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
513 :     progname, imagename, path);
514 :     exit(1);
515 : anton 1.7 }
516 :    
517 : pazsan 1.10 return image_file;
518 :     }
519 : pazsan 1.11 #endif
520 :    
521 :     #ifdef HAS_OS
522 :     UCell convsize(char *s, UCell elemsize)
523 :     /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
524 :     of bytes. the letter at the end indicates the unit, where e stands
525 :     for the element size. default is e */
526 :     {
527 :     char *endp;
528 :     UCell n,m;
529 :    
530 :     m = elemsize;
531 :     n = strtoul(s,&endp,0);
532 :     if (endp!=NULL) {
533 :     if (strcmp(endp,"b")==0)
534 :     m=1;
535 :     else if (strcmp(endp,"k")==0)
536 :     m=1024;
537 :     else if (strcmp(endp,"M")==0)
538 :     m=1024*1024;
539 :     else if (strcmp(endp,"G")==0)
540 :     m=1024*1024*1024;
541 :     else if (strcmp(endp,"T")==0) {
542 :     #if (SIZEOF_CHAR_P > 4)
543 : anton 1.24 m=1024L*1024*1024*1024;
544 : pazsan 1.11 #else
545 :     fprintf(stderr,"%s: size specification \"%s\" too large for this machine\n", progname, endp);
546 :     exit(1);
547 :     #endif
548 :     } else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
549 :     fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
550 :     exit(1);
551 :     }
552 :     }
553 :     return n*m;
554 :     }
555 : pazsan 1.10
556 :     void gforth_args(int argc, char ** argv, char ** path, char ** imagename)
557 :     {
558 :     int c;
559 :    
560 : anton 1.1 opterr=0;
561 :     while (1) {
562 :     int option_index=0;
563 :     static struct option opts[] = {
564 :     {"image-file", required_argument, NULL, 'i'},
565 :     {"dictionary-size", required_argument, NULL, 'm'},
566 :     {"data-stack-size", required_argument, NULL, 'd'},
567 :     {"return-stack-size", required_argument, NULL, 'r'},
568 :     {"fp-stack-size", required_argument, NULL, 'f'},
569 :     {"locals-stack-size", required_argument, NULL, 'l'},
570 :     {"path", required_argument, NULL, 'p'},
571 :     {"version", no_argument, NULL, 'v'},
572 :     {"help", no_argument, NULL, 'h'},
573 :     /* put something != 0 into offset_image */
574 :     {"offset-image", no_argument, &offset_image, 1},
575 :     {"no-offset-im", no_argument, &offset_image, 0},
576 :     {"clear-dictionary", no_argument, &clear_dictionary, 1},
577 : anton 1.4 {"die-on-signal", no_argument, &die_on_signal, 1},
578 : anton 1.1 {"debug", no_argument, &debug, 1},
579 :     {0,0,0,0}
580 :     /* no-init-file, no-rc? */
581 :     };
582 :    
583 :     c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vh", opts, &option_index);
584 :    
585 :     if (c==EOF)
586 :     break;
587 :     if (c=='?') {
588 :     optind--;
589 :     break;
590 :     }
591 :     switch (c) {
592 : pazsan 1.10 case 'i': *imagename = optarg; break;
593 : anton 1.1 case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
594 :     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
595 :     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
596 :     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
597 :     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
598 : pazsan 1.10 case 'p': *path = optarg; break;
599 : anton 1.8 case 'v': fprintf(stderr, "gforth %s\n", VERSION); exit(0);
600 : anton 1.1 case 'h':
601 :     fprintf(stderr, "Usage: %s [engine options] [image arguments]\n\
602 :     Engine Options:\n\
603 : pazsan 1.10 --clear-dictionary Initialize the dictionary with 0 bytes\n\
604 :     -d SIZE, --data-stack-size=SIZE Specify data stack size\n\
605 :     --debug Print debugging information during startup\n\
606 :     --die-on-signal exit instead of CATCHing some signals\n\
607 :     -f SIZE, --fp-stack-size=SIZE Specify floating point stack size\n\
608 :     -h, --help Print this message and exit\n\
609 :     -i FILE, --image-file=FILE Use image FILE instead of `gforth.fi'\n\
610 :     -l SIZE, --locals-stack-size=SIZE Specify locals stack size\n\
611 :     -m SIZE, --dictionary-size=SIZE Specify Forth dictionary size\n\
612 :     --no-offset-im Load image at normal position\n\
613 :     --offset-image Load image at a different position\n\
614 :     -p PATH, --path=PATH Search path for finding image and sources\n\
615 :     -r SIZE, --return-stack-size=SIZE Specify return stack size\n\
616 :     -v, --version Print version and exit\n\
617 : anton 1.1 SIZE arguments consist of an integer followed by a unit. The unit can be\n\
618 : pazsan 1.10 `b' (byte), `e' (element; default), `k' (KB), `M' (MB), `G' (GB) or `T' (TB).\n",
619 :     argv[0]);
620 :     optind--;
621 :     return;
622 :     exit(0);
623 : anton 1.1 }
624 :     }
625 : pazsan 1.10 }
626 : pazsan 1.11 #endif
627 : pazsan 1.10
628 :     #ifdef INCLUDE_IMAGE
629 :     extern Cell image[];
630 :     extern const char reloc_bits[];
631 :     #endif
632 :    
633 :     int main(int argc, char **argv, char **env)
634 :     {
635 :     char *path = getenv("GFORTHPATH") ? : DEFAULTPATH;
636 : pazsan 1.13 #ifndef INCLUDE_IMAGE
637 : pazsan 1.10 char *imagename="gforth.fi";
638 :     FILE *image_file;
639 :     Address image;
640 :     #endif
641 :     int retvalue;
642 :    
643 :     #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
644 :     /* turn on alignment checks on the 486.
645 :     * on the 386 this should have no effect. */
646 :     __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
647 :     /* this is unusable with Linux' libc.4.6.27, because this library is
648 :     not alignment-clean; we would have to replace some library
649 :     functions (e.g., memcpy) to make it work. Also GCC doesn't try to keep
650 :     the stack FP-aligned. */
651 :     #endif
652 :    
653 :     /* buffering of the user output device */
654 : pazsan 1.11 #ifdef _IONBF
655 : pazsan 1.10 if (isatty(fileno(stdout))) {
656 :     fflush(stdout);
657 :     setvbuf(stdout,NULL,_IONBF,0);
658 : anton 1.1 }
659 : pazsan 1.11 #endif
660 : anton 1.1
661 : pazsan 1.10 progname = argv[0];
662 :    
663 : pazsan 1.11 #ifdef HAS_OS
664 : pazsan 1.10 gforth_args(argc, argv, &path, &imagename);
665 : pazsan 1.11 #endif
666 : pazsan 1.10
667 :     #ifdef INCLUDE_IMAGE
668 :     set_stack_sizes((ImageHeader *)image);
669 : pazsan 1.22 if(((ImageHeader *)image)->base != image)
670 :     relocate(image, reloc_bits, ((ImageHeader *)image)->image_size,
671 :     (Label*)engine(0, 0, 0, 0, 0));
672 : pazsan 1.10 alloc_stacks((ImageHeader *)image);
673 :     #else
674 :     image_file = open_image_file(imagename, path);
675 :     image = loader(image_file, imagename);
676 :     #endif
677 : anton 1.24 gforth_header=(ImageHeader *)image; /* used in SIGSEGV handler */
678 : anton 1.1
679 :     {
680 : pazsan 1.10 char path2[strlen(path)+1];
681 : anton 1.1 char *p1, *p2;
682 :     Cell environ[]= {
683 :     (Cell)argc-(optind-1),
684 :     (Cell)(argv+(optind-1)),
685 : pazsan 1.10 (Cell)strlen(path),
686 : anton 1.1 (Cell)path2};
687 :     argv[optind-1] = progname;
688 :     /*
689 :     for (i=0; i<environ[0]; i++)
690 :     printf("%s\n", ((char **)(environ[1]))[i]);
691 :     */
692 :     /* make path OS-independent by replacing path separators with NUL */
693 : pazsan 1.10 for (p1=path, p2=path2; *p1!='\0'; p1++, p2++)
694 : anton 1.1 if (*p1==PATHSEP)
695 :     *p2 = '\0';
696 :     else
697 :     *p2 = *p1;
698 :     *p2='\0';
699 : pazsan 1.10 retvalue = go_forth(image, 4, environ);
700 : anton 1.1 deprep_terminal();
701 :     }
702 : pazsan 1.13 return retvalue;
703 : anton 1.1 }

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help