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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help